1178浏览
查看: 1178|回复: 3

[求助问答] ESP32-C6与手机蓝牙通信报错

[复制链接]
ESP32-C6与手机蓝牙通信报错
错误类型:error: conversion from 'String' to non-scalar type 'std::string' {aka 'std::__cxx11::basic_string<char>'} requested
终端错误详情:
C:\Users\jock\Documents\Arduino\c6\bluetoothtouch\bluetoothtouch.ino: In member function 'virtual void MyCallbacks::onWrite(BLECharacteristic*)':
bluetoothtouch:67:54: error: conversion from 'String' to non-scalar type 'std::string' {aka 'std::__cxx11::basic_string<char>'} requested
   67 |       std::string rxValue = pCharacteristic->getValue();//接收数据,并赋给rxValue
      |                             ~~~~~~~~~~~~~~~~~~~~~~~~~^~
exit status 1
conversion from 'String' to non-scalar type 'std::string' {aka 'std::__cxx11::basic_string<char>'} requested

错误截图:
ESP32-C6与手机蓝牙通信报错图1

代码:
  1. /*
  2.     使用该实例演示ESP32-C6与手机之间的数据传输,如果需要修改或使用数据,只需更改数据接收部分或数据发送部分代码
  3.     Video: https://www.youtube.com/watch?v=oCMOYS71NIU
  4.     Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp
  5.     Ported to Arduino ESP32 by Evandro Copercini
  6.    Create a BLE server that, once we receive a connection, will send periodic notifications.
  7.    The service advertises itself as: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E
  8.    Has a characteristic of: 6E400002-B5A3-F393-E0A9-E50E24DCCA9E - used for receiving data with "WRITE"
  9.    Has a characteristic of: 6E400003-B5A3-F393-E0A9-E50E24DCCA9E - used to send data with  "NOTIFY"
  10.    The design of creating the BLE server is:
  11.    1. Create a BLE Server
  12.    2. Create a BLE Service
  13.    3. Create a BLE Characteristic on the Service
  14.    4. Create a BLE Descriptor on the characteristic
  15.    5. Start the service.
  16.    6. Start advertising.
  17. */
  18. /* 该示例演示了蓝牙数据透传,烧录代码,打开串口监视器,打开手机的BLE调试助手
  19. * 1.即可看见ESP32发送的数据--见APP使用图
  20. * 2.通过BLE调试助手的输入框可向ESP32发送数据--见APP使用图
  21. * 该示例由BLE_uart示例更改而来
  22. */
  23. #include <BLEDevice.h>
  24. #include <BLEServer.h>
  25. #include <BLEUtils.h>
  26. #include <BLE2902.h>
  27. BLEServer *pServer = NULL;
  28. BLECharacteristic * pTxCharacteristic;
  29. bool deviceConnected = false;
  30. uint8_t txValue = 0;
  31. // See the following for generating UUIDs:
  32. // https://www.uuidgenerator.net/
  33. #define SERVICE_UUID           "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
  34. #define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
  35. #define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
  36. //蓝牙连接/断开处理。当有连接/断开事件发生时自动触发
  37. class MyServerCallbacks: public BLEServerCallbacks {
  38.     void onConnect(BLEServer* pServer) {   //当蓝牙连接时会执行该函数
  39.       Serial.println("蓝牙已连接");
  40.       deviceConnected = true;
  41.     };
  42.     void onDisconnect(BLEServer* pServer) {  //当蓝牙断开连接时会执行该函数
  43.       Serial.println("蓝牙已断开");
  44.       deviceConnected = false;
  45.       delay(500); // give the bluetooth stack the chance to get things ready
  46.       pServer->startAdvertising(); // restart advertising
  47.     }
  48. };
  49. /****************数据接收部分*************/
  50. /****************************************/
  51. //蓝牙接收数据处理。当收到数据时自动触发
  52. class MyCallbacks: public BLECharacteristicCallbacks {
  53.     void onWrite(BLECharacteristic *pCharacteristic) {
  54.       std::string rxValue = pCharacteristic->getValue();//接收数据,并赋给rxValue
  55.       //if(rxValue == "ON"){Serial.println("开灯");}   //判断接收的字符是否为"ON"
  56.       if (rxValue.length() > 0) {
  57.         Serial.println("*********");
  58.         Serial.print("Received Value: ");
  59.         for (int i = 0; i < rxValue.length(); i++){
  60.           Serial.print(rxValue[i]);
  61.         }
  62.         Serial.println();
  63.         Serial.println("*********");
  64.       }
  65.     }
  66. };
  67. /***************************************/
  68. /****************************************/
  69. void setup() {
  70.   Serial.begin(115200);
  71.   BLEBegin();  //初始化蓝牙
  72. }
  73. void loop() {
  74. /****************数据发送部分*************/
  75. /****************************************/
  76.   if (deviceConnected) {  //如果有蓝牙连接,就发送数据
  77.     pTxCharacteristic->setValue("Hello");  //发送字符串
  78.     pTxCharacteristic->notify();
  79.     delay(10); // bluetooth stack will go into congestion, if too many packets are sent
  80.     pTxCharacteristic->setValue("DFRobot");  //发送字符串
  81.     pTxCharacteristic->notify();
  82.     delay(10); // bluetooth stack will go into congestion, if too many packets are sent
  83.   }
  84. /****************************************/
  85. /****************************************/
  86. }
  87. void BLEBegin(){
  88.   // Create the BLE Device
  89.   BLEDevice::init(/*BLE名称*/"UART Service");
  90.   // Create the BLE Server
  91.   pServer = BLEDevice::createServer();
  92.   pServer->setCallbacks(new MyServerCallbacks());
  93.   // Create the BLE Service
  94.   BLEService *pService = pServer->createService(SERVICE_UUID);
  95.   // Create a BLE Characteristic
  96.   pTxCharacteristic = pService->createCharacteristic(
  97.                     CHARACTERISTIC_UUID_TX,
  98.                     BLECharacteristic::PROPERTY_NOTIFY
  99.                   );
  100.   pTxCharacteristic->addDescriptor(new BLE2902());
  101.   BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
  102.                       CHARACTERISTIC_UUID_RX,
  103.                       BLECharacteristic::PROPERTY_WRITE
  104.                     );
  105.   pRxCharacteristic->setCallbacks(new MyCallbacks());
  106.   // Start the service
  107.   pService->start();
  108.   // Start advertising
  109.   pServer->getAdvertising()->start();
  110.   Serial.println("Waiting a client connection to notify...");
  111. }
复制代码
环境都合适的。


烦请大神解决下




YeezB  初级技师

发表于 2024-5-15 10:45:07

哪个网页找到的代码?网页或者打开的路径分享一下。是不是用了基于V2 SDK的代码,C6只能用V3.0.0以上的SDK,有些函数不适配的。
回复

使用道具 举报

_深蓝_  高级技师
 楼主|

发表于 2024-5-23 08:51:59

YeezB 发表于 2024-5-15 10:45
哪个网页找到的代码?网页或者打开的路径分享一下。是不是用了基于V2 SDK的代码,C6只能用V3.0.0以上的SDK ...

https://wiki.dfrobot.com.cn/_SKU ... d_Tutorial#target_2
官方的
回复

使用道具 举报

YeezB  初级技师

发表于 2024-5-31 16:40:24

问题解决了,原因是SDK 3.0.0的底层API发生了变化。
你重新上传wiki的代码吧
FriMay-202405311474..png
回复

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies |上传

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail