This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Write to variable length characteristic

I want to send 512 bytes of data over ble through the characteristic value, as maximum length of the characterstic value is 512. When I try to do the transfer more than 20 bytes, it will fail. When I checked in this forum, I got know that we can only transfer 20 bytes each time and make a simulation of 512 byte transfer by continuous calling "write" function with offset increment. Even I checked the all sample code get this code of simulating this. I did not find any sample. If any one implemented the code which can transfer 512 byte, kindly share this.

Parents
  • If you don't need to write everything at once (i.e. data is not always consistent during writes), you can write with offsets. The way to do this is to run sd_ble_tx_buffer_count_get() to get the maximum number of buffers. Split the data into N 20-byte chunks, then for i=0 to N-1 queue them with gattc_write(data[i*20], len=min(20,total_len-20*i),offset=i*20), decrementing your buffer count manually. If you run out of buffers or receive a BUSY return code, wait/sleep until you receive more BLE_EVT_TX_COMPLETE events, and increment your buffer count by the count in the event.

    If you lose your connection, you can only assume that [Total count of TX_COMPLETE counts] packets were sent, and you should continue writing from that place upon reconnection.

Reply
  • If you don't need to write everything at once (i.e. data is not always consistent during writes), you can write with offsets. The way to do this is to run sd_ble_tx_buffer_count_get() to get the maximum number of buffers. Split the data into N 20-byte chunks, then for i=0 to N-1 queue them with gattc_write(data[i*20], len=min(20,total_len-20*i),offset=i*20), decrementing your buffer count manually. If you run out of buffers or receive a BUSY return code, wait/sleep until you receive more BLE_EVT_TX_COMPLETE events, and increment your buffer count by the count in the event.

    If you lose your connection, you can only assume that [Total count of TX_COMPLETE counts] packets were sent, and you should continue writing from that place upon reconnection.

Children
No Data
Related