add BLE associations to devices list

This commit is contained in:
andrekir 2022-04-28 23:09:06 -03:00
parent aaa5c1cf04
commit 0950e12bd0
2 changed files with 31 additions and 1 deletions

View file

@ -134,7 +134,7 @@ class BTScanModel @Inject constructor(
null
override fun toString(): String {
return "DeviceListEntry(name=${name.anonymize}, addr=${address.anonymize})"
return "DeviceListEntry(name=${name.anonymize}, addr=${address.anonymize}, bonded=$bonded)"
}
val isBluetooth: Boolean get() = address[0] == 'x'
@ -153,7 +153,9 @@ class BTScanModel @Inject constructor(
}
val bluetoothAdapter = context.bluetoothManager?.adapter
private val deviceManager get() = context.deviceManager
val hasCompanionDeviceApi get() = context.hasCompanionDeviceApi()
private val hasConnectPermission get() = context.hasConnectPermission()
private val usbManager get() = context.usbManager
var selectedAddress: String? = null
@ -230,6 +232,8 @@ class BTScanModel @Inject constructor(
}
}
private fun addDevice(entry: DeviceListEntry) {
val oldDevs = devices.value!!
oldDevs[entry.address] = entry // Add/replace entry
@ -298,6 +302,9 @@ class BTScanModel @Inject constructor(
// Include a placeholder for "None"
addDevice(DeviceListEntry(context.getString(R.string.none), "n", true))
if (hasCompanionDeviceApi && hasConnectPermission)
addAssociations()
serialDevices.forEach { (_, d) ->
addDevice(USBDeviceListEntry(usbManager, d))
}
@ -334,6 +341,22 @@ class BTScanModel @Inject constructor(
}
}
@SuppressLint("MissingPermission", "NewApi")
private fun addAssociations() {
deviceManager?.associations?.forEach { bleAddress ->
bluetoothAdapter?.getRemoteDevice(bleAddress)?.let { device ->
if (device.name.startsWith("Mesh")) {
val entry = DeviceListEntry(
device.name,
"x${device.address}", // full address with the bluetooth prefix added
device.bondState == BOND_BONDED
)
addDevice(entry)
}
}
}
}
val devices = object : MutableLiveData<MutableMap<String, DeviceListEntry>>(mutableMapOf()) {
/**