How to send data (write char) to BLE without delay?

leesz123
Posts: 2
Joined: Tue Jun 13, 2017 2:06 am

How to send data (write char) to BLE without delay?

Postby leesz123 » Tue Jun 13, 2017 2:14 am

I call the esp_ble_gattc_write_char() method to send a command to my BLE device, the command is an uint16_t array contain only 40 data like this:

Code: Select all

uint16_t rawCodes[] = {2550, 950, 350, ……., 600, 300};
I think my data is small enough, but I still get 500ms delay when I send the command from ESP32 to my BLE device. I found the delay is almost in the method "esp_ble_gattc_write_char()". It's too long to me , I hope the delay should not more than 50ms at least.

So maybe I have something wrong with the method called, or I should call another method to send data. I hope you can give me some suggestion on how to send the data without delay, thank you very much!

This is my code that I call the method "esp_ble_gattc_write_char()" to send my data(rawCodes):

Code: Select all

sendData(conn_id, (uint8_t*)&rawCodes, sizeof(rawCodes));

void sendData(uint16_t conn_id, uint8_t *value, uint16_t len) {
   esp_ble_gattc_write_char(
     4,                                          
     conn_id,                           
     &service_id,                              
     &tx_rx_id,                                 
     len,                                               
     value,                                               
     ESP_GATT_WRITE_TYPE_RSP,           
     ESP_GATT_AUTH_REQ_NONE);
 }
Log Info:

Code: Select all

**********************GATT CALLBACK  4**************************
WRITE: status 0
sending...
E (220278) BT: value resp op_code = ATT_RSP_PREPARE_WRITE len = 22
E (220378) BT: value resp op_code = ATT_RSP_PREPARE_WRITE len = 22
E (220478) BT: value resp op_code = ATT_RSP_PREPARE_WRITE len = 22
E (220578) BT: value resp op_code = ATT_RSP_PREPARE_WRITE len = 22
E (220678) BT: value resp op_code = ATT_RSP_PREPARE_WRITE len = 16

frax84
Posts: 42
Joined: Thu Jan 07, 2016 11:26 am

Re: How to send data (write char) to BLE without delay?

Postby frax84 » Tue Jun 13, 2017 9:49 am

Hi,
some considerations about what you're asking.
You say that you send to a char a uint16_t array of 40 values. If i well understand this make a total of 80 bytes to send using BLE package (even if they're probably 108 for a total of 5 transfers based on the code you posted). If i'm not wrong, considering that the payload for BLE packets are about 20 bytes (don't remember exactly how many...probably 22) you need 4 complete transfers to write your characteristic. Moreover, in send_data function you're setting ESP_GATT_WRITE_TYPE_RSP and this affects the total latency of the procedure. This is what happens:

1) You ask your client to send 80 bytes to char
2) First 22 bytes are prepared and sent through BT at the first connection interval available
3) First 22 bytes are acknoledged by the server at the second connection interval
4) Second 22 bytes are sent at third connection interval
2) Second 22 byters are acknoledged at 4th connection interval
etc.

If your connection interval is set, let's say, to 50ms, you need a total of 8x50ms (400ms) to send and acknowledge all the data and this would explain the total latency of your write.
In my opinion, you can try 2 things:
1) set packet to ESP_GATT_WRITE_TYPE_NO_RSP: this should cut by half the transfer because you don't need to acknoledge your data
2) set connection interval to more little values.

I hope this can provide you some help,
frax84

leesz123
Posts: 2
Joined: Tue Jun 13, 2017 2:06 am

Re: How to send data (write char) to BLE without delay?

Postby leesz123 » Wed Jun 14, 2017 8:06 am

It works! Thank you for your very detailed and helpful reply.

I try to divide the data array to several arrays which size of less than 20 bytes,and set the packet to ESP_GATT_WRITE_TYPE_NO_RSP, then write the arrays one by one to char, finally it dose work, my BLE device receive the data without latency .

Thank you again, best wish!

frax84
Posts: 42
Joined: Thu Jan 07, 2016 11:26 am

Re: How to send data (write char) to BLE without delay?

Postby frax84 » Thu Jun 15, 2017 5:34 am

Nice to see it could help ;)

frax84

Who is online

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