0
votes

I am trying to write next packet synchronously based on the OnCharacteristicWrite call back condition to achieve a maximum throughput. But for some reason it stops triggering OnCharacteristicWrite callback at very initial after 1-2 sec of period and it never get called even I resend the packets. It works well if I add the delay per packet but I do not want to add any delay to achieve maximum throughput.

Is there any way I could achieve the maximum throughput without adding any delay?

Also what exactly sending multiple packets per connection interval means (and Is there any way I could achieve it through the peripheral)?

1
What Android device / OS version do you have? - Emil
Android 4.4.2 kitkat - Nikhil Shinde
@Emil Also my BLE version is 4.2. But I am not getting how to send multiple packets per conn interval. I am sending packets one by one based on when the onCharacteristicWrite callback is received for each packet. But after certain period (after around 300 packets with a onCharacteristicWrite callback status 0 successful) the callback stops suddenly and never gets triggered even on packet resend. So I am adding a delay of 8ms in betwn two packets which writes all the packets successfully with a callback status 0 but This actually lowers the throughput. Is there any better way to implement this? - Nikhil Shinde
@Emil Android 4.4.2 kitkat - Nikhil Shinde
@Emil My Gatt WriteCharacteristic function returns the boolean true when I send any packet. What is the difference between this return true and between OnWriteCharacteristic Callback status -0 successful after each packet is sent?? - Nikhil Shinde

1 Answers

1
votes

If you use Write Without Response (see https://developer.android.com/reference/android/bluetooth/BluetoothGattCharacteristic.html#setWriteType(int)), you will be able to send multiple packets per connection interval.

Android KitKat unfortunately has broken flow control when you send multiple packets with "Write Without Response". If you try on a newer Android device, it should work properly.

If the writeCharacteristic method returns true, it just means it has passed your packet to the Bluetooth process. You can see the exact logic in the source code at https://android.googlesource.com/platform/frameworks/base/+/fe2bf16a2b287c3c748cd6fa7c14026becfe83ff/core/java/android/bluetooth/BluetoothGatt.java#1081. Basically it returns true if the characteristic has the write property, the gatt object is valid and there is currently no other pending GATT operation going on.

The onCharacteristicWrite callback will send status=0 when the Write Response has arrived (for Write With Response) or the Bluetooth stack is ready and has buffer space to accept a new packet (for Write Without Response).

I recently wrote a post about that here you could read: onCharacteristicWrite and onNotificationSent are being called too fast - how to acquire real outgoing data rates?.

If you want a simple workaround for KitKat you could write 10 packets as Write Without Response and then the 11th packet as Write With Response and then start over with Write Without Responses. That should give you decent performance.