近期做了一個項目需要用到微信小程序藍牙與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