kopia lustrzana https://github.com/meshtastic/Meshtastic-Android
feat: collect bondedDevices flow in BTScanModel
rodzic
84c85a8c3e
commit
057f94e423
|
@ -93,6 +93,10 @@ class BTScanModel @Inject constructor(
|
||||||
private val context: Context get() = application.applicationContext
|
private val context: Context get() = application.applicationContext
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
bluetoothRepository.state.value.bondedDevices.onEach {
|
||||||
|
setupScan() // TODO clean up device list updates
|
||||||
|
}.launchIn(viewModelScope)
|
||||||
|
|
||||||
debug("BTScanModel created")
|
debug("BTScanModel created")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,6 @@ import com.geeksville.mesh.android.hasBluetoothPermission
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.flow.*
|
import kotlinx.coroutines.flow.*
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
@ -95,19 +94,13 @@ class BluetoothRepository @Inject constructor(
|
||||||
* Creates a cold Flow used to obtain the set of bonded devices.
|
* Creates a cold Flow used to obtain the set of bonded devices.
|
||||||
*/
|
*/
|
||||||
@SuppressLint("MissingPermission") // Already checked prior to calling
|
@SuppressLint("MissingPermission") // Already checked prior to calling
|
||||||
private suspend fun createBondedDevicesFlow(adapter: BluetoothAdapter): Flow<Set<BluetoothDevice>>? {
|
private suspend fun createBondedDevicesFlow(adapter: BluetoothAdapter): Flow<Set<BluetoothDevice>> {
|
||||||
return if (adapter.isEnabled) {
|
return flow<Set<BluetoothDevice>> {
|
||||||
flow<Set<BluetoothDevice>> {
|
while (true) {
|
||||||
withContext(dispatchers.default) {
|
emit(adapter.bondedDevices ?: emptySet())
|
||||||
while (true) {
|
delay(REFRESH_DELAY_MS)
|
||||||
emit(adapter.bondedDevices)
|
}
|
||||||
delay(REFRESH_DELAY_MS)
|
}.flowOn(dispatchers.default).distinctUntilChanged()
|
||||||
}
|
|
||||||
}
|
|
||||||
}.flowOn(dispatchers.default)
|
|
||||||
} else {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
|
@ -2,6 +2,7 @@ package com.geeksville.mesh.repository.bluetooth
|
||||||
|
|
||||||
import android.bluetooth.BluetoothDevice
|
import android.bluetooth.BluetoothDevice
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.flowOf
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A snapshot in time of the state of the bluetooth subsystem.
|
* 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 */
|
/** If we have adequate permissions and bluetooth is enabled */
|
||||||
val enabled: Boolean = false,
|
val enabled: Boolean = false,
|
||||||
/** If enabled, a cold flow of the currently bonded devices */
|
/** If enabled, a cold flow of the currently bonded devices */
|
||||||
val bondedDevices: Flow<Set<BluetoothDevice>>? = null
|
val bondedDevices: Flow<Set<BluetoothDevice>> = flowOf(emptySet())
|
||||||
)
|
)
|
||||||
|
|
|
@ -290,10 +290,6 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
|
||||||
regionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
|
regionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
|
||||||
spinner.adapter = regionAdapter
|
spinner.adapter = regionAdapter
|
||||||
|
|
||||||
bluetoothViewModel.enabled.observe(viewLifecycleOwner) { enabled ->
|
|
||||||
if (enabled) scanModel.setupScan()
|
|
||||||
}
|
|
||||||
|
|
||||||
model.ownerName.observe(viewLifecycleOwner) { name ->
|
model.ownerName.observe(viewLifecycleOwner) { name ->
|
||||||
binding.usernameEditText.isEnabled = !name.isNullOrEmpty()
|
binding.usernameEditText.isEnabled = !name.isNullOrEmpty()
|
||||||
binding.usernameEditText.setText(name)
|
binding.usernameEditText.setText(name)
|
||||||
|
|
Ładowanie…
Reference in New Issue