mirror of
https://github.com/aljazceru/Android-nRF-Toolbox.git
synced 2025-12-23 17:34:28 +01:00
Change BluetoothDevice usage to DiscoveredBluetoothDevice where possible
This commit is contained in:
@@ -14,6 +14,7 @@ import no.nordicsemi.android.bps.data.BPSManager
|
||||
import no.nordicsemi.android.logger.ToolboxLogger
|
||||
import no.nordicsemi.android.logger.ToolboxLoggerFactory
|
||||
import no.nordicsemi.android.service.BleManagerResult
|
||||
import no.nordicsemi.ui.scanner.DiscoveredBluetoothDevice
|
||||
import javax.inject.Inject
|
||||
|
||||
@ViewModelScoped
|
||||
@@ -25,9 +26,9 @@ internal class BPSRepository @Inject constructor(
|
||||
|
||||
private var logger: ToolboxLogger? = null
|
||||
|
||||
fun downloadData(device: BluetoothDevice): Flow<BleManagerResult<BPSData>> = callbackFlow {
|
||||
fun downloadData(device: DiscoveredBluetoothDevice): Flow<BleManagerResult<BPSData>> = callbackFlow {
|
||||
val scope = this
|
||||
val createdLogger = toolboxLoggerFactory.create("BPS", device.address).also {
|
||||
val createdLogger = toolboxLoggerFactory.create("BPS", device.address()).also {
|
||||
logger = it
|
||||
}
|
||||
val manager = BPSManager(context, scope, createdLogger)
|
||||
@@ -36,7 +37,7 @@ internal class BPSRepository @Inject constructor(
|
||||
trySend(it)
|
||||
}.launchIn(scope)
|
||||
|
||||
manager.connect(device)
|
||||
manager.connect(device.device)
|
||||
.useAutoConnect(false)
|
||||
.retry(3, 100)
|
||||
.enqueue()
|
||||
|
||||
@@ -5,5 +5,8 @@ import no.nordicsemi.android.service.BleManagerResult
|
||||
|
||||
internal sealed class BPSViewState
|
||||
|
||||
internal data class WorkingState(val result: BleManagerResult<BPSData>) : BPSViewState()
|
||||
internal data class WorkingState(
|
||||
val result: BleManagerResult<BPSData>
|
||||
) : BPSViewState()
|
||||
|
||||
internal object NoDeviceState : BPSViewState()
|
||||
|
||||
@@ -51,7 +51,7 @@ internal class BPSViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
private fun connectDevice(device: DiscoveredBluetoothDevice) {
|
||||
repository.downloadData(device.device).onEach {
|
||||
repository.downloadData(device).onEach {
|
||||
_state.value = WorkingState(it)
|
||||
}.launchIn(viewModelScope)
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import no.nordicsemi.android.logger.ToolboxLoggerFactory
|
||||
import no.nordicsemi.android.service.BleManagerResult
|
||||
import no.nordicsemi.android.service.ConnectingResult
|
||||
import no.nordicsemi.android.service.ServiceManager
|
||||
import no.nordicsemi.ui.scanner.DiscoveredBluetoothDevice
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@@ -33,12 +34,12 @@ class CGMRepository @Inject constructor(
|
||||
val isRunning = data.map { it.isRunning() }
|
||||
val hasBeenDisconnected = data.map { it.hasBeenDisconnected() }
|
||||
|
||||
fun launch(device: BluetoothDevice) {
|
||||
fun launch(device: DiscoveredBluetoothDevice) {
|
||||
serviceManager.startService(CGMService::class.java, device)
|
||||
}
|
||||
|
||||
fun start(device: BluetoothDevice, scope: CoroutineScope) {
|
||||
val createdLogger = toolboxLoggerFactory.create("CGMS", device.address).also {
|
||||
fun start(device: DiscoveredBluetoothDevice, scope: CoroutineScope) {
|
||||
val createdLogger = toolboxLoggerFactory.create("CGMS", device.address()).also {
|
||||
logger = it
|
||||
}
|
||||
val manager = CGMManager(context, scope, createdLogger)
|
||||
@@ -53,9 +54,9 @@ class CGMRepository @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun CGMManager.start(device: BluetoothDevice) {
|
||||
private suspend fun CGMManager.start(device: DiscoveredBluetoothDevice) {
|
||||
try {
|
||||
connect(device)
|
||||
connect(device.device)
|
||||
.useAutoConnect(false)
|
||||
.retry(3, 100)
|
||||
.suspend()
|
||||
|
||||
@@ -8,6 +8,7 @@ import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import no.nordicsemi.android.service.DEVICE_DATA
|
||||
import no.nordicsemi.android.service.NotificationService
|
||||
import no.nordicsemi.ui.scanner.DiscoveredBluetoothDevice
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
@@ -19,7 +20,7 @@ internal class CGMService : NotificationService() {
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
super.onStartCommand(intent, flags, startId)
|
||||
|
||||
val device = intent!!.getParcelableExtra<BluetoothDevice>(DEVICE_DATA)!!
|
||||
val device = intent!!.getParcelableExtra<DiscoveredBluetoothDevice>(DEVICE_DATA)!!
|
||||
|
||||
repository.start(device, lifecycleScope)
|
||||
|
||||
|
||||
@@ -58,7 +58,7 @@ internal class CGMScreenViewModel @Inject constructor(
|
||||
private fun handleArgs(args: DestinationResult) {
|
||||
when (args) {
|
||||
is CancelDestinationResult -> navigationManager.navigateUp()
|
||||
is SuccessDestinationResult -> repository.launch(args.getDevice().device)
|
||||
is SuccessDestinationResult -> repository.launch(args.getDevice())
|
||||
}.exhaustive
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ import no.nordicsemi.android.logger.ToolboxLoggerFactory
|
||||
import no.nordicsemi.android.service.BleManagerResult
|
||||
import no.nordicsemi.android.service.ConnectingResult
|
||||
import no.nordicsemi.android.service.ServiceManager
|
||||
import no.nordicsemi.ui.scanner.DiscoveredBluetoothDevice
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@@ -34,12 +35,12 @@ class CSCRepository @Inject constructor(
|
||||
val isRunning = data.map { it.isRunning() }
|
||||
val hasBeenDisconnected = data.map { it.hasBeenDisconnected() }
|
||||
|
||||
fun launch(device: BluetoothDevice) {
|
||||
fun launch(device: DiscoveredBluetoothDevice) {
|
||||
serviceManager.startService(CSCService::class.java, device)
|
||||
}
|
||||
|
||||
fun start(device: BluetoothDevice, scope: CoroutineScope) {
|
||||
val createdLogger = toolboxLoggerFactory.create("CSC", device.address).also {
|
||||
fun start(device: DiscoveredBluetoothDevice, scope: CoroutineScope) {
|
||||
val createdLogger = toolboxLoggerFactory.create("CSC", device.address()).also {
|
||||
logger = it
|
||||
}
|
||||
val manager = CSCManager(context, scope, createdLogger)
|
||||
@@ -58,9 +59,9 @@ class CSCRepository @Inject constructor(
|
||||
manager?.setWheelSize(wheelSize)
|
||||
}
|
||||
|
||||
private suspend fun CSCManager.start(device: BluetoothDevice) {
|
||||
private suspend fun CSCManager.start(device: DiscoveredBluetoothDevice) {
|
||||
try {
|
||||
connect(device)
|
||||
connect(device.device)
|
||||
.useAutoConnect(false)
|
||||
.retry(3, 100)
|
||||
.suspend()
|
||||
|
||||
@@ -8,6 +8,7 @@ import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import no.nordicsemi.android.service.DEVICE_DATA
|
||||
import no.nordicsemi.android.service.NotificationService
|
||||
import no.nordicsemi.ui.scanner.DiscoveredBluetoothDevice
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
@@ -19,7 +20,7 @@ internal class CSCService : NotificationService() {
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
super.onStartCommand(intent, flags, startId)
|
||||
|
||||
val device = intent!!.getParcelableExtra<BluetoothDevice>(DEVICE_DATA)!!
|
||||
val device = intent!!.getParcelableExtra<DiscoveredBluetoothDevice>(DEVICE_DATA)!!
|
||||
|
||||
repository.start(device, lifecycleScope)
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ internal class CSCViewModel @Inject constructor(
|
||||
private fun handleArgs(args: DestinationResult) {
|
||||
when (args) {
|
||||
is CancelDestinationResult -> navigationManager.navigateUp()
|
||||
is SuccessDestinationResult -> repository.launch(args.getDevice().device)
|
||||
is SuccessDestinationResult -> repository.launch(args.getDevice())
|
||||
}.exhaustive
|
||||
}
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ internal class GLSViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
private fun connectDevice(device: DiscoveredBluetoothDevice) {
|
||||
repository.downloadData(device.device).onEach {
|
||||
repository.downloadData(device).onEach {
|
||||
_state.value = WorkingState(it)
|
||||
}.launchIn(viewModelScope)
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ import no.nordicsemi.android.logger.ToolboxLogger
|
||||
import no.nordicsemi.android.logger.ToolboxLoggerFactory
|
||||
import no.nordicsemi.android.service.BleManagerResult
|
||||
import no.nordicsemi.android.utils.exhaustive
|
||||
import no.nordicsemi.ui.scanner.DiscoveredBluetoothDevice
|
||||
import javax.inject.Inject
|
||||
|
||||
@ViewModelScoped
|
||||
@@ -30,14 +31,14 @@ internal class GLSRepository @Inject constructor(
|
||||
private var manager: GLSManager? = null
|
||||
private var logger: ToolboxLogger? = null
|
||||
|
||||
fun downloadData(device: BluetoothDevice): Flow<BleManagerResult<GLSData>> = callbackFlow {
|
||||
fun downloadData(device: DiscoveredBluetoothDevice): Flow<BleManagerResult<GLSData>> = callbackFlow {
|
||||
val scope = this
|
||||
val createdLogger = toolboxLoggerFactory.create("GLS", device.address).also {
|
||||
val createdLogger = toolboxLoggerFactory.create("GLS", device.address()).also {
|
||||
logger = it
|
||||
}
|
||||
val managerInstance = manager ?: GLSManager(context, scope, createdLogger).apply {
|
||||
try {
|
||||
connect(device)
|
||||
connect(device.device)
|
||||
.useAutoConnect(false)
|
||||
.retry(3, 100)
|
||||
.suspend()
|
||||
|
||||
@@ -14,6 +14,7 @@ import no.nordicsemi.android.logger.ToolboxLoggerFactory
|
||||
import no.nordicsemi.android.service.BleManagerResult
|
||||
import no.nordicsemi.android.service.ConnectingResult
|
||||
import no.nordicsemi.android.service.ServiceManager
|
||||
import no.nordicsemi.ui.scanner.DiscoveredBluetoothDevice
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@@ -33,12 +34,12 @@ class HRSRepository @Inject constructor(
|
||||
val isRunning = data.map { it.isRunning() }
|
||||
val hasBeenDisconnected = data.map { it.hasBeenDisconnected() }
|
||||
|
||||
fun launch(device: BluetoothDevice) {
|
||||
fun launch(device: DiscoveredBluetoothDevice) {
|
||||
serviceManager.startService(HRSService::class.java, device)
|
||||
}
|
||||
|
||||
fun start(device: BluetoothDevice, scope: CoroutineScope) {
|
||||
val createdLogger = toolboxLoggerFactory.create("HRS", device.address).also {
|
||||
fun start(device: DiscoveredBluetoothDevice, scope: CoroutineScope) {
|
||||
val createdLogger = toolboxLoggerFactory.create("HRS", device.address()).also {
|
||||
logger = it
|
||||
}
|
||||
val manager = HRSManager(context, scope, createdLogger)
|
||||
@@ -57,9 +58,9 @@ class HRSRepository @Inject constructor(
|
||||
logger?.openLogger()
|
||||
}
|
||||
|
||||
private suspend fun HRSManager.start(device: BluetoothDevice) {
|
||||
private suspend fun HRSManager.start(device: DiscoveredBluetoothDevice) {
|
||||
try {
|
||||
connect(device)
|
||||
connect(device.device)
|
||||
.useAutoConnect(false)
|
||||
.retry(3, 100)
|
||||
.suspend()
|
||||
|
||||
@@ -8,6 +8,7 @@ import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import no.nordicsemi.android.service.DEVICE_DATA
|
||||
import no.nordicsemi.android.service.NotificationService
|
||||
import no.nordicsemi.ui.scanner.DiscoveredBluetoothDevice
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
@@ -19,7 +20,7 @@ internal class HRSService : NotificationService() {
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
super.onStartCommand(intent, flags, startId)
|
||||
|
||||
val device = intent!!.getParcelableExtra<BluetoothDevice>(DEVICE_DATA)!!
|
||||
val device = intent!!.getParcelableExtra<DiscoveredBluetoothDevice>(DEVICE_DATA)!!
|
||||
|
||||
repository.start(device, lifecycleScope)
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ internal class HRSViewModel @Inject constructor(
|
||||
private fun handleArgs(args: DestinationResult) {
|
||||
when (args) {
|
||||
is CancelDestinationResult -> navigationManager.navigateUp()
|
||||
is SuccessDestinationResult -> repository.launch(args.getDevice().device)
|
||||
is SuccessDestinationResult -> repository.launch(args.getDevice())
|
||||
}.exhaustive
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ import no.nordicsemi.android.logger.ToolboxLoggerFactory
|
||||
import no.nordicsemi.android.service.BleManagerResult
|
||||
import no.nordicsemi.android.service.ConnectingResult
|
||||
import no.nordicsemi.android.service.ServiceManager
|
||||
import no.nordicsemi.ui.scanner.DiscoveredBluetoothDevice
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@@ -33,12 +34,12 @@ class HTSRepository @Inject constructor(
|
||||
val isRunning = data.map { it.isRunning() }
|
||||
val hasBeenDisconnected = data.map { it.hasBeenDisconnected() }
|
||||
|
||||
fun launch(device: BluetoothDevice) {
|
||||
fun launch(device: DiscoveredBluetoothDevice) {
|
||||
serviceManager.startService(HTSService::class.java, device)
|
||||
}
|
||||
|
||||
fun start(device: BluetoothDevice, scope: CoroutineScope) {
|
||||
val createdLogger = toolboxLoggerFactory.create("HTS", device.address).also {
|
||||
fun start(device: DiscoveredBluetoothDevice, scope: CoroutineScope) {
|
||||
val createdLogger = toolboxLoggerFactory.create("HTS", device.address()).also {
|
||||
logger = it
|
||||
}
|
||||
val manager = HTSManager(context, scope, createdLogger)
|
||||
@@ -57,9 +58,9 @@ class HTSRepository @Inject constructor(
|
||||
logger?.openLogger()
|
||||
}
|
||||
|
||||
private suspend fun HTSManager.start(device: BluetoothDevice) {
|
||||
private suspend fun HTSManager.start(device: DiscoveredBluetoothDevice) {
|
||||
try {
|
||||
connect(device)
|
||||
connect(device.device)
|
||||
.useAutoConnect(false)
|
||||
.retry(3, 100)
|
||||
.suspend()
|
||||
|
||||
@@ -8,6 +8,7 @@ import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import no.nordicsemi.android.service.DEVICE_DATA
|
||||
import no.nordicsemi.android.service.NotificationService
|
||||
import no.nordicsemi.ui.scanner.DiscoveredBluetoothDevice
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
@@ -19,7 +20,7 @@ internal class HTSService : NotificationService() {
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
super.onStartCommand(intent, flags, startId)
|
||||
|
||||
val device = intent!!.getParcelableExtra<BluetoothDevice>(DEVICE_DATA)!!
|
||||
val device = intent!!.getParcelableExtra<DiscoveredBluetoothDevice>(DEVICE_DATA)!!
|
||||
|
||||
repository.start(device, lifecycleScope)
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ internal class HTSViewModel @Inject constructor(
|
||||
private fun handleArgs(args: DestinationResult) {
|
||||
when (args) {
|
||||
is CancelDestinationResult -> navigationManager.navigateUp()
|
||||
is SuccessDestinationResult -> repository.launch(args.getDevice().device)
|
||||
is SuccessDestinationResult -> repository.launch(args.getDevice())
|
||||
}.exhaustive
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ import no.nordicsemi.android.prx.data.PRXData
|
||||
import no.nordicsemi.android.prx.data.PRXManager
|
||||
import no.nordicsemi.android.prx.data.ProximityServerManager
|
||||
import no.nordicsemi.android.service.*
|
||||
import no.nordicsemi.ui.scanner.DiscoveredBluetoothDevice
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@@ -34,13 +35,13 @@ class PRXRepository @Inject internal constructor(
|
||||
val isRunning = data.map { it.isRunning() }
|
||||
val hasBeenDisconnectedWithoutLinkLoss = data.map { it.hasBeenDisconnectedWithoutLinkLoss() }
|
||||
|
||||
fun launch(device: BluetoothDevice) {
|
||||
fun launch(device: DiscoveredBluetoothDevice) {
|
||||
serviceManager.startService(PRXService::class.java, device)
|
||||
proximityServerManager.open()
|
||||
}
|
||||
|
||||
fun start(device: BluetoothDevice, scope: CoroutineScope) {
|
||||
val createdLogger = toolboxLoggerFactory.create("PRX", device.address).also {
|
||||
fun start(device: DiscoveredBluetoothDevice, scope: CoroutineScope) {
|
||||
val createdLogger = toolboxLoggerFactory.create("PRX", device.address()).also {
|
||||
logger = it
|
||||
}
|
||||
val manager = PRXManager(context, scope, createdLogger)
|
||||
@@ -52,7 +53,7 @@ class PRXRepository @Inject internal constructor(
|
||||
handleLocalAlarm(it)
|
||||
}.launchIn(scope)
|
||||
|
||||
manager.connect(device)
|
||||
manager.connect(device.device)
|
||||
.useAutoConnect(true)
|
||||
.retry(3, 100)
|
||||
.enqueue()
|
||||
|
||||
@@ -8,6 +8,7 @@ import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import no.nordicsemi.android.service.DEVICE_DATA
|
||||
import no.nordicsemi.android.service.NotificationService
|
||||
import no.nordicsemi.ui.scanner.DiscoveredBluetoothDevice
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
@@ -19,7 +20,7 @@ internal class PRXService : NotificationService() {
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
super.onStartCommand(intent, flags, startId)
|
||||
|
||||
val device = intent!!.getParcelableExtra<BluetoothDevice>(DEVICE_DATA)!!
|
||||
val device = intent!!.getParcelableExtra<DiscoveredBluetoothDevice>(DEVICE_DATA)!!
|
||||
|
||||
repository.start(device, lifecycleScope)
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ internal class PRXViewModel @Inject constructor(
|
||||
private fun handleArgs(args: DestinationResult) {
|
||||
when (args) {
|
||||
is CancelDestinationResult -> navigationManager.navigateUp()
|
||||
is SuccessDestinationResult -> repository.launch(args.getDevice().device)
|
||||
is SuccessDestinationResult -> repository.launch(args.getDevice())
|
||||
}.exhaustive
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ import no.nordicsemi.android.rscs.data.RSCSManager
|
||||
import no.nordicsemi.android.service.BleManagerResult
|
||||
import no.nordicsemi.android.service.ConnectingResult
|
||||
import no.nordicsemi.android.service.ServiceManager
|
||||
import no.nordicsemi.ui.scanner.DiscoveredBluetoothDevice
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@@ -33,12 +34,12 @@ class RSCSRepository @Inject constructor(
|
||||
val isRunning = data.map { it.isRunning() }
|
||||
val hasBeenDisconnected = data.map { it.hasBeenDisconnected() }
|
||||
|
||||
fun launch(device: BluetoothDevice) {
|
||||
fun launch(device: DiscoveredBluetoothDevice) {
|
||||
serviceManager.startService(RSCSService::class.java, device)
|
||||
}
|
||||
|
||||
fun start(device: BluetoothDevice, scope: CoroutineScope) {
|
||||
val createdLogger = toolboxLoggerFactory.create("RSCS", device.address).also {
|
||||
fun start(device: DiscoveredBluetoothDevice, scope: CoroutineScope) {
|
||||
val createdLogger = toolboxLoggerFactory.create("RSCS", device.address()).also {
|
||||
logger = it
|
||||
}
|
||||
val manager = RSCSManager(context, scope, createdLogger)
|
||||
@@ -57,9 +58,9 @@ class RSCSRepository @Inject constructor(
|
||||
logger?.openLogger()
|
||||
}
|
||||
|
||||
private suspend fun RSCSManager.start(device: BluetoothDevice) {
|
||||
private suspend fun RSCSManager.start(device: DiscoveredBluetoothDevice) {
|
||||
try {
|
||||
connect(device)
|
||||
connect(device.device)
|
||||
.useAutoConnect(false)
|
||||
.retry(3, 100)
|
||||
.suspend()
|
||||
|
||||
@@ -8,6 +8,7 @@ import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import no.nordicsemi.android.service.DEVICE_DATA
|
||||
import no.nordicsemi.android.service.NotificationService
|
||||
import no.nordicsemi.ui.scanner.DiscoveredBluetoothDevice
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
@@ -19,7 +20,7 @@ internal class RSCSService : NotificationService() {
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
super.onStartCommand(intent, flags, startId)
|
||||
|
||||
val device = intent!!.getParcelableExtra<BluetoothDevice>(DEVICE_DATA)!!
|
||||
val device = intent!!.getParcelableExtra<DiscoveredBluetoothDevice>(DEVICE_DATA)!!
|
||||
|
||||
repository.start(device, lifecycleScope)
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ internal class RSCSViewModel @Inject constructor(
|
||||
private fun handleArgs(args: DestinationResult) {
|
||||
when (args) {
|
||||
is CancelDestinationResult -> navigationManager.navigateUp()
|
||||
is SuccessDestinationResult -> repository.launch(args.getDevice().device)
|
||||
is SuccessDestinationResult -> repository.launch(args.getDevice())
|
||||
}.exhaustive
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ import no.nordicsemi.android.service.IdleResult
|
||||
import no.nordicsemi.android.service.ServiceManager
|
||||
import no.nordicsemi.android.uart.data.*
|
||||
import no.nordicsemi.android.utils.EMPTY
|
||||
import no.nordicsemi.ui.scanner.DiscoveredBluetoothDevice
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@@ -31,20 +32,20 @@ class UARTRepository @Inject internal constructor(
|
||||
private val _data = MutableStateFlow<BleManagerResult<UARTData>>(IdleResult())
|
||||
internal val data = _data.asStateFlow()
|
||||
|
||||
var device: BluetoothDevice? = null
|
||||
var device: DiscoveredBluetoothDevice? = null
|
||||
|
||||
val isRunning = data.map { it.isRunning() }
|
||||
val hasBeenDisconnected = data.map { it.hasBeenDisconnected() }
|
||||
|
||||
val lastConfigurationName = configurationDataSource.lastConfigurationName
|
||||
|
||||
fun launch(device: BluetoothDevice) {
|
||||
fun launch(device: DiscoveredBluetoothDevice) {
|
||||
serviceManager.startService(UARTService::class.java, device)
|
||||
}
|
||||
|
||||
fun start(device: BluetoothDevice, scope: CoroutineScope) {
|
||||
fun start(device: DiscoveredBluetoothDevice, scope: CoroutineScope) {
|
||||
this.device = device
|
||||
val createdLogger = toolboxLoggerFactory.create("UART", device.address).also {
|
||||
val createdLogger = toolboxLoggerFactory.create("UART", device.address()).also {
|
||||
logger = it
|
||||
}
|
||||
val manager = UARTManager(context, scope, createdLogger)
|
||||
@@ -80,9 +81,9 @@ class UARTRepository @Inject internal constructor(
|
||||
configurationDataSource.saveConfigurationName(name)
|
||||
}
|
||||
|
||||
private suspend fun UARTManager.start(device: BluetoothDevice) {
|
||||
private suspend fun UARTManager.start(device: DiscoveredBluetoothDevice) {
|
||||
try {
|
||||
connect(device)
|
||||
connect(device.device)
|
||||
.useAutoConnect(false)
|
||||
.retry(3, 100)
|
||||
.suspend()
|
||||
|
||||
@@ -8,6 +8,7 @@ import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import no.nordicsemi.android.service.DEVICE_DATA
|
||||
import no.nordicsemi.android.service.NotificationService
|
||||
import no.nordicsemi.ui.scanner.DiscoveredBluetoothDevice
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
@@ -19,7 +20,7 @@ internal class UARTService : NotificationService() {
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
super.onStartCommand(intent, flags, startId)
|
||||
|
||||
val device = intent!!.getParcelableExtra<BluetoothDevice>(DEVICE_DATA)!!
|
||||
val device = intent!!.getParcelableExtra<DiscoveredBluetoothDevice>(DEVICE_DATA)!!
|
||||
|
||||
repository.start(device, lifecycleScope)
|
||||
|
||||
|
||||
@@ -24,7 +24,6 @@ import no.nordicsemi.ui.scanner.ui.DeviceDisconnectedView
|
||||
import no.nordicsemi.ui.scanner.ui.NoDeviceView
|
||||
import no.nordicsemi.ui.scanner.ui.Reason
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
@Composable
|
||||
fun UARTScreen() {
|
||||
val viewModel: UARTViewModel = hiltViewModel()
|
||||
@@ -33,25 +32,7 @@ fun UARTScreen() {
|
||||
Column {
|
||||
val navigateUp = { viewModel.onEvent(NavigateUp) }
|
||||
|
||||
Column(modifier = Modifier) {
|
||||
when (state.uartManagerState) {
|
||||
NoDeviceState -> BackIconAppBar(stringResource(id = R.string.uart_title), navigateUp)
|
||||
is WorkingState -> when (state.uartManagerState.result) {
|
||||
is IdleResult,
|
||||
is DisconnectedResult,
|
||||
is LinkLossResult,
|
||||
is MissingServiceResult,
|
||||
is UnknownErrorResult -> BackIconAppBar(stringResource(id = R.string.uart_title), navigateUp)
|
||||
is ConnectingResult,
|
||||
is SuccessResult -> {
|
||||
val text = state.uartManagerState.device.name ?: state.uartManagerState.device.address ?: stringResource(id = R.string.uart_title)
|
||||
LoggerIconAppBar(text, navigateUp, { viewModel.onEvent(DisconnectEvent) }) {
|
||||
viewModel.onEvent(OpenLogger)
|
||||
}
|
||||
}
|
||||
}
|
||||
}.exhaustive
|
||||
}
|
||||
AppBar(state = state, navigateUp = navigateUp) { viewModel.onEvent(it) }
|
||||
|
||||
Column(modifier = Modifier) {
|
||||
when (state.uartManagerState) {
|
||||
@@ -70,6 +51,30 @@ fun UARTScreen() {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
@Composable
|
||||
private fun AppBar(state: UARTViewState, navigateUp: () -> Unit, onEvent: (UARTViewEvent) -> Unit) {
|
||||
Column(modifier = Modifier) {
|
||||
when (state.uartManagerState) {
|
||||
NoDeviceState -> BackIconAppBar(stringResource(id = R.string.uart_title), navigateUp)
|
||||
is WorkingState -> when (state.uartManagerState.result) {
|
||||
is IdleResult,
|
||||
is DisconnectedResult,
|
||||
is LinkLossResult,
|
||||
is MissingServiceResult,
|
||||
is UnknownErrorResult -> BackIconAppBar(stringResource(id = R.string.uart_title), navigateUp)
|
||||
is ConnectingResult,
|
||||
is SuccessResult -> {
|
||||
val text = state.uartManagerState.device.nameOrAddress()
|
||||
LoggerIconAppBar(text, navigateUp, { onEvent(DisconnectEvent) }) {
|
||||
onEvent(OpenLogger)
|
||||
}
|
||||
}
|
||||
}
|
||||
}.exhaustive
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun SuccessScreen(data: UARTData, state: UARTViewState, viewModel: UARTViewModel) {
|
||||
val viewEntity = PagerViewEntity(
|
||||
|
||||
@@ -5,6 +5,7 @@ import no.nordicsemi.android.service.BleManagerResult
|
||||
import no.nordicsemi.android.uart.data.UARTConfiguration
|
||||
import no.nordicsemi.android.uart.data.UARTData
|
||||
import no.nordicsemi.android.uart.data.UARTMacro
|
||||
import no.nordicsemi.ui.scanner.DiscoveredBluetoothDevice
|
||||
|
||||
internal data class UARTViewState(
|
||||
val editedPosition: Int? = null,
|
||||
@@ -28,7 +29,7 @@ internal data class UARTViewState(
|
||||
internal sealed class HTSManagerState
|
||||
|
||||
internal data class WorkingState(
|
||||
val device: BluetoothDevice,
|
||||
val device: DiscoveredBluetoothDevice,
|
||||
val result: BleManagerResult<UARTData>
|
||||
) : HTSManagerState()
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ internal class UARTViewModel @Inject constructor(
|
||||
private fun handleArgs(args: DestinationResult) {
|
||||
when (args) {
|
||||
is CancelDestinationResult -> navigationManager.navigateUp()
|
||||
is SuccessDestinationResult -> repository.launch(args.getDevice().device)
|
||||
is SuccessDestinationResult -> repository.launch(args.getDevice())
|
||||
}.exhaustive
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user