add BLE associations to devices list

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

View file

@ -1,8 +1,10 @@
package com.geeksville.mesh.android
import android.Manifest
import android.annotation.SuppressLint
import android.app.NotificationManager
import android.bluetooth.BluetoothManager
import android.companion.CompanionDeviceManager
import android.content.Context
import android.content.pm.PackageManager
import android.hardware.usb.UsbManager
@ -14,6 +16,11 @@ import androidx.core.content.ContextCompat
*/
val Context.bluetoothManager: BluetoothManager? get() = getSystemService(Context.BLUETOOTH_SERVICE) as? BluetoothManager?
val Context.deviceManager: CompanionDeviceManager?
@SuppressLint("InlinedApi")
get() = if (hasCompanionDeviceApi()) getSystemService(Context.COMPANION_DEVICE_SERVICE) as? CompanionDeviceManager?
else null
val Context.usbManager: UsbManager get() = requireNotNull(getSystemService(Context.USB_SERVICE) as? UsbManager?) { "USB_SERVICE is not available"}
val Context.notificationManager: NotificationManager get() = requireNotNull(getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager?)

View file

@ -126,7 +126,7 @@ class BTScanModel(app: Application) : AndroidViewModel(app), Logging {
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'
@ -145,7 +145,9 @@ class BTScanModel(app: Application) : AndroidViewModel(app), Logging {
}
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
@ -222,6 +224,8 @@ class BTScanModel(app: Application) : AndroidViewModel(app), Logging {
}
}
private fun addDevice(entry: DeviceListEntry) {
val oldDevs = devices.value!!
oldDevs[entry.address] = entry // Add/replace entry
@ -290,6 +294,9 @@ class BTScanModel(app: Application) : AndroidViewModel(app), Logging {
// Include a placeholder for "None"
addDevice(DeviceListEntry(context.getString(R.string.none), "n", true))
if (hasCompanionDeviceApi && hasConnectPermission)
addAssociations()
usbDrivers.forEach { d ->
addDevice(
USBDeviceListEntry(usbManager, d)
@ -328,6 +335,22 @@ class BTScanModel(app: Application) : AndroidViewModel(app), Logging {
}
}
@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()) {
/**