2

I am working with the Android SDK's android.bluetooth and android.bluetooth.le APIs.

I want to implement an app (takes central role, and connects to a BLE peripheral to read characteristic values and descriptors).

The characteristic value that the app needs to read is large, and therefore it needs to read it in successive chunks. My understanding of how this is achieved is that the app should include an offset in read requests which indicates to the peripheral the sub-portion of the total data to return in the response.

However, the API only provides a BluetoothGatt.readCharacteristic(BluetoothGattCharacteristic) method. I cannot find a read characteristic API call that includes an offset argument, for making the type of partial read characteristic request I describe in the previous paragraph.

Does anyone know how I achieve these chunked characteristic reads?

Note: I'm aware that there's a similar stack-overflow question here. It didn't provide answer for the Android aspect I'm dealing with.

Community
  • 1
  • 1
user1310850
  • 87
  • 2
  • 6

1 Answers1

2

Just call readCharacteristic and it will in the background read all chunks. When all chunks are read, onCharacteristicRead will be called.

Emil
  • 16,784
  • 2
  • 41
  • 52
  • o.k, but shouldn't the GATT client be able to specify the offset when making a characteristic read request? What about a use-case where the client has already read some of the characteristic value, and therefore needs to read only the remainder, through a request that includes an appropriate offset? – user1310850 Jul 29 '16 at 12:45
  • Yes you are correct. But the GATT client is inside Android's Bluetooth stack, and it is able to make requests for different offset. The outer API between the GATT client and the application is not defined by any standard so Android may include or exclude any functionality they want. And they have indeed not included a "read from offset" functionality. – Emil Jul 29 '16 at 22:03
  • Thanks, I am seeing the Bluetooth stack take care of breaking the value being read into smaller, individual responses, as you describe. Do you know what triggers this behaviour? Is it triggered when the byte[] value parameter passed to BluetoothGattServer.sendResponse(...., byte[] value) equals or exceeds a maximum length? – user1310850 Aug 03 '16 at 14:59
  • When Android's gatt client gets a read response of max possible length, it issues more read blob requests to get the rest until the end. – Emil Aug 03 '16 at 16:56
  • As you have mentioned, I have made request to read the characteristic from Peripheral, and on my peripheral, I have added the `byte []` array on the sendResponse() method. But in my case, data gets appended since the `onCharacteristicReadRequest()` method calls multiple times due to size of `byte []` exceeding 20 bytes. I have tried to resolve this by dividing `byte []` into multiple `byte []` arrays, this is working, but I'm losing some data. – Ritwik Jamuar Sep 20 '17 at 13:55
  • What platform are you using on your peripheral? – Emil Sep 20 '17 at 17:39