mirror of
https://github.com/aljazceru/Android-nRF-Toolbox.git
synced 2025-12-19 15:34:26 +01:00
Make logger button working after disconnect
This commit is contained in:
@@ -41,13 +41,14 @@ import no.nordicsemi.android.cgms.data.CGMRecordWithSequenceNumber
|
|||||||
import no.nordicsemi.android.cgms.data.CGMServiceCommand
|
import no.nordicsemi.android.cgms.data.CGMServiceCommand
|
||||||
import no.nordicsemi.android.cgms.data.CGMServiceData
|
import no.nordicsemi.android.cgms.data.CGMServiceData
|
||||||
import no.nordicsemi.android.common.core.simpleSharedFlow
|
import no.nordicsemi.android.common.core.simpleSharedFlow
|
||||||
|
import no.nordicsemi.android.common.logger.NordicBlekLogger
|
||||||
import no.nordicsemi.android.kotlin.ble.core.ServerDevice
|
import no.nordicsemi.android.kotlin.ble.core.ServerDevice
|
||||||
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionState
|
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionState
|
||||||
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionStateWithStatus
|
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionStateWithStatus
|
||||||
import no.nordicsemi.android.kotlin.ble.profile.gls.data.RequestStatus
|
import no.nordicsemi.android.kotlin.ble.profile.gls.data.RequestStatus
|
||||||
import no.nordicsemi.android.service.DisconnectAndStopEvent
|
import no.nordicsemi.android.service.DisconnectAndStopEvent
|
||||||
import no.nordicsemi.android.service.OpenLoggerEvent
|
|
||||||
import no.nordicsemi.android.service.ServiceManager
|
import no.nordicsemi.android.service.ServiceManager
|
||||||
|
import no.nordicsemi.android.ui.view.StringConst
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
@@ -56,7 +57,10 @@ class CGMRepository @Inject constructor(
|
|||||||
@ApplicationContext
|
@ApplicationContext
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
private val serviceManager: ServiceManager,
|
private val serviceManager: ServiceManager,
|
||||||
|
private val stringConst: StringConst
|
||||||
) {
|
) {
|
||||||
|
private var logger: NordicBlekLogger? = null
|
||||||
|
|
||||||
private val _data = MutableStateFlow(CGMServiceData())
|
private val _data = MutableStateFlow(CGMServiceData())
|
||||||
internal val data = _data.asStateFlow()
|
internal val data = _data.asStateFlow()
|
||||||
|
|
||||||
@@ -66,9 +70,6 @@ class CGMRepository @Inject constructor(
|
|||||||
private val _command = simpleSharedFlow<CGMServiceCommand>()
|
private val _command = simpleSharedFlow<CGMServiceCommand>()
|
||||||
internal val command = _command.asSharedFlow()
|
internal val command = _command.asSharedFlow()
|
||||||
|
|
||||||
private val _loggerEvent = simpleSharedFlow<OpenLoggerEvent>()
|
|
||||||
internal val loggerEvent = _loggerEvent.asSharedFlow()
|
|
||||||
|
|
||||||
val isRunning = data.map { it.connectionState?.state == GattConnectionState.STATE_CONNECTED }
|
val isRunning = data.map { it.connectionState?.state == GattConnectionState.STATE_CONNECTED }
|
||||||
val hasRecords = data.value.records.isNotEmpty()
|
val hasRecords = data.value.records.isNotEmpty()
|
||||||
val highestSequenceNumber = data.value.records.maxOfOrNull { it.sequenceNumber } ?: -1
|
val highestSequenceNumber = data.value.records.maxOfOrNull { it.sequenceNumber } ?: -1
|
||||||
@@ -91,6 +92,7 @@ class CGMRepository @Inject constructor(
|
|||||||
private fun shouldClean() = !isOnScreen && !isServiceRunning
|
private fun shouldClean() = !isOnScreen && !isServiceRunning
|
||||||
|
|
||||||
fun launch(device: ServerDevice) {
|
fun launch(device: ServerDevice) {
|
||||||
|
logger = NordicBlekLogger(context, stringConst.APP_NAME, "CGM", device.address)
|
||||||
_data.value = _data.value.copy(deviceName = device.name)
|
_data.value = _data.value.copy(deviceName = device.name)
|
||||||
serviceManager.startService(CGMService::class.java, device)
|
serviceManager.startService(CGMService::class.java, device)
|
||||||
}
|
}
|
||||||
@@ -121,7 +123,11 @@ class CGMRepository @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun openLogger() {
|
fun openLogger() {
|
||||||
_loggerEvent.tryEmit(OpenLoggerEvent())
|
logger?.launch()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun log(priority: Int, message: String) {
|
||||||
|
logger?.log(priority, message)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun clear() {
|
fun clear() {
|
||||||
@@ -133,6 +139,7 @@ class CGMRepository @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun clean() {
|
private fun clean() {
|
||||||
|
logger = null
|
||||||
_data.value = CGMServiceData()
|
_data.value = CGMServiceData()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -93,9 +93,6 @@ internal class CGMService : NotificationService() {
|
|||||||
@Inject
|
@Inject
|
||||||
lateinit var repository: CGMRepository
|
lateinit var repository: CGMRepository
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var stringConst: StringConst
|
|
||||||
|
|
||||||
private lateinit var client: BleGattClient
|
private lateinit var client: BleGattClient
|
||||||
|
|
||||||
private var secured = false
|
private var secured = false
|
||||||
@@ -136,13 +133,7 @@ internal class CGMService : NotificationService() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun startGattClient(device: ServerDevice) = lifecycleScope.launch {
|
private fun startGattClient(device: ServerDevice) = lifecycleScope.launch {
|
||||||
val logger = NordicBlekLogger(this@CGMService, stringConst.APP_NAME, "CGM", device.address)
|
client = device.connect(this@CGMService, logger = { p, s -> repository.log(p, s) })
|
||||||
|
|
||||||
client = device.connect(this@CGMService, logger = logger)
|
|
||||||
|
|
||||||
repository.loggerEvent
|
|
||||||
.onEach { logger.launch() }
|
|
||||||
.launchIn(lifecycleScope)
|
|
||||||
|
|
||||||
client.connectionStateWithStatus
|
client.connectionStateWithStatus
|
||||||
.onEach { repository.onConnectionStateChanged(it) }
|
.onEach { repository.onConnectionStateChanged(it) }
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ import kotlinx.coroutines.flow.asSharedFlow
|
|||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
import no.nordicsemi.android.common.core.simpleSharedFlow
|
import no.nordicsemi.android.common.core.simpleSharedFlow
|
||||||
|
import no.nordicsemi.android.common.logger.NordicBlekLogger
|
||||||
import no.nordicsemi.android.csc.data.CSCServiceData
|
import no.nordicsemi.android.csc.data.CSCServiceData
|
||||||
import no.nordicsemi.android.csc.data.SpeedUnit
|
import no.nordicsemi.android.csc.data.SpeedUnit
|
||||||
import no.nordicsemi.android.kotlin.ble.core.ServerDevice
|
import no.nordicsemi.android.kotlin.ble.core.ServerDevice
|
||||||
@@ -49,6 +50,7 @@ import no.nordicsemi.android.kotlin.ble.profile.csc.data.WheelSizes
|
|||||||
import no.nordicsemi.android.service.DisconnectAndStopEvent
|
import no.nordicsemi.android.service.DisconnectAndStopEvent
|
||||||
import no.nordicsemi.android.service.OpenLoggerEvent
|
import no.nordicsemi.android.service.OpenLoggerEvent
|
||||||
import no.nordicsemi.android.service.ServiceManager
|
import no.nordicsemi.android.service.ServiceManager
|
||||||
|
import no.nordicsemi.android.ui.view.StringConst
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
@@ -57,7 +59,10 @@ class CSCRepository @Inject constructor(
|
|||||||
@ApplicationContext
|
@ApplicationContext
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
private val serviceManager: ServiceManager,
|
private val serviceManager: ServiceManager,
|
||||||
|
private val stringConst: StringConst
|
||||||
) {
|
) {
|
||||||
|
private var logger: NordicBlekLogger? = null
|
||||||
|
|
||||||
private val _wheelSize = MutableStateFlow(WheelSizes.default)
|
private val _wheelSize = MutableStateFlow(WheelSizes.default)
|
||||||
internal val wheelSize = _wheelSize.asStateFlow()
|
internal val wheelSize = _wheelSize.asStateFlow()
|
||||||
|
|
||||||
@@ -67,9 +72,6 @@ class CSCRepository @Inject constructor(
|
|||||||
private val _stopEvent = simpleSharedFlow<DisconnectAndStopEvent>()
|
private val _stopEvent = simpleSharedFlow<DisconnectAndStopEvent>()
|
||||||
internal val stopEvent = _stopEvent.asSharedFlow()
|
internal val stopEvent = _stopEvent.asSharedFlow()
|
||||||
|
|
||||||
private val _loggerEvent = simpleSharedFlow<OpenLoggerEvent>()
|
|
||||||
internal val loggerEvent = _loggerEvent.asSharedFlow()
|
|
||||||
|
|
||||||
val isRunning = data.map { it.connectionState?.state == GattConnectionState.STATE_CONNECTED }
|
val isRunning = data.map { it.connectionState?.state == GattConnectionState.STATE_CONNECTED }
|
||||||
|
|
||||||
private var isOnScreen = false
|
private var isOnScreen = false
|
||||||
@@ -90,6 +92,7 @@ class CSCRepository @Inject constructor(
|
|||||||
private fun shouldClean() = !isOnScreen && !isServiceRunning
|
private fun shouldClean() = !isOnScreen && !isServiceRunning
|
||||||
|
|
||||||
fun launch(device: ServerDevice) {
|
fun launch(device: ServerDevice) {
|
||||||
|
logger = NordicBlekLogger(context, stringConst.APP_NAME, "CSC", device.address)
|
||||||
_data.value = _data.value.copy(deviceName = device.name)
|
_data.value = _data.value.copy(deviceName = device.name)
|
||||||
serviceManager.startService(CSCService::class.java, device)
|
serviceManager.startService(CSCService::class.java, device)
|
||||||
}
|
}
|
||||||
@@ -120,7 +123,11 @@ class CSCRepository @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun openLogger() {
|
fun openLogger() {
|
||||||
_loggerEvent.tryEmit(OpenLoggerEvent())
|
logger?.launch()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun log(priority: Int, message: String) {
|
||||||
|
logger?.log(priority, message)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun disconnect() {
|
fun disconnect() {
|
||||||
@@ -128,6 +135,7 @@ class CSCRepository @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun clean() {
|
private fun clean() {
|
||||||
|
logger = null
|
||||||
_data.value = CSCServiceData()
|
_data.value = CSCServiceData()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,9 +69,6 @@ internal class CSCService : NotificationService() {
|
|||||||
@Inject
|
@Inject
|
||||||
lateinit var repository: CSCRepository
|
lateinit var repository: CSCRepository
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var stringConst: StringConst
|
|
||||||
|
|
||||||
private lateinit var client: BleGattClient
|
private lateinit var client: BleGattClient
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
@@ -91,13 +88,7 @@ internal class CSCService : NotificationService() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun startGattClient(device: ServerDevice) = lifecycleScope.launch {
|
private fun startGattClient(device: ServerDevice) = lifecycleScope.launch {
|
||||||
val logger = NordicBlekLogger(this@CSCService, stringConst.APP_NAME, "CSC", device.address)
|
client = device.connect(this@CSCService, logger = { p, s -> repository.log(p, s) })
|
||||||
|
|
||||||
client = device.connect(this@CSCService, logger = logger)
|
|
||||||
|
|
||||||
repository.loggerEvent
|
|
||||||
.onEach { logger.launch() }
|
|
||||||
.launchIn(lifecycleScope)
|
|
||||||
|
|
||||||
client.connectionStateWithStatus
|
client.connectionStateWithStatus
|
||||||
.onEach { repository.onConnectionStateChanged(it) }
|
.onEach { repository.onConnectionStateChanged(it) }
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ import no.nordicsemi.android.kotlin.ble.profile.hrs.data.HRSData
|
|||||||
import no.nordicsemi.android.service.DisconnectAndStopEvent
|
import no.nordicsemi.android.service.DisconnectAndStopEvent
|
||||||
import no.nordicsemi.android.service.OpenLoggerEvent
|
import no.nordicsemi.android.service.OpenLoggerEvent
|
||||||
import no.nordicsemi.android.service.ServiceManager
|
import no.nordicsemi.android.service.ServiceManager
|
||||||
|
import no.nordicsemi.android.ui.view.StringConst
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
@@ -54,7 +55,8 @@ import javax.inject.Singleton
|
|||||||
class HRSRepository @Inject constructor(
|
class HRSRepository @Inject constructor(
|
||||||
@ApplicationContext
|
@ApplicationContext
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
private val serviceManager: ServiceManager
|
private val serviceManager: ServiceManager,
|
||||||
|
private val stringConst: StringConst
|
||||||
) {
|
) {
|
||||||
private var logger: NordicBlekLogger? = null
|
private var logger: NordicBlekLogger? = null
|
||||||
|
|
||||||
@@ -64,9 +66,6 @@ class HRSRepository @Inject constructor(
|
|||||||
private val _stopEvent = simpleSharedFlow<DisconnectAndStopEvent>()
|
private val _stopEvent = simpleSharedFlow<DisconnectAndStopEvent>()
|
||||||
internal val stopEvent = _stopEvent.asSharedFlow()
|
internal val stopEvent = _stopEvent.asSharedFlow()
|
||||||
|
|
||||||
private val _loggerEvent = simpleSharedFlow<OpenLoggerEvent>()
|
|
||||||
internal val loggerEvent = _loggerEvent.asSharedFlow()
|
|
||||||
|
|
||||||
val isRunning = data.map { it.connectionState?.state == GattConnectionState.STATE_CONNECTED }
|
val isRunning = data.map { it.connectionState?.state == GattConnectionState.STATE_CONNECTED }
|
||||||
|
|
||||||
private var isOnScreen = false
|
private var isOnScreen = false
|
||||||
@@ -87,6 +86,7 @@ class HRSRepository @Inject constructor(
|
|||||||
private fun shouldClean() = !isOnScreen && !isServiceRunning
|
private fun shouldClean() = !isOnScreen && !isServiceRunning
|
||||||
|
|
||||||
fun launch(device: ServerDevice) {
|
fun launch(device: ServerDevice) {
|
||||||
|
logger = NordicBlekLogger(context, stringConst.APP_NAME, "HRS", device.address)
|
||||||
_data.value = _data.value.copy(deviceName = device.name)
|
_data.value = _data.value.copy(deviceName = device.name)
|
||||||
serviceManager.startService(HRSService::class.java, device)
|
serviceManager.startService(HRSService::class.java, device)
|
||||||
}
|
}
|
||||||
@@ -117,7 +117,11 @@ class HRSRepository @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun openLogger() {
|
fun openLogger() {
|
||||||
_loggerEvent.tryEmit(OpenLoggerEvent())
|
logger?.launch()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun log(priority: Int, message: String) {
|
||||||
|
logger?.log(priority, message)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun disconnect() {
|
fun disconnect() {
|
||||||
@@ -125,6 +129,7 @@ class HRSRepository @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun clean() {
|
private fun clean() {
|
||||||
|
logger = null
|
||||||
_data.value = HRSServiceData()
|
_data.value = HRSServiceData()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,7 +41,6 @@ import kotlinx.coroutines.flow.launchIn
|
|||||||
import kotlinx.coroutines.flow.mapNotNull
|
import kotlinx.coroutines.flow.mapNotNull
|
||||||
import kotlinx.coroutines.flow.onEach
|
import kotlinx.coroutines.flow.onEach
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import no.nordicsemi.android.common.logger.NordicBlekLogger
|
|
||||||
import no.nordicsemi.android.kotlin.ble.client.main.callback.BleGattClient
|
import no.nordicsemi.android.kotlin.ble.client.main.callback.BleGattClient
|
||||||
import no.nordicsemi.android.kotlin.ble.client.main.connect
|
import no.nordicsemi.android.kotlin.ble.client.main.connect
|
||||||
import no.nordicsemi.android.kotlin.ble.client.main.service.BleGattServices
|
import no.nordicsemi.android.kotlin.ble.client.main.service.BleGattServices
|
||||||
@@ -53,7 +52,6 @@ import no.nordicsemi.android.kotlin.ble.profile.hrs.BodySensorLocationParser
|
|||||||
import no.nordicsemi.android.kotlin.ble.profile.hrs.HRSDataParser
|
import no.nordicsemi.android.kotlin.ble.profile.hrs.HRSDataParser
|
||||||
import no.nordicsemi.android.service.DEVICE_DATA
|
import no.nordicsemi.android.service.DEVICE_DATA
|
||||||
import no.nordicsemi.android.service.NotificationService
|
import no.nordicsemi.android.service.NotificationService
|
||||||
import no.nordicsemi.android.ui.view.StringConst
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@@ -71,9 +69,6 @@ internal class HRSService : NotificationService() {
|
|||||||
@Inject
|
@Inject
|
||||||
lateinit var repository: HRSRepository
|
lateinit var repository: HRSRepository
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var stringConst: StringConst
|
|
||||||
|
|
||||||
private lateinit var client: BleGattClient
|
private lateinit var client: BleGattClient
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
@@ -93,16 +88,10 @@ internal class HRSService : NotificationService() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun startGattClient(device: ServerDevice) = lifecycleScope.launch {
|
private fun startGattClient(device: ServerDevice) = lifecycleScope.launch {
|
||||||
val logger = NordicBlekLogger(this@HRSService, stringConst.APP_NAME, "HRS", device.address)
|
client = device.connect(this@HRSService, logger = { p, s -> repository.log(p, s) })
|
||||||
|
|
||||||
client = device.connect(this@HRSService, logger = logger)
|
|
||||||
|
|
||||||
client.waitForBonding()
|
client.waitForBonding()
|
||||||
|
|
||||||
repository.loggerEvent
|
|
||||||
.onEach { logger.launch() }
|
|
||||||
.launchIn(lifecycleScope)
|
|
||||||
|
|
||||||
client.connectionStateWithStatus
|
client.connectionStateWithStatus
|
||||||
.onEach { repository.onConnectionStateChanged(it) }
|
.onEach { repository.onConnectionStateChanged(it) }
|
||||||
.filterNotNull()
|
.filterNotNull()
|
||||||
|
|||||||
@@ -38,14 +38,15 @@ import kotlinx.coroutines.flow.asSharedFlow
|
|||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
import no.nordicsemi.android.common.core.simpleSharedFlow
|
import no.nordicsemi.android.common.core.simpleSharedFlow
|
||||||
|
import no.nordicsemi.android.common.logger.NordicBlekLogger
|
||||||
import no.nordicsemi.android.kotlin.ble.core.ServerDevice
|
import no.nordicsemi.android.kotlin.ble.core.ServerDevice
|
||||||
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionState
|
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionState
|
||||||
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionStateWithStatus
|
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionStateWithStatus
|
||||||
import no.nordicsemi.android.kotlin.ble.profile.prx.AlarmLevel
|
import no.nordicsemi.android.kotlin.ble.profile.prx.AlarmLevel
|
||||||
import no.nordicsemi.android.prx.data.PRXServiceData
|
import no.nordicsemi.android.prx.data.PRXServiceData
|
||||||
import no.nordicsemi.android.service.DisconnectAndStopEvent
|
import no.nordicsemi.android.service.DisconnectAndStopEvent
|
||||||
import no.nordicsemi.android.service.OpenLoggerEvent
|
|
||||||
import no.nordicsemi.android.service.ServiceManager
|
import no.nordicsemi.android.service.ServiceManager
|
||||||
|
import no.nordicsemi.android.ui.view.StringConst
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
@@ -53,8 +54,10 @@ import javax.inject.Singleton
|
|||||||
class PRXRepository @Inject internal constructor(
|
class PRXRepository @Inject internal constructor(
|
||||||
@ApplicationContext
|
@ApplicationContext
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
private val serviceManager: ServiceManager
|
private val serviceManager: ServiceManager,
|
||||||
|
private val stringConst: StringConst
|
||||||
) {
|
) {
|
||||||
|
private var logger: NordicBlekLogger? = null
|
||||||
|
|
||||||
private val _data = MutableStateFlow(PRXServiceData())
|
private val _data = MutableStateFlow(PRXServiceData())
|
||||||
internal val data = _data.asStateFlow()
|
internal val data = _data.asStateFlow()
|
||||||
@@ -62,9 +65,6 @@ class PRXRepository @Inject internal constructor(
|
|||||||
private val _stopEvent = simpleSharedFlow<DisconnectAndStopEvent>()
|
private val _stopEvent = simpleSharedFlow<DisconnectAndStopEvent>()
|
||||||
internal val stopEvent = _stopEvent.asSharedFlow()
|
internal val stopEvent = _stopEvent.asSharedFlow()
|
||||||
|
|
||||||
private val _loggerEvent = simpleSharedFlow<OpenLoggerEvent>()
|
|
||||||
internal val loggerEvent = _loggerEvent.asSharedFlow()
|
|
||||||
|
|
||||||
private val _remoteAlarmLevel = simpleSharedFlow<AlarmLevel>()
|
private val _remoteAlarmLevel = simpleSharedFlow<AlarmLevel>()
|
||||||
internal val remoteAlarmLevel = _remoteAlarmLevel.asSharedFlow()
|
internal val remoteAlarmLevel = _remoteAlarmLevel.asSharedFlow()
|
||||||
|
|
||||||
@@ -87,8 +87,8 @@ class PRXRepository @Inject internal constructor(
|
|||||||
|
|
||||||
private fun shouldClean() = !isOnScreen && !isServiceRunning
|
private fun shouldClean() = !isOnScreen && !isServiceRunning
|
||||||
|
|
||||||
|
|
||||||
fun launch(device: ServerDevice) {
|
fun launch(device: ServerDevice) {
|
||||||
|
logger = NordicBlekLogger(context, stringConst.APP_NAME, "PRX", device.address)
|
||||||
_data.value = _data.value.copy(deviceName = device.name)
|
_data.value = _data.value.copy(deviceName = device.name)
|
||||||
serviceManager.startService(PRXService::class.java, device)
|
serviceManager.startService(PRXService::class.java, device)
|
||||||
}
|
}
|
||||||
@@ -118,7 +118,11 @@ class PRXRepository @Inject internal constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun openLogger() {
|
fun openLogger() {
|
||||||
_loggerEvent.tryEmit(OpenLoggerEvent())
|
logger?.launch()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun log(priority: Int, message: String) {
|
||||||
|
logger?.log(priority, message)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onMissingServices() {
|
fun onMissingServices() {
|
||||||
@@ -132,6 +136,7 @@ class PRXRepository @Inject internal constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun clean() {
|
private fun clean() {
|
||||||
|
logger = null
|
||||||
_data.value = PRXServiceData()
|
_data.value = PRXServiceData()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -84,9 +84,6 @@ internal class PRXService : NotificationService() {
|
|||||||
@Inject
|
@Inject
|
||||||
lateinit var repository: PRXRepository
|
lateinit var repository: PRXRepository
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var stringConst: StringConst
|
|
||||||
|
|
||||||
private lateinit var client: BleGattClient
|
private lateinit var client: BleGattClient
|
||||||
private lateinit var server: BleGattServer
|
private lateinit var server: BleGattServer
|
||||||
|
|
||||||
@@ -162,16 +159,14 @@ internal class PRXService : NotificationService() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun startGattClient(device: ServerDevice) = lifecycleScope.launch {
|
private fun startGattClient(device: ServerDevice) = lifecycleScope.launch {
|
||||||
val logger = NordicBlekLogger(this@PRXService, stringConst.APP_NAME, "PRX", device.address)
|
client = device.connect(
|
||||||
|
this@PRXService,
|
||||||
client = device.connect(this@PRXService, logger = logger, options = BleGattConnectOptions(autoConnect = true))
|
logger = { p, s -> repository.log(p, s) },
|
||||||
|
options = BleGattConnectOptions(autoConnect = true)
|
||||||
|
)
|
||||||
|
|
||||||
client.waitForBonding()
|
client.waitForBonding()
|
||||||
|
|
||||||
repository.loggerEvent
|
|
||||||
.onEach { logger.launch() }
|
|
||||||
.launchIn(lifecycleScope)
|
|
||||||
|
|
||||||
client.connectionStateWithStatus
|
client.connectionStateWithStatus
|
||||||
.filterNotNull()
|
.filterNotNull()
|
||||||
.onEach { repository.onConnectionStateChanged(it) }
|
.onEach { repository.onConnectionStateChanged(it) }
|
||||||
|
|||||||
@@ -45,8 +45,8 @@ import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionStateWithStatus
|
|||||||
import no.nordicsemi.android.kotlin.ble.profile.rscs.data.RSCSData
|
import no.nordicsemi.android.kotlin.ble.profile.rscs.data.RSCSData
|
||||||
import no.nordicsemi.android.rscs.data.RSCSServiceData
|
import no.nordicsemi.android.rscs.data.RSCSServiceData
|
||||||
import no.nordicsemi.android.service.DisconnectAndStopEvent
|
import no.nordicsemi.android.service.DisconnectAndStopEvent
|
||||||
import no.nordicsemi.android.service.OpenLoggerEvent
|
|
||||||
import no.nordicsemi.android.service.ServiceManager
|
import no.nordicsemi.android.service.ServiceManager
|
||||||
|
import no.nordicsemi.android.ui.view.StringConst
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
@@ -54,7 +54,8 @@ import javax.inject.Singleton
|
|||||||
class RSCSRepository @Inject constructor(
|
class RSCSRepository @Inject constructor(
|
||||||
@ApplicationContext
|
@ApplicationContext
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
private val serviceManager: ServiceManager
|
private val serviceManager: ServiceManager,
|
||||||
|
private val stringConst: StringConst
|
||||||
) {
|
) {
|
||||||
private var logger: NordicBlekLogger? = null
|
private var logger: NordicBlekLogger? = null
|
||||||
|
|
||||||
@@ -64,9 +65,6 @@ class RSCSRepository @Inject constructor(
|
|||||||
private val _stopEvent = simpleSharedFlow<DisconnectAndStopEvent>()
|
private val _stopEvent = simpleSharedFlow<DisconnectAndStopEvent>()
|
||||||
internal val stopEvent = _stopEvent.asSharedFlow()
|
internal val stopEvent = _stopEvent.asSharedFlow()
|
||||||
|
|
||||||
private val _loggerEvent = simpleSharedFlow<OpenLoggerEvent>()
|
|
||||||
internal val loggerEvent = _loggerEvent.asSharedFlow()
|
|
||||||
|
|
||||||
val isRunning = data.map { it.connectionState?.state == GattConnectionState.STATE_CONNECTED }
|
val isRunning = data.map { it.connectionState?.state == GattConnectionState.STATE_CONNECTED }
|
||||||
|
|
||||||
private var isOnScreen = false
|
private var isOnScreen = false
|
||||||
@@ -87,6 +85,7 @@ class RSCSRepository @Inject constructor(
|
|||||||
private fun shouldClean() = !isOnScreen && !isServiceRunning
|
private fun shouldClean() = !isOnScreen && !isServiceRunning
|
||||||
|
|
||||||
fun launch(device: ServerDevice) {
|
fun launch(device: ServerDevice) {
|
||||||
|
logger = NordicBlekLogger(context, stringConst.APP_NAME, "RSCS", device.address)
|
||||||
_data.value = _data.value.copy(deviceName = device.name)
|
_data.value = _data.value.copy(deviceName = device.name)
|
||||||
serviceManager.startService(RSCSService::class.java, device)
|
serviceManager.startService(RSCSService::class.java, device)
|
||||||
}
|
}
|
||||||
@@ -109,7 +108,11 @@ class RSCSRepository @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun openLogger() {
|
fun openLogger() {
|
||||||
_loggerEvent.tryEmit(OpenLoggerEvent())
|
logger?.launch()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun log(priority: Int, message: String) {
|
||||||
|
logger?.log(priority, message)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun disconnect() {
|
fun disconnect() {
|
||||||
|
|||||||
@@ -69,9 +69,6 @@ internal class RSCSService : NotificationService() {
|
|||||||
@Inject
|
@Inject
|
||||||
lateinit var repository: RSCSRepository
|
lateinit var repository: RSCSRepository
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var stringConst: StringConst
|
|
||||||
|
|
||||||
private lateinit var client: BleGattClient
|
private lateinit var client: BleGattClient
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
@@ -91,13 +88,7 @@ internal class RSCSService : NotificationService() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun startGattClient(device: ServerDevice) = lifecycleScope.launch {
|
private fun startGattClient(device: ServerDevice) = lifecycleScope.launch {
|
||||||
val logger = NordicBlekLogger(this@RSCSService, stringConst.APP_NAME, "RSCS", device.address)
|
client = device.connect(this@RSCSService, logger = { p, s -> repository.log(p, s) })
|
||||||
|
|
||||||
client = device.connect(this@RSCSService, logger = logger)
|
|
||||||
|
|
||||||
repository.loggerEvent
|
|
||||||
.onEach { logger.launch() }
|
|
||||||
.launchIn(lifecycleScope)
|
|
||||||
|
|
||||||
client.connectionStateWithStatus
|
client.connectionStateWithStatus
|
||||||
.onEach { repository.onConnectionStateChanged(it) }
|
.onEach { repository.onConnectionStateChanged(it) }
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ import kotlinx.coroutines.flow.asSharedFlow
|
|||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
import no.nordicsemi.android.common.core.simpleSharedFlow
|
import no.nordicsemi.android.common.core.simpleSharedFlow
|
||||||
|
import no.nordicsemi.android.common.logger.NordicBlekLogger
|
||||||
import no.nordicsemi.android.kotlin.ble.core.ServerDevice
|
import no.nordicsemi.android.kotlin.ble.core.ServerDevice
|
||||||
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionState
|
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionState
|
||||||
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionStateWithStatus
|
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionStateWithStatus
|
||||||
@@ -51,6 +52,7 @@ import no.nordicsemi.android.uart.data.UARTRecord
|
|||||||
import no.nordicsemi.android.uart.data.UARTRecordType
|
import no.nordicsemi.android.uart.data.UARTRecordType
|
||||||
import no.nordicsemi.android.uart.data.UARTServiceData
|
import no.nordicsemi.android.uart.data.UARTServiceData
|
||||||
import no.nordicsemi.android.uart.data.parseWithNewLineChar
|
import no.nordicsemi.android.uart.data.parseWithNewLineChar
|
||||||
|
import no.nordicsemi.android.ui.view.StringConst
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
@@ -59,8 +61,11 @@ class UARTRepository @Inject internal constructor(
|
|||||||
@ApplicationContext
|
@ApplicationContext
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
private val serviceManager: ServiceManager,
|
private val serviceManager: ServiceManager,
|
||||||
private val configurationDataSource: ConfigurationDataSource
|
private val configurationDataSource: ConfigurationDataSource,
|
||||||
|
private val stringConst: StringConst
|
||||||
) {
|
) {
|
||||||
|
private var logger: NordicBlekLogger? = null
|
||||||
|
|
||||||
private val _data = MutableStateFlow(UARTServiceData())
|
private val _data = MutableStateFlow(UARTServiceData())
|
||||||
internal val data = _data.asStateFlow()
|
internal val data = _data.asStateFlow()
|
||||||
|
|
||||||
@@ -70,9 +75,6 @@ class UARTRepository @Inject internal constructor(
|
|||||||
private val _command = simpleSharedFlow<String>()
|
private val _command = simpleSharedFlow<String>()
|
||||||
internal val command = _command.asSharedFlow()
|
internal val command = _command.asSharedFlow()
|
||||||
|
|
||||||
private val _loggerEvent = simpleSharedFlow<OpenLoggerEvent>()
|
|
||||||
internal val loggerEvent = _loggerEvent.asSharedFlow()
|
|
||||||
|
|
||||||
val isRunning = data.map { it.connectionState?.state == GattConnectionState.STATE_CONNECTED }
|
val isRunning = data.map { it.connectionState?.state == GattConnectionState.STATE_CONNECTED }
|
||||||
|
|
||||||
val lastConfigurationName = configurationDataSource.lastConfigurationName
|
val lastConfigurationName = configurationDataSource.lastConfigurationName
|
||||||
@@ -95,6 +97,7 @@ class UARTRepository @Inject internal constructor(
|
|||||||
private fun shouldClean() = !isOnScreen && !isServiceRunning
|
private fun shouldClean() = !isOnScreen && !isServiceRunning
|
||||||
|
|
||||||
fun launch(device: ServerDevice) {
|
fun launch(device: ServerDevice) {
|
||||||
|
logger = NordicBlekLogger(context, stringConst.APP_NAME, "UART", device.address)
|
||||||
_data.value = _data.value.copy(deviceName = device.name)
|
_data.value = _data.value.copy(deviceName = device.name)
|
||||||
serviceManager.startService(UARTService::class.java, device)
|
serviceManager.startService(UARTService::class.java, device)
|
||||||
}
|
}
|
||||||
@@ -131,7 +134,11 @@ class UARTRepository @Inject internal constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun openLogger() {
|
fun openLogger() {
|
||||||
_loggerEvent.tryEmit(OpenLoggerEvent())
|
logger?.launch()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun log(priority: Int, message: String) {
|
||||||
|
logger?.log(priority, message)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onMissingServices() {
|
fun onMissingServices() {
|
||||||
@@ -148,6 +155,7 @@ class UARTRepository @Inject internal constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun clean() {
|
private fun clean() {
|
||||||
|
logger = null
|
||||||
_data.value = UARTServiceData()
|
_data.value = UARTServiceData()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,7 +41,6 @@ import kotlinx.coroutines.flow.launchIn
|
|||||||
import kotlinx.coroutines.flow.mapNotNull
|
import kotlinx.coroutines.flow.mapNotNull
|
||||||
import kotlinx.coroutines.flow.onEach
|
import kotlinx.coroutines.flow.onEach
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import no.nordicsemi.android.common.logger.NordicBlekLogger
|
|
||||||
import no.nordicsemi.android.kotlin.ble.client.main.callback.BleGattClient
|
import no.nordicsemi.android.kotlin.ble.client.main.callback.BleGattClient
|
||||||
import no.nordicsemi.android.kotlin.ble.client.main.connect
|
import no.nordicsemi.android.kotlin.ble.client.main.connect
|
||||||
import no.nordicsemi.android.kotlin.ble.client.main.service.BleGattCharacteristic
|
import no.nordicsemi.android.kotlin.ble.client.main.service.BleGattCharacteristic
|
||||||
@@ -53,7 +52,6 @@ import no.nordicsemi.android.kotlin.ble.core.data.Mtu
|
|||||||
import no.nordicsemi.android.kotlin.ble.profile.battery.BatteryLevelParser
|
import no.nordicsemi.android.kotlin.ble.profile.battery.BatteryLevelParser
|
||||||
import no.nordicsemi.android.service.DEVICE_DATA
|
import no.nordicsemi.android.service.DEVICE_DATA
|
||||||
import no.nordicsemi.android.service.NotificationService
|
import no.nordicsemi.android.service.NotificationService
|
||||||
import no.nordicsemi.android.ui.view.StringConst
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@@ -71,9 +69,6 @@ internal class UARTService : NotificationService() {
|
|||||||
@Inject
|
@Inject
|
||||||
lateinit var repository: UARTRepository
|
lateinit var repository: UARTRepository
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var stringConst: StringConst
|
|
||||||
|
|
||||||
private lateinit var client: BleGattClient
|
private lateinit var client: BleGattClient
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
@@ -93,16 +88,10 @@ internal class UARTService : NotificationService() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun startGattClient(device: ServerDevice) = lifecycleScope.launch {
|
private fun startGattClient(device: ServerDevice) = lifecycleScope.launch {
|
||||||
val logger = NordicBlekLogger(this@UARTService, stringConst.APP_NAME, "UART", device.address)
|
client = device.connect(this@UARTService, logger = { p, s -> repository.log(p, s) })
|
||||||
|
|
||||||
client = device.connect(this@UARTService, logger = logger)
|
|
||||||
|
|
||||||
client.requestMtu(Mtu.max)
|
client.requestMtu(Mtu.max)
|
||||||
|
|
||||||
repository.loggerEvent
|
|
||||||
.onEach { logger.launch() }
|
|
||||||
.launchIn(lifecycleScope)
|
|
||||||
|
|
||||||
client.connectionStateWithStatus
|
client.connectionStateWithStatus
|
||||||
.onEach { repository.onConnectionStateChanged(it) }
|
.onEach { repository.onConnectionStateChanged(it) }
|
||||||
.filterNotNull()
|
.filterNotNull()
|
||||||
@@ -114,12 +103,12 @@ internal class UARTService : NotificationService() {
|
|||||||
|
|
||||||
client.discoverServices()
|
client.discoverServices()
|
||||||
.filterNotNull()
|
.filterNotNull()
|
||||||
.onEach { configureGatt(it, logger) }
|
.onEach { configureGatt(it) }
|
||||||
.catch { repository.onMissingServices() }
|
.catch { repository.onMissingServices() }
|
||||||
.launchIn(lifecycleScope)
|
.launchIn(lifecycleScope)
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun configureGatt(services: BleGattServices, logger: NordicBlekLogger) {
|
private suspend fun configureGatt(services: BleGattServices) {
|
||||||
val uartService = services.findService(UART_SERVICE_UUID)!!
|
val uartService = services.findService(UART_SERVICE_UUID)!!
|
||||||
val rxCharacteristic = uartService.findCharacteristic(UART_RX_CHARACTERISTIC_UUID)!!
|
val rxCharacteristic = uartService.findCharacteristic(UART_RX_CHARACTERISTIC_UUID)!!
|
||||||
val txCharacteristic = uartService.findCharacteristic(UART_TX_CHARACTERISTIC_UUID)!!
|
val txCharacteristic = uartService.findCharacteristic(UART_TX_CHARACTERISTIC_UUID)!!
|
||||||
@@ -134,14 +123,14 @@ internal class UARTService : NotificationService() {
|
|||||||
|
|
||||||
txCharacteristic.getNotifications()
|
txCharacteristic.getNotifications()
|
||||||
.onEach { repository.onNewMessageReceived(String(it)) }
|
.onEach { repository.onNewMessageReceived(String(it)) }
|
||||||
.onEach { logger.log(10, "Received: ${String(it)}") }
|
.onEach { repository.log(10, "Received: ${String(it)}") }
|
||||||
.catch { it.printStackTrace() }
|
.catch { it.printStackTrace() }
|
||||||
.launchIn(lifecycleScope)
|
.launchIn(lifecycleScope)
|
||||||
|
|
||||||
repository.command
|
repository.command
|
||||||
.onEach { rxCharacteristic.splitWrite(it.toByteArray(), getWriteType(rxCharacteristic)) }
|
.onEach { rxCharacteristic.splitWrite(it.toByteArray(), getWriteType(rxCharacteristic)) }
|
||||||
.onEach { repository.onNewMessageSent(it) }
|
.onEach { repository.onNewMessageSent(it) }
|
||||||
.onEach { logger.log(10, "Sent: $it") }
|
.onEach { repository.log(10, "Sent: $it") }
|
||||||
.launchIn(lifecycleScope)
|
.launchIn(lifecycleScope)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user