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);
}Code: Untitled.cpp Select all
E (4026) BLE_INIT: Failed to count BLE services: 3Code: 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
};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();
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);
}