0

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)?

  • What Android device / OS version do you have? – Emil May 18 '17 at 07:18
  • Android 4.4.2 kitkat – Nikhil Shinde May 18 '17 at 07:20
  • @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 May 18 '17 at 07:43
  • @Emil Android 4.4.2 kitkat – Nikhil Shinde May 18 '17 at 07:43
  • @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 May 18 '17 at 07:54

1 Answers1

1

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.

Community
  • 1
  • 1
Emil
  • 16,784
  • 2
  • 41
  • 52
  • Thank you for the detailed info. I am able to write the packets without response within short period but to check data integrity I loop back my Tx and Rx pin of BLE and checking if the same data I am receiving after OnCharacteristicChange calback and reading the same characteristic. I am sending next packet based on this data comparison. But reading the packet based on OnCharacteristicChanges with same characteristic seems to be very very slow process. Also can I use two different characteristic one for write/send and other for read and how? Thanks! – Nikhil Shinde May 18 '17 at 22:38