【Esp32连接微信小程序蓝牙】附Arduino源码《 返回10007 相同特征id冲突问题》

前言

最近接了一个外包,发现了esp32连接小程序会有很多bug,所以接下来会慢慢更新解决方案,还是需要多接触项目才能进步呀兄弟们!

附上uuid的生成链接:

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

问题

这里借用一下,别人博客遇到的问题。

后面重新开看流程时发现使用 wx.getBLEDeviceCharacteristics的时候有出现了三个特征值,以至于报错,博主问题原链接

我们可以看到微信小程序维护的也....现在2024年了这个bug还没修好.....

链接

解决办法

问题发现

解决办法只能从esp32代码来入手,首先来看看原本的蓝牙连接代码,我们可以看到,首先在开头就写了四个uuid特征码来进行蓝牙初始化,创建、发送、接收,这就是导致问题出现的关键


#define SERVICE_UUID        "1596c77c-cf40-4137-9957-d24916f8e50b"   //你可以通过上面的网址去生成UUID
#define CHARACTERISTIC_UUID "1596c77c-cf40-4137-9957-d24916f8e50b"
#define CHARACTERISTIC_UUID_RX "1596c77c-cf40-4137-9957-d24916f8e50b"
#define CHARACTERISTIC_UUID_TX "1596c77c-cf40-4137-9957-d24916f8e50b"



void setup() {
  
  chipId = String((uint32_t)ESP.getEfuseMac(), HEX);
  chipId.toUpperCase();
//  chipid =ESP.getEfuseMac();
//  Serial.printf("Chip id: %s\n", chipid.c_str());
  Serial.println("chipId:"+chipId);
  Serial.println();
  Serial.printf("Chip id: %s\n", chipId.c_str());
  // Create the BLE Device
  BLEDevice::init("xhn_Service");

  // Create the BLE Server
  pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  //随机生成的uuid放入

  BLEService *pService = pServer->createService(SERVICE_UUID);
  // Create a BLE Characteristic
  pTxCharacteristic = pService->createCharacteristic(
                        CHARACTERISTIC_UUID_TX,  
                       
                        BLECharacteristic::PROPERTY_NOTIFY
                      );

  pTxCharacteristic->addDescriptor(new BLE2902());

  BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
      CHARACTERISTIC_UUID_RX,                                  
      uuid_RX,                                                   
      BLECharacteristic::PROPERTY_WRITE
                                          );

  pRxCharacteristic->setCallbacks(new MyCallbacks());

  // Start the service
  pService->start();

  // Start advertising
  pServer->getAdvertising()->start();
  Serial.println("Waiting a client connection to notify...");
}

String readString;

void loop() {

  if (deviceConnected) {
    //        pTxCharacteristic->setValue(&txValue, 1);
    //        pTxCharacteristic->notify();
    //        txValue++;
    //    delay(10); // bluetooth stack will go into congestion, if too many packets are sent
  }
  while (Serial.available() > 0) {
    if (deviceConnected) {
      delay(3);
      readString += Serial.read();
      pTxCharacteristic->setValue(chipId.c_str());
//      pTxCharacteristic->setValue((uint32_t)ESP.getEfuseMac());
      pTxCharacteristic->notify();
      Serial.println(chipId);

    }
  }
  // disconnecting
  if (!deviceConnected && oldDeviceConnected) {
    delay(500); // give the bluetooth stack the chance to get things ready
    pServer->startAdvertising(); // restart advertising
    Serial.println("start advertising");
    oldDeviceConnected = deviceConnected;
  }
  // connecting
  if (deviceConnected && !oldDeviceConnected) {
    // do stuff here on connecting
    oldDeviceConnected = deviceConnected;
  }
}


问题解决

因为我们初始化,接收、发送时,传递的都是同一个uuid,所以导致特征码重复,而报错,所以我们就可以在初始化的时候使用一个uuid,在发送或接收使用uuid时,切换另一个,因为获取uuid的目的是为了让小程序绑定设备码,所以在初始化的时候我们就可以绑定成功,从而uuid的作用就不重要了。

这边以修改接收的uuid为例:(其实修改一行就解决问题了),或者你将发送的UUID的修改成别的uuid也可以,只要你在小程序绑定号设备号就行,因为设备号是不会改变的。


#define SERVICE_UUID        "1596c77c-cf40-4137-9957-d24916f8e50b"   //你可以通过上面的网址去生成UUID
#define CHARACTERISTIC_UUID "1596c77c-cf40-4137-9957-d24916f8e50b"
#define CHARACTERISTIC_UUID_RX "2abe697b-cad9-409b-802e-624646c3e69c"
#define CHARACTERISTIC_UUID_TX "1596c77c-cf40-4137-9957-d24916f8e50b"



void setup() {
  
  chipId = String((uint32_t)ESP.getEfuseMac(), HEX);
  chipId.toUpperCase();
//  chipid =ESP.getEfuseMac();
//  Serial.printf("Chip id: %s\n", chipid.c_str());
  Serial.println("chipId:"+chipId);
  Serial.println();
  Serial.printf("Chip id: %s\n", chipId.c_str());
  // Create the BLE Device
  BLEDevice::init("xhn_Service");

  // Create the BLE Server
  pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  //随机生成的uuid放入

  BLEService *pService = pServer->createService(SERVICE_UUID);
  // Create a BLE Characteristic
  pTxCharacteristic = pService->createCharacteristic(
                        CHARACTERISTIC_UUID_TX,  
                       
                        BLECharacteristic::PROPERTY_NOTIFY
                      );

  pTxCharacteristic->addDescriptor(new BLE2902());

  BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
      CHARACTERISTIC_UUID_RX,                                                                                   
      BLECharacteristic::PROPERTY_WRITE
                                          );

  pRxCharacteristic->setCallbacks(new MyCallbacks());

  // Start the service
  pService->start();

  // Start advertising
  pServer->getAdvertising()->start();
  Serial.println("Waiting a client connection to notify...");
}

