|
问题是这样的,先上图:
手机端接收的,发现接收的notify都是同一个值:00 01 01 00 C0 C8 FD 3F 54 C0 FD.....3F 00 00 00 00
切回utf-8,就变成乱码了,变成这样:
由于我是菜鸟,只懂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:
// 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");
}
}
理想的蓝牙情况是这样的,把其它微控制器传上来的值(sEMG和6个数值)通过UART发送到ESP32,ESP32再把UART缓冲区的单个字符一个个的当通知信息发出去:
请问咋解决啊,为啥蓝牙接收同一个值,还是乱码(;´д`)ゞ
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|