How add another Characteristic in the example 'gatt_server' (BLE-Bluedroid)

GustavoGB
Posts: 18
Joined: Fri Jan 15, 2021 1:46 am

How add another Characteristic in the example 'gatt_server' (BLE-Bluedroid)

Postby GustavoGB » Tue Jan 19, 2021 5:13 pm

Hello, I'm using the example https://github.com/espressif/esp-idf/bl ... tts_demo.c, that creates two Profiles. Each Profile has one Service and a single Characteristic.

However I would like to have more than one Characteristic in this Service.
I'm having a lot of problems since it seems that this Characteristic is unique for this Service and I can't find a way to add another one.
Has anyone done it before? I will be very grateful.
Thanks a lot :D :D

Gustavo

GustavoGB
Posts: 18
Joined: Fri Jan 15, 2021 1:46 am

Re: How add another Characteristic in the example 'gatt_server' (BLE-Bluedroid)

Postby GustavoGB » Wed Jan 20, 2021 10:59 pm

Well as it usually happens to me, sometimes I solve things for myself haha.

We have to do this:
1) Add a new UUID (for char2) and increase the Number of Handles of the Service.

Code: Select all

#define GATTS_SERVICE_UUID_TEST_A   0x00FF
#define GATTS_CHAR_UUID_TEST_A      0xFF01
#define GATTS_CHAR2_UUID_TEST_A     0xFF02   /* New UUID for Char2 */
#define GATTS_DESCR_UUID_TEST_A     0x3333
#define GATTS_NUM_HANDLE_TEST_A     8           /* Modify to 6 or more */
Now you just have to copy the piece of code where the Characteristic is created inside the function event_handler (gatts_profile_a_event_handler), in the event ESP_GATTS_CREATE_EVT.
It would look something like this:

Code: Select all

  case ESP_GATTS_CREATE_EVT:
  
        // First Characteristic
        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);
        a_property = ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_WRITE | ESP_GATT_CHAR_PROP_BIT_NOTIFY;
        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,
                                                        ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE,
                                                        a_property,
                                                        &gatts_initial_char_val, NULL);
        if (add_char_ret){
            ESP_LOGE(GATTS_TAG, "add char failed, error code =%x",add_char_ret);
        }
        
        // Second Characteristic
        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_CHAR2_UUID_TEST_A;

        esp_ble_gatts_start_service(gl_profile_tab[PROFILE_A_APP_ID].service_handle);
        a_property = ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_WRITE | ESP_GATT_CHAR_PROP_BIT_NOTIFY;
        esp_err_t add_char2_ret = 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,
                                                        a_property,
                                                        &gatts_initial_char_val, NULL);
        if (add_char2_ret){
            ESP_LOGE(GATTS_TAG, "add char failed, error code =%x",add_char2_ret);
        }
        
        break;
The only thing I would like to know and I don't know how to do.
I would like to be able to retrieve the Handle of the Characteristic after creating it. If someone can do it it would be of great help to me.
I hope my solution serves you.

tiradedepirate
Posts: 12
Joined: Tue Jan 15, 2019 5:12 am

Re: How add another Characteristic in the example 'gatt_server' (BLE-Bluedroid)

Postby tiradedepirate » Sun Jan 24, 2021 6:14 pm

Hello Gustavo,

I was working on this as well. I'd like to show you what I've learned if you're still struggling with this.
When the service is created, the following event is triggered in the gatts_profile_event_handler

Code: Select all

  	case ESP_GATTS_CREAT_ATTR_TAB_EVT:{
    	    ESP_LOGI(GATTS_TABLE_TAG, "The number handle =%x\n",param->add_attr_tab.num_handle);
    	    if (param->add_attr_tab.status != ESP_GATT_OK){
    	        ESP_LOGE(GATTS_TABLE_TAG, "Create attribute table failed, error code=0x%x", param->add_attr_tab.status);
    	    }
    	    else if (param->add_attr_tab.num_handle != SPP_IDX_NB){
    	        ESP_LOGE(GATTS_TABLE_TAG, "Create attribute table abnormally, num_handle (%d) doesn't equal to HRS_IDX_NB(%d)", param->add_attr_tab.num_handle, SPP_IDX_NB);
    	    }
    	    else {
    	        memcpy(spp_handle_table, param->add_attr_tab.handles, sizeof(spp_handle_table));
    	        esp_ble_gatts_start_service(spp_handle_table[SPP_IDX_SVC]);
    	    }
    	    break;
    	}
    	default:
    	    break;
    }
}
Look at the memcpy line. Here the handles for each ATT is copied into the service handle table. In this example, this is spp_handle_table. Therefore, to find the handle it's just the following:

uint16_t whatever_att_handle = spp_handle_table[ WHATEVER_ATT_INDEX ]

Look at the SPP BLE Example. This helped me the most.

hamza azelmad
Posts: 1
Joined: Wed Mar 23, 2022 2:58 pm

Re: How add another Characteristic in the example 'gatt_server' (BLE-Bluedroid)

Postby hamza azelmad » Wed Mar 23, 2022 3:00 pm

hello gustavo , i have the same problem as yours , and i would like to know if you find a solution for this ? it been a days i'm struggling with it , any help pls !!!!!

Who is online

Users browsing this forum: No registered users and 70 guests