From 0950e12bd094f5e8cefa6c914f5d3e797b696b1a Mon Sep 17 00:00:00 2001 From: andrekir Date: Thu, 28 Apr 2022 23:09:06 -0300 Subject: [PATCH] add BLE associations to devices list --- .../mesh/android/ContextServices.kt | 7 ++++++ .../geeksville/mesh/ui/SettingsFragment.kt | 25 ++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/geeksville/mesh/android/ContextServices.kt b/app/src/main/java/com/geeksville/mesh/android/ContextServices.kt index ab7bdf2b4..f3bd9ee51 100644 --- a/app/src/main/java/com/geeksville/mesh/android/ContextServices.kt +++ b/app/src/main/java/com/geeksville/mesh/android/ContextServices.kt @@ -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?) diff --git a/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt b/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt index 25684cc6b..8ece3085f 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt @@ -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>(mutableMapOf()) { /**