近期做了一个项目需要用到微信小程序蓝牙与ESP32进行配网及数据设置,因此在一个前辈提供的demo上进行一部分优化和修改,踩了一些坑,因此记录一下
ESP32 代码:
1 // 包含所必需的库 2 #include <BLEDevice.h> 3 #include <BLEServer.h> 4 #include <BLEUtils.h> 5 #include <BLE2902.h> 6 7 BLEServer *pServer = NULL; 8 BLECharacteristic *pTxCharacteristic; 9 bool deviceConnected = false; 10 bool oldDeviceConnected = false; 11 12 char BLEbuf[32] = {0}; 13 String data = ""; 14 15 // 定义收发服务的UUID(唯一标识) 16 #define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" 17 // RX串口标识 18 #define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E" 19 // TX串口标识 20 #define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E" 21 22 23 class MyServerCallbacks: public BLEServerCallbacks { 24 void onConnect(BLEServer* pServer) { 25 deviceConnected = true; 26 }; 27 28 void onDisconnect(BLEServer* pServer) { 29 deviceConnected = false; 30 } 31 }; 32 33 class MyCallbacks: public BLECharacteristicCallbacks { 34 void onWrite(BLECharacteristic *pCharacteristic) { 35 std::string rxValue = pCharacteristic->getValue(); 36 37 if (rxValue.length() > 0) { 38 Serial.println("*********"); 39 Serial.print("Received Value: "); 40 for (int i = 0; i < rxValue.length(); i++){ 41 Serial.print(rxValue[i]); 42 } 43 Serial.println(); 44 data =rxValue.c_str(); 45 //Serial.println(data); 46 Serial.println("*********"); 47 Serial.println(); 48 } 49 } 50 }; 51 52 // setup()在复位或上电后运行一次: 53 void setup() { 54 Serial.begin(115200); 55 // 初始化蓝牙设备 56 BLEDevice::init("ESP32 test"); 57 // 为蓝牙设备创建服务器 58 pServer = BLEDevice::createServer(); 59 pServer->setCallbacks(new MyServerCallbacks()); 60 // 基于SERVICE_UUID来创建一个服务 61 BLEService *pService = pServer->createService(SERVICE_UUID); 62 pTxCharacteristic = pService->createCharacteristic( 63 CHARACTERISTIC_UUID_TX, 64 BLECharacteristic::PROPERTY_NOTIFY 65 ); 66 pTxCharacteristic->addDescriptor(new BLE2902()); 67 BLECharacteristic * pRxCharacteristic = pService->createCharacteristic( 68 CHARACTERISTIC_UUID_RX, 69 BLECharacteristic::PROPERTY_WRITE 70 ); 71 pRxCharacteristic->setCallbacks(new MyCallbacks()); 72 // 开启服务 73 pService->start(); 74 // 开启通知 75 pServer->getAdvertising()->start(); 76 Serial.println("Waiting a client connection to notify..."); 77 Serial.println(); 78 } 79 80 // loop()一直循环执行: 81 void loop() { 82 83 if (deviceConnected==1&data.length()>0) { 84 memset(BLEbuf, 0, 32); 85 memcpy(BLEbuf, data.c_str(), 32); 86 Serial.println(BLEbuf); 87 88 pTxCharacteristic->setValue(BLEbuf); //收到数据后返回数据 89 pTxCharacteristic->notify(); 90 data = ""; //返回数据后进行清空,否则一直发送data 91 } 92 93 // 没有新连接时 94 if (!deviceConnected && oldDeviceConnected) { 95 // 给蓝牙堆栈准备数据的时间 96 delay(500); 97 pServer->startAdvertising(); 98 // 重新开始广播 99 Serial.println("start advertising"); 100 oldDeviceConnected = deviceConnected; 101 } 102 // 正在连接时 103 if (deviceConnected && !oldDeviceConnected) { 104 // 正在连接时进行的操作 105 oldDeviceConnected = deviceConnected; 106 } 107 }
小程序的代码需要有几个需要注意的地方:
1. 启用低功耗蓝牙设备特征值变化时的 notify 功能,订阅特征值。注意:必须设备的特征值支持notify或者indicate才可以成功调用。另外,必须先启用notifyBLECharacteristicValueChange才能监听到设备characteristicValueChange事件
2.数据发送时所发的CHARACTERISTIC_UUID一定要与ESP32端的ID对应,否则会发送失败。
1 #define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E" // RX串口标识
小程序源代码地址:
https://gitee.com/hejinlv/WeChat-Ble-To-ESP32-Ble/tree/master