NimBLE-Arduino icon indicating copy to clipboard operation
NimBLE-Arduino copied to clipboard

Advertisment Problem with 128bit UUIDs

Open rainerfritz opened this issue 5 months ago • 1 comments
trafficstars

Hi!

I am using the nordic uart service and characteristic UUID which is:

#define SERVICE_UUID           "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"

With the newest 2.3.0 and 2.3.1 it throws an error on advertising as shown below and especially on ESP32-S3 based boards like the Heltec Wifi Lora v3 for example and similar, advertising is not enabled and so they disappear when doing a scan and can not be connected anymore. Is the only solution to use a 16bit UUID? If yes, on BLE what would be recommended as BLE UART service as I can not find it in the official documentation of the BLE SIG.

Thanks for any help in advance!

Here is the log:

[BLE ]...Device started with BLE-Name <MC-f25c-OE1KFR-6>
I NimBLEDevice: BLE Host Task Started
I NimBLEDevice: NimBle host synced.
[BLE ]...Device-Address <64:e8:33:69:f2:5d>
D NimBLEDevice: Setting bonding: 0, mitm: 0, sc: 0
D NimBLEService: >> start(): Starting service: UUID: 6e400001-b5a3-f393-e0a9-e50e24dcca9e, handle: 0x0000
D NimBLEService: Adding 2 characteristics for service UUID: 6e400001-b5a3-f393-e0a9-e50e24dcca9e, handle: 0x0000
D NimBLEService: << start()
E NimBLEAdvertisementData: Data length exceeded
E NimBLEAdvertisementData: Cannot add UUID, data length exceeded!
E NimBLEAdvertisementData: Cannot add UUID, data length exceeded!
D NimBLEAdvertising: >> Advertising start: duration=0, dirAddr=NULL
primary service
           uuid 0x1800
         handle 1
     end_handle 5
characteristic
           uuid 0x2a00
     def_handle 2
     val_handle 3
   min_key_size 0
          flags [READ]
characteristic
           uuid 0x2a01
     def_handle 4
     val_handle 5
   min_key_size 0
          flags [READ]
primary service
           uuid 0x1801
         handle 6
     end_handle 13
characteristic
           uuid 0x2a05
     def_handle 7
     val_handle 8
   min_key_size 0
          flags [INDICATE]
ccc descriptor
           uuid 0x2902
         handle 9
   min_key_size 0
          flags [READ|WRITE]
characteristic
           uuid 0x2b3a
     def_handle 10
     val_handle 11
   min_key_size 0
          flags [READ]
characteristic
           uuid 0x2b29
     def_handle 12
     val_handle 13
   min_key_size 0
          flags [READ|WRITE]
primary service
           uuid 6e400001-b5a3-f393-e0a9-e50e24dcca9e
         handle 14
     end_handle 20
characteristic
           uuid 6e400003-b5a3-f393-e0a9-e50e24dcca9e
     def_handle 15
     val_handle 16
   min_key_size 0
          flags [READ|WRITE|NOTIFY]
ccc descriptor
           uuid 0x2902
         handle 17
   min_key_size 0
          flags [READ|WRITE]
characteristic
           uuid 6e400002-b5a3-f393-e0a9-e50e24dcca9e
     def_handle 18
     val_handle 19
   min_key_size 0
          flags [READ|WRITE|NOTIFY]
ccc descriptor
           uuid 0x2902
         handle 20
   min_key_size 0
          flags [READ|WRITE]
D NimBLEAdvertising: setAdvertisementData: 02 01 06 11 09 4d 43 2d 66 32 35 63 2d 4f 45 31 4b 46 52 2d 36
D NimBLEAdvertising: setScanResponseData: 13 ff 4d 43 4d 43 2d 66 32 35 63 2d 4f 45 31 4b 46 52 2d 36
E NimBLEAdvertising: Error enabling advertising; rc=519, 

rainerfritz avatar Jun 15 '25 14:06 rainerfritz

Your device name is too long to add a 128 bit service uuid, either remove or shorten the name or put it in the scan response.

h2zero avatar Jun 15 '25 14:06 h2zero

I am encountering rc=519 when starting advertising for the third time, using NimBLE version 2.3.2. Am I missing something? Any help would be appreciated.

#include "esp32-hal.h"
#include "esp_log.h"
#include <Arduino.h>
#include <esp_ota_ops.h>

#include "NimBLECharacteristic.h"
#include "NimBLEService.h"
#include "NimBLEUUID.h"
#include <NimBLEDevice.h>
#include <NimBLEExtAdvertising.h>
#include <string>
#include <strings.h>

#define SERIAL_TIMEOUT 100 // in milliseconds
#define START_DELAY 1000   // in milliseconds

static const char *TAG = "main";

