C++ Serial.println中的VSCode ESP32类型错误
我决定为我的ESP32从ArduinoIDE切换到VSCode和平台。我使用带有回调的BLE服务器示例作为测试:C++ Serial.println中的VSCode ESP32类型错误,c++,visual-studio-code,arduino,esp32,C++,Visual Studio Code,Arduino,Esp32,我决定为我的ESP32从ArduinoIDE切换到VSCode和平台。我使用带有回调的BLE服务器示例作为测试: #include <Arduino.h> #include <BLEDevice.h> #include <BLEUtils.h> #include <BLEServer.h> #define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" #
#include <Arduino.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
class MyCallBack: public BLECharacteristicCallbacks {
void onRead(BLECharacteristic *pCharacteristic) {
std::__cxx11::string val = pCharacteristic->getValue();
if ((val.length() > 0)) {
Serial.println(val);
}
}
};
void setup() {
Serial.begin(115200);
Serial.println("Starting BLE work!");
BLEDevice::init("Long name works now");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ
);
pCharacteristic->setValue("Hello World!");
pCharacteristic->setCallbacks(new MyCallBack());
pService->start();
// BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
pAdvertising->setMinPreferred(0x12);
BLEDevice::startAdvertising();
Serial.println("Characteristic defined! Now you can read it in your phone!");
}
void loop() {
// put your main code here, to run repeatedly:
delay(2000);
}
除了BLECharacteristic
的getValue
之外,返回std::\uu cxx11::string
我还尝试了#define_GLIBCXX_USE_cx11_ABI 0
,根据:但我只是用std::string
得到了相同的错误
如果
getValue
返回正确的类型,为什么我无法将值打印到序列号?您看到的错误是编译器无法找到hardwareseerial
的println
方法的版本,该方法将std:\ucxx11::string
作为参数。问题不在于getValue()
方法返回了错误的东西;问题在于,Serial.println()
不是为了接受getValue()
作为参数返回的内容而编写的
您需要强制转换或以其他方式转换蓝牙功能正在返回的字符串或字符*
,这两种方法都是println()
方法接受的。您可以使用.c_str()
方法来执行此操作
if ((val.length() > 0)) {
Serial.println(val.c_str());
}
您可以通过阅读Serial.println
和std::uu cxx11::string
上的文档来找到这一点。我想我把错误解释错了。当它谈到参数类型时,我认为这是它想要的类型,但实际上这就是我给它的类型。使用c_str
方法有效-谢谢!
if ((val.length() > 0)) {
Serial.println(val.c_str());
}