feat: collect bondedDevices flow in BTScanModel

This commit is contained in:
andrekir 2023-03-27 15:27:26 -03:00
parent 84c85a8c3e
commit 057f94e423
4 changed files with 13 additions and 19 deletions

View file

@ -13,7 +13,6 @@ import com.geeksville.mesh.android.hasBluetoothPermission
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject
import javax.inject.Singleton
@ -95,19 +94,13 @@ class BluetoothRepository @Inject constructor(
* Creates a cold Flow used to obtain the set of bonded devices.
*/
@SuppressLint("MissingPermission") // Already checked prior to calling
private suspend fun createBondedDevicesFlow(adapter: BluetoothAdapter): Flow<Set<BluetoothDevice>>? {
return if (adapter.isEnabled) {
flow<Set<BluetoothDevice>> {
withContext(dispatchers.default) {
while (true) {
emit(adapter.bondedDevices)
delay(REFRESH_DELAY_MS)
}
}
}.flowOn(dispatchers.default)
} else {
null
}
private suspend fun createBondedDevicesFlow(adapter: BluetoothAdapter): Flow<Set<BluetoothDevice>> {
return flow<Set<BluetoothDevice>> {
while (true) {
emit(adapter.bondedDevices ?: emptySet())
delay(REFRESH_DELAY_MS)
}
}.flowOn(dispatchers.default).distinctUntilChanged()
}
companion object {

View file

@ -2,6 +2,7 @@ package com.geeksville.mesh.repository.bluetooth
import android.bluetooth.BluetoothDevice
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
/**
* A snapshot in time of the state of the bluetooth subsystem.
@ -12,5 +13,5 @@ data class BluetoothState(
/** If we have adequate permissions and bluetooth is enabled */
val enabled: Boolean = false,
/** If enabled, a cold flow of the currently bonded devices */
val bondedDevices: Flow<Set<BluetoothDevice>>? = null
val bondedDevices: Flow<Set<BluetoothDevice>> = flowOf(emptySet())
)