找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 834|回复: 2
打印 上一主题 下一主题
收起左侧

菜鸟问题:ESP32S蓝牙为什么发送同一个值?TX传的是不同值啊

[复制链接]
回帖奖励 1 黑币 回复本帖可获得 1 黑币奖励! 每人限 1 次(中奖概率 50%)
跳转到指定楼层
楼主
问题是这样的,先上图:
手机端接收的,发现接收的notify都是同一个值:00 01 01 00 C0 C8 FD 3F 54 C0 FD.....3F 00 00 00 00

切回utf-8,就变成乱码了,变成这样:


由于我是菜鸟,只懂arduino软编程,C语言也只是略懂

蓝牙代码是用ESP32 example文件改来的,分析了很久没分析出什么,只好向高手求助了

以下是代码:
  1. #include <BLEDevice.h>
  2. #include <BLEServer.h>
  3. #include <BLEUtils.h>
  4. #include <BLE2902.h>
  5. BLEServer *pServer = NULL;
  6. BLECharacteristic *pTxCharacteristic;
  7. bool deviceConnected = false;
  8. bool oldDeviceConnected = false;
  9. uint8_t txValue = 0;

  10. // See the following for generating UUIDs:

  11. #define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
  12. #define CHARACTERISTIC_UUID_RX "f78ebbff-c8b7-4107-93de-889a6a06d408"
  13. #define CHARACTERISTIC_UUID_TX "ca73b3ba-39f6-4ab3-91ae-186dc9577d99"

  14. class MyServerCallbacks : public BLEServerCallbacks
  15. {
  16.   void onConnect(BLEServer *pServer)
  17.   {
  18.     deviceConnected = true;
  19.   };

  20.   void onDisconnect(BLEServer *pServer)
  21.   {
  22.     deviceConnected = false;
  23.   }
  24. };

  25. class MyCallbacks : public BLECharacteristicCallbacks
  26. {
  27.   void onWrite(BLECharacteristic *pCharacteristic)
  28.   {
  29.     std::string rxValue = pCharacteristic->getValue();

  30.     if (rxValue.length() > 0)
  31.     {
  32.       Serial2.println("*********");
  33.       Serial2.print("Received Value: ");
  34.       for (int i = 0; i < rxValue.length(); i++)
  35.         Serial2.print(rxValue[i);

  36.       Serial2.println();
  37.       Serial2.println("*********");
  38.     }
  39.   }
  40. };
  41. void setup()
  42. {
  43.   Serial1.begin(250000, SERIAL_8N1, 3, 1);
  44.   Serial2.begin(250000, SERIAL_8N1, 16, 17);

  45.   // Create the BLE Device
  46.   BLEDevice::init("UART Service");

  47.   // Create the BLE Server
  48.   pServer = BLEDevice::createServer();
  49.   pServer->setCallbacks(new MyServerCallbacks());

  50.   // Create the BLE Service
  51.   BLEService *pService = pServer->createService(SERVICE_UUID);

  52.   // Create a BLE Characteristic
  53.   pTxCharacteristic = pService->createCharacteristic(
  54.       CHARACTERISTIC_UUID_TX,
  55.       BLECharacteristic::PROPERTY_NOTIFY);

  56.   pTxCharacteristic->addDescriptor(new BLE2902());

  57.   BLECharacteristic *pRxCharacteristic = pService->createCharacteristic(
  58.       CHARACTERISTIC_UUID_RX,
  59.       BLECharacteristic::PROPERTY_WRITE);

  60.   pRxCharacteristic->setCallbacks(new MyCallbacks());

  61.   // Start the service
  62.   pService->start();

  63.   // Start advertising
  64.   pServer->getAdvertising()->start();

  65.   Serial2.println("\nSet Serial1 ok!");
  66.   Serial2.println("Waiting a client connection to notify...");
  67. }

  68. void loop()
  69. {
  70.   if (deviceConnected)
  71.   {
  72.     // long time=millis();
  73.     // for(int i=0;i<1000;){
  74.     if (Serial1.available() > 0)
  75.     {
  76.       // int x=0;
  77.       // if(char(Serial1.peek())=='s'){i++;}
  78.       //   if(Serial1.peek()!='\n'){x++;}else{x=0;}


  79.       //tx传值,通过蓝牙notify不间断发送通知
  80.       Serial2.print(char(Serial1.peek()));
  81.       pTxCharacteristic->setValue(&txValue, Serial1.read());
  82.       pTxCharacteristic->notify();
  83.       // if(x>10){
  84.       //   Serial2.println();
  85.       //   Serial2.println("Error!!!");
  86.       //   delay(10000000000);
  87.       // }
  88.       delay(10);
  89.     }
  90.     // // }
  91.     //   float a=1000/((millis()-time)/1000);
  92.     //   Serial2.printf("Frequency domain:%f /n",a);
  93.     //   delay(1000000);
  94.   }
  95.   // disconnecting
  96.   if (!deviceConnected && oldDeviceConnected)
  97.   {
  98.     delay(500);                  // 让蓝牙堆栈有机会做好准备
  99.     pServer->startAdvertising(); // 重启广播
  100.     Serial2.println("start advertising");
  101.     oldDeviceConnected = deviceConnected;
  102.   }
  103.   // connecting
  104.   if (deviceConnected && !oldDeviceConnected)
  105.   {
  106.     // do stuff here on connecting在连接上做点事情
  107.     oldDeviceConnected = deviceConnected;
  108.     Serial2.println("start advertising");
  109.   }
  110. }
复制代码



理想的蓝牙情况是这样的,把其它微控制器传上来的值(sEMG和6个数值)通过UART发送到ESP32,ESP32再把UART缓冲区的单个字符一个个的当通知信息发出去:


请问咋解决啊,为啥蓝牙接收同一个值,还是乱码(;′д`)ゞ

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:301191 发表于 2022-3-19 16:51 | 只看该作者
顶一下
回复

使用道具 举报

板凳
ID:752974 发表于 2022-3-21 08:52 | 只看该作者
注意字符与HEX的转换。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表