NimBLE_GATT_Server example characteristic descriptor

UJSWangx
Posts: 7
Joined: Sun Apr 25, 2021 8:02 am

NimBLE_GATT_Server example characteristic descriptor

Postby UJSWangx » Fri Jun 27, 2025 1:48 am

hi there:
I use example project NimBLE_GATT_Server in ESPIDF V5.4.0, and i add some code in this example try to describe an attribute, but it's failure. The phenomenon is as follows: i use Nrf Connect android application to connect to the ESP32-C3 board, when i click the Descriptor read button, app shows: Error 2(0x2):GATT READ NOT PERMIT, How can I solve this problem?

Code as follows:

Code: Select all

/* Wifi config service */
static const ble_uuid128_t wifi_config_svc_uuid = BLE_UUID128_INIT(0x2d, 0x71, 0xa2, 0x59, 0xb4, 0x58, 0xc8, 0x12,
                                                                   0x99, 0x99, 0x43, 0x95, 0x12, 0x2f, 0x46, 0x59);
static uint16_t wifi_config_chr_val_handle;
static const ble_uuid128_t wifi_config_chr_uuid = BLE_UUID128_INIT(0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11,
                                                                   0x22, 0x22, 0x22, 0x22, 0x33, 0x33, 0x33, 0x33);
static const ble_uuid128_t wifi_config_desc_uuid = BLE_UUID128_INIT(0x01, 0x01, 0x01, 0x01, 0x12, 0x12, 0x12, 0x12,
                                                                    0x23, 0x23, 0x23, 0x23, 0x34, 0x34, 0x34, 0x34);
                                                                    
/* GATT services table */
static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
    /* Wifi config service */
    {
        .type = BLE_GATT_SVC_TYPE_PRIMARY,
        .uuid = &wifi_config_svc_uuid.u,
        .characteristics =
            (struct ble_gatt_chr_def[]){
                {.uuid = &wifi_config_chr_uuid.u,
                 .descriptors = (struct ble_gatt_dsc_def[]){
                     {
                         .uuid = &wifi_config_desc_uuid.u,
                         .att_flags = BLE_GATT_CHR_F_READ ,
                         .access_cb = wifi_config_chr_access,
                     },
                     {
                         0 // No more descriptors
                     },
                 },
                 .access_cb = wifi_config_chr_access,
                 .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_INDICATE,
                 .val_handle = &wifi_config_chr_val_handle},
                {0}
                },
    },

    {
        0, /* No more services. */
    },
}; 


static int wifi_config_chr_access(uint16_t conn_handle, uint16_t attr_handle,
                                  struct ble_gatt_access_ctxt *ctxt, void *arg)
{
    /* Local variables */
    int rc = 0;
    const ble_uuid_t *uuid;
    ESP_LOGE(TAG,
             "access operation to wifi config characteristic, opcode: %d",
             ctxt->op);
    /* Handle access events */
    /* Note: Wifi config characteristic is write only */
    switch (ctxt->op)
    {

    /* Write characteristic event */
    case BLE_GATT_ACCESS_OP_WRITE_CHR:
        /* Verify connection handle */
        if (conn_handle != BLE_HS_CONN_HANDLE_NONE)
        {
            ESP_LOGI(TAG, "characteristic write; conn_handle=%d attr_handle=%d",
                     conn_handle, attr_handle);
        }
        else
        {
            ESP_LOGI(TAG,
                     "characteristic write by nimble stack; attr_handle=%d",
                     attr_handle);
        }

        /* Verify attribute handle */
        if (attr_handle == wifi_config_chr_val_handle)
        {
            /* Process the received data for WiFi configuration */
            /* Verify access buffer length */
            if (ctxt->om->om_len > 0 && ctxt->om->om_len < 30)
            {
                /* Turn the LED on or off according to the operation bit */
                ESP_LOGI(TAG, "wifi config:%s", ctxt->om->om_data);
            }
            else
            {
                goto error;
            }
            return rc;
        }
    case BLE_GATT_ACCESS_OP_READ_CHR:
        ESP_LOGI(TAG, "characteristic read; conn_handle=%d attr_handle=%d",
                 conn_handle, attr_handle);
        break;
    case BLE_GATT_ACCESS_OP_READ_DSC:
        ESP_LOGI(TAG, "descriptor read; conn_handle=%d attr_handle=%d",
                 conn_handle, attr_handle);
        if (conn_handle != BLE_HS_CONN_HANDLE_NONE)
        {
            ESP_LOGI(TAG, "Descriptor read; conn_handle=%d attr_handle=%d\n",
                     conn_handle, attr_handle);
        }
        else
        {
            ESP_LOGI(TAG, "Descriptor read by NimBLE stack; attr_handle=%d\n",
                     attr_handle);
        }
        uuid = ctxt->dsc->uuid;
        if (ble_uuid_cmp(uuid, &wifi_config_desc_uuid.u) == 0)
        {
            rc = os_mbuf_append(ctxt->om,
                                "wifi config",
                                11);
            return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
        }
    default:
        goto error;
    }
error:
    ESP_LOGE(TAG,
             "unexpected access operation to wifi config characteristic, opcode: %d",
             ctxt->op);
    return BLE_ATT_ERR_UNLIKELY;
}                                                                   
Debug output:

