From 7a3cda314e80500576d35f4c9be69437f8fed583 Mon Sep 17 00:00:00 2001 From: geeksville Date: Sun, 7 Jun 2020 18:05:18 -0700 Subject: [PATCH] Fix recent bug where sometimes phone would not auto-reconnect to devices --- .../java/com/geeksville/mesh/service/SafeBluetooth.kt | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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 218810824..eaaa3c155 100644 --- a/app/src/main/java/com/geeksville/mesh/service/SafeBluetooth.kt +++ b/app/src/main/java/com/geeksville/mesh/service/SafeBluetooth.kt @@ -214,7 +214,10 @@ class SafeBluetooth(private val context: Context, private val device: BluetoothD debug("queuing a reconnection callback") assert(currentWork == null) - // note - we don't need an init fn (because that would normally redo the connectGatt call - which we don't need + if (!currentConnectIsAuto) // we must have been running during that 1-time manual connect, switch to auto-mode from now on + lowLevelConnect(true) + + // note - we don't need an init fn (because that would normally redo the connectGatt call - which we don't need) queueWork("reconnect", CallbackContinuation(cb)) { -> true } } else { debug("No connectionCallback registered") @@ -465,7 +468,13 @@ class SafeBluetooth(private val context: Context, private val device: BluetoothD // Is the gatt trying to repeatedly connect as needed? private var autoConnect = false + /// True if the current active connection is auto (possible for this to be false but autoConnect to be true + /// if we are in the first non-automated lowLevel connect. + private var currentConnectIsAuto = false + private fun lowLevelConnect(autoNow: Boolean): BluetoothGatt? { + currentConnectIsAuto = autoNow + val g = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { device.connectGatt( context,