new bt scan works on emulator

1.2-legacy
geeksville 2020-02-18 09:09:49 -08:00
rodzic f715091399
commit 8a7de21814
3 zmienionych plików z 46 dodań i 43 usunięć

Wyświetl plik

@ -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()
}

Wyświetl plik

@ -25,40 +25,9 @@ object ScanUIState {
val devices = modelMapOf<String, BTScanEntry>()
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

Wyświetl plik

@ -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")