static char errBuffer[30];

// BLE PHY configuration
extern uint8_t primaryPhy;
extern uint8_t secondaryPhy;
extern NimBLEExtAdvertisement extAdv;
NimBLEServer *InitBLE();
bool StartAdvertising();
uint16_t GetConnHandle();

String msg;

void setup() {
  Serial.begin(115200);
  Serial.setTimeout(SERIAL_TIMEOUT);

  InitBLE();

  bool ok = StartAdvertising();
}

void loop() { delay(100); }

uint8_t primaryPhy = BLE_HCI_LE_PHY_1M;
uint8_t secondaryPhy = BLE_HCI_LE_PHY_1M;

NimBLEExtAdvertising *pAdvertising = nullptr;

NimBLEExtAdvertisement extAdv(primaryPhy, secondaryPhy);

#define EXTENDED_INSTANCE 0
#define LIGHT_SERVICE "3f9015f2-cc5e-4cb8-9332-a0b821f83052"
#define COLOR_RGB_CHARACTERISTIC "80be3f4e-d48e-48c4-be9c-c117ba27e6a9"

static uint16_t connHandle;

class ServerCallbacks : public NimBLEServerCallbacks {
  void onConnect(NimBLEServer *pServer, NimBLEConnInfo &connInfo) {
    NimBLEAddress centralAddr = connInfo.getAddress();
    ESP_LOGI(TAG, "central connected: %s", centralAddr.toString().c_str());
    connHandle = connInfo.getConnHandle();
    pServer->updateConnParams(connHandle, 24, 40, 0, 500);
    ESP_LOGI(TAG, "Connected count: %d", pServer->getConnectedCount());
    if (pAdvertising->start(EXTENDED_INSTANCE)) {
      ESP_LOGI(TAG, "Started advertising for instace 0");
    } else {
      // Serial.printf("Failed to start advertising for instace 0\n");
      ESP_LOGE(TAG, "Failed to start advertising for instace 0");
    }
  };

  void onDisconnect(NimBLEServer *pServer, NimBLEConnInfo &connInfo, int reason) {
    NimBLEAddress centralAddr = connInfo.getAddress();
    ESP_LOGI(TAG, "central disconnected: %s", centralAddr.toString().c_str());
    ESP_LOGI(TAG, "Connected count: %d", pServer->getConnectedCount());

    if (pAdvertising->start(EXTENDED_INSTANCE)) {
      ESP_LOGI(TAG, "Started advertising for instace 0");
    } else {
      ESP_LOGE(TAG, "Failed to start advertising for instace 0");
    }
  };
};

class CharacteristicCallbacks : public NimBLECharacteristicCallbacks {
  void onRead(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo) {};

  void onWrite(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo) {

  };
};

static CharacteristicCallbacks chrCallbacks;

void addServices(NimBLEServer *pServer);

NimBLEServer *InitBLE() {
  NimBLEDevice::init("VST-CL");

  // Get the device mac address
  NimBLEAddress address = NimBLEDevice::getAddress();

  // const uint8_t *native = address.getNative();
  const uint8_t *native = address.getBase()->val;

  char buffer[14];
  sprintf(buffer, "VST-CL-%02X%02X%02X", native[2], native[1], native[0]);
  NimBLEDevice::setDeviceName(buffer);
  ESP_LOGI(TAG, "Device name: %s", buffer);

  NimBLEDevice::setPower(ESP_PWR_LVL_P9); /** +9db*/
  NimBLEDevice::setSecurityAuth(true, false, true);
  NimBLEDevice::setSecurityIOCap(BLE_HS_IO_NO_INPUT_OUTPUT);

  NimBLEServer *pServer = NimBLEDevice::createServer();
  addServices(pServer);

  extAdv.setConnectable(true);

  extAdv.setTxPower(9);
  extAdv.addTxPower();
  extAdv.setMinInterval(0x20);
  extAdv.setMaxInterval(0x40);
  return pServer;
}

void addServices(NimBLEServer *pServer) {
  pServer->setCallbacks(new ServerCallbacks());

  // Setup Light service and its characteristrics
  NimBLEService *pLightService = pServer->createService(NimBLEUUID(LIGHT_SERVICE));

  NimBLECharacteristic *pColorRGBChar =
      pLightService->createCharacteristic(NimBLEUUID(COLOR_RGB_CHARACTERISTIC), READ | WRITE, 3);
  pColorRGBChar->setCallbacks(&chrCallbacks);

  pLightService->start();
  extAdv.setServiceData(NimBLEUUID(LIGHT_SERVICE), "");
}

