0

I am sending data to BLE Device as 20 bytes chunks.
I am receiving back large response. But onCharacteristicRead call back, I get only the last piece of the data.

byte[] messageBytes = characteristic.getValue();

if (messageBytes != null && messageBytes.length > 0) {
  for(byte byteChar : messageBytes) {
     stringBuilder.append((char)byteChar);
  }
}
  • Can anyone help understand where I am going wrong?
  • Should I read the data back also as chunks?
  • If so, how?
Jeff Hoang
  • 68
  • 6
JnJ11
  • 35
  • 5

1 Answers1

2

Characteristic's value updates everytime you write to it, that's why when you read, it only reflects the latest value (the last one you write).

To read the data continuously, you should enable the notification on the characteristics first.

mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID_DESCRIPTOR);
descriptor.setValue(enabled
  ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
  : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);

Then you can start writing data

byte[] data = <Your data here>;
BluetoothGattService Service = mBluetoothGatt.getService(UUID_TARGET_SERVICE);

BluetoothGattCharacteristic charac = Service.getCharacteristic(UUID_TARGET_CHARACTERISTIC);

charac.setValue(data);
mBluetoothGatt.writeCharacteristic(charac);

Now everytime you write, the client side will receive a callback of onCharactersticChanged that contains the newly updated value (data). You don't need to call the read operation actually.

Remember mBluetoothGatt can only handle 1 operation at a time, if you execute another one while the previous one is unfinished, it won't put in queue, but will return false.

Yauheni Pakala
  • 868
  • 9
  • 16
Jeff Hoang
  • 68
  • 6
  • Thank you for your time & reply. – JnJ11 May 08 '20 at 15:15
  • I write a register command to write characteristic on my ble device. And I have to get the response around 60-80 bytes from read. I get last 11 bytes of data. Register command is send as 2 packets of data and I read after the write is successful. – JnJ11 May 08 '20 at 17:00
  • Yea you can break down the command into packets of 20 bytes each and write them in the characteristics (one by one). Each time you write, the client side will receive callback of `onCharacteristicChanged` to process the packet. After writing all data you can send a special packet to signal `End Of Stream` so the client can finish the read and combine all the received packets to process the whole data. – Jeff Hoang May 09 '20 at 08:12
  • I am writing to char-W and reading from char-R. So if i put a notification on char-R since i am not writing on it, will it return me onCharacteristicChanged() ? – JnJ11 May 11 '20 at 20:27
  • what do you mean by `char-W` and `char-R`? Once you turn on a notification on a characteristic, all the written data will be returned in onCharacteristicChanged(), so you just do a loop writing one-by-one and on the client side put them all together. – Jeff Hoang May 24 '20 at 08:36
  • Jeff Hoang yes i started receiving the notification. It was a typo error in code. FIxed it and now its working fine. Your solution did help me fix it. Thank you – JnJ11 May 29 '20 at 02:42