fix: update CompanionDeviceManager associate() to Activity context

This commit is contained in:
andrekir 2023-04-06 19:10:45 -03:00
parent 4e9ec5f096
commit 2d0d7b3986
3 changed files with 26 additions and 25 deletions

View file

@ -1,6 +1,7 @@
package com.geeksville.mesh.model
import android.annotation.SuppressLint
import android.app.Activity
import android.app.Application
import android.bluetooth.BluetoothDevice
import android.bluetooth.le.*
@ -91,10 +92,6 @@ class BTScanModel @Inject constructor(
debug("BTScanModel cleared")
}
val hasCompanionDeviceApi get() = application.hasCompanionDeviceApi()
val hasBluetoothPermission get() = application.hasBluetoothPermission()
private val usbManager get() = context.usbManager
var selectedAddress: String? = null
val errorText = object : MutableLiveData<String?>(null) {}
@ -211,7 +208,7 @@ class BTScanModel @Inject constructor(
val serialDevices by lazy { usbRepository.serialDevicesWithDrivers.value }
serialDevices.forEach { (_, d) ->
addDevice(USBDeviceListEntry(usbManager, d))
addDevice(USBDeviceListEntry(context.usbManager, d))
}
} else {
debug("scan already running")
@ -221,7 +218,7 @@ class BTScanModel @Inject constructor(
}
private var networkDiscovery: Job? = null
fun startScan() {
fun startScan(activity: Activity?) {
_spinner.value = true
// Start Network Service Discovery (find TCP devices)
@ -229,10 +226,7 @@ class BTScanModel @Inject constructor(
.onEach { addDevice(TCPDeviceListEntry(it)) }
.launchIn(viewModelScope)
if (hasBluetoothPermission) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.S && hasCompanionDeviceApi)
startCompanionScan() else startClassicScan()
}
if (activity != null) startCompanionScan(activity) else startClassicScan()
}
@SuppressLint("MissingPermission")
@ -320,9 +314,9 @@ class BTScanModel @Inject constructor(
}
@SuppressLint("NewApi")
private fun startCompanionScan() {
private fun startCompanionScan(activity: Activity) {
debug("starting companion scan")
context.companionDeviceManager?.associate(
activity.companionDeviceManager?.associate(
associationRequest(),
@SuppressLint("NewApi")
object : CompanionDeviceManager.Callback() {