From 38f45a9413c25081fef310791e4e2aa0d1056bdd Mon Sep 17 00:00:00 2001 From: geeksville Date: Sun, 24 May 2020 11:07:15 -0700 Subject: [PATCH] fix autobug that showed on a MIX2 (whatever that is), ignore BLE state changes when we don't have a gatt --- .../geeksville/mesh/service/SafeBluetooth.kt | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 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 f4430630..4715763d 100644 --- a/app/src/main/java/com/geeksville/mesh/service/SafeBluetooth.kt +++ b/app/src/main/java/com/geeksville/mesh/service/SafeBluetooth.kt @@ -72,18 +72,21 @@ class SafeBluetooth(private val context: Context, private val device: BluetoothD /// When we see the BT stack getting disabled/renabled we handle that as a connect/disconnect event private val btStateReceiver = BluetoothStateReceiver { enabled -> - if (!enabled) { - if (state == BluetoothProfile.STATE_CONNECTED) - gattCallback.onConnectionStateChange( - gatt!!, - 0, - BluetoothProfile.STATE_DISCONNECTED - ) - else - debug("We were not connected, so ignoring bluetooth shutdown") - } else { - warn("requeue a connect anytime bluetooth is reenabled") - reconnect() + // Sometimes we might not have a gatt object, while that is true, we don't care about BLE state changes + gatt?.let { g -> + if (!enabled) { + if (state == BluetoothProfile.STATE_CONNECTED) + gattCallback.onConnectionStateChange( + g, + 0, + BluetoothProfile.STATE_DISCONNECTED + ) + else + debug("We were not connected, so ignoring bluetooth shutdown") + } else { + warn("requeue a connect anytime bluetooth is reenabled") + reconnect() + } } }