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 15bbd7f4..359b775b 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 @@ -55,7 +55,8 @@ public abstract class BluetoothCommunication { private Queue descriptorRequestQueue; private Queue characteristicRequestQueue; - private Boolean openRequest; + private boolean openRequest; + private final Object lock = new Object(); public BluetoothCommunication(Context context) { @@ -192,9 +193,10 @@ public abstract class BluetoothCommunication { * @param btMachineState the machine state that should be set. */ protected void setBtMachineState(BT_MACHINE_STATE btMachineState) { - this.btMachineState = btMachineState; - - handleRequests(); + synchronized (lock) { + this.btMachineState = btMachineState; + handleRequests(); + } } /** @@ -209,7 +211,7 @@ public abstract class BluetoothCommunication { .getCharacteristic(characteristic); gattCharacteristic.setValue(bytes); - synchronized (openRequest) { + synchronized (lock) { characteristicRequestQueue.add(gattCharacteristic); handleRequests(); } @@ -244,7 +246,7 @@ public abstract class BluetoothCommunication { BluetoothGattDescriptor gattDescriptor = gattCharacteristic.getDescriptor(descriptor); gattDescriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); - synchronized (openRequest) { + synchronized (lock) { descriptorRequestQueue.add(gattDescriptor); handleRequests(); } @@ -264,7 +266,7 @@ public abstract class BluetoothCommunication { BluetoothGattDescriptor gattDescriptor = gattCharacteristic.getDescriptor(descriptor); gattDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); - synchronized (openRequest) { + synchronized (lock) { descriptorRequestQueue.add(gattDescriptor); handleRequests(); } @@ -284,7 +286,7 @@ public abstract class BluetoothCommunication { BluetoothGattDescriptor gattDescriptor = gattCharacteristic.getDescriptor(descriptor); gattDescriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE); - synchronized (openRequest) { + synchronized (lock) { descriptorRequestQueue.add(gattDescriptor); handleRequests(); } @@ -347,9 +349,13 @@ public abstract class BluetoothCommunication { return; } - if (btMachineState != BT_MACHINE_STATE.BT_CLEANUP_STATE && doCleanup) { - setBtMachineState(BT_MACHINE_STATE.BT_CLEANUP_STATE); - nextMachineStateStep(); + if (doCleanup) { + synchronized (lock) { + if (btMachineState != BT_MACHINE_STATE.BT_CLEANUP_STATE) { + setBtMachineState(BT_MACHINE_STATE.BT_CLEANUP_STATE); + nextMachineStateStep(); + } + } } bluetoothGatt.disconnect(); @@ -384,7 +390,7 @@ public abstract class BluetoothCommunication { } private void handleRequests() { - synchronized (openRequest) { + synchronized (lock) { // check for pending request if (openRequest) { return; // yes, do nothing @@ -436,14 +442,16 @@ public abstract class BluetoothCommunication { @Override public void onServicesDiscovered(final BluetoothGatt gatt, int status) { - cmdStepNr = 0; - initStepNr = 0; - cleanupStepNr = 0; + synchronized (lock) { + cmdStepNr = 0; + initStepNr = 0; + cleanupStepNr = 0; - // Clear from possible previous setups - characteristicRequestQueue = new LinkedList<>(); - descriptorRequestQueue = new LinkedList<>(); - openRequest = false; + // Clear from possible previous setups + characteristicRequestQueue = new LinkedList<>(); + descriptorRequestQueue = new LinkedList<>(); + openRequest = false; + } try { // Sleeping a while after discovering services fixes connection problems. @@ -463,7 +471,7 @@ public abstract class BluetoothCommunication { public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { - synchronized (openRequest) { + synchronized (lock) { openRequest = false; handleRequests(); } @@ -473,7 +481,7 @@ public abstract class BluetoothCommunication { public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { - synchronized (openRequest) { + synchronized (lock) { openRequest = false; handleRequests(); } @@ -483,8 +491,8 @@ public abstract class BluetoothCommunication { public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { - onBluetoothDataRead(gatt, characteristic, status); - synchronized (openRequest) { + synchronized (lock) { + onBluetoothDataRead(gatt, characteristic, status); openRequest = false; handleRequests(); } @@ -493,7 +501,9 @@ public abstract class BluetoothCommunication { @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { - onBluetoothDataChange(gatt, characteristic); + synchronized (lock) { + onBluetoothDataChange(gatt, characteristic); + } } } }