mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
new bt scan works on emulator
This commit is contained in:
parent
f715091399
commit
8a7de21814
3 changed files with 46 additions and 43 deletions
|
|
@ -22,6 +22,7 @@ import com.geeksville.mesh.model.TextMessage
|
||||||
import com.geeksville.mesh.model.UIState
|
import com.geeksville.mesh.model.UIState
|
||||||
import com.geeksville.mesh.service.*
|
import com.geeksville.mesh.service.*
|
||||||
import com.geeksville.mesh.ui.MeshApp
|
import com.geeksville.mesh.ui.MeshApp
|
||||||
|
import com.geeksville.mesh.ui.ScanState
|
||||||
import com.geeksville.mesh.ui.getInitials
|
import com.geeksville.mesh.ui.getInitials
|
||||||
import com.geeksville.util.exceptionReporter
|
import com.geeksville.util.exceptionReporter
|
||||||
import com.google.android.gms.auth.api.signin.GoogleSignIn
|
import com.google.android.gms.auth.api.signin.GoogleSignIn
|
||||||
|
|
@ -389,15 +390,16 @@ class MainActivity : AppCompatActivity(), Logging,
|
||||||
UIState.meshService = null
|
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
|
unregisterMeshReceiver() // No point in receiving updates while the GUI is gone, we'll get them when the user launches the activity
|
||||||
unbindMeshService()
|
unbindMeshService()
|
||||||
|
|
||||||
super.onPause()
|
super.onStop()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onResume() {
|
override fun onStart() {
|
||||||
super.onResume()
|
super.onStart()
|
||||||
|
|
||||||
bindMeshService()
|
bindMeshService()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,40 +25,9 @@ object ScanUIState {
|
||||||
|
|
||||||
val devices = modelMapOf<String, BTScanEntry>()
|
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
|
/// 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")
|
ScanState.info("Changing BT device to $newAddr")
|
||||||
val context = ambient(ContextAmbient)
|
|
||||||
selectedMacAddr = newAddr
|
selectedMacAddr = newAddr
|
||||||
RadioInterfaceService.setBondedDeviceAddress(context, newAddr)
|
RadioInterfaceService.setBondedDeviceAddress(context, newAddr)
|
||||||
}
|
}
|
||||||
|
|
@ -95,6 +64,35 @@ fun BTScanScreen() {
|
||||||
onActive {
|
onActive {
|
||||||
ScanUIState.selectedMacAddr = RadioInterfaceService.getBondedDeviceAddress(context)
|
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) {
|
if (bluetoothAdapter == null) {
|
||||||
ScanState.warn("No bluetooth adapter. Running under emulation?")
|
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 nothing was selected, by default select the first thing we see
|
||||||
if (ScanUIState.selectedMacAddr == null)
|
if (ScanUIState.selectedMacAddr == null)
|
||||||
ScanUIState.changeSelection(testnodes.first().macAddress)
|
ScanUIState.changeSelection(context, testnodes.first().macAddress)
|
||||||
} else {
|
} else {
|
||||||
val s = bluetoothAdapter.bluetoothLeScanner
|
val s = bluetoothAdapter.bluetoothLeScanner
|
||||||
// ScanState.scanner = scanner
|
// ScanState.scanner = scanner
|
||||||
|
|
@ -122,9 +120,9 @@ fun BTScanScreen() {
|
||||||
|
|
||||||
val settings =
|
val settings =
|
||||||
ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build()
|
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.scanner = s
|
||||||
ScanState.callback = ScanUIState.scanCallback
|
ScanState.callback = scanCallback
|
||||||
}
|
}
|
||||||
|
|
||||||
onDispose {
|
onDispose {
|
||||||
|
|
@ -160,9 +158,9 @@ fun BTScanScreen() {
|
||||||
selected = (it.isSelected),
|
selected = (it.isSelected),
|
||||||
onSelect = {
|
onSelect = {
|
||||||
// If the device is paired, let user select it, otherwise start the pairing flow
|
// If the device is paired, let user select it, otherwise start the pairing flow
|
||||||
if (it.bonded)
|
if (it.bonded) {
|
||||||
ScanUIState.changeSelection(it.macAddress)
|
ScanUIState.changeSelection(context, it.macAddress)
|
||||||
else {
|
} else {
|
||||||
ScanState.info("Starting bonding for $it")
|
ScanState.info("Starting bonding for $it")
|
||||||
|
|
||||||
// We ignore missing BT adapters, because it lets us run on the emulator
|
// We ignore missing BT adapters, because it lets us run on the emulator
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,10 @@ fun HomeContent() {
|
||||||
Column {
|
Column {
|
||||||
Row {
|
Row {
|
||||||
Container(LayoutSize(40.dp, 40.dp)) {
|
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")
|
Text(if (UIState.isConnected.value) "Connected" else "Not Connected")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue