kopia lustrzana https://github.com/meshtastic/Meshtastic-Android
add BLE associations to devices list
rodzic
aaa5c1cf04
commit
0950e12bd0
|
@ -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?)
|
||||
|
|
|
@ -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()) {
|
||||
|
||||
/**
|
||||
|
|
Ładowanie…
Reference in New Issue