1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-20 23:41:45 +02:00

Synchronize BT disconnect method

Hopefully fixes the crash reported in #252.
This commit is contained in:
Erik Johansson
2018-05-03 23:20:29 +02:00
parent 262237bcbf
commit 806b5127ef

View File

@@ -421,27 +421,27 @@ public abstract class BluetoothCommunication {
* Disconnect from a Bluetooth device * Disconnect from a Bluetooth device
*/ */
public void disconnect(boolean doCleanup) { public void disconnect(boolean doCleanup) {
if (bluetoothGatt == null) { synchronized (lock) {
return; if (bluetoothGatt == null) {
} return;
if (leScanCallback != null) { }
btAdapter.stopLeScan(leScanCallback); if (leScanCallback != null) {
leScanCallback = null; btAdapter.stopLeScan(leScanCallback);
} leScanCallback = null;
}
Timber.i("Disconnecting%s", doCleanup ? " (with cleanup)" : ""); Timber.i("Disconnecting%s", doCleanup ? " (with cleanup)" : "");
if (doCleanup) { if (doCleanup) {
synchronized (lock) {
if (btMachineState != BT_MACHINE_STATE.BT_CLEANUP_STATE) { if (btMachineState != BT_MACHINE_STATE.BT_CLEANUP_STATE) {
setBtMachineState(BT_MACHINE_STATE.BT_CLEANUP_STATE); setBtMachineState(BT_MACHINE_STATE.BT_CLEANUP_STATE);
nextMachineStateStep(); nextMachineStateStep();
} }
} }
}
bluetoothGatt.close(); bluetoothGatt.close();
bluetoothGatt = null; bluetoothGatt = null;
}
} }
/** /**
@@ -521,9 +521,11 @@ public abstract class BluetoothCommunication {
Timber.d("onConnectionStateChange: status=%d, newState=%d", status, newState); Timber.d("onConnectionStateChange: status=%d, newState=%d", status, newState);
if (newState == BluetoothProfile.STATE_CONNECTED) { if (newState == BluetoothProfile.STATE_CONNECTED) {
if (leScanCallback != null) { synchronized (lock) {
btAdapter.stopLeScan(leScanCallback); if (leScanCallback != null) {
leScanCallback = null; btAdapter.stopLeScan(leScanCallback);
leScanCallback = null;
}
} }
connectionEstablished = true; connectionEstablished = true;