Code: Select all

I (323) main_task: Started on CPU0
I (323) main_task: Calling app_main()
I (323) NimBLE_GATT_Server: example configured to blink addressable led!
I (333) BLE_INIT: BT controller compile version [d752dea]
I (333) BLE_INIT: Bluetooth MAC: 7c:df:a1:ba:4d:62
I (333) phy_init: phy_version 1180,01f2a49,Jun  4 2024,16:34:25
I (403) NimBLE_GATT_Server: nimble host task has been started!
I (403) NimBLE: GAP procedure initiated: stop advertising.

I (403) NimBLE_GATT_Server: device address: 62:4D:BA:A1:DF:7C
I (403) NimBLE: GAP procedure initiated: advertise;
I (413) NimBLE: disc_mode=2
I (413) NimBLE:  adv_channel_map=0 own_addr_type=0 adv_filter_policy=0 adv_itvl_min=800 adv_itvl_max=816
I (423) NimBLE:

I (423) NimBLE_GATT_Server: advertising started!
I (433) NimBLE_GATT_Server: heart rate task has been started!
I (433) main_task: Returned from app_main()
I (3003) NimBLE_GATT_Server: connection established; status=0
I (3003) NimBLE_GATT_Server: connection handle: 1
I (3003) NimBLE_GATT_Server: device id address: type=0, value=62:4D:BA:A1:DF:7C
I (3013) NimBLE_GATT_Server: peer id address: type=1, value=45:9C:4C:B5:57:50
I (3013) NimBLE_GATT_Server: conn_itvl=24, conn_latency=0, supervision_timeout=500, encrypted=0, authenticated=0, bonded=0

I (3033) NimBLE: GAP procedure initiated:
I (3033) NimBLE: connection parameter update; conn_handle=1 itvl_min=24 itvl_max=24 latency=3 supervision_timeout=500 min_ce_len=0 max_ce_len=0
I (3043) NimBLE:

I (3483) NimBLE_GATT_Server: connection updated; status=0
I (3483) NimBLE_GATT_Server: connection handle: 1
I (3483) NimBLE_GATT_Server: device id address: type=0, value=62:4D:BA:A1:DF:7C
I (3493) NimBLE_GATT_Server: peer id address: type=1, value=45:9C:4C:B5:57:50
I (3493) NimBLE_GATT_Server: conn_itvl=24, conn_latency=3, supervision_timeout=500, encrypted=0, authenticated=0, bonded=0

I (4263) NimBLE_GATT_Server: connection updated; status=0
I (4263) NimBLE_GATT_Server: connection handle: 1
I (4263) NimBLE_GATT_Server: device id address: type=0, value=62:4D:BA:A1:DF:7C
I (4273) NimBLE_GATT_Server: peer id address: type=1, value=45:9C:4C:B5:57:50
I (4283) NimBLE_GATT_Server: conn_itvl=6, conn_latency=0, supervision_timeout=500, encrypted=0, authenticated=0, bonded=0

I (4473) NimBLE_GATT_Server: connection updated; status=0
I (4473) NimBLE_GATT_Server: connection handle: 1
I (4473) NimBLE_GATT_Server: device id address: type=0, value=62:4D:BA:A1:DF:7C
I (4483) NimBLE_GATT_Server: peer id address: type=1, value=45:9C:4C:B5:57:50
I (4483) NimBLE_GATT_Server: conn_itvl=24, conn_latency=3, supervision_timeout=500, encrypted=0, authenticated=0, bonded=0
Pictures: (i delete the Heart rate service and Automation IO service in the above code)
Image
Image

irahul
Espressif staff
Espressif staff
Posts: 57
Joined: Fri Jun 18, 2021 10:07 am

Re: NimBLE_GATT_Server example characteristic descriptor

Postby irahul » Wed Jul 02, 2025 5:07 am

Hi,

.att_flags = BLE_GATT_CHR_F_READ


This is incorrect in descriptor.

Plesae instead use .att_flags = BLE_ATT_F_READ , and try

UJSWangx
Posts: 7
Joined: Sun Apr 25, 2021 8:02 am

Re: NimBLE_GATT_Server example characteristic descriptor

Postby UJSWangx » Mon Sep 08, 2025 2:33 am

OH, Thanks, but i change my project based on 'BLE Peripheral with ICMP Echo-Reply' example, and characteristic descriptor works fine. And i will try this way as your said. Thanks again.

UJSWangx
Posts: 7
Joined: Sun Apr 25, 2021 8:02 am

Re: NimBLE_GATT_Server example characteristic descriptor

Postby UJSWangx » Mon Sep 08, 2025 2:38 am

OH, Thanks, but i change my project based on 'BLE Peripheral with ICMP Echo-Reply' example, and characteristic descriptor works fine. And i will try this way as your said. Thanks again.

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot], Google [Bot], Qwantbot and 10 guests