_深蓝_ 发表于 2024-5-14 11:22:33

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与手机之间的数据传输,如果需要修改或使用数据,只需更改数据接收部分或数据发送部分代码
    Video: https://www.youtube.com/watch?v=oCMOYS71NIU
    Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp
    Ported to Arduino ESP32 by Evandro Copercini

   Create a BLE server that, once we receive a connection, will send periodic notifications.
   The service advertises itself as: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E
   Has a characteristic of: 6E400002-B5A3-F393-E0A9-E50E24DCCA9E - used for receiving data with "WRITE"
   Has a characteristic of: 6E400003-B5A3-F393-E0A9-E50E24DCCA9E - used to send data with"NOTIFY"

   The design of creating the BLE server is:
   1. Create a BLE Server
   2. Create a BLE Service
   3. Create a BLE Characteristic on the Service
   4. Create a BLE Descriptor on the characteristic
   5. Start the service.
   6. Start advertising.

*/

/* 该示例演示了蓝牙数据透传,烧录代码,打开串口监视器,打开手机的BLE调试助手
* 1.即可看见ESP32发送的数据--见APP使用图
* 2.通过BLE调试助手的输入框可向ESP32发送数据--见APP使用图
* 该示例由BLE_uart示例更改而来
*/

#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>

BLEServer *pServer = NULL;
BLECharacteristic * pTxCharacteristic;
bool deviceConnected = 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 "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"


//蓝牙连接/断开处理。当有连接/断开事件发生时自动触发
class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {   //当蓝牙连接时会执行该函数
      Serial.println("蓝牙已连接");
      deviceConnected = true;
    };

    void onDisconnect(BLEServer* pServer) {//当蓝牙断开连接时会执行该函数
      Serial.println("蓝牙已断开");
      deviceConnected = false;
      delay(500); // give the bluetooth stack the chance to get things ready
      pServer->startAdvertising(); // restart advertising

    }
};

/****************数据接收部分*************/
/****************************************/
//蓝牙接收数据处理。当收到数据时自动触发
class MyCallbacks: public BLECharacteristicCallbacks {
    void onWrite(BLECharacteristic *pCharacteristic) {
      std::string rxValue = pCharacteristic->getValue();//接收数据,并赋给rxValue

      //if(rxValue == "ON"){Serial.println("开灯");}   //判断接收的字符是否为"ON"

      if (rxValue.length() > 0) {
      Serial.println("*********");
      Serial.print("Received Value: ");
      for (int i = 0; i < rxValue.length(); i++){
          Serial.print(rxValue);
      }
      Serial.println();
      Serial.println("*********");
      }
    }
};
/***************************************/
/****************************************/


void setup() {
Serial.begin(115200);
BLEBegin();//初始化蓝牙

}

void loop() {
/****************数据发送部分*************/
/****************************************/
if (deviceConnected) {//如果有蓝牙连接,就发送数据
    pTxCharacteristic->setValue("Hello");//发送字符串
    pTxCharacteristic->notify();
    delay(10); // bluetooth stack will go into congestion, if too many packets are sent

    pTxCharacteristic->setValue("DFRobot");//发送字符串
    pTxCharacteristic->notify();
    delay(10); // bluetooth stack will go into congestion, if too many packets are sent
}
/****************************************/
/****************************************/
}


void BLEBegin(){
// Create the BLE Device
BLEDevice::init(/*BLE名称*/"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();
Serial.println("Waiting a client connection to notify...");
}环境都合适的。


烦请大神解决下




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_DFR1075_FireBeetle_2_Board_ESP32_C6_Advanced_Tutorial#target_2
官方的

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

问题解决了,原因是SDK 3.0.0的底层API发生了变化。
你重新上传wiki的代码吧
页: [1]
查看完整版本: ESP32-C6与手机蓝牙通信报错