mirror of
https://github.com/aljazceru/Android-nRF-Toolbox.git
synced 2026-02-11 09:44:27 +01:00
Add missing services message
This commit is contained in:
@@ -80,7 +80,7 @@ fun BPSScreen() {
|
||||
null,
|
||||
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
|
||||
GattConnectionState.STATE_DISCONNECTED,
|
||||
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(state.result.connectionState.status) {
|
||||
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(state.disconnectStatus) {
|
||||
NavigateUpButton(navigateUp)
|
||||
}
|
||||
GattConnectionState.STATE_CONNECTED -> BPSContentView(state.result) { viewModel.onEvent(it) }
|
||||
|
||||
@@ -32,8 +32,17 @@
|
||||
package no.nordicsemi.android.bps.view
|
||||
|
||||
import no.nordicsemi.android.bps.data.BPSServiceData
|
||||
import no.nordicsemi.android.kotlin.ble.core.data.BleGattConnectionStatus
|
||||
|
||||
internal data class BPSViewState(
|
||||
val result: BPSServiceData = BPSServiceData(),
|
||||
val deviceName: String? = null
|
||||
)
|
||||
val deviceName: String? = null,
|
||||
val missingServices: Boolean = false
|
||||
) {
|
||||
|
||||
val disconnectStatus = if (missingServices) {
|
||||
BleGattConnectionStatus.NOT_SUPPORTED
|
||||
} else {
|
||||
result.connectionState?.status ?: BleGattConnectionStatus.UNKNOWN
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,11 +112,16 @@ internal class BPSViewModel @Inject constructor(
|
||||
|
||||
fun onEvent(event: BPSViewEvent) {
|
||||
when (event) {
|
||||
DisconnectEvent -> navigationManager.navigateUp()
|
||||
DisconnectEvent -> onDisconnectEvent()
|
||||
OpenLoggerEvent -> logger.launch()
|
||||
}
|
||||
}
|
||||
|
||||
private fun onDisconnectEvent() {
|
||||
client.disconnect()
|
||||
navigationManager.navigateUp()
|
||||
}
|
||||
|
||||
private fun startGattClient(device: ServerDevice) = viewModelScope.launch {
|
||||
_state.value = _state.value.copy(deviceName = device.name)
|
||||
|
||||
@@ -139,7 +144,7 @@ internal class BPSViewModel @Inject constructor(
|
||||
client.discoverServices()
|
||||
.filterNotNull()
|
||||
.onEach { configureGatt(it) }
|
||||
.catch { it.printStackTrace() }
|
||||
.catch { onMissingServices() }
|
||||
.launchIn(viewModelScope)
|
||||
}
|
||||
|
||||
@@ -169,6 +174,11 @@ internal class BPSViewModel @Inject constructor(
|
||||
?.launchIn(viewModelScope)
|
||||
}
|
||||
|
||||
private fun onMissingServices() {
|
||||
_state.value = _state.value.copy(missingServices = true)
|
||||
client.disconnect()
|
||||
}
|
||||
|
||||
private fun onDataUpdate(connectionState: GattConnectionStateWithStatus) {
|
||||
val newResult = _state.value.result.copy(connectionState = connectionState)
|
||||
_state.value = _state.value.copy(result = newResult)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package no.nordicsemi.android.cgms.data
|
||||
|
||||
import no.nordicsemi.android.kotlin.ble.core.data.BleGattConnectionStatus
|
||||
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionStateWithStatus
|
||||
import no.nordicsemi.android.kotlin.ble.profile.cgm.data.CGMRecord
|
||||
import no.nordicsemi.android.kotlin.ble.profile.gls.data.RequestStatus
|
||||
@@ -9,8 +10,16 @@ internal data class CGMServiceData(
|
||||
val batteryLevel: Int? = null,
|
||||
val connectionState: GattConnectionStateWithStatus? = null,
|
||||
val requestStatus: RequestStatus = RequestStatus.IDLE,
|
||||
val deviceName: String? = null
|
||||
)
|
||||
val deviceName: String? = null,
|
||||
val missingServices: Boolean = false
|
||||
) {
|
||||
|
||||
val disconnectStatus = if (missingServices) {
|
||||
BleGattConnectionStatus.NOT_SUPPORTED
|
||||
} else {
|
||||
connectionState?.status ?: BleGattConnectionStatus.UNKNOWN
|
||||
}
|
||||
}
|
||||
|
||||
data class CGMRecordWithSequenceNumber(
|
||||
val sequenceNumber: Int,
|
||||
|
||||
@@ -115,6 +115,11 @@ class CGMRepository @Inject constructor(
|
||||
_data.value = _data.value.copy(requestStatus = requestStatus)
|
||||
}
|
||||
|
||||
fun onMissingServices() {
|
||||
_data.value = _data.value.copy(missingServices = true)
|
||||
_stopEvent.tryEmit(DisconnectAndStopEvent())
|
||||
}
|
||||
|
||||
fun openLogger() {
|
||||
_loggerEvent.tryEmit(OpenLoggerEvent())
|
||||
}
|
||||
|
||||
@@ -157,7 +157,7 @@ internal class CGMService : NotificationService() {
|
||||
client.discoverServices()
|
||||
.filterNotNull()
|
||||
.onEach { configureGatt(it) }
|
||||
.catch { it.printStackTrace() }
|
||||
.catch { repository.onMissingServices() }
|
||||
.launchIn(lifecycleScope)
|
||||
}
|
||||
|
||||
|
||||
@@ -81,7 +81,7 @@ fun CGMScreen() {
|
||||
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
|
||||
|
||||
GattConnectionState.STATE_DISCONNECTED,
|
||||
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(state.connectionState.status) {
|
||||
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(state.disconnectStatus) {
|
||||
NavigateUpButton(navigateUp)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package no.nordicsemi.android.csc.data
|
||||
|
||||
import no.nordicsemi.android.kotlin.ble.core.data.BleGattConnectionStatus
|
||||
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionStateWithStatus
|
||||
import no.nordicsemi.android.kotlin.ble.profile.csc.data.CSCData
|
||||
|
||||
@@ -8,5 +9,13 @@ internal data class CSCServiceData(
|
||||
val batteryLevel: Int? = null,
|
||||
val connectionState: GattConnectionStateWithStatus? = null,
|
||||
val speedUnit: SpeedUnit = SpeedUnit.M_S,
|
||||
val deviceName: String? = null
|
||||
)
|
||||
val deviceName: String? = null,
|
||||
val missingServices: Boolean = false
|
||||
) {
|
||||
|
||||
val disconnectStatus = if (missingServices) {
|
||||
BleGattConnectionStatus.NOT_SUPPORTED
|
||||
} else {
|
||||
connectionState?.status ?: BleGattConnectionStatus.UNKNOWN
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,6 +114,11 @@ class CSCRepository @Inject constructor(
|
||||
_data.value = _data.value.copy(data = cscData)
|
||||
}
|
||||
|
||||
fun onMissingServices() {
|
||||
_data.value = _data.value.copy(missingServices = true)
|
||||
_stopEvent.tryEmit(DisconnectAndStopEvent())
|
||||
}
|
||||
|
||||
fun openLogger() {
|
||||
_loggerEvent.tryEmit(OpenLoggerEvent())
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ internal class CSCService : NotificationService() {
|
||||
client.discoverServices()
|
||||
.filterNotNull()
|
||||
.onEach { configureGatt(it) }
|
||||
.catch { it.printStackTrace() }
|
||||
.catch { repository.onMissingServices() }
|
||||
.launchIn(lifecycleScope)
|
||||
}
|
||||
|
||||
|
||||
@@ -80,7 +80,7 @@ fun CSCScreen() {
|
||||
null,
|
||||
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
|
||||
GattConnectionState.STATE_DISCONNECTED,
|
||||
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(state.connectionState.status) {
|
||||
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(state.disconnectStatus) {
|
||||
NavigateUpButton(navigateUp)
|
||||
}
|
||||
GattConnectionState.STATE_CONNECTED -> CSCContentView(state) { viewModel.onEvent(it) }
|
||||
|
||||
@@ -80,7 +80,7 @@ fun GLSScreen() {
|
||||
null,
|
||||
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
|
||||
GattConnectionState.STATE_DISCONNECTED,
|
||||
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(state.glsServiceData.connectionState.status) {
|
||||
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(state.disconnectStatus) {
|
||||
NavigateUpButton(navigateUp)
|
||||
}
|
||||
GattConnectionState.STATE_CONNECTED -> GLSContentView(state.glsServiceData) { viewModel.onEvent(it) }
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
package no.nordicsemi.android.gls.main.view
|
||||
|
||||
import no.nordicsemi.android.gls.data.GLSServiceData
|
||||
import no.nordicsemi.android.kotlin.ble.core.data.BleGattConnectionStatus
|
||||
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionStateWithStatus
|
||||
import no.nordicsemi.android.kotlin.ble.profile.gls.data.GLSMeasurementContext
|
||||
import no.nordicsemi.android.kotlin.ble.profile.gls.data.GLSRecord
|
||||
@@ -39,9 +40,16 @@ import no.nordicsemi.android.kotlin.ble.profile.gls.data.RequestStatus
|
||||
|
||||
internal data class GLSViewState(
|
||||
val glsServiceData: GLSServiceData = GLSServiceData(),
|
||||
val deviceName: String? = null
|
||||
val deviceName: String? = null,
|
||||
val missingServices: Boolean = false
|
||||
) {
|
||||
|
||||
val disconnectStatus = if (missingServices) {
|
||||
BleGattConnectionStatus.NOT_SUPPORTED
|
||||
} else {
|
||||
glsServiceData.connectionState?.status ?: BleGattConnectionStatus.UNKNOWN
|
||||
}
|
||||
|
||||
fun copyWithNewConnectionState(connectionState: GattConnectionStateWithStatus): GLSViewState {
|
||||
return copy(glsServiceData = glsServiceData.copy(connectionState = connectionState))
|
||||
}
|
||||
|
||||
@@ -137,10 +137,15 @@ internal class GLSViewModel @Inject constructor(
|
||||
OpenLoggerEvent -> logger.launch()
|
||||
is OnWorkingModeSelected -> onEvent(event)
|
||||
is OnGLSRecordClick -> navigateToDetails(event.record)
|
||||
DisconnectEvent -> navigationManager.navigateUp()
|
||||
DisconnectEvent -> onDisconnectEvent()
|
||||
}
|
||||
}
|
||||
|
||||
private fun onDisconnectEvent() {
|
||||
client.disconnect()
|
||||
navigationManager.navigateUp()
|
||||
}
|
||||
|
||||
private fun navigateToDetails(record: GLSRecord) {
|
||||
val context = state.value.glsServiceData.records[record]
|
||||
navigationManager.navigateTo(GlsDetailsDestinationId, record to context)
|
||||
@@ -180,10 +185,15 @@ internal class GLSViewModel @Inject constructor(
|
||||
client.discoverServices()
|
||||
.filterNotNull()
|
||||
.onEach { configureGatt(it) }
|
||||
.catch { it.printStackTrace() }
|
||||
.catch { onMissingServices() }
|
||||
.launchIn(viewModelScope)
|
||||
}
|
||||
|
||||
private fun onMissingServices() {
|
||||
_state.value = state.value.copy(missingServices = true)
|
||||
client.disconnect()
|
||||
}
|
||||
|
||||
private fun logAnalytics(connectionState: GattConnectionStateWithStatus) {
|
||||
if (connectionState.state == GattConnectionState.STATE_CONNECTED) {
|
||||
analytics.logEvent(ProfileConnectedEvent(Profile.GLS))
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
|
||||
package no.nordicsemi.android.hrs.data
|
||||
|
||||
import no.nordicsemi.android.kotlin.ble.core.data.BleGattConnectionStatus
|
||||
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionStateWithStatus
|
||||
import no.nordicsemi.android.kotlin.ble.profile.hrs.data.HRSData
|
||||
|
||||
@@ -40,7 +41,15 @@ internal data class HRSServiceData(
|
||||
val batteryLevel: Int? = null,
|
||||
val connectionState: GattConnectionStateWithStatus? = null,
|
||||
val zoomIn: Boolean = false,
|
||||
val deviceName: String? = null
|
||||
val deviceName: String? = null,
|
||||
val missingServices: Boolean = false
|
||||
) {
|
||||
|
||||
val disconnectStatus = if (missingServices) {
|
||||
BleGattConnectionStatus.NOT_SUPPORTED
|
||||
} else {
|
||||
connectionState?.status ?: BleGattConnectionStatus.UNKNOWN
|
||||
}
|
||||
|
||||
val heartRates = data.map { it.heartRate }
|
||||
}
|
||||
|
||||
@@ -111,6 +111,11 @@ class HRSRepository @Inject constructor(
|
||||
_data.value = _data.value.copy(batteryLevel = batteryLevel)
|
||||
}
|
||||
|
||||
fun onMissingServices() {
|
||||
_data.value = _data.value.copy(missingServices = true)
|
||||
_stopEvent.tryEmit(DisconnectAndStopEvent())
|
||||
}
|
||||
|
||||
fun openLogger() {
|
||||
_loggerEvent.tryEmit(OpenLoggerEvent())
|
||||
}
|
||||
|
||||
@@ -116,7 +116,7 @@ internal class HRSService : NotificationService() {
|
||||
client.discoverServices()
|
||||
.filterNotNull()
|
||||
.onEach { configureGatt(it) }
|
||||
.catch { it.printStackTrace() }
|
||||
.catch { repository.onMissingServices() }
|
||||
.launchIn(lifecycleScope)
|
||||
}
|
||||
|
||||
|
||||
@@ -80,7 +80,7 @@ fun HRSScreen() {
|
||||
null,
|
||||
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
|
||||
GattConnectionState.STATE_DISCONNECTED,
|
||||
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(state.connectionState.status) {
|
||||
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(state.disconnectStatus) {
|
||||
NavigateUpButton(navigateUp)
|
||||
}
|
||||
GattConnectionState.STATE_CONNECTED -> HRSContentView(state) { viewModel.onEvent(it) }
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
package no.nordicsemi.android.hts.data
|
||||
|
||||
import no.nordicsemi.android.hts.view.TemperatureUnit
|
||||
import no.nordicsemi.android.kotlin.ble.core.data.BleGattConnectionStatus
|
||||
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionStateWithStatus
|
||||
import no.nordicsemi.android.kotlin.ble.profile.hts.data.HTSData
|
||||
|
||||
@@ -40,5 +41,13 @@ internal data class HTSServiceData(
|
||||
val batteryLevel: Int? = null,
|
||||
val connectionState: GattConnectionStateWithStatus? = null,
|
||||
val temperatureUnit: TemperatureUnit = TemperatureUnit.CELSIUS,
|
||||
val deviceName: String? = null
|
||||
)
|
||||
val deviceName: String? = null,
|
||||
val missingServices: Boolean = false
|
||||
) {
|
||||
|
||||
val disconnectStatus = if (missingServices) {
|
||||
BleGattConnectionStatus.NOT_SUPPORTED
|
||||
} else {
|
||||
connectionState?.status ?: BleGattConnectionStatus.UNKNOWN
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,8 +46,8 @@ import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionState
|
||||
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionStateWithStatus
|
||||
import no.nordicsemi.android.kotlin.ble.profile.hts.data.HTSData
|
||||
import no.nordicsemi.android.service.DisconnectAndStopEvent
|
||||
import no.nordicsemi.android.service.OpenLoggerEvent
|
||||
import no.nordicsemi.android.service.ServiceManager
|
||||
import no.nordicsemi.android.ui.view.StringConst
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@@ -55,7 +55,8 @@ import javax.inject.Singleton
|
||||
class HTSRepository @Inject constructor(
|
||||
@ApplicationContext
|
||||
private val context: Context,
|
||||
private val serviceManager: ServiceManager
|
||||
private val serviceManager: ServiceManager,
|
||||
private val stringConst: StringConst
|
||||
) {
|
||||
private var logger: NordicBlekLogger? = null
|
||||
|
||||
@@ -65,9 +66,6 @@ class HTSRepository @Inject constructor(
|
||||
private val _stopEvent = simpleSharedFlow<DisconnectAndStopEvent>()
|
||||
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 }
|
||||
|
||||
private var isOnScreen = false
|
||||
@@ -89,6 +87,7 @@ class HTSRepository @Inject constructor(
|
||||
|
||||
fun launch(device: ServerDevice) {
|
||||
_data.value = _data.value.copy(deviceName = device.name)
|
||||
logger = NordicBlekLogger(context, stringConst.APP_NAME, "HTS", device.address)
|
||||
serviceManager.startService(HTSService::class.java, device)
|
||||
}
|
||||
|
||||
@@ -109,13 +108,22 @@ class HTSRepository @Inject constructor(
|
||||
}
|
||||
|
||||
fun openLogger() {
|
||||
_loggerEvent.tryEmit(OpenLoggerEvent())
|
||||
logger?.launch()
|
||||
}
|
||||
|
||||
fun log(priority: Int, message: String) {
|
||||
logger?.log(priority, message)
|
||||
}
|
||||
|
||||
fun disconnect() {
|
||||
_stopEvent.tryEmit(DisconnectAndStopEvent())
|
||||
}
|
||||
|
||||
fun onMissingServices() {
|
||||
_data.value = _data.value.copy(missingServices = true)
|
||||
_stopEvent.tryEmit(DisconnectAndStopEvent())
|
||||
}
|
||||
|
||||
private fun clean() {
|
||||
logger = null
|
||||
_data.value = HTSServiceData()
|
||||
|
||||
@@ -41,7 +41,6 @@ import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.mapNotNull
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
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.connect
|
||||
import no.nordicsemi.android.kotlin.ble.client.main.service.BleGattServices
|
||||
@@ -52,7 +51,6 @@ import no.nordicsemi.android.kotlin.ble.profile.battery.BatteryLevelParser
|
||||
import no.nordicsemi.android.kotlin.ble.profile.hts.HTSDataParser
|
||||
import no.nordicsemi.android.service.DEVICE_DATA
|
||||
import no.nordicsemi.android.service.NotificationService
|
||||
import no.nordicsemi.android.ui.view.StringConst
|
||||
import java.util.*
|
||||
import javax.inject.Inject
|
||||
|
||||
@@ -69,9 +67,6 @@ internal class HTSService : NotificationService() {
|
||||
@Inject
|
||||
lateinit var repository: HTSRepository
|
||||
|
||||
@Inject
|
||||
lateinit var stringConst: StringConst
|
||||
|
||||
private lateinit var client: BleGattClient
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
@@ -91,16 +86,10 @@ internal class HTSService : NotificationService() {
|
||||
}
|
||||
|
||||
private fun startGattClient(device: ServerDevice) = lifecycleScope.launch {
|
||||
val logger = NordicBlekLogger(this@HTSService, stringConst.APP_NAME, "HTS", device.address)
|
||||
|
||||
client = device.connect(this@HTSService, logger = logger)
|
||||
client = device.connect(this@HTSService, logger = { p, s -> repository.log(p, s) })
|
||||
|
||||
client.waitForBonding()
|
||||
|
||||
repository.loggerEvent
|
||||
.onEach { logger.launch() }
|
||||
.launchIn(lifecycleScope)
|
||||
|
||||
client.connectionStateWithStatus
|
||||
.onEach { repository.onConnectionStateChanged(it) }
|
||||
.filterNotNull()
|
||||
@@ -114,7 +103,7 @@ internal class HTSService : NotificationService() {
|
||||
client.discoverServices()
|
||||
.filterNotNull()
|
||||
.onEach { configureGatt(it) }
|
||||
.catch { it.printStackTrace() }
|
||||
.catch { repository.onMissingServices() }
|
||||
.launchIn(lifecycleScope)
|
||||
}
|
||||
|
||||
|
||||
@@ -80,7 +80,7 @@ fun HTSScreen() {
|
||||
null,
|
||||
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
|
||||
GattConnectionState.STATE_DISCONNECTED,
|
||||
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(state.connectionState.status) {
|
||||
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(state.disconnectStatus) {
|
||||
NavigateUpButton(navigateUp)
|
||||
}
|
||||
GattConnectionState.STATE_CONNECTED -> HTSContentView(state) { viewModel.onEvent(it) }
|
||||
|
||||
@@ -11,8 +11,15 @@ data class PRXServiceData(
|
||||
val connectionState: GattConnectionStateWithStatus? = null,
|
||||
val connectionStatus: BleGattConnectionStatus? = null,
|
||||
val isRemoteAlarm: Boolean = false,
|
||||
val deviceName: String? = null
|
||||
val deviceName: String? = null,
|
||||
val missingServices: Boolean = false
|
||||
) {
|
||||
|
||||
val disconnectStatus = if (missingServices) {
|
||||
BleGattConnectionStatus.NOT_SUPPORTED
|
||||
} else {
|
||||
connectionState?.status ?: BleGattConnectionStatus.UNKNOWN
|
||||
}
|
||||
|
||||
val isLinkLossDisconnected = connectionStatus?.isLinkLoss ?: false
|
||||
}
|
||||
|
||||
@@ -121,6 +121,11 @@ class PRXRepository @Inject internal constructor(
|
||||
_loggerEvent.tryEmit(OpenLoggerEvent())
|
||||
}
|
||||
|
||||
fun onMissingServices() {
|
||||
_data.value = _data.value.copy(missingServices = true)
|
||||
_stopEvent.tryEmit(DisconnectAndStopEvent())
|
||||
}
|
||||
|
||||
fun disconnect() {
|
||||
_remoteAlarmLevel.tryEmit(AlarmLevel.NONE)
|
||||
_stopEvent.tryEmit(DisconnectAndStopEvent())
|
||||
|
||||
@@ -90,7 +90,7 @@ internal class PRXService : NotificationService() {
|
||||
private lateinit var client: BleGattClient
|
||||
private lateinit var server: BleGattServer
|
||||
|
||||
private lateinit var alertLevelCharacteristic: BleGattCharacteristic
|
||||
private var alertLevelCharacteristic: BleGattCharacteristic? = null
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
super.onStartCommand(intent, flags, startId)
|
||||
@@ -185,7 +185,7 @@ internal class PRXService : NotificationService() {
|
||||
client.discoverServices()
|
||||
.filterNotNull()
|
||||
.onEach { configureGatt(it) }
|
||||
.catch { it.printStackTrace() }
|
||||
.catch { repository.onMissingServices() }
|
||||
.launchIn(lifecycleScope)
|
||||
|
||||
repository.remoteAlarmLevel
|
||||
@@ -212,9 +212,11 @@ internal class PRXService : NotificationService() {
|
||||
|
||||
private suspend fun writeAlertLevel(alarmLevel: AlarmLevel) {
|
||||
try {
|
||||
alertLevelCharacteristic.write(AlertLevelInputParser.parse(alarmLevel), BleWriteType.NO_RESPONSE)
|
||||
repository.onRemoteAlarmLevelSet(alarmLevel)
|
||||
} catch (e: GattOperationException) {
|
||||
alertLevelCharacteristic?.run {
|
||||
write(AlertLevelInputParser.parse(alarmLevel), BleWriteType.NO_RESPONSE)
|
||||
repository.onRemoteAlarmLevelSet(alarmLevel)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ fun PRXScreen() {
|
||||
null,
|
||||
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
|
||||
GattConnectionState.STATE_DISCONNECTED,
|
||||
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(state.connectionState.status) {
|
||||
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(state.disconnectStatus) {
|
||||
NavigateUpButton(navigateUp)
|
||||
}
|
||||
GattConnectionState.STATE_CONNECTED -> ContentView(state) { viewModel.onEvent(it) }
|
||||
|
||||
@@ -33,6 +33,7 @@ package no.nordicsemi.android.rscs.data
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import no.nordicsemi.android.kotlin.ble.core.data.BleGattConnectionStatus
|
||||
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionStateWithStatus
|
||||
import no.nordicsemi.android.kotlin.ble.profile.rscs.data.RSCSData
|
||||
import no.nordicsemi.android.rscs.R
|
||||
@@ -41,8 +42,16 @@ internal data class RSCSServiceData(
|
||||
val data: RSCSData = RSCSData(),
|
||||
val batteryLevel: Int? = null,
|
||||
val connectionState: GattConnectionStateWithStatus? = null,
|
||||
val deviceName: String? = null
|
||||
val deviceName: String? = null,
|
||||
val missingServices: Boolean = false
|
||||
) {
|
||||
|
||||
val disconnectStatus = if (missingServices) {
|
||||
BleGattConnectionStatus.NOT_SUPPORTED
|
||||
} else {
|
||||
connectionState?.status ?: BleGattConnectionStatus.UNKNOWN
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun displayActivity(): String {
|
||||
return if (data.running) {
|
||||
|
||||
@@ -103,6 +103,11 @@ class RSCSRepository @Inject constructor(
|
||||
_data.value = _data.value.copy(batteryLevel = batteryLevel)
|
||||
}
|
||||
|
||||
fun onMissingServices() {
|
||||
_data.value = _data.value.copy(missingServices = true)
|
||||
_stopEvent.tryEmit(DisconnectAndStopEvent())
|
||||
}
|
||||
|
||||
fun openLogger() {
|
||||
_loggerEvent.tryEmit(OpenLoggerEvent())
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ internal class RSCSService : NotificationService() {
|
||||
client.discoverServices()
|
||||
.filterNotNull()
|
||||
.onEach { configureGatt(it) }
|
||||
.catch { it.printStackTrace() }
|
||||
.catch { repository.onMissingServices() }
|
||||
.launchIn(lifecycleScope)
|
||||
}
|
||||
|
||||
|
||||
@@ -80,7 +80,7 @@ fun RSCSScreen() {
|
||||
null,
|
||||
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
|
||||
GattConnectionState.STATE_DISCONNECTED,
|
||||
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(state.connectionState.status) {
|
||||
GattConnectionState.STATE_DISCONNECTING -> DeviceDisconnectedView(state.disconnectStatus) {
|
||||
NavigateUpButton(navigateUp)
|
||||
}
|
||||
GattConnectionState.STATE_CONNECTED -> RSCSContentView(state) { viewModel.onEvent(it) }
|
||||
|
||||
@@ -65,6 +65,8 @@ internal class RSCSViewModel @Inject constructor(
|
||||
val state = repository.data
|
||||
|
||||
init {
|
||||
repository.setOnScreen(true)
|
||||
|
||||
viewModelScope.launch {
|
||||
if (repository.isRunning.firstOrNull() == false) {
|
||||
requestBluetoothDevice()
|
||||
@@ -109,4 +111,9 @@ internal class RSCSViewModel @Inject constructor(
|
||||
repository.disconnect()
|
||||
navigationManager.navigateUp()
|
||||
}
|
||||
|
||||
override fun onCleared() {
|
||||
super.onCleared()
|
||||
repository.setOnScreen(false)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,15 +31,23 @@
|
||||
|
||||
package no.nordicsemi.android.uart.data
|
||||
|
||||
import no.nordicsemi.android.kotlin.ble.core.data.BleGattConnectionStatus
|
||||
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionStateWithStatus
|
||||
|
||||
internal data class UARTServiceData(
|
||||
val messages: List<UARTRecord> = emptyList(),
|
||||
val connectionState: GattConnectionStateWithStatus? = null,
|
||||
val batteryLevel: Int? = null,
|
||||
val deviceName: String? = null
|
||||
val deviceName: String? = null,
|
||||
val missingServices: Boolean = false
|
||||
) {
|
||||
|
||||
val disconnectStatus = if (missingServices) {
|
||||
BleGattConnectionStatus.NOT_SUPPORTED
|
||||
} else {
|
||||
connectionState?.status ?: BleGattConnectionStatus.UNKNOWN
|
||||
}
|
||||
|
||||
val displayMessages = messages
|
||||
}
|
||||
|
||||
|
||||
@@ -134,6 +134,11 @@ class UARTRepository @Inject internal constructor(
|
||||
_loggerEvent.tryEmit(OpenLoggerEvent())
|
||||
}
|
||||
|
||||
fun onMissingServices() {
|
||||
_data.value = _data.value.copy(missingServices = true)
|
||||
_stopEvent.tryEmit(DisconnectAndStopEvent())
|
||||
}
|
||||
|
||||
suspend fun saveConfigurationName(name: String) {
|
||||
configurationDataSource.saveConfigurationName(name)
|
||||
}
|
||||
|
||||
@@ -49,8 +49,6 @@ import no.nordicsemi.android.kotlin.ble.client.main.service.BleGattServices
|
||||
import no.nordicsemi.android.kotlin.ble.core.ServerDevice
|
||||
import no.nordicsemi.android.kotlin.ble.core.data.BleGattProperty
|
||||
import no.nordicsemi.android.kotlin.ble.core.data.BleWriteType
|
||||
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.Mtu
|
||||
import no.nordicsemi.android.kotlin.ble.profile.battery.BatteryLevelParser
|
||||
import no.nordicsemi.android.service.DEVICE_DATA
|
||||
@@ -108,7 +106,6 @@ internal class UARTService : NotificationService() {
|
||||
client.connectionStateWithStatus
|
||||
.onEach { repository.onConnectionStateChanged(it) }
|
||||
.filterNotNull()
|
||||
.onEach { stopIfDisconnected(it) }
|
||||
.launchIn(lifecycleScope)
|
||||
|
||||
if (!client.isConnected) {
|
||||
@@ -118,7 +115,7 @@ internal class UARTService : NotificationService() {
|
||||
client.discoverServices()
|
||||
.filterNotNull()
|
||||
.onEach { configureGatt(it, logger) }
|
||||
.catch { it.printStackTrace() }
|
||||
.catch { repository.onMissingServices() }
|
||||
.launchIn(lifecycleScope)
|
||||
}
|
||||
|
||||
@@ -156,12 +153,6 @@ internal class UARTService : NotificationService() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun stopIfDisconnected(connectionState: GattConnectionStateWithStatus) {
|
||||
if (connectionState.state == GattConnectionState.STATE_DISCONNECTED) {
|
||||
stopSelf()
|
||||
}
|
||||
}
|
||||
|
||||
private fun disconnect() {
|
||||
client.disconnect()
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ fun UARTScreen() {
|
||||
GattConnectionState.STATE_CONNECTING -> PaddingBox { DeviceConnectingView { NavigateUpButton(navigateUp) } }
|
||||
GattConnectionState.STATE_DISCONNECTED,
|
||||
GattConnectionState.STATE_DISCONNECTING -> PaddingBox {
|
||||
DeviceDisconnectedView(Reason.UNKNOWN) { NavigateUpButton(navigateUp) }
|
||||
DeviceDisconnectedView(state.uartManagerState.disconnectStatus) { NavigateUpButton(navigateUp) }
|
||||
}
|
||||
GattConnectionState.STATE_CONNECTED -> SuccessScreen()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user