From 0b7f853abce9b3ecb7cc0ab8dd4ad60975ab821e Mon Sep 17 00:00:00 2001 From: geeksville Date: Sun, 10 May 2020 21:39:49 -0700 Subject: [PATCH] fix an autobug - race condition on some phones BLE reconnect --- .../com/geeksville/mesh/service/SafeBluetooth.kt | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/service/SafeBluetooth.kt b/app/src/main/java/com/geeksville/mesh/service/SafeBluetooth.kt index 8dd81830e..feb2af321 100644 --- a/app/src/main/java/com/geeksville/mesh/service/SafeBluetooth.kt +++ b/app/src/main/java/com/geeksville/mesh/service/SafeBluetooth.kt @@ -187,8 +187,10 @@ class SafeBluetooth(private val context: Context, private val device: BluetoothD // Cancel any notifications - because when the device comes back it might have forgotten about us notifyHandlers.clear() - debug("calling lostConnect handler") - lostConnectCallback?.invoke() + lostConnectCallback?.let { + debug("calling lostConnect handler") + it.invoke() + } // Queue a new connection attempt val cb = connectionCallback @@ -420,7 +422,7 @@ class SafeBluetooth(private val context: Context, private val device: BluetoothD ) { logAssert(workQueue.isEmpty()) logAssert(currentWork == null) // I don't think anything should be able to sneak in front - + lostConnectCallback = lostConnectCb connectionCallback = if (autoConnect) cb @@ -508,6 +510,10 @@ class SafeBluetooth(private val context: Context, private val device: BluetoothD * cancelled and you'll need to recall connect to use this againt */ fun closeConnection() { + // Set these to null _before_ calling gatt.disconnect(), because we don't want the old lostConnectCallback to get called + lostConnectCallback = null + connectionCallback = null + failAllWork(BLEException("Connection closing")) if (gatt != null) { @@ -515,8 +521,6 @@ class SafeBluetooth(private val context: Context, private val device: BluetoothD gatt!!.disconnect() gatt!!.close() gatt = null - lostConnectCallback = null - connectionCallback = null } }