ArduinoBLE doesn't work inside library

I am writing a custom library (with a custom service and two custom characteristics) which uses the ArduinoBLE library.

Running the code on Nano 33 iot all the characteristics are present and everything works as expected:

but when it runs on Uno R4 WiFi the custom characteristics are not included in the service:

I have already updated the radio module to V: 0.2.0 and updated the ArduinoBLE library (following instructions here: Radio module firmware version 0.2.0 is now available.

Below my code (simplified).


#include <ArduinoBLE.h>
#include "Lib.h"

Lib bleLib;

void setup() {
  Serial.begin(115200);
  while (!Serial)
    ;

  delay(500);
  bleLib.begin();
  Serial.println("Ready");
}

void loop() {
  BLE.poll(); 
  delay(200);
}

---- Library -----

Lib.h

#ifndef LIB_H
#define LIB_H

#include <ArduinoBLE.h>

class Lib {
private:
  BLEService mainService = BLEService("19B10000-E8F2-537E-4F6C-D104768A1214");
  BLECharacteristic rxCharacteristic = BLECharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLEWrite, 20, true);
  BLECharacteristic txCharacteristic = BLECharacteristic("19B10002-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify, 20, true);

  BLEService batteryService = BLEService("180F");
  BLEUnsignedCharCharacteristic batteryLevelChar = BLEUnsignedCharCharacteristic("2A19", BLERead | BLENotify);

  bool isConnected;

public:
  Lib(void);
  void begin(void);

  void connected(void);
  void disconnected(void);
  void dataAvailable(String string);
};

#endif

Lib.cpp

#include <BLECharacteristic.h>
#include "Lib.h"

Lib *myGlobal;

void connectHandler(BLEDevice central);
void disconnectHandler(BLEDevice central);
void characteristicWritten(BLEDevice central, BLECharacteristic characteristic);

Lib::Lib(void) {
  isConnected = false;
  myGlobal = this;
};

void Lib::begin(void) {

  // Initialise the NINA module
  Serial.println("Initializing BLE module");

  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");
    return;
  }
  BLE.setLocalName("AManager");
  BLE.setAdvertisedService(mainService);


  mainService.addCharacteristic(rxCharacteristic);
  delay(1000);

  mainService.addCharacteristic(txCharacteristic);
  delay(1000);

  BLE.addService(mainService);
  delay(1000);


  batteryService.addCharacteristic(batteryLevelChar);
  delay(1000);

  BLE.addService(batteryService);
  delay(1000);

  BLE.setEventHandler(BLEConnected, connectHandler);
  BLE.setEventHandler(BLEDisconnected, disconnectHandler);
  rxCharacteristic.setEventHandler(BLEWritten, characteristicWritten);

  BLE.advertise();

  Serial.println("Advertising started");
};

void Lib::connected(void) {
  isConnected = true;
};
void Lib::disconnected(void) {
  isConnected = false;
};

void Lib::dataAvailable(String data) {
  Serial.println(data);
}

////////////////

void connectHandler(BLEDevice central) {
  // central connected event handler
  myGlobal->connected();
  Serial.print("Connected event, central: ");
  Serial.println(central.address());
}

void disconnectHandler(BLEDevice central) {
  // central disconnected event handler
  myGlobal->disconnected();
  Serial.print("Disconnected event, central: ");
  Serial.println(central.address());
}

void characteristicWritten(BLEDevice central, BLECharacteristic characteristic) {

  Serial.print("Characteristic event, written: ");

  char buffer[40];
  int n = characteristic.readValue(buffer, sizeof(buffer));
  memset(&buffer[n], 0, sizeof(buffer) - n);
  String d = String(buffer);

  Serial.print("R >");
  Serial.print(d);
  Serial.print("< ");
  Serial.println(n);

  myGlobal->dataAvailable(d);
}

Thanks for testing the beta version of the ArduinoBLE library and for your feedback @fab64! I added a note to the pull request about your feedback to bring it to the attention of the developer:

In order to make all relevant information available to any who are interested in this subject, I'll share a link to the dedicated issue report here: