From 590e76731f45e880ff4dda2fe65a827062fc869a Mon Sep 17 00:00:00 2001 From: geeksville Date: Tue, 9 Jun 2020 12:18:35 -0700 Subject: [PATCH] One more fix for Soyes XS phones (but see disclaimer below) The bluetooth implementation of this phone calls the gatt callbacks *before* the connect call returns to the client. This is incorrect because the client won't have a reference to the gatt at that time. Fortunately, we only need to check gatt on disconnect, so I moved that later. But one problem I've noticed in my testing. Sometimes this phone stops being able to scan for BLE devices. The only fix I've found is to click to turn bluetooth off briefly and then back on. A major problem with this phone is that if you reboot the phone it seems the time the phone has forgotten its paring data and devices must be re-paired. This is a huge bummer and I don't think my app can fix this. --- .../geeksville/mesh/service/SafeBluetooth.kt | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 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 67c5a6c5c..f6bd079c7 100644 --- a/app/src/main/java/com/geeksville/mesh/service/SafeBluetooth.kt +++ b/app/src/main/java/com/geeksville/mesh/service/SafeBluetooth.kt @@ -165,24 +165,23 @@ class SafeBluetooth(private val context: Context, private val device: BluetoothD status: Int, newState: Int ) = exceptionReporter { + info("new bluetooth connection state $newState, status $status") - if (gatt == null) - info("No gatt: ignoring connection state $newState, status $status") // Probably just shutting down - else { - info("new bluetooth connection state $newState, status $status") + when (newState) { + BluetoothProfile.STATE_CONNECTED -> { + state = + newState // we only care about connected/disconnected - not the transitional states - when (newState) { - BluetoothProfile.STATE_CONNECTED -> { - state = - newState // we only care about connected/disconnected - not the transitional states - - // If autoconnect is on and this connect attempt failed, hopefully some future attempt will succeed - if (status != BluetoothGatt.GATT_SUCCESS && autoConnect) { - errormsg("Connect attempt failed $status, not calling connect completion handler...") - } else - completeWork(status, Unit) - } - BluetoothProfile.STATE_DISCONNECTED -> { + // If autoconnect is on and this connect attempt failed, hopefully some future attempt will succeed + if (status != BluetoothGatt.GATT_SUCCESS && autoConnect) { + errormsg("Connect attempt failed $status, not calling connect completion handler...") + } else + completeWork(status, Unit) + } + BluetoothProfile.STATE_DISCONNECTED -> { + if (gatt == null) + info("No gatt: ignoring connection state $newState, status $status") // Probably just shutting down + else { // cancel any queued ops if we were already connected val oldstate = state state = newState