bool StartAdvertising() {
  pAdvertising = NimBLEDevice::getAdvertising();

  if (pAdvertising->setInstanceData(EXTENDED_INSTANCE, extAdv)) {
    if (pAdvertising->start(EXTENDED_INSTANCE)) {
      ESP_LOGI(TAG, "Started advertising for instace 0");
    } else {
      ESP_LOGE(TAG, "Failed to start advertising for instace 0");
      return false;
    }
  }

  else {
    Serial.printf("Failed to register to register advertisement data\n");
  }
  return true;
}

uint16_t GetConnHandle() { return connHandle; }

##Serial logs

[18:41:57:260] D NimBLEServer: >> handleGapEvent: ␍␊
[18:41:57:260] D NimBLEExtAdvertisingCallbacks: onStopped: Default␍␊
[18:41:57:319] D NimBLEServer: >> handleGapEvent: ␍␊
[18:41:57:321] [ 17690][I][firmware.ino:85] onConnect(): [main] central connected: 60:45:2e:e2:aa:e4␍␊
[18:41:57:330] [ 17701][I][firmware.ino:88] onConnect(): [main] Connected count: 1␍␊
[18:41:57:336] [ 17707][I][firmware.ino:90] onConnect(): [main] Started advertising for instace 0␍␊
[18:41:57:343] D NimBLEServer: << handleGapEvent␍␊
[18:41:57:346] D NimBLEServer: >> handleGapEvent: ␍␊
[18:41:57:346] D NimBLEServer: << handleGapEvent␍␊
[18:41:57:363] D NimBLEServer: >> handleGapEvent: ␍␊
[18:41:57:363] D NimBLEServer: << handleGapEvent␍␊
[18:41:57:367] D NimBLEServer: >> handleGapEvent: ␍␊
[18:41:57:367] I NimBLEServer: mtu update event; conn_handle=1 mtu=255␍␊
[18:41:57:371] D NimBLEServerCallbacks: onMTUChange(): Default␍␊
[18:41:57:374] D NimBLEServer: << handleGapEvent␍␊
[18:41:57:737] D NimBLEServer: >> handleGapEvent: ␍␊
[18:41:57:737] D NimBLEServerCallbacks: onConnParamsUpdate: default␍␊
[18:41:57:742] D NimBLEServer: << handleGapEvent␍␊
[18:41:58:174] D NimBLEServer: >> handleGapEvent: ␍␊
[18:41:58:174] D NimBLEServer: << handleGapEvent␍␊
[18:41:58:182] D NimBLEServer: >> handleGapEvent: ␍␊
[18:41:58:182] D NimBLEServerCallbacks: onAuthenticationComplete: default␍␊
[18:41:58:185] D NimBLEServer: << handleGapEvent␍␊
[18:41:58:939] D NimBLEServer: >> handleGapEvent: ␍␊
[18:41:58:943] I NimBLEServer: subscribe event; attr_handle=8, subscribed: true␍␊
[18:41:58:943] D NimBLEServer: << handleGapEvent␍␊
[18:42:07:231] D NimBLEServer: >> handleGapEvent: ␍␊
[18:42:07:231] D NimBLEExtAdvertisingCallbacks: onStopped: Default␍␊
[18:42:07:289] D NimBLEServer: >> handleGapEvent: ␍␊
[18:42:07:293] [ 27660][I][firmware.ino:85] onConnect(): [main] central connected: 04:cf:4b:2f:f8:2c␍␊
[18:42:07:300] [ 27671][I][firmware.ino:88] onConnect(): [main] Connected count: 2␍␊
[18:42:07:306] E NimBLEExtAdvertising: Error enabling advertising; rc=519, ␍␊
[18:42:07:311] [ 27678][E][firmware.ino:93] onConnect(): [main] Failed to start advertising for instace 0␍␊
[18:42:07:320] D NimBLEServer: << handleGapEvent␍␊
[18:42:07:330] D NimBLEServer: >> handleGapEvent: ␍␊
[18:42:07:330] D NimBLEServer: << handleGapEvent␍␊
[18:42:07:340] D NimBLEServer: >> handleGapEvent: ␍␊
[18:42:07:340] D NimBLEServer: << handleGapEvent␍␊
[18:42:07:739] D NimBLEServer: >> handleGapEvent: ␍␊
[18:42:07:739] D NimBLEServerCallbacks: onConnParamsUpdate: default␍␊
[18:42:07:745] D NimBLEServer: << handleGapEvent␍␊
[18:42:07:745] D NimBLEServer: >> handleGapEvent: ␍␊
[18:42:07:745] D NimBLEServer: << handleGapEvent␍␊
[18:42:07:754] D NimBLEServer: >> handleGapEvent: ␍␊
[18:42:07:760] [ 28120][I][firmware.ino:99] onDisconnect(): [main] central disconnected: 04:cf:4b:2f:f8:2c␍␊
[18:42:07:768] [ 28139][I][firmware.ino:100] onDisconnect(): [main] Connected count: 1␍␊
[18:42:07:775] [ 28146][I][firmware.ino:103] onDisconnect(): [main] Started advertising for instace 0␍␊
[18:42:07:782] D NimBLEServer: << handleGapEvent␍␊
[18:42:08:441] D NimBLEServer: >> handleGapEvent: ␍␊
[18:42:08:441] D NimBLEExtAdvertisingCallbacks: onStopped: Default␍␊
[18:42:08:504] D NimBLEServer: >> handleGapEvent: ␍␊
[18:42:08:510] [ 28875][I][firmware.ino:85] onConnect(): [main] central connected: 04:cf:4b:2f:f8:2c␍␊
[18:42:08:515] [ 28886][I][firmware.ino:88] onConnect(): [main] Connected count: 2␍␊
[18:42:08:521] E NimBLEExtAdvertising: Error enabling advertising; rc=519, ␍␊
[18:42:08:525] [ 28893][E][firmware.ino:93] onConnect(): [main] Failed to start advertising for instace 0␍␊
[18:42:08:535] D NimBLEServer: << handleGapEvent␍␊
[18:42:08:535] D NimBLEServer: >> handleGapEvent: ␍␊
[18:42:08:535] D NimBLEServer: << handleGapEvent␍␊
[18:42:08:594] D NimBLEServer: >> handleGapEvent: ␍␊
[18:42:08:594] D NimBLEServer: << handleGapEvent␍␊
[18:42:08:909] D NimBLEServer: >> handleGapEvent: ␍␊
[18:42:08:915] D NimBLEServerCallbacks: onConnParamsUpdate: default␍␊
[18:42:08:915] D NimBLEServer: << handleGapEvent␍␊
[18:42:08:915] D NimBLEServer: >> handleGapEvent: ␍␊
[18:42:08:919] D NimBLEServer: << handleGapEvent␍␊
[18:42:08:919] D NimBLEServer: >> handleGapEvent: ␍␊
[18:42:08:926] [ 29290][I][firmware.ino:99] onDisconnect(): [main] central disconnected: 04:cf:4b:2f:f8:2c␍␊
[18:42:08:937] [ 29309][I][firmware.ino:100] onDisconnect(): [main] Connected count: 1␍␊
[18:42:08:944] [ 29316][I][firmware.ino:103] onDisconnect(): [main] Started advertising for instace 0␍␊
[18:42:08:952] D NimBLEServer: << handleGapEvent␍␊
[18:44:48:679] D NimBLEServer: >> handleGapEvent: ␍␊
[18:44:48:679] I NimBLEServer: subscribe event; attr_handle=8, subscribed: false␍␊
[18:44:48:690] D NimBLEServer: << handleGapEvent␍␊
[18:44:48:690] D NimBLEServer: >> handleGapEvent: ␍␊
[18:44:48:697] [189055][I][firmware.ino:99] onDisconnect(): [main] central disconnected: 60:45:2e:e2:aa:e4␍␊
[18:44:48:702] [189074][I][firmware.ino:100] onDisconnect(): [main] Connected count: 0␍␊
[18:44:48:709] [189081][I][firmware.ino:103] onDisconnect(): [main] Started advertising for instace 0␍␊
[18:44:48:716] D NimBLEServer: << handleGapEvent␍␊

