diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothBeurerSanitas.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothBeurerSanitas.java index 38305a2c..d2b87051 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothBeurerSanitas.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothBeurerSanitas.java @@ -146,7 +146,7 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication { } @Override - boolean nextInitCmd(int stateNr) { + protected boolean nextInitCmd(int stateNr) { switch (stateNr) { case 0: @@ -245,7 +245,7 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication { } @Override - boolean nextBluetoothCmd(int stateNr) { + protected boolean nextBluetoothCmd(int stateNr) { switch (stateNr) { case 0: @@ -274,7 +274,7 @@ public class BluetoothBeurerSanitas extends BluetoothCommunication { } @Override - boolean nextCleanUpCmd(int stateNr) { + protected boolean nextCleanUpCmd(int stateNr) { switch (stateNr) { case 0: // Force disconnect 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 561a5bca..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) { @@ -126,7 +127,7 @@ public abstract class BluetoothCommunication { * @param stateNr the current step number * @return false if no next step is available otherwise true */ - abstract boolean nextInitCmd(int stateNr); + abstract protected boolean nextInitCmd(int stateNr); /** * State machine for the normal/command process of the Bluetooth device. @@ -136,7 +137,7 @@ public abstract class BluetoothCommunication { * @param stateNr the current step number * @return false if no next step is available otherwise true */ - abstract boolean nextBluetoothCmd(int stateNr); + abstract protected boolean nextBluetoothCmd(int stateNr); /** * Set the next command number of the current state. @@ -165,7 +166,7 @@ public abstract class BluetoothCommunication { * @param stateNr the current step number * @return false if no next step is available otherwise true */ - abstract boolean nextCleanUpCmd(int stateNr); + abstract protected boolean nextCleanUpCmd(int stateNr); /** * Method is triggered if a Bluetooth data is read from a device. @@ -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(); } @@ -332,6 +334,9 @@ public abstract class BluetoothCommunication { public void connect(String hwAddress) { btAdapter.cancelDiscovery(); + // Don't do any cleanup if disconnected before fully connected + btMachineState = BT_MACHINE_STATE.BT_CLEANUP_STATE; + BluetoothDevice device = btAdapter.getRemoteDevice(hwAddress); bluetoothGatt = device.connectGatt(context, false, gattCallback); } @@ -344,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(); @@ -381,7 +390,7 @@ public abstract class BluetoothCommunication { } private void handleRequests() { - synchronized (openRequest) { + synchronized (lock) { // check for pending request if (openRequest) { return; // yes, do nothing @@ -433,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. @@ -460,7 +471,7 @@ public abstract class BluetoothCommunication { public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { - synchronized (openRequest) { + synchronized (lock) { openRequest = false; handleRequests(); } @@ -470,7 +481,7 @@ public abstract class BluetoothCommunication { public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { - synchronized (openRequest) { + synchronized (lock) { openRequest = false; handleRequests(); } @@ -480,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(); } @@ -490,7 +501,9 @@ public abstract class BluetoothCommunication { @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { - onBluetoothDataChange(gatt, characteristic); + synchronized (lock) { + onBluetoothDataChange(gatt, characteristic); + } } } } diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCustomOpenScale.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCustomOpenScale.java index 4f89d14b..c593656e 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCustomOpenScale.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCustomOpenScale.java @@ -47,17 +47,17 @@ public class BluetoothCustomOpenScale extends BluetoothCommunication { } @Override - boolean nextInitCmd(int stateNr) { + protected boolean nextInitCmd(int stateNr) { return false; } @Override - boolean nextBluetoothCmd(int stateNr) { + protected boolean nextBluetoothCmd(int stateNr) { return false; } @Override - boolean nextCleanUpCmd(int stateNr) { + protected boolean nextCleanUpCmd(int stateNr) { return false; } diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothDigooDGSO38H.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothDigooDGSO38H.java index dfa4f6f5..93384699 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothDigooDGSO38H.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothDigooDGSO38H.java @@ -56,7 +56,7 @@ public class BluetoothDigooDGSO38H extends BluetoothCommunication { @Override - boolean nextInitCmd(int stateNr) { + protected boolean nextInitCmd(int stateNr) { switch (stateNr) { case 0: //Tell device to send us weight measurements @@ -68,7 +68,7 @@ public class BluetoothDigooDGSO38H extends BluetoothCommunication { } @Override - boolean nextBluetoothCmd(int stateNr) { + protected boolean nextBluetoothCmd(int stateNr) { switch (stateNr) { default: return false; @@ -76,7 +76,7 @@ public class BluetoothDigooDGSO38H extends BluetoothCommunication { } @Override - boolean nextCleanUpCmd(int stateNr) { + protected boolean nextCleanUpCmd(int stateNr) { switch (stateNr) { default: diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothExcelvanCF369BLE.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothExcelvanCF369BLE.java index 27415c63..7b04a100 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothExcelvanCF369BLE.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothExcelvanCF369BLE.java @@ -46,12 +46,12 @@ public class BluetoothExcelvanCF369BLE extends BluetoothCommunication { } @Override - boolean nextInitCmd(int stateNr) { + protected boolean nextInitCmd(int stateNr) { return false; } @Override - boolean nextBluetoothCmd(int stateNr) { + protected boolean nextBluetoothCmd(int stateNr) { switch (stateNr) { case 0: final ScaleUser selectedUser = OpenScale.getInstance(context).getSelectedScaleUser(); @@ -87,7 +87,7 @@ public class BluetoothExcelvanCF369BLE extends BluetoothCommunication { } @Override - boolean nextCleanUpCmd(int stateNr) { + protected boolean nextCleanUpCmd(int stateNr) { return false; } diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothExingtechY1.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothExingtechY1.java index 39d5b2d8..e4257584 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothExingtechY1.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothExingtechY1.java @@ -43,7 +43,7 @@ public class BluetoothExingtechY1 extends BluetoothCommunication { } @Override - boolean nextInitCmd(int stateNr) { + protected boolean nextInitCmd(int stateNr) { switch (stateNr) { case 0: setNotificationOn(WEIGHT_MEASUREMENT_SERVICE, WEIGHT_MEASUREMENT_CHARACTERISTIC, WEIGHT_MEASUREMENT_CONFIG); @@ -69,12 +69,12 @@ public class BluetoothExingtechY1 extends BluetoothCommunication { } @Override - boolean nextBluetoothCmd(int stateNr) { + protected boolean nextBluetoothCmd(int stateNr) { return false; } @Override - boolean nextCleanUpCmd(int stateNr) { + protected boolean nextCleanUpCmd(int stateNr) { return false; } diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothHesley.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothHesley.java index 4e81b9af..00fa2b92 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothHesley.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothHesley.java @@ -41,7 +41,7 @@ public class BluetoothHesley extends BluetoothCommunication { } @Override - boolean nextInitCmd(int stateNr) { + protected boolean nextInitCmd(int stateNr) { switch (stateNr) { case 0: setNotificationOn(WEIGHT_MEASUREMENT_SERVICE, WEIGHT_MEASUREMENT_CHARACTERISTIC, WEIGHT_MEASUREMENT_CONFIG); @@ -58,12 +58,12 @@ public class BluetoothHesley extends BluetoothCommunication { } @Override - boolean nextBluetoothCmd(int stateNr) { + protected boolean nextBluetoothCmd(int stateNr) { return false; } @Override - boolean nextCleanUpCmd(int stateNr) { + protected boolean nextCleanUpCmd(int stateNr) { return false; } diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothIhealthHS3.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothIhealthHS3.java index ebe5f9ae..9e2a406b 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothIhealthHS3.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothIhealthHS3.java @@ -54,19 +54,19 @@ public class BluetoothIhealthHS3 extends BluetoothCommunication { } @Override - boolean nextInitCmd(int stateNr) { + protected boolean nextInitCmd(int stateNr) { Log.w("openscale","ihealthHS3 - nextInitCmd - returning false"); return false; } @Override - boolean nextBluetoothCmd(int stateNr) { + protected boolean nextBluetoothCmd(int stateNr) { Log.w("openscale","ihealthHS3 - nextBluetoothCmd - returning false"); return false; } @Override - boolean nextCleanUpCmd(int stateNr) { + protected boolean nextCleanUpCmd(int stateNr) { Log.w("openscale","ihealthHS3 - nextCleanUpCmd - returning false"); return false; } diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothMGB.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothMGB.java index 9036bdd7..c313cea3 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothMGB.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothMGB.java @@ -84,7 +84,7 @@ public class BluetoothMGB extends BluetoothCommunication { } @Override - boolean nextInitCmd(int stateNr) { + protected boolean nextInitCmd(int stateNr) { switch (stateNr) { case 0: setNotificationOn(uuid_service, uuid_char_ctrl, uuid_desc_ctrl); @@ -125,13 +125,13 @@ public class BluetoothMGB extends BluetoothCommunication { @Override - boolean nextBluetoothCmd(int stateNr) { + protected boolean nextBluetoothCmd(int stateNr) { return false; } @Override - boolean nextCleanUpCmd(int stateNr) { + protected boolean nextCleanUpCmd(int stateNr) { return false; } diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothMedisanaBS444.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothMedisanaBS444.java index ab17dd5f..20d844c6 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothMedisanaBS444.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothMedisanaBS444.java @@ -46,12 +46,12 @@ public class BluetoothMedisanaBS444 extends BluetoothCommunication { } @Override - boolean nextInitCmd(int stateNr) { + protected boolean nextInitCmd(int stateNr) { return false; } @Override - boolean nextBluetoothCmd(int stateNr) { + protected boolean nextBluetoothCmd(int stateNr) { switch (stateNr) { case 0: // set indication on for feature characteristic @@ -89,7 +89,7 @@ public class BluetoothMedisanaBS444 extends BluetoothCommunication { } @Override - boolean nextCleanUpCmd(int stateNr) { + protected boolean nextCleanUpCmd(int stateNr) { return false; } diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothMiScale.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothMiScale.java index 3c93fc35..bff3b2e2 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothMiScale.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothMiScale.java @@ -98,7 +98,7 @@ public class BluetoothMiScale extends BluetoothCommunication { @Override - boolean nextInitCmd(int stateNr) { + protected boolean nextInitCmd(int stateNr) { switch (stateNr) { case 0: // read device time @@ -136,7 +136,7 @@ public class BluetoothMiScale extends BluetoothCommunication { } @Override - boolean nextBluetoothCmd(int stateNr) { + protected boolean nextBluetoothCmd(int stateNr) { switch (stateNr) { case 0: // set notification on for weight measurement history @@ -173,7 +173,7 @@ public class BluetoothMiScale extends BluetoothCommunication { } @Override - boolean nextCleanUpCmd(int stateNr) { + protected boolean nextCleanUpCmd(int stateNr) { switch (stateNr) { case 0: diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothMiScale2.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothMiScale2.java index 44950297..99efd95a 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothMiScale2.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothMiScale2.java @@ -85,7 +85,7 @@ public class BluetoothMiScale2 extends BluetoothCommunication { @Override - boolean nextInitCmd(int stateNr) { + protected boolean nextInitCmd(int stateNr) { switch (stateNr) { case 0: // set scale units @@ -119,7 +119,7 @@ public class BluetoothMiScale2 extends BluetoothCommunication { } @Override - boolean nextBluetoothCmd(int stateNr) { + protected boolean nextBluetoothCmd(int stateNr) { switch (stateNr) { case 0: // configure scale to get only last measurements @@ -148,7 +148,7 @@ public class BluetoothMiScale2 extends BluetoothCommunication { } @Override - boolean nextCleanUpCmd(int stateNr) { + protected boolean nextCleanUpCmd(int stateNr) { switch (stateNr) { case 0: diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothOneByone.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothOneByone.java index f13e14c1..d39b99f5 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothOneByone.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothOneByone.java @@ -45,7 +45,7 @@ public class BluetoothOneByone extends BluetoothCommunication { } @Override - boolean nextInitCmd(int stateNr) { + protected boolean nextInitCmd(int stateNr) { switch (stateNr) { case 0: setIndicationOn(WEIGHT_MEASUREMENT_SERVICE_BODY_COMPOSITION, WEIGHT_MEASUREMENT_CHARACTERISTIC_BODY_COMPOSITION, WEIGHT_MEASUREMENT_CONFIG); @@ -69,12 +69,12 @@ public class BluetoothOneByone extends BluetoothCommunication { } @Override - boolean nextBluetoothCmd(int stateNr) { + protected boolean nextBluetoothCmd(int stateNr) { return false; } @Override - boolean nextCleanUpCmd(int stateNr) { + protected boolean nextCleanUpCmd(int stateNr) { return false; } diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothYunmaiSE_Mini.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothYunmaiSE_Mini.java index 7f1ae210..663fcf85 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothYunmaiSE_Mini.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothYunmaiSE_Mini.java @@ -52,7 +52,7 @@ public class BluetoothYunmaiSE_Mini extends BluetoothCommunication { } @Override - boolean nextInitCmd(int stateNr) { + protected boolean nextInitCmd(int stateNr) { switch (stateNr) { case 0: byte[] userId = Converters.toUnsignedInt16Be(getUniqueNumber()); @@ -94,12 +94,12 @@ public class BluetoothYunmaiSE_Mini extends BluetoothCommunication { } @Override - boolean nextBluetoothCmd(int stateNr) { + protected boolean nextBluetoothCmd(int stateNr) { return false; } @Override - boolean nextCleanUpCmd(int stateNr) { + protected boolean nextCleanUpCmd(int stateNr) { return false; }