From ccbd62fdeb333a9df63cd7aa24b1f70338f7ce9b Mon Sep 17 00:00:00 2001 From: Erik Johansson Date: Thu, 5 Jul 2018 00:03:49 +0200 Subject: [PATCH] Delay the response handling in BLE with ~60 ms Hopefully this avoids error 133 seen in #159. At least that's what https://android.jlelse.eu/lessons-for-first-time-android-bluetooth-le-developers-i-learned-the-hard-way-fee07646624 suggests. --- .../bluetooth/BluetoothCommunication.java | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java index fa832579..bbc9a742 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCommunication.java @@ -684,24 +684,32 @@ public abstract class BluetoothCommunication { setBtMachineState(BT_MACHINE_STATE.BT_INIT_STATE); } + private void postDelayedHandleRequests() { + // Wait a short while before starting the next operation as suggested + // on the android.jlelse.eu link above. + handler.postDelayed(new Runnable() { + @Override + public void run() { + synchronized (lock) { + openRequest = false; + handleRequests(); + } + } + }, 60); + } + @Override public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { - synchronized (lock) { - openRequest = false; - handleRequests(); - } + postDelayedHandleRequests(); } @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { - synchronized (lock) { - openRequest = false; - handleRequests(); - } + postDelayedHandleRequests(); } @Override @@ -713,8 +721,7 @@ public abstract class BluetoothCommunication { synchronized (lock) { onBluetoothDataRead(gatt, characteristic, status); - openRequest = false; - handleRequests(); + postDelayedHandleRequests(); } } @@ -736,10 +743,7 @@ public abstract class BluetoothCommunication { Timber.d("onDescriptorRead %s (status=%d): %s", descriptor.getUuid(), status, byteInHex(descriptor.getValue())); - synchronized (lock) { - openRequest = false; - handleRequests(); - } + postDelayedHandleRequests(); } } }