mani-slx avatar Jun 28 '25 13:06 mani-slx

But with the same constellation with the "long" device name, there ist no problem at ESP32 Modules. The problem seems to be at ESP32-S3

karamo avatar Jul 13 '25 09:07 karamo

With shorter Node-Name "MC-4eac" the same problem: no advertising.

karamo avatar Jul 13 '25 10:07 karamo

I tested this today with a C6 and it is advertising, however the servicedata does not show unless a value is provided: extAdv.setServiceData(NimBLEUUID(LIGHT_SERVICE), ""); < need to add some data.

Since the name isn't set here with extAdv.setName() then it will not be added to the advertisement and the device name will not show in a scan.

h2zero avatar Sep 02 '25 15:09 h2zero

When CONFIG_BT_NIMBLE_EXT_ADV is set to 1 to be able to use the class NimBLEExtAdvertising it throws the error that it is not supported on ESP32 because of:

#if CONFIG_BT_NIMBLE_EXT_ADV || CONFIG_BT_NIMBLE_ENABLE_PERIODIC_ADV
#  if defined(CONFIG_IDF_TARGET_ESP32)
#    error Extended advertising is not supported on ESP32.
#  endif
#endif

in nimconfig.h

Could you please give a working example on how to properly set scanresponse data?

rainerfritz avatar Oct 12 '25 10:10 rainerfritz