feat: update devices list from repositories

This commit is contained in:
andrekir 2023-04-06 21:04:03 -03:00
parent 2d0d7b3986
commit 804d4f0e27
5 changed files with 31 additions and 35 deletions

View file

@ -66,13 +66,6 @@ class BluetoothRepository @Inject constructor(
?.bluetoothLeScanner
}
@SuppressLint("MissingPermission")
fun getBondedDevices(): Set<BluetoothDevice>? {
return bluetoothAdapterLazy.get()
?.takeIf { application.hasBluetoothPermission() }
?.bondedDevices
}
@SuppressLint("MissingPermission")
internal suspend fun updateBluetoothState() {
val newState: BluetoothState = bluetoothAdapterLazy.get()?.takeIf {
@ -96,16 +89,18 @@ 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 flow<Set<BluetoothDevice>> {
private suspend fun createBondedDevicesFlow(adapter: BluetoothAdapter): Flow<List<BluetoothDevice>> {
return flow<List<BluetoothDevice>> {
val devices = adapter.bondedDevices ?: emptySet()
while (true) {
emit(adapter.bondedDevices ?: emptySet())
emit(devices.filter { it.name != null && it.name.matches(Regex(BLE_NAME_PATTERN)) })
delay(REFRESH_DELAY_MS)
}
}.flowOn(dispatchers.default).distinctUntilChanged()
}
companion object {
const val BLE_NAME_PATTERN = "^.*_([0-9a-fA-F]{4})$"
const val REFRESH_DELAY_MS = 1000L
}
}

View file

@ -13,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>> = flowOf(emptySet())
val bondedDevices: Flow<List<BluetoothDevice>> = flowOf(emptyList())
)

View file

@ -12,7 +12,6 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Semaphore
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.collections.ArrayList
@Singleton
class NsdRepository @Inject constructor(
@ -21,7 +20,7 @@ class NsdRepository @Inject constructor(
) : Logging {
private val resolveQueue = Semaphore(1)
private var hostsList: ArrayList<NsdServiceInfo>? = ArrayList()
private var hostsList: ArrayList<NsdServiceInfo>? = null
val resolvedList: List<NsdServiceInfo>? get() = hostsList
@ -91,7 +90,7 @@ class NsdRepository @Inject constructor(
companion object {
//To find all the available networks SERVICE_TYPE = "_services._dns-sd._udp"
const val SERVICE_TYPE = "_http._tcp."
const val SERVICE_TYPE = "_https._tcp."
const val serviceName = "Meshtastic"
}
}