nimBLE service definition failing

tigr88
Posts: 2
Joined: Fri Mar 28, 2025 7:58 pm

nimBLE service definition failing

Postby tigr88 » Fri Mar 28, 2025 8:07 pm

I have a fairly simple use-case, trying to get BLE working on an ESP32-CAM.
I have nimBLE settings configured properly.

My issue is that no matter what I try:

Code: Untitled.cpp Select all


int rc = ble_gatts_count_cfg(serial_service);
if (rc != 0) {
ESP_LOGE(instance->TAG, "Failed to count BLE services: %d", rc);
}
returns:

Code: Untitled.cpp Select all

E (4026) BLE_INIT: Failed to count BLE services: 3
My service definitions are as follows:

Code: Untitled.cpp Select all

// Define static handles
uint16_t serial_tx_handle = 0;
uint16_t serial_rx_handle = 0;

// Define UUIDs for the custom BLE Serial Service and its characteristics
const ble_uuid128_t serial_service_uuid =
BLE_UUID128_INIT( 0x12, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);

const ble_uuid128_t serial_tx_uuid =
BLE_UUID128_INIT( 0x56, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);

const ble_uuid128_t serial_rx_uuid =
BLE_UUID128_INIT( 0x9A, 0xBC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);

static ble_uuid_t *uuid_ss = (ble_uuid_t *)&serial_service_uuid;
static ble_uuid_t *uuid_tx = (ble_uuid_t *)&serial_tx_uuid;
static ble_uuid_t *uuid_rx = (ble_uuid_t *)&serial_rx_uuid;

static char received_bt_data[MAX_BT_DATA_LEN];
int onWriteCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) {
struct os_mbuf *om = ctxt->om;

ESP_LOGI("BLE", "Received data on TX characteristic");

size_t copy_len = (om->om_len < MAX_BT_DATA_LEN - 1) ? om->om_len : (MAX_BT_DATA_LEN - 1);
memset(received_bt_data, 0, MAX_BT_DATA_LEN);
ble_hs_mbuf_to_flat(om, received_bt_data, copy_len, NULL);

// Ensure null termination & remove trailing newline/carriage return
received_bt_data[copy_len] = '\0';
while (copy_len > 0 && (received_bt_data[copy_len - 1] == '\n' || received_bt_data[copy_len - 1] == '\r')) {
received_bt_data[--copy_len] = '\0';
}

ESP_LOGI("BLE", "Received data: %s", received_bt_data);
return 0;
}

// Static characteristic definitions to ensure lifetime
static struct ble_gatt_chr_def serial_service_characteristics[] = {
{
.uuid = uuid_tx,
.access_cb = onWriteCallback,
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
.val_handle = &serial_tx_handle,
},
{
.uuid = uuid_rx,
.access_cb = nullptr,
.flags = BLE_GATT_CHR_F_NOTIFY,
.val_handle = &serial_rx_handle,
},
{ 0 } // End of characteristics list
};

// Define static BLE GATT service array
static struct ble_gatt_svc_def serial_service[] = {
{
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = uuid_ss,
.characteristics = serial_service_characteristics // Use the static characteristics array
},
{ 0 } // End of service list
};
in main, after initializing UART and the camera:

Code: Untitled.cpp Select all

  ESP_LOGI(TAG, "Initializing NVS...");
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ESP_ERROR_CHECK(nvs_flash_init());
}
ble_init.init();
ble_init() is simple:

Code: Untitled.cpp Select all

void BLEHandler::init() {
ESP_LOGI(TAG, "Initializing BLE stack...");
// Initialize the NimBLE stack (must be done before using any BLE functions)
esp_err_t err = nimble_port_init();
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize NimBLE port. Error code: %d", err);
return;
}
else {
ESP_LOGI(instance->TAG, "Nimble port initialized");
}

ble_hs_cfg.sync_cb = onSync;
ble_hs_cfg.reset_cb = onReset;

ESP_LOGI(instance->TAG, "initialize host");
ble_hs_init();

ESP_LOGI(instance->TAG, "Init gap...");
ble_svc_gap_init();
ESP_LOGI(instance->TAG, "Init gatt...");
ble_svc_gatt_init();

int rc = ble_gatts_count_cfg(serial_service);
if (rc != 0) {
ESP_LOGE(instance->TAG, "Failed to count BLE services: %d", rc);
}

nopnop2002
Posts: 362
Joined: Thu Oct 03, 2019 10:52 pm
Contact:

Re: nimBLE service definition failing

Postby nopnop2002 » Sat Mar 29, 2025 11:46 pm

There may be a missing configuration.

https://github.com/espressif/esp-idf/bl ... g.defaults

tigr88
Posts: 2
Joined: Fri Mar 28, 2025 7:58 pm

Re: nimBLE service definition failing

Postby tigr88 » Sun Mar 30, 2025 4:46 pm

There may be a missing configuration.

https://github.com/espressif/esp-idf/bl ... g.defaults
Yes, I double checked, my sdk:

Code: Untitled.cpp Select all


CONFIG_BT_ENABLED=y

CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y
# CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY is not set
# CONFIG_BTDM_CTRL_MODE_BTDM is not set

# CONFIG_BLUEDROID_ENABLED is not set

CONFIG_NIMBLE_ENABLED=y

nopnop2002
Posts: 362
Joined: Thu Oct 03, 2019 10:52 pm
Contact:

Re: nimBLE service definition failing

Postby nopnop2002 » Mon Mar 31, 2025 11:10 am

Is this correct?
```
static ble_uuid_t *uuid_tx = (ble_uuid_t *)&serial_tx_uuid;

.uuid = uuid_tx,
```

This is how I always do it.
```
.uuid = BLE_UUID16_DECLARE(0xABF1)
```

Who is online

Users browsing this forum: ChatGPT-User, PetalBot and 2 guests