String readString;

void loop() {

  if (deviceConnected) {
    //        pTxCharacteristic->setValue(&txValue, 1);
    //        pTxCharacteristic->notify();
    //        txValue++;
    //    delay(10); // bluetooth stack will go into congestion, if too many packets are sent
  }
  while (Serial.available() > 0) {
    if (deviceConnected) {
      delay(3);
      readString += Serial.read();
      pTxCharacteristic->setValue(chipId.c_str());
//      pTxCharacteristic->setValue((uint32_t)ESP.getEfuseMac());
      pTxCharacteristic->notify();
      Serial.println(chipId);

    }
  }
  // disconnecting
  if (!deviceConnected && oldDeviceConnected) {
    delay(500); // give the bluetooth stack the chance to get things ready
    pServer->startAdvertising(); // restart advertising
    Serial.println("start advertising");
    oldDeviceConnected = deviceConnected;
  }
  // connecting
  if (deviceConnected && !oldDeviceConnected) {
    // do stuff here on connecting
    oldDeviceConnected = deviceConnected;
  }
}


### 回答1: 要让ESP32通过蓝牙发送数据到微信小程序,你可以使用ESP32内置的蓝牙模块,以及微信小程序提供的蓝牙API接口。 以下是一些基本步骤: 1. 使用Arduino IDE或其他编程工具,编写ESP32蓝牙发送程序。例如,你可以使用Arduino蓝牙库,通过Serial通信接口将数据发送到ESP32内置的蓝牙模块。 2. 在微信小程序中,使用微信提供的蓝牙API接口,连接ESP32蓝牙模块,并接收来自ESP32的数据。例如,你可以使用wx.createBLEConnection()函数建立连接,使用wx.onBLECharacteristicValueChange()函数监听数据变化事件,以及使用wx.readBLECharacteristicValue()函数读取数据。 需要注意的是,蓝牙连接和数据传输的稳定性需要根据具体情况进行优化。同时,你需要确保ESP32微信小程序之间的通信协议一致,例如数据格式、编码方式等。 ### 回答2: ESP32是一款功能强大的开发板,它支持蓝牙功能,并可以通过蓝牙将数据发送给微信小程序进行接收。 在ESP32上,我们可以使用它内置的蓝牙模块来实现数据的发送。首先,我们需要在ESP32上启用蓝牙功能,并创建一个蓝牙服务,用于发送数据。可以使用ESP-IDF开发框架来实现这些功能。 接下来,在微信小程序中,我们需要使用wx.startBluetoothDevicesDiscovery函数来启用蓝牙设备的搜索功能。然后,使用wx.onBluetoothDeviceFound函数来监听蓝牙设备的发现事件,并获取到ESP32蓝牙设备信息。 一旦微信小程序找到了ESP32蓝牙设备,我们可以使用wx.createBLEConnection函数来建立与ESP32蓝牙设备的连接连接建立之后,可以使用wx.onBLEConnectionStateChange函数来监听蓝牙连接状态的变化,并在连接成功后发送数据请求到ESP32。 在ESP32上,当接收到来自微信小程序的数据请求时,可以通过蓝牙通信协议进行通信。ESP32可以将需要发送的数据打包成特定格式的数据包,并通过蓝牙发送给微信小程序。 在微信小程序中,可以使用wx.onBLECharacteristicValueChange函数监听从ESP32接收到的数据,并进行处理。 总而言之,通过在ESP32上启用蓝牙功能,并在微信小程序中使用蓝牙接口进行连接和数据接收处理,我们可以实现ESP32微信小程序发送数据的功能。这样,我们就可以利用ESP32微信小程序相互通信,实现更多有趣的功能。 ### 回答3: ESP32是一种高集成度的蓝牙Wi-Fi芯片,可以用于无线传输数据。要在ESP32上发送数据到微信小程序蓝牙接收端,需要经过以下几个步骤: 1. 配置ESP32蓝牙模块:首先,需要在ESP32上配置蓝牙模块,使其能够与其他设备进行通信。可以使用Arduino编程语言来编写代码,在代码中引入ESP32蓝牙库,并设置蓝牙的名称和特性。 2. 连接微信小程序蓝牙接收端:在微信小程序中,需要使用wx.getBLEDevice函数获取到ESP32蓝牙设备对象。然后通过wx.createBLEConnection函数进行连接。 3. 发送数据:一旦连接建立,就可以使用ESP32蓝牙发送函数发送数据。可以使用ESP32蓝牙库提供的函数来发送字符串、数字或二进制数据。 4. 接收数据:在微信小程序中,可以通过wx.onBLECharacteristicValueChange事件监听接收到的数据。当ESP32蓝牙发送数据时,该事件将被触发,从而可以获取到接收到的数据。 需要注意的是,ESP32微信小程序蓝牙接收端的通信需要使用相同的数据格式和通信协议。通常情况下,可以使用文本字符串作为通信格式,例如发送JSON格式的数据。 总之,要实现ESP32蓝牙发送数据到微信小程序蓝牙接收端,需要先配置ESP32蓝牙模块,然后在微信小程序中建立连接并监听接收到的数据。通过这种方式,可以实现两者之间的无线数据传输。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值