list devices from adapter.bondedDevices (#536)

This commit is contained in:
Andre K
2022-12-10 00:16:14 -03:00
committed by GitHub
parent 6ede4ed3bd
commit d538c2afff
2 changed files with 15 additions and 12 deletions

View File

@@ -251,8 +251,8 @@ class BTScanModel @Inject constructor(
// Include a placeholder for "None"
addDevice(DeviceListEntry(context.getString(R.string.none), "n", true))
// Include CompanionDeviceManager valid associations
addDeviceAssociations()
// Include paired Bluetooth devices
addBluetoothDevices()
// Include Network Service Discovery
nsdRepository.resolvedList?.forEach { service ->
@@ -324,17 +324,13 @@ class BTScanModel @Inject constructor(
}
}
@SuppressLint("NewApi")
fun addDeviceAssociations() {
if (hasCompanionDeviceApi) deviceManager?.associations?.forEach { bleAddress ->
val bleDevice = getDeviceListEntry("x$bleAddress", true)
// Disassociate after pairing is removed (if BLE is disabled, assume bonded)
if (!bleDevice.bonded) {
debug("Forgetting old BLE association ${bleAddress.anonymize}")
deviceManager?.disassociate(bleAddress)
@SuppressLint("MissingPermission")
private fun addBluetoothDevices() {
bluetoothRepository.getBondedDevices()
?.filter { it.name.matches(Regex("^\\S+\$")) }
?.forEach {
addDevice(DeviceListEntry(it.name, "x${it.address}", true))
}
addDevice(bleDevice)
}
}
private val _spinner = MutableLiveData(false)

View File

@@ -65,6 +65,13 @@ class BluetoothRepository @Inject constructor(
return bluetoothAdapterLazy.get()?.bluetoothLeScanner
}
@SuppressLint("MissingPermission")
fun getBondedDevices(): Set<BluetoothDevice>? {
return bluetoothAdapterLazy.get()
?.takeIf { application.hasBluetoothPermission() }
?.bondedDevices
}
@SuppressLint("MissingPermission")
internal suspend fun updateBluetoothState() {
val newState: BluetoothState = bluetoothAdapterLazy.get()?.takeIf {