From 4f47b619a334ea86f5e0f99f94d84c276c5f7da7 Mon Sep 17 00:00:00 2001 From: geeksville Date: Mon, 30 Mar 2020 17:37:02 -0700 Subject: [PATCH] do the painful process of waiting for initial pairing to complete and once it completes automatically connect to the radio should improve user experience for brand new app installs with new devices --- .../com/geeksville/mesh/ui/BTScanScreen.kt | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/app/src/main/java/com/geeksville/mesh/ui/BTScanScreen.kt b/app/src/main/java/com/geeksville/mesh/ui/BTScanScreen.kt index 1811faafc..a239af3ea 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/BTScanScreen.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/BTScanScreen.kt @@ -3,7 +3,10 @@ package com.geeksville.mesh.ui import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothManager import android.bluetooth.le.* +import android.content.BroadcastReceiver import android.content.Context +import android.content.Intent +import android.content.IntentFilter import android.os.ParcelUuid import androidx.compose.Composable import androidx.compose.Model @@ -20,6 +23,7 @@ import androidx.ui.material.RadioGroup import androidx.ui.tooling.preview.Preview import com.geeksville.android.Logging import com.geeksville.mesh.service.RadioInterfaceService +import com.geeksville.util.exceptionReporter @Model @@ -185,6 +189,34 @@ fun BTScanScreen() { } else { ScanState.info("Starting bonding for $it") + // We need this receiver to get informed when the bond attempt finished + val bondChangedReceiver = object : BroadcastReceiver() { + + override fun onReceive( + context: Context, + intent: Intent + ) = exceptionReporter { + val state = + intent.getIntExtra( + BluetoothDevice.EXTRA_BOND_STATE, + -1 + ) + ScanState.debug("Received bond state changed $state") + context.unregisterReceiver(this) + if (state == BluetoothDevice.BOND_BONDED || state == BluetoothDevice.BOND_BONDING) { + ScanState.debug("Bonding completed, connecting service") + ScanUIState.changeSelection( + context, + it.macAddress + ) + } + } + } + + val filter = IntentFilter() + filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED) + context.registerReceiver(bondChangedReceiver, filter) + // We ignore missing BT adapters, because it lets us run on the emulator bluetoothAdapter ?.getRemoteDevice(it.macAddress)