問題是這樣的,先上圖:
手機端接收的,發現接收的notify都是同一個值:00 01 01 00 C0 C8 FD 3F 54 C0 FD.....3F 00 00 00 00
51hei.png (141.29 KB, 下載次數: 34)
下載附件
2022-3-19 16:48 上傳
切回utf-8,就變成亂碼了,變成這樣:
51hei.png (139.55 KB, 下載次數: 28)
下載附件
2022-3-19 16:49 上傳
由于我是菜鳥,只懂arduino軟編程,C語言也只是略懂
藍牙代碼是用ESP32 example文件改來的,分析了很久沒分析出什么,只好向高手求助了
以下是代碼:
- #include <BLEDevice.h>
- #include <BLEServer.h>
- #include <BLEUtils.h>
- #include <BLE2902.h>
- BLEServer *pServer = NULL;
- BLECharacteristic *pTxCharacteristic;
- bool deviceConnected = false;
- bool oldDeviceConnected = false;
- uint8_t txValue = 0;
- // See the following for generating UUIDs:
- #define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
- #define CHARACTERISTIC_UUID_RX "f78ebbff-c8b7-4107-93de-889a6a06d408"
- #define CHARACTERISTIC_UUID_TX "ca73b3ba-39f6-4ab3-91ae-186dc9577d99"
- class MyServerCallbacks : public BLEServerCallbacks
- {
- void onConnect(BLEServer *pServer)
- {
- deviceConnected = true;
- };
- void onDisconnect(BLEServer *pServer)
- {
- deviceConnected = false;
- }
- };
- class MyCallbacks : public BLECharacteristicCallbacks
- {
- void onWrite(BLECharacteristic *pCharacteristic)
- {
- std::string rxValue = pCharacteristic->getValue();
- if (rxValue.length() > 0)
- {
- Serial2.println("*********");
- Serial2.print("Received Value: ");
- for (int i = 0; i < rxValue.length(); i++)
- Serial2.print(rxValue[i);
- Serial2.println();
- Serial2.println("*********");
- }
- }
- };
- void setup()
- {
- Serial1.begin(250000, SERIAL_8N1, 3, 1);
- Serial2.begin(250000, SERIAL_8N1, 16, 17);
- // Create the BLE Device
- BLEDevice::init("UART Service");
- // Create the BLE Server
- pServer = BLEDevice::createServer();
- pServer->setCallbacks(new MyServerCallbacks());
- // Create the BLE Service
- 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();
- Serial2.println("\nSet Serial1 ok!");
- Serial2.println("Waiting a client connection to notify...");
- }
- void loop()
- {
- if (deviceConnected)
- {
- // long time=millis();
- // for(int i=0;i<1000;){
- if (Serial1.available() > 0)
- {
- // int x=0;
- // if(char(Serial1.peek())=='s'){i++;}
- // if(Serial1.peek()!='\n'){x++;}else{x=0;}
- //tx傳值,通過藍牙notify不間斷發送通知
- Serial2.print(char(Serial1.peek()));
- pTxCharacteristic->setValue(&txValue, Serial1.read());
- pTxCharacteristic->notify();
- // if(x>10){
- // Serial2.println();
- // Serial2.println("Error!!!");
- // delay(10000000000);
- // }
- delay(10);
- }
- // // }
- // float a=1000/((millis()-time)/1000);
- // Serial2.printf("Frequency domain:%f /n",a);
- // delay(1000000);
- }
- // disconnecting
- if (!deviceConnected && oldDeviceConnected)
- {
- delay(500); // 讓藍牙堆棧有機會做好準備
- pServer->startAdvertising(); // 重啟廣播
- Serial2.println("start advertising");
- oldDeviceConnected = deviceConnected;
- }
- // connecting
- if (deviceConnected && !oldDeviceConnected)
- {
- // do stuff here on connecting在連接上做點事情
- oldDeviceConnected = deviceConnected;
- Serial2.println("start advertising");
- }
- }
復制代碼
理想的藍牙情況是這樣的,把其它微控制器傳上來的值(sEMG和6個數值)通過UART發送到ESP32,ESP32再把UART緩沖區的單個字符一個個的當通知信息發出去:
51hei.png (97.24 KB, 下載次數: 31)
下載附件
2022-3-19 16:50 上傳
請問咋解決啊,為啥藍牙接收同一個值,還是亂碼(;′д`)ゞ
|