From 8a7de21814ebe3cd5b6bc7beca4011e5f64483f7 Mon Sep 17 00:00:00 2001 From: geeksville Date: Tue, 18 Feb 2020 09:09:49 -0800 Subject: [PATCH] new bt scan works on emulator --- .../java/com/geeksville/mesh/MainActivity.kt | 10 ++- .../com/geeksville/mesh/ui/BTScanScreen.kt | 74 +++++++++---------- .../java/com/geeksville/mesh/ui/MeshApp.kt | 5 +- 3 files changed, 46 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/MainActivity.kt b/app/src/main/java/com/geeksville/mesh/MainActivity.kt index d4060bcc9..382836f18 100644 --- a/app/src/main/java/com/geeksville/mesh/MainActivity.kt +++ b/app/src/main/java/com/geeksville/mesh/MainActivity.kt @@ -22,6 +22,7 @@ import com.geeksville.mesh.model.TextMessage import com.geeksville.mesh.model.UIState import com.geeksville.mesh.service.* import com.geeksville.mesh.ui.MeshApp +import com.geeksville.mesh.ui.ScanState import com.geeksville.mesh.ui.getInitials import com.geeksville.util.exceptionReporter import com.google.android.gms.auth.api.signin.GoogleSignIn @@ -389,15 +390,16 @@ class MainActivity : AppCompatActivity(), Logging, UIState.meshService = null } - override fun onPause() { + override fun onStop() { + ScanState.stopScan() unregisterMeshReceiver() // No point in receiving updates while the GUI is gone, we'll get them when the user launches the activity unbindMeshService() - super.onPause() + super.onStop() } - override fun onResume() { - super.onResume() + override fun onStart() { + super.onStart() bindMeshService() } 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 1e397922b..b596ee67e 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/BTScanScreen.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/BTScanScreen.kt @@ -25,40 +25,9 @@ object ScanUIState { val devices = modelMapOf() - val scanCallback = object : ScanCallback() { - override fun onScanFailed(errorCode: Int) { - val msg = "Unexpected bluetooth scan failure: $errorCode" - ScanUIState.errorText = msg - ScanState.reportError(msg) - } - - // For each device that appears in our scan, ask for its GATT, when the gatt arrives, - // check if it is an eligable device and store it in our list of candidates - // if that device later disconnects remove it as a candidate - override fun onScanResult(callbackType: Int, result: ScanResult) { - - val addr = result.device.address - // prevent logspam because weill get get lots of redundant scan results - if (!devices.contains(addr)) { - val entry = BTScanEntry( - result.device.name, - addr, - result.device.bondState == BluetoothDevice.BOND_BONDED - ) - ScanState.debug("onScanResult ${entry}") - devices[addr] = entry - - // If nothing was selected, by default select the first thing we see - if (ScanUIState.selectedMacAddr == null && entry.bonded) - changeSelection(addr) - } - } - } - /// Change to a new macaddr selection, updating GUI and radio - fun changeSelection(newAddr: String) { + fun changeSelection(context: Context, newAddr: String) { ScanState.info("Changing BT device to $newAddr") - val context = ambient(ContextAmbient) selectedMacAddr = newAddr RadioInterfaceService.setBondedDeviceAddress(context, newAddr) } @@ -95,6 +64,35 @@ fun BTScanScreen() { onActive { ScanUIState.selectedMacAddr = RadioInterfaceService.getBondedDeviceAddress(context) + val scanCallback = object : ScanCallback() { + override fun onScanFailed(errorCode: Int) { + val msg = "Unexpected bluetooth scan failure: $errorCode" + ScanUIState.errorText = msg + ScanState.reportError(msg) + } + + // For each device that appears in our scan, ask for its GATT, when the gatt arrives, + // check if it is an eligable device and store it in our list of candidates + // if that device later disconnects remove it as a candidate + override fun onScanResult(callbackType: Int, result: ScanResult) { + + val addr = result.device.address + // prevent logspam because weill get get lots of redundant scan results + if (!ScanUIState.devices.contains(addr)) { + val entry = BTScanEntry( + result.device.name, + addr, + result.device.bondState == BluetoothDevice.BOND_BONDED + ) + ScanState.debug("onScanResult ${entry}") + ScanUIState.devices[addr] = entry + + // If nothing was selected, by default select the first thing we see + if (ScanUIState.selectedMacAddr == null && entry.bonded) + ScanUIState.changeSelection(context, addr) + } + } + } if (bluetoothAdapter == null) { ScanState.warn("No bluetooth adapter. Running under emulation?") @@ -107,7 +105,7 @@ fun BTScanScreen() { // If nothing was selected, by default select the first thing we see if (ScanUIState.selectedMacAddr == null) - ScanUIState.changeSelection(testnodes.first().macAddress) + ScanUIState.changeSelection(context, testnodes.first().macAddress) } else { val s = bluetoothAdapter.bluetoothLeScanner // ScanState.scanner = scanner @@ -122,9 +120,9 @@ fun BTScanScreen() { val settings = ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build() - s.startScan(listOf(filter), settings, ScanUIState.scanCallback) + s.startScan(listOf(filter), settings, scanCallback) ScanState.scanner = s - ScanState.callback = ScanUIState.scanCallback + ScanState.callback = scanCallback } onDispose { @@ -160,9 +158,9 @@ fun BTScanScreen() { selected = (it.isSelected), onSelect = { // If the device is paired, let user select it, otherwise start the pairing flow - if (it.bonded) - ScanUIState.changeSelection(it.macAddress) - else { + if (it.bonded) { + ScanUIState.changeSelection(context, it.macAddress) + } else { ScanState.info("Starting bonding for $it") // We ignore missing BT adapters, because it lets us run on the emulator diff --git a/app/src/main/java/com/geeksville/mesh/ui/MeshApp.kt b/app/src/main/java/com/geeksville/mesh/ui/MeshApp.kt index 5a0909075..f53f7d24e 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/MeshApp.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/MeshApp.kt @@ -34,7 +34,10 @@ fun HomeContent() { Column { Row { Container(LayoutSize(40.dp, 40.dp)) { - VectorImage(id = if (UIState.isConnected.value) R.drawable.cloud_on else R.drawable.cloud_off) + VectorImage( + id = if (UIState.isConnected.value) R.drawable.cloud_on else R.drawable.cloud_off, + tint = palette.onBackground + ) } Text(if (UIState.isConnected.value) "Connected" else "Not Connected")