Closed an-erd closed 5 years ago
@an-erd In this case, the above error can be returned when write is not permitted on the characteristic. Can you please check if write permissions (in addition to write Property) are given to characteristic RSC Measurement on the BLE peripheral you are using?
I replaced the existing gattc_demo.c with the one above with the following change to send appropriate values: uint8_t write_char_data[4] = {0x00, 0x01, 0x02, 0x00};
On the BLE peripheral app I could see the write request was received "Speed 2.00 m/s Cadence: RPM and Status: Walking" and it was successful. On the ESP32 console: "I (2043) GATTC_DEMO: write char success"
Now, If I remove write permissions to characteristic RSC measurement in the peripheral app I get the following error on the ESP32 console: "E (2443) GATTC_DEMO: write char failed, error status = 3"
@dhrishi Good point. I've a log from an old test with Arduino. The both characteristics I'm interested in are 0x2a53
(RSC) and 0xff00
(custom data). In the old log there's only notify available for them, see first line.
[D][BLERemoteCharacteristic.cpp:442] registerForNotify(): >> registerForNotify(): Characteristic: uuid: 00002a53-0000-1000-8000-00805f9b34fb, handle: 18 0x12, props: broadcast: 0, read: 0, write_nr: 0, write: 0, notify: 1, indicate: 0, auth: 0
[D][BLERemoteCharacteristic.cpp:327] getDescriptor(): >> getDescriptor: uuid: 00002902-0000-1000-8000-00805f9b34fb
[V][BLEUtils.cpp:950] gattClientEventTypeToString(): Unknown GATT Client event type: 38
[D][BLERemoteCharacteristic.cpp:331] getDescriptor(): << getDescriptor: found
[D][BLEDevice.cpp:148] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLERemoteDescriptor.cpp:135] writeValue(): >> writeValue: handle: 19, uuid: 00002902-0000-1000-8000-00805f9b34fb
[V][BLEUtils.cpp:950] gattClientEventTypeToString(): Unknown GATT Client event type: 38
[D][BLERemoteDescriptor.cpp:154] writeValue(): << writeValue
[V][BLEUtils.cpp:1282] dumpGattClientEvent(): GATT Event: Unknown
[V][BLEUtils.cpp:950] gattClientEventTypeToString(): Unknown GATT Client event type: 38
[D][BLEClient.cpp:162] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLERemoteCharacteristic.cpp:482] registerForNotify(): << registerForNotify()
[W][disPOD.ino:602] my_gattc_event_handler(): custom gattc event handler, event: 38
[D][BLERemoteCharacteristic.cpp:327] getDescriptor(): >> getDescriptor: uuid: 00002902-0000-1000-8000-00805f9b34fb
[V][BLEUtils.cpp:950] gattClientEventTypeToString(): Unknown GATT Client event type: 9
[D][BLERemoteCharacteristic.cpp:331] getDescriptor(): << getDescriptor: found
[D][BLEDevice.cpp:148] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLERemoteDescriptor.cpp:135] writeValue(): >> writeValue: handle: 19, uuid: 00002902-0000-1000-8000-00805f9b34fb
[V][BLEUtils.cpp:950] gattClientEventTypeToString(): Unknown GATT Client event type: 9
[D][BLERemoteDescriptor.cpp:154] writeValue(): << writeValue
[V][BLEUtils.cpp:1282] dumpGattClientEvent(): GATT Event: Unknown
[D][BLERemoteCharacteristic.cpp:442] registerForNotify(): >> registerForNotify(): Characteristic: uuid: 0000ff00-0000-1000-8000-00805f9b34fb, handle: 28 0x1c, props: broadcast: 0, read: 0, write_nr: 0, write: 0, notify: 1, indicate: 0, auth: 0
[V][BLEUtils.cpp:950] gattClientEventTypeToString(): Unknown GATT Client event type: 9
[D][BLERemoteCharacteristic.cpp:327] getDescriptor(): >> getDescriptor: uuid: 00002902-0000-1000-8000-00805f9b34fb
[D][BLEClient.cpp:162] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLERemoteCharacteristic.cpp:331] getDescriptor(): << getDescriptor: found
[W][disPOD.ino:602] my_gattc_event_handler(): custom gattc event handler, event: 9
[D][BLERemoteDescriptor.cpp:135] writeValue(): >> writeValue: handle: 29, uuid: 00002902-0000-1000-8000-00805f9b34fb
[V][BLEUtils.cpp:950] gattClientEventTypeToString(): Unknown GATT Client event type: 38
[D][BLERemoteDescriptor.cpp:154] writeValue(): << writeValue
[D][BLEDevice.cpp:148] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[V][BLEUtils.cpp:950] gattClientEventTypeToString(): Unknown GATT Client event type: 38
[V][BLEUtils.cpp:1282] dumpGattClientEvent(): GATT Event: Unknown
[V][BLEUtils.cpp:950] gattClientEventTypeToString(): Unknown GATT Client event type: 38
[D][BLEClient.cpp:162] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLERemoteCharacteristic.cpp:482] registerForNotify(): << registerForNotify()
[W][disPOD.ino:602] my_gattc_event_handler(): custom gattc event handler, event: 38
[D][BLERemoteCharacteristic.cpp:327] getDescriptor(): >> getDescriptor: uuid: 00002902-0000-1000-8000-00805f9b34fb
[V][BLEUtils.cpp:950] gattClientEventTypeToString(): Unknown GATT Client event type: 9
[D][BLERemoteCharacteristic.cpp:331] getDescriptor(): << getDescriptor: found
[D][BLEDevice.cpp:148] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLERemoteDescriptor.cpp:135] writeValue(): >> writeValue: handle: 29, uuid: 00002902-0000-1000-8000-00805f9b34fb
[V][BLEUtils.cpp:950] gattClientEventTypeToString(): Unknown GATT Client event type: 9
[D][BLERemoteDescriptor.cpp:154] writeValue(): << writeValue
[V][BLEUtils.cpp:1282] dumpGattClientEvent(): GATT Event: Unknown
[V][BLEUtils.cpp:950] gattClientEventTypeToString(): Unknown GATT Client event type: 9
[D][BLEClient.cpp:162] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[W][disPOD.ino:602] my_gattc_event_handler(): custom gattc event handler, event: 9
[V][BLEUtils.cpp:950] gattClientEventTypeToString(): Unknown GATT Client event type: 9
[D][BLEDevice.cpp:148] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[V][BLEUtils.cpp:950] gattClientEventTypeToString(): Unknown GATT Client event type: 9
[V][BLEUtils.cpp:1282] dumpGattClientEvent(): GATT Event: Unknown
[V][BLEUtils.cpp:950] gattClientEventTypeToString(): Unknown GATT Client event type: 9
[D][BLEClient.cpp:162] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[W][disPOD.ino:602] my_gattc_event_handler(): custom gattc event handler, event: 9
Preparation done, waiting for NOTIFY
Actually, I don't want to write to the device, but only receive the RSC and custom values from the device.
Why does this "write char failed" occur? I see its called in gattc_profile_event_handler()
with ESP_GATTC_WRITE_DESCR_EVT
, but I don't yet understand why this is called at all?
@an-erd It is called because under ESP_GATTC_WRITE_DESCR_EVT (where it receives the result for enabling notify) it writes to that characteristic, see API esp_ble_gattc_write_char() in there. If you don't want to write to that characteristic, you can simply remove this call. You have already subscribed to notifications through the call esp_ble_gattc_write_char_descr()
This may be present in the example just to demonstrate the API for writing to a characteristic.
Ok, to summarize:
@dhrishi Thanks!
GATTC_DEMO: write char failed, error status : d
What is the reason for this??
write char failed,error status d @dhrishi What is the reason for this??
Hi @monikharajs the error status translates to ESP_GATT_INVALID_ATTR_LEN
, Can you please let me know the changes you have made to gattc_demo
?
case ESP_GATTC_SEARCH_CMPL_EVT: if (p_data->search_cmpl.status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "search service failed, error status = %x", p_data->search_cmpl.status); break; } if(p_data->search_cmpl.searched_service_source == ESP_GATT_SERVICE_FROM_REMOTE_DEVICE) { ESP_LOGI(GATTC_TAG, "Get service information from remote device"); } else if (p_data->search_cmpl.searched_service_source == ESP_GATT_SERVICE_FROM_NVS_FLASH) { ESP_LOGI(GATTC_TAG, "Get service information from flash"); } else { ESP_LOGI(GATTC_TAG, "unknown service source"); } ESP_LOGI(GATTC_TAG, "ESP_GATTC_SEARCH_CMPL_EVT"); if (get_server){ uint16_t count = 0; esp_gatt_status_t status = esp_ble_gattc_get_attr_count( gattc_if, p_data->search_cmpl.conn_id, ESP_GATT_DB_CHARACTERISTIC, gl_profile_tab[PROFILE_A_APP_ID].service_start_handle, gl_profile_tab[PROFILE_A_APP_ID].service_end_handle, INVALID_HANDLE, &count); if (status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_attr_count error"); }
if (count > 0){
printf("cunt %d\n",count);
char_elem_result = (esp_gattc_char_elem_t *)malloc(sizeof(esp_gattc_char_elem_t) * count);
if (!char_elem_result){
ESP_LOGE(GATTC_TAG, "gattc no mem");
}else{
printf("Count before gattc no mem %d",count);
printf("Gattc MEMMM\n");
status = esp_ble_gattc_get_char_by_uuid( gattc_if,
p_data->search_cmpl.conn_id,
gl_profile_tab[PROFILE_A_APP_ID].service_start_handle,
gl_profile_tab[PROFILE_A_APP_ID].service_end_handle,
remote_filter_char_uuid,
char_elem_result,
&count);
if (status != ESP_GATT_OK){
printf("Get char by uuid not success\n");
ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_char_by_uuid error");
}
else
{
printf("Get char by uuid success\n");
}
printf("Count before if %d\n",count);
/* Every service have only one char in our 'ESP_GATTS_DEMO' demo, so we used first 'char_elem_result' */
if (count > 0 && (char_elem_result[0].properties & ESP_GATT_CHAR_PROP_BIT_NOTIFY)){
printf("Before Write\n");
gl_profile_tab[PROFILE_A_APP_ID].char_handle = char_elem_result[0].char_handle;
esp_ble_gattc_register_for_notify (gattc_if, gl_profile_tab[PROFILE_A_APP_ID].remote_bda, char_elem_result[0].char_handle);
esp_err_t write_status= esp_ble_gattc_write_char(gattc_if,
gl_profile_tab[PROFILE_A_APP_ID].conn_id,
gl_profile_tab[PROFILE_A_APP_ID].char_handle,
sizeof(write_char_data), write_char_data,
ESP_GATT_WRITE_TYPE_RSP,
ESP_GATT_AUTH_REQ_NONE);
if(write_status==ESP_OK)
{
printf("Write success %s\n",write_char_data);
}
else
{
printf("Write Not OKkk\n");
}
}
else
{
printf("Not going to write\n");
}
}
/* free char_elem_result */
free(char_elem_result);
}else{
ESP_LOGE(GATTC_TAG, "no char found");
}
}
break;
case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
ESP_LOGI(GATTC_TAG, "ESP_GATTC_REG_FOR_NOTIFY_EVT");
if (p_data->reg_for_notify.status != ESP_GATT_OK){
ESP_LOGE(GATTC_TAG, "REG FOR NOTIFY failed: error status = %d", p_data->reg_for_notify.status);
}else{
uint16_t count = 0;
uint16_t notify_en = 1;
esp_gatt_status_t ret_status = esp_ble_gattc_get_attr_count( gattc_if,
gl_profile_tab[PROFILE_A_APP_ID].conn_id,
ESP_GATT_DB_DESCRIPTOR,
gl_profile_tab[PROFILE_A_APP_ID].service_start_handle,
gl_profile_tab[PROFILE_A_APP_ID].service_end_handle,
gl_profile_tab[PROFILE_A_APP_ID].char_handle,
&count);
if (ret_status != ESP_GATT_OK){
ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_attr_count error");
}
/*if (count > 0){
descr_elem_result = malloc(sizeof(esp_gattc_descr_elem_t) * count);
if (!descr_elem_result){
ESP_LOGE(GATTC_TAG, "malloc error, gattc no mem");
}else{
ret_status = esp_ble_gattc_get_descr_by_char_handle( gattc_if,
gl_profile_tab[PROFILE_A_APP_ID].conn_id,
p_data->reg_for_notify.handle,
notify_descr_uuid,
descr_elem_result,
&count);
if (ret_status != ESP_GATT_OK){
ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_descr_by_char_handle error");
}
Every char has only one descriptor in our 'ESP_GATTS_DEMO' demo, so we used first 'descr_elem_result'
if (count > 0 && descr_elem_result[0].uuid.len == ESP_UUID_LEN_16 && descr_elem_result[0].uuid.uuid.uuid16 == ESP_GATT_UUID_CHAR_CLIENT_CONFIG){
ret_status = esp_ble_gattc_write_char_descr( gattc_if,
gl_profile_tab[PROFILE_A_APP_ID].conn_id,
descr_elem_result[0].handle,
sizeof(notify_en),
(uint8_t *)¬ify_en,
ESP_GATT_WRITE_TYPE_RSP,
ESP_GATT_AUTH_REQ_NONE);
}
if (ret_status != ESP_GATT_OK){
ESP_LOGE(GATTC_TAG, "esp_ble_gattc_write_char_descr error");
}
free descr_elem_result
free(descr_elem_result);
}
}
else{
ESP_LOGE(GATTC_TAG, "decsr not found");
}
/ } break; } case ESP_GATTC_NOTIFY_EVT: if (p_data->notify.is_notify){ ESP_LOGI(GATTC_TAG, "ESP_GATTC_NOTIFY_EVT, receive notify value:"); }else{ ESP_LOGI(GATTC_TAG, "ESP_GATTC_NOTIFY_EVT, receive indicate value:"); } esp_log_buffer_hex(GATTC_TAG, p_data->notify.value, p_data->notify.value_len); break; case ESP_GATTC_WRITE_DESCR_EVT: if (p_data->write.status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "write descr failed, error status = %x", p_data->write.status); break; } ESP_LOGI(GATTC_TAG, "write descr success "); / uint8_t write_char_data[35]; for (int i = 0; i < sizeof(write_char_data); ++i) { write_char_data[i] = i % 256; }*/ esp_ble_gattc_write_char( gattc_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id, gl_profile_tab[PROFILE_A_APP_ID].char_handle, sizeof(write_char_data), write_char_data, ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE); printf("success dcsr\n"); break; case ESP_GATTC_SRVC_CHG_EVT: { esp_bd_addr_t bda; memcpy(bda, p_data->srvc_chg.remote_bda, sizeof(esp_bd_addr_t)); ESP_LOGI(GATTC_TAG, "ESP_GATTC_SRVC_CHG_EVT, bd_addr:"); esp_log_buffer_hex(GATTC_TAG, bda, sizeof(esp_bd_addr_t)); break; } case ESP_GATTC_WRITE_CHAR_EVT: printf("in case\n"); if (p_data->write.status != ESP_GATT_OK){ printf("Write not success in case\n"); ESP_LOGE(GATTC_TAG, "write char failed, error status = %x", p_data->write.status); break; } printf("success in case\n"); ESP_LOGI(GATTC_TAG, "write char success ");
break;
case ESP_GATTC_DISCONNECT_EVT:
printf("ESP_GATTC_DISCONNECT EVENT\n");
connect = false;
get_server = false;
ESP_LOGI(GATTC_TAG, "ESP_GATTC_DISCONNECT_EVT, reason = %d", p_data->disconnect.reason);
break;
default:
printf("Default\n");
break;
}
} i used gatt_client example , just declared write char data as global unsigned char write_char_data[100] = {"hii" };
output: mac in fun..ea:46:5f:da:d6:74 I (15569) SEC_GATTC_DEMO: searched Adv Data Len 16, Scan Response Len 0 I (15579) SEC_GATTC_DEMO: searched Device Name Len 7 I (15589) SEC_GATTC_DEMO: DfuTarg I (15589) SEC_GATTC_DEMO:
I (15589) SEC_GATTC_DEMO: searched device DfuTarg
I (15599) TW_MQTT: Queue empty I (15599) SEC_GATTC_DEMO: connect to the remote device. connected deviceI (15609) SEC_GATTC_DEMO: 03 9f d7 d0 5d 12 mac in fun..ea:46:5f:da:d6:74 I (15619) SEC_GATTC_DEMO: searched Adv Data Len 31, Scan Response Len 0 I (15619) SEC_GATTC_DEMO: searched Device Name Len 0 I (15629) SEC_GATTC_DEMO:
not compare I (15629) SEC_GATTC_DEMO: stop scan successfully E (15639) BT_BTM: BTM_BleScan scan not active
W (15649) BT_APPL: bta_dm_ble_scan stop scan failed, status=0x6
E (15649) SEC_GATTC_DEMO: scan stop failed, error status = 1 I (15669) SEC_GATTC_DEMO: ESP_GATTC_CONNECT_EVT conn_id 0, if 4 I (15669) SEC_GATTC_DEMO: REMOTE BDA: I (15669) SEC_GATTC_DEMO: ea 46 5f da d6 74 I (15669) SEC_GATTC_DEMO: open success I (15699) TW_MQTT: Queue empty I (15779) SEC_GATTC_DEMO: update connection params status = 0, min_int = 12, max_int = 12,conn_int = 12,latency = 0, timeout = 600 I (15799) TW_MQTT: Queue empty I (15899) TW_MQTT: Queue empty I (15999) TW_MQTT: Queue empty I (16099) TW_MQTT: Queue empty I (16139) SEC_GATTC_DEMO: ESP_GATTC_CFG_MTU_EVT, Status 0, MTU 247, conn_id 0 I (16149) SEC_GATTC_DEMO: SEARCH RES: conn_id = 0 is primary service 1 I (16149) SEC_GATTC_DEMO: start handle 11 end handle 65535 current handle value 11 I (16159) SEC_GATTC_DEMO: service found I (16159) SEC_GATTC_DEMO: UUID16: fe59 I (16169) SEC_GATTC_DEMO: Get service information from remote device I (16169) SEC_GATTC_DEMO: ESP_GATTC_SEARCH_CMPL_EVT cunt 2 Count before gattc no mem 2Gattc MEMMM Get char by uuid success Count before if 1 Before Write Write success jkasdkljdfjfdadnfkfihorkfjfgfGFGhyuytat I (16189) SEC_GATTC_DEMO: ESP_GATTC_REG_FOR_NOTIFY_EVT I (16199) TW_MQTT: Queue empty in case Write not success in case E (16239) SEC_GATTC_DEMO: write char failed, error status = 13
i am able to pair with device when i tried to send a char getting error like this
@prasad-alatkar sir, The above code is the changes I have made in gattc example Can you please able to find the solution?
Thank you in advance
@monikharajs Can you please provide the code change from the existing example in a better form (the diff or may be the entire file)
EDIT: Also, for a different error, please file a new issue going forward.
This can be issue with peer device. Check if characteristic you are trying write to is allowing to write, maybe requires permission (pairing/bonding)?
@dhrishi W (11091) BT_APPL: bta_gattc_conn_cback() - cif=3 connected=0 conn_id=3 reason=0x0008 E (11091) GATTC_DEMO: write char failed, error status = 85 I (11091) GATTC_DEMO: ESP_GATTC_DISCONNECT_EVT, reason = 8
...
ESP_GATT_CONN_TIMEOUT = 0x08, /*!< Connection timeout */
...
...
ESP_GATT_ERROR = 0x85,
...
what is the reason for this?
Because its bluedroid port im guessing this is accurate answer: https://github.com/qiu-yongheng/GAIAControl/blob/master/blelibrary/src/main/java/com/qualcomm/libraries/ble/ErrorStatus.java#L131
Its just generic GATT error.
Esto puede ser un problema con el dispositivo de igual. Verifique si la característica a la que está intentando escribir le permite escribir, ¿tal vez requiere permiso (emparejamiento / vinculación)?
Como hago para hacer emparejamiento y vinculacion?
Environment
Problem Description
I used the code from the IDF from
esp-idf/examples/bluetooth/gatt_client/main/
and only made the following changes:and the decision whether we found our device just to check the beginning of the string
remote_device_name
:Everything else is unchanged. Full code:
The device I want to connect to is a fitness tracker, providing the Running Speed and Cadence service (
UUID 0x1814
), and the characteristic (UUID 0x2a53
). The device is alive, provides the services (can be checked with an app lika a BLE scanner), and I have an old running code version on one ESP32 device running.Log file is as follows:
Here it stops with the line
E (4055) GATTC_DEMO: write char failed, error status = 3
The last line appears with a little delay of 3-5 seconds, no lines in the next minutes.
Expected behaviour:
register notify and get notifications with updated values, at least not receiving this error.