Page 1 of 1

Addition of multiple characteristics to a BLE service

Posted: Thu Aug 31, 2017 9:57 pm
by halfro
I am currently needing to implement a BLE GATT server with 128 bit server and characteristic UUID's and I am using the GATTS_DEMO provided. I have dug through the internals till the btc level and I have implemented almost everything but I cant seem to figure out how to add multiple characteristics to a service.

I have read that without using the attribute table one has to call the server creation->characteristic addition ->descriptor addition functions sequentially when using the Bluedroid method. Now to my question, where in the GATTS event handler am I supposed to write this sequentially? When I try adding the ( server creation->characteristic addition ->descriptor addition) after the ESP_GATTS_REG_EVT section I just get the following output:

Code: Select all

I (599) GATTS_DEMO: REGISTER_APP_EVT, status 0, app_id 0

E (599) BT: service not created

E (599) BT: service not created

E (609) BT: service not created

Re: Addition of multiple characteristics to a BLE service

Posted: Fri Sep 01, 2017 3:39 am
by kolban
There is a high level BLE library available for ESP32 written in C++. If you are willing to leverage that language, the following may be of interest:

https://github.com/nkolban/esp32-snippe ... 0Guide.pdf

Re: Addition of multiple characteristics to a BLE service

Posted: Thu Oct 26, 2017 11:59 am
by Trialblazer47
Hi I am also struggling to add multiple characteristics.

Code: Select all

    case ESP_GATTS_CREATE_EVT:
        ESP_LOGI(GATTS_TAG, "CREATE_SERVICE_EVT, status %d,  service_handle %d\n", param->create.status, param->create.service_handle);
        gl_profile_tab[PROFILE_A_APP_ID].service_handle = param->create.service_handle;
        gl_profile_tab[PROFILE_A_APP_ID].char_uuid.len = ESP_UUID_LEN_16;
        gl_profile_tab[PROFILE_A_APP_ID].char_uuid.uuid.uuid16 = GATTS_CHAR_UUID_TEST_A;

        esp_ble_gatts_start_service(gl_profile_tab[PROFILE_A_APP_ID].service_handle);

        esp_ble_gatts_add_char(gl_profile_tab[PROFILE_A_APP_ID].service_handle, &gl_profile_tab[PROFILE_A_APP_ID].char_uuid,
                                 ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE,
                                 ESP_GATT_CHAR_PROP_BIT_READ |  ESP_GATT_CHAR_PROP_BIT_NOTIFY,
                                 &gatts_demo_char1_val, NULL);
        esp_ble_gatts_add_char(gl_profile_tab[PROFILE_A_APP_ID].service_handle, &gl_profile_tab[PROFILE_A_APP_ID].char_uuid,
                               ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE,
                               ESP_GATT_CHAR_PROP_BIT_WRITE,
                               &gatts_demo_char2_val, NULL);
right now I modified gatts demo app like this and I gett following thing . and does not show other char in bleScanner android app.

Code: Select all

 (532) GATTS_DEMO: REGISTER_APP_EVT, status 0, app_id 0

I (532) GATTS_DEMO: CREATE_SERVICE_EVT, status 0,  service_handle 40

I (542) GATTS_DEMO: SERVICE_START_EVT, status 0, service_handle 40

I (542) GATTS_DEMO: ADD_CHAR_EVT, status 0,  attr_handle 42, service_handle 40

I (552) GATTS_DEMO: the gatts demo char length = 3

I (552) GATTS_DEMO: prf_char[0] =11

I (562) GATTS_DEMO: prf_char[1] =22

I (562) GATTS_DEMO: prf_char[2] =33

I (562) GATTS_DEMO: ADD_CHAR_EVT, status 133,  attr_handle 0, service_handle 40 <---- -why status 133 here? what does it mean

I (572) GATTS_DEMO: the gatts demo char length = 0  <---- this means its not added 

I (582) GATTS_DEMO: ADD_DESCR_EVT, status 0, attr_handle 43, service_handle 40

I (592) GATTS_DEMO: ADD_DESCR_EVT, status 133, attr_handle 0, service_handle 40

Re: Addition of multiple characteristics to a BLE service

Posted: Thu Oct 26, 2017 12:48 pm
by badaboum
Hi,

I struggled quite a lot with this as well, but more or less figured it out by trial and error:

I suspect you can't add characteristics immediately after each other, but have to wait for the ESP_GATTS_ADD_CHAR_EVT and add the next characteristic or descriptor from the gatts event handler.

I can't share all my code yet, because it's a horrible mess, but I uploaded the BLE part here in the hope that it helps: https://gist.github.com/heiko-r/f284d95 ... d9070d61b4

It's built on the gatts demo example as well, but uses two services, and one of them has 3 characteristics with multiple descriptors each.
Use with care though, there's at least one known bug in assigning the service UUID...

Btw., you can find the GATT error codes in this file: https://github.com/espressif/esp-idf/bl ... att_defs.h, from line 169. Your error code 133 is a very helpful "ESP_GATT_ERROR" :P

Kind regards.

Re: Addition of multiple characteristics to a BLE service

Posted: Fri Oct 27, 2017 7:17 am
by Trialblazer47
Thank you for the reply and code I will look into it. I thought I would use two services with one char each. As this custom profile will be handled by phone so I would not have any issues but I think the better way is to add 2 related characteristics to one service. Anyways thanks I will look and understand your code. its too detailed and nicely organised. thanks again.

Re: Addition of multiple characteristics to a BLE service

Posted: Fri Oct 27, 2017 11:30 am
by jumjum123
@Heiko,
thank you very much for sharing your code.
Just working on Bluetooth for Espruino, and its a big help for me.
Much better to understand and follow than Espressifs example

Re: Addition of multiple characteristics to a BLE service

Posted: Wed May 02, 2018 11:54 am
by cattaka
I had same issue and I resolved.
The main cause is num_handle arg of esp_ble_gatts_create_service.

It is explained in https://github.com/espressif/esp-idf/is ... -274398685

Re: Addition of multiple characteristics to a BLE service

Posted: Mon Mar 18, 2019 10:50 am
by philmatthews
The recommended way to have multiple characteristics is by using a table to initialise the GATT server.
See the example: gatt_server_service_table

All the characteristics and descriptors are added at once when the GATT server is started. You don't have to add a characteristic and wait for the characteristic added event before adding the next characteristic or descriptor.