The question is this:
The notification received by the mobile phone is the same as 00 01 01 00 C0 C8 FD 3F 54 C0 FD..... 3F 00 00 00 00

If you cut back to UTF-8, it becomes garbled:

The bluetooth code is changed from the ESP32 example file, which has been analyzed for a long time....I am a rookie...
example file is ESP32 BLE Arduino----->BLE_uart.ino
Code: Select all
#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:
// https://www.uuidgenerator.net/
#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");
}
}
