From 0350173a2ba2c745d54f59303b95f4c1b3dab1a9 Mon Sep 17 00:00:00 2001 From: Erik Johansson Date: Sun, 17 Jun 2018 20:53:58 +0200 Subject: [PATCH] Let all clean up steps run when disconnecting --- .../bluetooth/BluetoothCommunication.java | 40 +++++++++++++++---- 1 file changed, 32 insertions(+), 8 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 23c4c67c..1e8cf8bf 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 @@ -126,7 +126,10 @@ public abstract class BluetoothCommunication { * @param infoText the information text that is displayed to the status code. */ protected void setBtStatus(BT_STATUS_CODE statusCode, String infoText) { - callbackBtHandler.obtainMessage(statusCode.ordinal(), infoText).sendToTarget(); + if (callbackBtHandler != null) { + callbackBtHandler.obtainMessage( + statusCode.ordinal(), infoText).sendToTarget(); + } } /** @@ -135,7 +138,10 @@ public abstract class BluetoothCommunication { * @param scaleMeasurement the scale data that should be added to openScale */ protected void addScaleData(ScaleMeasurement scaleMeasurement) { - callbackBtHandler.obtainMessage(BT_STATUS_CODE.BT_RETRIEVE_SCALE_DATA.ordinal(), scaleMeasurement).sendToTarget(); + if (callbackBtHandler != null) { + callbackBtHandler.obtainMessage( + BT_STATUS_CODE.BT_RETRIEVE_SCALE_DATA.ordinal(), scaleMeasurement).sendToTarget(); + } } /** @@ -145,7 +151,10 @@ public abstract class BluetoothCommunication { * @param value the value to be used */ protected void sendMessage(int msg, Object value) { - callbackBtHandler.obtainMessage(BT_STATUS_CODE.BT_SCALE_MESSAGE.ordinal(), msg, 0, value).sendToTarget(); + if (callbackBtHandler != null) { + callbackBtHandler.obtainMessage( + BT_STATUS_CODE.BT_SCALE_MESSAGE.ordinal(), msg, 0, value).sendToTarget(); + } } /** @@ -496,17 +505,32 @@ public abstract class BluetoothCommunication { Timber.i("Disconnecting%s", doCleanup ? " (with cleanup)" : ""); + handler.removeCallbacksAndMessages(null); + callbackBtHandler = null; + if (doCleanup) { if (btMachineState != BT_MACHINE_STATE.BT_CLEANUP_STATE) { setBtMachineState(BT_MACHINE_STATE.BT_CLEANUP_STATE); nextMachineStateStep(); } + handler.post(new Runnable() { + @Override + public void run() { + synchronized (lock) { + if (openRequest) { + handler.postDelayed(this, 10); + } else { + bluetoothGatt.close(); + bluetoothGatt = null; + } + } + } + }); + } + else { + bluetoothGatt.close(); + bluetoothGatt = null; } - - bluetoothGatt.close(); - bluetoothGatt = null; - - setBtStatus(BT_STATUS_CODE.BT_CONNECTION_LOST); } }