BLE esp_ble_gatts_send_response() clarification

zazas321
Posts: 231
Joined: Mon Feb 01, 2021 9:41 am

BLE esp_ble_gatts_send_response() clarification

Postby zazas321 » Wed Sep 15, 2021 9:10 am

Hello. I am learning the BLE and want to confirm regarding the esp_ble_gatts_send_response() method. I use lightblue app to connect between my ESP32 device and my phone.

When I click read on my lightblue APP, the read event is triggered and in my code, the following code is executed:

Code: Select all

case ESP_GATTS_READ_EVT:
ESP_LOGI(GATTS_TABLE_TAG, "ESP_GATTS_READ_EVT");
            esp_gatt_rsp_t rsp;
            memset(&rsp, 0, sizeof(esp_gatt_rsp_t));
            rsp.attr_value.handle = param->read.handle;
            rsp.attr_value.len = 4;
            rsp.attr_value.value[0] = 'r';
            rsp.attr_value.value[1] = 'e';
            rsp.attr_value.value[2] = 'a';
            rsp.attr_value.value[3] = 'd';
            esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, &rsp);
            break;


I can see the string "read" dispalyed on my lightblue app. So the esp_ble_gatts_send_response sends the data to the lightblue app right?

Now during my write event:

Code: Select all

case ESP_GATTS_WRITE_EVT:
            if (!param->write.is_prep){
                // the data length of gattc write  must be less than GATTS_DEMO_CHAR_VAL_LEN_MAX.
                ESP_LOGI(GATTS_TABLE_TAG, "GATT_WRITE_EVT, handle = %d, value len = %d, value :", param->write.handle, param->write.len);
                esp_log_buffer_hex(GATTS_TABLE_TAG, param->write.value, param->write.len);

                if (heart_rate_handle_table[IDX_CHAR_CFG_C] == param->write.handle && param->write.len == 2){
                    printf("inside if statement\n");
                    uint16_t descr_value = param->write.value[1]<<8 | param->write.value[0];
                    if (descr_value == 0x0001){
                        ESP_LOGI(GATTS_TABLE_TAG, "notify enable");
                        uint8_t notify_data[15];
                        for (int i = 0; i < sizeof(notify_data); ++i)
                        {
                            notify_data[i] = i % 0xff;
                        }
                        //the size of notify_data[] need less than MTU size
                        esp_ble_gatts_send_indicate(gatts_if, param->write.conn_id, heart_rate_handle_table[IDX_CHAR_VAL_C],
                                                sizeof(notify_data), notify_data, false);
                    }else if (descr_value == 0x0002){
                        ESP_LOGI(GATTS_TABLE_TAG, "indicate enable");
                        uint8_t indicate_data[15];
                        for (int i = 0; i < sizeof(indicate_data); ++i)
                        {
                            indicate_data[i] = i % 0xff;
                        }
                        //the size of indicate_data[] need less than MTU size
                        esp_ble_gatts_send_indicate(gatts_if, param->write.conn_id, heart_rate_handle_table[IDX_CHAR_VAL_C],
                                            sizeof(indicate_data), indicate_data, true);
                    }
                    else if (descr_value == 0x0000){
                        ESP_LOGI(GATTS_TABLE_TAG, "notify/indicate disable ");
                    }else{
                        ESP_LOGE(GATTS_TABLE_TAG, "unknown descr value");
                        esp_log_buffer_hex(GATTS_TABLE_TAG, param->write.value, param->write.len);
                    }

                }


                // THIS WILL NEVER TRIGGER IF AUTO RSP IS SET
                /* send response when param->write.need_rsp is true*/
                if (param->write.need_rsp){
                    printf("******************PARAM NEED RESPONSE****************\n");
                    esp_gatt_rsp_t rsp;
                    memset(&rsp, 0, sizeof(esp_gatt_rsp_t));
                    rsp.attr_value.handle = param->read.handle;
                    rsp.attr_value.len = 5;
                    rsp.attr_value.value[0] = 'w';
                    rsp.attr_value.value[1] = 'r;
                    rsp.attr_value.value[2] = 'i';
                    rsp.attr_value.value[3] = 't';
                    rsp.attr_value.value[4] = 'e';
                    esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, &rsp);
                }
            }else{
                /* handle prepare write */
                printf("before exmaple prepare write event env \n");
                example_prepare_write_event_env(gatts_if, &prepare_write_env, param);
            }
      	    break;
As you can see, in my write event, I have this code:

Code: Select all

                if (param->write.need_rsp){
                    printf("******************PARAM NEED RESPONSE****************\n");
                    esp_gatt_rsp_t rsp;
                    memset(&rsp, 0, sizeof(esp_gatt_rsp_t));
                    rsp.attr_value.handle = param->read.handle;
                    rsp.attr_value.len = 5;
                    rsp.attr_value.value[0] = 'w';
                    rsp.attr_value.value[1] = 'r;
                    rsp.attr_value.value[2] = 'i';
                    rsp.attr_value.value[3] = 't';
                    rsp.attr_value.value[4] = 'e';
                    esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, &rsp);
                }

Since my characteristic is set to ESP_GATT_RSP_BY_APP this gets triggered and I want to display the string "write" as a response. But this does not get displayed on my lightblue app. Can someone help me understand why

zazas321
Posts: 231
Joined: Mon Feb 01, 2021 9:41 am

Re: BLE esp_ble_gatts_send_response() clarification

Postby zazas321 » Thu Sep 16, 2021 11:04 am

Still hoping to get clarification regarding this issue. I have not managed to solve it yet.

shaomingya
Posts: 4
Joined: Mon Feb 21, 2022 3:48 am

Re: BLE esp_ble_gatts_send_response() clarification

Postby shaomingya » Wed Feb 28, 2024 1:13 am

esp_ble_gatts_send_indicate(..., ..., ..., ..., ..., false);

User avatar
jgustavoam
Posts: 165
Joined: Thu Feb 01, 2018 2:43 pm
Location: Belo Horizonte , Brazil
Contact:

Re: BLE esp_ble_gatts_send_response() clarification

Postby jgustavoam » Fri Apr 05, 2024 12:54 am

With this piece of code it is difficult to assess the problem.
But why are you activating indications and notifications?
Some data can only be sent if there are indications or notifications.

Good reference about BLE:
https://punchthrough.com/resources/
Retired IBM Brasil
Electronic hobbyist since 1976.

eriksl
Posts: 116
Joined: Thu Dec 14, 2023 3:23 pm
Location: Netherlands

Re: BLE esp_ble_gatts_send_response() clarification

Postby eriksl » Fri May 10, 2024 5:29 pm

The receiver of notifications must have been subscribed to notifications (or indications) to actually receive them.

Who is online

Users browsing this forum: Bing [Bot], prashant.anlg and 80 guests