Reading BLE characteristics on server


I have a Xiao ESP32S3 Sense. I am trying use BLE to execute code based on the value of a characteristic.
I find this to be very difficult, and I’m sure that’s because I’ve missed something simple. The server example works unmodified.

I add a global declaration of pCharacteristic so that I can refer to it in loop().
Then I add an if function to turn an LED on/off based on the characteristic. This is essentially pseudocode, it doesn’t work. I get string conversion errors. I’ve also tried simply printing the characteristic value to serial, but I can’t find a conversion that works.

Is there a simpler way to do something like this, or a string conversion to either string og int that works?

Here is the code:

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

// See the following for generating UUIDs:

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
BLECharacteristic* pCharacteristic = NULL;

void setup() {
  pinMode(D2, OUTPUT);
  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(
                                         BLECharacteristic::PROPERTY_READ |

  // BLEAdvertising *pAdvertising = pServer->getAdvertising();  // this still is working for backward compatibility
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->setMinPreferred(0x06);  // functions that help with iPhone connections issue
  Serial.println("Characteristic defined! Now you can read it in your phone!");

void loop() {
  int value = pCharacteristic->getValue();
  if (value == 1) { //if characteristic is 1, set lLED to on
    digitalWrite(D2, HIGH);
  } else {
    digitalWrite(D2, LOW);

And the error:

Compilation error: cannot convert 'std::__cxx11::string' {aka 'std::__cxx11::basic_string<char>'} to 'int' in initialization

Hi there,
Do you want to read the Advertisement , only ? Like scan for a flag and use the presents of it to turn on the LED?
Typically the server advertises a service and a characteristic that’s relevant with Read, Write or Notify. Strings , boleens, numeri’s :wink:
You may want to look at the LED examples and see how the handshake and flow works, but your NOT to far away.
Check out the FreeFall code and demo.
GL :slight_smile: PJ :v:

Here is the most Basic example I know of.