[英]VSCode ESP32 type error in Serial.println
我决定为我的 ESP32 从 Arduino IDE 切换到 VSCode 和 PlatformIO。 我正在使用带有回调的 BLE 服务器示例作为测试:
#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);
}
我很确定当我在 Arduino IDE 中使用它时,它上传并运行良好,但在 VSCode 中出现错误:
no instance of overloaded function "HardwareSerial::println" matches the argument list -- argument types are: (std::__cxx11::string) -- object type is: HardwareSerial
除了BLECharacteristic
的getValue
返回std::__cxx11::string
。
我也尝试过#define _GLIBCXX_USE_CXX11_ABI 0
根据: Converting std::__cxx11::string to std::string但我得到了同样的错误,只是用std::string
代替。
如果getValue
返回正确的类型,为什么我无法将值打印到串行?
您看到的错误是抱怨编译器无法找到以std::__cxx11::string
作为参数的HardwareSerial
的println
方法版本。 问题不在于getValue()
方法返回错误的东西; 问题是Serial.println()
没有被写入接受getValue()
作为参数返回的内容。
您需要将蓝牙函数返回的std::__cxx11::string
转换或以其他方式转换为String
或char *
, println()
方法都接受这两者。 您可以使用.c_str()
方法来执行此操作。
if ((val.length() > 0)) {
Serial.println(val.c_str());
}
您可以通过阅读有关Serial.println
和std::__cxx11::string
的文档来找到它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.