why call esp_ble_gatt_get_characteristic() in ESP_GATTC_GET_CHAR_EVENT?

Trialblazer47
Posts: 60
Joined: Mon Jun 26, 2017 5:36 am

why call esp_ble_gatt_get_characteristic() in ESP_GATTC_GET_CHAR_EVENT?

Postby Trialblazer47 » Thu Jul 27, 2017 1:20 pm

I am trying to understand demo code of BLE gattc_demo.c and I am running it and testing it with NRF51.
What I don't understand is esp_ble_gatt_get_characteristic is called after search is commplete(ESP_GATTC_SEARCH_CMPL_EVT) thats fine. But onces its called for Charateristics Search in that event again its called in ESP_GATTC_GET_CHAR_EVENT event I dont understand why again? when I comment that line only one characteristic is detected. I don't understand this function.

Is there any where all explained about how and when to call this functions or sequences?

Code: Select all

case ESP_GATTC_SEARCH_CMPL_EVT:
        conn_id = p_data->search_cmpl.conn_id;
        ESP_LOGI(GATTC_TAG, "SEARCH_CMPL: conn_id = %x, status %d", conn_id, p_data->search_cmpl.status);
        esp_ble_gattc_get_characteristic(gattc_if, conn_id, &alert_service_id, NULL);
        break;
    case ESP_GATTC_GET_CHAR_EVT:
        if (p_data->get_char.status != ESP_GATT_OK) {
            break;
        }
        ESP_LOGI(GATTC_TAG, "GET CHAR: conn_id = %x, status %d", p_data->get_char.conn_id, p_data->get_char.status);
        ESP_LOGI(GATTC_TAG, "GET CHAR: srvc_id = %04x, char_id = %04x", p_data->get_char.srvc_id.id.uuid.uuid.uuid16, p_data->get_char.char_id.uuid.uuid.uuid16);

        ESP_LOGI(GATTC_TAG, "Char UUID128: %x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x,%x",p_data->get_char.char_id.uuid.uuid.uuid128[0],
                           p_data->get_char.char_id.uuid.uuid.uuid128[1], p_data->get_char.char_id.uuid.uuid.uuid128[2], p_data->get_char.char_id.uuid.uuid.uuid128[3],
                           p_data->get_char.char_id.uuid.uuid.uuid128[4], p_data->get_char.char_id.uuid.uuid.uuid128[5], p_data->get_char.char_id.uuid.uuid.uuid128[6],
                           p_data->get_char.char_id.uuid.uuid.uuid128[7], p_data->get_char.char_id.uuid.uuid.uuid128[8], p_data->get_char.char_id.uuid.uuid.uuid128[9],
                           p_data->get_char.char_id.uuid.uuid.uuid128[10], p_data->get_char.char_id.uuid.uuid.uuid128[11], p_data->get_char.char_id.uuid.uuid.uuid128[12],
                           p_data->get_char.char_id.uuid.uuid.uuid128[13], p_data->get_char.char_id.uuid.uuid.uuid128[14], p_data->get_char.char_id.uuid.uuid.uuid128[15]);



        if (p_data->get_char.char_id.uuid.uuid.uuid16 ==0x941D){// 0x2a46) {
            ESP_LOGI(GATTC_TAG, "register notify");
            esp_ble_gattc_register_for_notify(gattc_if, gl_profile_tab[PROFILE_A_APP_ID].remote_bda, &alert_service_id, &p_data->get_char.char_id);
        }

        esp_ble_gattc_get_characteristic(gattc_if, conn_id, &alert_service_id, &p_data->get_char.char_id);
        break;
Thanks.

f.h-f.s.
Posts: 216
Joined: Thu Dec 08, 2016 2:53 pm

Re: why call esp_ble_gatt_get_characteristic() in ESP_GATTC_GET_CHAR_EVENT?

Postby f.h-f.s. » Thu Jul 27, 2017 1:43 pm

it finds the next one, if the last param != 0. See esp_gattc_api.c:172 :176 :180

This gets the first characteristic

Code: Select all

esp_ble_gattc_get_characteristic(gattc_if, conn_id, &alert_service_id, NULL);
The result then contains a char_id, and will get the next characteristic

Code: Select all

esp_ble_gattc_get_characteristic(gattc_if, conn_id, &alert_service_id, &p_data->get_char.char_id);

Trialblazer47
Posts: 60
Joined: Mon Jun 26, 2017 5:36 am

Re: why call esp_ble_gatt_get_characteristic() in ESP_GATTC_GET_CHAR_EVENT?

Postby Trialblazer47 » Thu Jul 27, 2017 3:11 pm

Ok thanks I thought once it is called all the characteristics should be discovered but I think thats why there is an if with break. that is if the characteristics are all found then break..

so if there are 3 characteristics then ESP_GATTC_GET_CHAR_EVT will be called 4 times and will break in 4th call. Right.
Thanks.

f.h-f.s.
Posts: 216
Joined: Thu Dec 08, 2016 2:53 pm

Re: why call esp_ble_gatt_get_characteristic() in ESP_GATTC_GET_CHAR_EVENT?

Postby f.h-f.s. » Thu Jul 27, 2017 3:15 pm

case switch should always break;
if you are at the last characteristic, the next get will cause some other case or error.

Trialblazer47
Posts: 60
Joined: Mon Jun 26, 2017 5:36 am

Re: why call esp_ble_gatt_get_characteristic() in ESP_GATTC_GET_CHAR_EVENT?

Postby Trialblazer47 » Fri Jul 28, 2017 4:54 am

I wasn't taking about case switch break. there is and if()
here
case ESP_GATTC_GET_CHAR_EVT:
if (p_data->get_char.status != ESP_GATT_OK) {
break;
}
which you said if get_char.status !=0 .
Thanks.

f.h-f.s.
Posts: 216
Joined: Thu Dec 08, 2016 2:53 pm

Re: why call esp_ble_gatt_get_characteristic() in ESP_GATTC_GET_CHAR_EVENT?

Postby f.h-f.s. » Fri Jul 28, 2017 6:16 am

Ooh that is if there for a invalid response.
If you call a get characteristic that does not exist or smthn, there will be a get_char event with status != ESP_GATT_OK

Who is online

Users browsing this forum: Google [Bot] and 70 guests