DreamingInsanity
DreamingInsanity

Reputation: 167

VSCode ESP32 type error in Serial.println

I decided to switch from the Arduino IDE to VSCode and PlatformIO for my ESP32. I am using the BLE Server example with a callback, as a test:

#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);
}

I'm pretty sure when I was using this in the Arduino IDE it uploaded and ran fine but in VSCode I am getting an error:

no instance of overloaded function "HardwareSerial::println" matches the argument list -- argument types are: (std::__cxx11::string) -- object type is: HardwareSerial

Except getValue of BLECharacteristic returns std::__cxx11::string.

I have also tried #define _GLIBCXX_USE_CXX11_ABI 0 according to: Converting std::__cxx11::string to std::string but I get the same error just with std::string instead.

If getValue returns the right type, why I am not able to print the value to the Serial?

Upvotes: 0

Views: 674

Answers (1)

romkey
romkey

Reputation: 7089

The error you're seeing is complaining that the compiler can't locate a version of the println method of HardwareSerial that takes std::__cxx11::string as an argument. The problem isn't that the getValue() method returns the wrong thing; the problem is that Serial.println() isn't written to accept what getValue() returns as an argument.

You need to cast or otherwise convert the std::__cxx11::string that the Bluetooth functions are returning to a String or char *, both of which the println() method accepts. You can use the .c_str() method to do this.

      if ((val.length() > 0)) {
        Serial.println(val.c_str());
      }

You could find this by reading documentation on Serial.println and std::__cxx11::string.

Upvotes: 1

Related Questions