BLE: How to add multiple characteristics in one service?

newsettler_AI
Posts: 121
Joined: Wed Apr 05, 2017 12:49 pm

BLE: How to add multiple characteristics in one service?

Postby newsettler_AI » Thu Nov 22, 2018 5:28 pm

Hi.
I need to create custom 128bit service with some characteristics (which will be 128 bits too)

As basic I'm using this https://github.com/espressif/esp-idf/bl ... tts_demo.c


I have sucessfully created custom 128bit service.

But when I'm trying add 2nd characteristic I'm facing this error:
  1. [2018-11-22_18:41:57][0;32mI (87) BTDM_INIT: BT controller compile version [8353b1b]
  2. [2018-11-22_18:41:57][0m
  3. [2018-11-22_18:41:57][0;32mI (88) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE[0m
  4. [2018-11-22_18:41:57][0;32mI (154) phy: phy_version: 3960, 5211945, Jul 18 2018, 10:40:07, 0, 0[0m
  5. [2018-11-22_18:41:57][0;32mI (333) BLE: ESP_GATTS_REG_EVT_A[0m
  6. [2018-11-22_18:41:57][0;32mI (334) BLE: REGISTER_APP_EVT, status 0, app_id 0
  7. [2018-11-22_18:41:57][0m
  8. [2018-11-22_18:41:57][0;31mE (335) BT_BTC: btc128_to_bta_uuid: Unknown UUID length 0![0m
  9. [2018-11-22_18:41:57][0;31mE (339) BT_BTC: btc128_to_bta_uuid: Unknown UUID length 0![0m
  10. [2018-11-22_18:41:57][0;32mI (345) BLE: ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT[0m
  11. [2018-11-22_18:41:57][0;32mI (350) BLE: ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT[0m
  12. [2018-11-22_18:41:57][0;32mI (356) BLE: CREATE_SERVICE_EVT, status 0,  service_handle 40
  13. [2018-11-22_18:41:57][0m
  14. [2018-11-22_18:41:57][0;31mE (380) BT_GATT: Active Service Found [0m
  15. [2018-11-22_18:41:57][0;31mE (382) BT_GATT: Duplicate Service start - Service already started[0m
  16. [2018-11-22_18:41:57][0;32mI (389) BLE: ESP_GAP_BLE_ADV_START_COMPLETE_EVT[0m
  17. [2018-11-22_18:41:57][0;32mI (394) BLE: SERVICE_START_EVT_A, status 0, service_handle 40
  18. [2018-11-22_18:41:57][0m
  19. [2018-11-22_18:41:57][0;32mI (401) BLE: ESP_GATTS_ADD_CHAR_EVT_A[0m
  20. [2018-11-22_18:41:57][0;32mI (405) BLE: ADD_CHAR_EVT, status 0,  attr_handle 42, service_handle 40
  21. [2018-11-22_18:41:57][0m
  22. [2018-11-22_18:41:57][0;32mI (413) BLE: the gatts demo char length = 3
  23. [2018-11-22_18:41:57][0m
  24. [2018-11-22_18:41:57][0;32mI (417) BLE: prf_char[0] =11
  25. [2018-11-22_18:41:57][0m
  26. [2018-11-22_18:41:57][0;32mI (421) BLE: prf_char[1] =22
  27. [2018-11-22_18:41:57][0m
  28. [2018-11-22_18:41:57][0;32mI (425) BLE: prf_char[2] =33
  29. [2018-11-22_18:41:57][0m
  30. [2018-11-22_18:41:57][0;32mI (429) BLE: SERVICE_START_EVT_A, status 133, service_handle 40
  31. [2018-11-22_18:41:57][0m
  32. [2018-11-22_18:41:57][0;32mI (435) BLE: ESP_GATTS_ADD_CHAR_EVT_A[0m
  33. [2018-11-22_18:41:57][0;32mI (440) BLE: ADD_CHAR_EVT, status 133,  attr_handle 0, service_handle 40
  34. [2018-11-22_18:41:57][0m
  35. [2018-11-22_18:41:57][0;32mI (447) BLE: the gatts demo char length = 0
  36. [2018-11-22_18:41:57][0m
  37. [2018-11-22_18:41:57][0;32mI (453) BLE: ESP_GATTS_ADD_CHAR_DESCR_EVT_A[0m
  38. [2018-11-22_18:41:57][0;32mI (457) BLE: ADD_DESCR_EVT, status 0, attr_handle 43, service_handle 40
  39. [2018-11-22_18:41:57][0m
  40. [2018-11-22_18:41:57][0;32mI (465) BLE: ESP_GATTS_ADD_CHAR_DESCR_EVT_A[0m
  41. [2018-11-22_18:41:57][0;32mI (470) BLE: ADD_DESCR_EVT, status 133, attr_handle 0, service_handle 40
  42. [2018-11-22_18:41:57][0m
My code part in gatts_profile_a_event_handler()

  1.    case ESP_GATTS_CREATE_EVT:
  2.         ESP_LOGI(BLE_TAG, "CREATE_SERVICE_EVT, status %d,  service_handle %d\n", param->create.status, param->create.service_handle);
  3.         gl_profile_tab[PROFILE_A_APP_ID].service_handle = param->create.service_handle;
  4.         gl_profile_tab[PROFILE_A_APP_ID].char_uuid.len = ESP_UUID_LEN_16;
  5.         gl_profile_tab[PROFILE_A_APP_ID].char_uuid.uuid.uuid16 = GATTS_CHAR_UUID_TEST_A;
  6.  
  7.         esp_ble_gatts_start_service(gl_profile_tab[PROFILE_A_APP_ID].service_handle);
  8.         a_property = ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_WRITE | ESP_GATT_CHAR_PROP_BIT_NOTIFY;
  9.         esp_err_t add_char_ret = esp_ble_gatts_add_char(gl_profile_tab[PROFILE_A_APP_ID].service_handle, &gl_profile_tab[PROFILE_A_APP_ID].char_uuid,
  10.                                                         ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE,
  11.                                                         a_property,
  12.                                                         &gatts_demo_char1_val, NULL);
  13.         if (add_char_ret){
  14.             ESP_LOGE(BLE_TAG, "add char failed, error code =%x",add_char_ret);
  15.         }
  16.  
  17.         // add 2nd characteristic
  18.         gl_profile_tab[PROFILE_A_APP_ID].service_handle = param->create.service_handle;
  19.         gl_profile_tab[PROFILE_A_APP_ID].char_uuid.len = ESP_UUID_LEN_16;
  20.         gl_profile_tab[PROFILE_A_APP_ID].char_uuid.uuid.uuid16 = 0xDD01;
  21.  
  22.         esp_ble_gatts_start_service(gl_profile_tab[PROFILE_A_APP_ID].service_handle);
  23.         a_property = ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_WRITE | ESP_GATT_CHAR_PROP_BIT_NOTIFY;
  24.         add_char_ret = esp_ble_gatts_add_char(gl_profile_tab[PROFILE_A_APP_ID].service_handle, &gl_profile_tab[PROFILE_A_APP_ID].char_uuid,
  25.                                                         ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE,
  26.                                                         a_property,
  27.                                                         &gatts_demo_char1_val, NULL);
  28.         if (add_char_ret){
  29.             ESP_LOGE(BLE_TAG, "add char failed, error code =%x",add_char_ret);
  30.         }
  31.         break;
Please give me advice how should I create it.

chegewara
Posts: 2378
Joined: Wed Jun 14, 2017 9:00 pm

Re: BLE: How to add multiple characteristics in one service?

Postby chegewara » Thu Nov 22, 2018 7:51 pm

In this example you have array of services with 1 characteristic for every service, instead you can have array of characteristic for every service and duplicate this code for every characteristic added to service:
https://github.com/espressif/esp-idf/bl ... #L581-L596

You have to remember one thing:
- when you start service you have to follow chain of events:
-- all characteristics and descriptors that belongs to that service have to be created (add) before you start another service, so you start esp_ble_gatts_start_service() -> loop(esp_ble_gatts_add_char() -> loop(esp_ble_gatts_add_char_descr())) (all GATT attributes have to be grouped and have consecutive handles)


So, basically its up to you how you store services, characteristics and descriptors as long as you follow that rule.

Namrata Vaishnav
Posts: 3
Joined: Wed Jul 10, 2024 10:59 am

Re: BLE: How to add multiple characteristics in one service?

Postby Namrata Vaishnav » Fri Aug 02, 2024 5:43 am

Hi. I am facing same issue. In my case only one characteristics have been added. For rest of the characteristics it shows status code 133. Can you share your code snippet for multiple characteristics in single service?

Who is online

Users browsing this forum: Baidu [Spider] and 99 guests