Prevent calling init after disconnect

This commit is contained in:
Sylwester Zielinski
2023-04-26 15:32:59 +02:00
parent a7ba6fb64c
commit 1155359dd5
9 changed files with 45 additions and 8 deletions

View File

@@ -104,6 +104,8 @@ internal class CGMService : NotificationService() {
private lateinit var recordAccessControlPointCharacteristic: BleGattCharacteristic private lateinit var recordAccessControlPointCharacteristic: BleGattCharacteristic
private var hasBeenInitialized: Boolean = false
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId) super.onStartCommand(intent, flags, startId)
@@ -148,6 +150,7 @@ internal class CGMService : NotificationService() {
.launchIn(lifecycleScope) .launchIn(lifecycleScope)
if (!client.isConnected) { if (!client.isConnected) {
hasBeenInitialized = true
repository.onInitComplete(device) repository.onInitComplete(device)
return@launch return@launch
} }
@@ -230,6 +233,7 @@ internal class CGMService : NotificationService() {
opsControlPointCharacteristic.write(CGMSpecificOpsControlPointData.startSession(secured).value!!) opsControlPointCharacteristic.write(CGMSpecificOpsControlPointData.startSession(secured).value!!)
} }
hasBeenInitialized = true
repository.onInitComplete(device) repository.onInitComplete(device)
} }
@@ -314,7 +318,7 @@ internal class CGMService : NotificationService() {
} }
private fun unlockUiIfDisconnected(connectionState: GattConnectionStateWithStatus, device: ServerDevice) { private fun unlockUiIfDisconnected(connectionState: GattConnectionStateWithStatus, device: ServerDevice) {
if (connectionState.state == GattConnectionState.STATE_DISCONNECTED) { if (connectionState.state == GattConnectionState.STATE_DISCONNECTED && !hasBeenInitialized) {
repository.onInitComplete(device) repository.onInitComplete(device)
} }
} }

View File

@@ -32,13 +32,13 @@
package no.nordicsemi.android.csc.repository package no.nordicsemi.android.csc.repository
import android.content.Context import android.content.Context
import android.util.Log
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow 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
@@ -78,7 +78,11 @@ class CSCRepository @Inject constructor(
} }
fun onInitComplete(device: ServerDevice) { fun onInitComplete(device: ServerDevice) {
_data.value = _data.value.copy(deviceName = device.name) Log.d("AAATESTAAA", "onInitComplete: ${data.value}")
if (_data.value.deviceName == null) {
Log.d("AAATESTAAA", "AAA")
_data.value = _data.value.copy(deviceName = device.name)
}
} }
internal fun setSpeedUnit(speedUnit: SpeedUnit) { internal fun setSpeedUnit(speedUnit: SpeedUnit) {
@@ -106,6 +110,7 @@ class CSCRepository @Inject constructor(
} }
fun release() { fun release() {
Log.d("AAATESTAAA", "release: ${data.value}")
_data.value = CSCServiceData() _data.value = CSCServiceData()
_stopEvent.tryEmit(DisconnectAndStopEvent()) _stopEvent.tryEmit(DisconnectAndStopEvent())
} }

View File

@@ -73,6 +73,8 @@ internal class CSCService : NotificationService() {
private lateinit var client: BleGattClient private lateinit var client: BleGattClient
private var hasBeenInitialized: Boolean = false
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId) super.onStartCommand(intent, flags, startId)
@@ -104,6 +106,7 @@ internal class CSCService : NotificationService() {
.launchIn(lifecycleScope) .launchIn(lifecycleScope)
if (!client.isConnected) { if (!client.isConnected) {
hasBeenInitialized = true
repository.onInitComplete(device) repository.onInitComplete(device)
return@launch return@launch
} }
@@ -131,6 +134,7 @@ internal class CSCService : NotificationService() {
.onEach { repository.onCSCDataChanged(it) } .onEach { repository.onCSCDataChanged(it) }
.launchIn(lifecycleScope) .launchIn(lifecycleScope)
hasBeenInitialized = true
repository.onInitComplete(device) repository.onInitComplete(device)
} }
@@ -141,7 +145,8 @@ internal class CSCService : NotificationService() {
} }
private fun unlockUiIfDisconnected(connectionState: GattConnectionStateWithStatus, device: ServerDevice) { private fun unlockUiIfDisconnected(connectionState: GattConnectionStateWithStatus, device: ServerDevice) {
if (connectionState.state == GattConnectionState.STATE_DISCONNECTED) { if (connectionState.state == GattConnectionState.STATE_DISCONNECTED && !hasBeenInitialized) {
hasBeenInitialized = true
repository.onInitComplete(device) repository.onInitComplete(device)
} }
} }

View File

@@ -31,6 +31,7 @@
package no.nordicsemi.android.csc.view package no.nordicsemi.android.csc.view
import android.util.Log
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.rememberScrollState
@@ -57,6 +58,8 @@ fun CSCScreen() {
val viewModel: CSCViewModel = hiltViewModel() val viewModel: CSCViewModel = hiltViewModel()
val state = viewModel.state.collectAsState().value val state = viewModel.state.collectAsState().value
Log.d("AAATESTAAA", "State: ${state}")
val navigateUp = { viewModel.onEvent(NavigateUp) } val navigateUp = { viewModel.onEvent(NavigateUp) }
Scaffold( Scaffold(

View File

@@ -75,6 +75,8 @@ internal class HRSService : NotificationService() {
private lateinit var client: BleGattClient private lateinit var client: BleGattClient
private var hasBeenInitialized: Boolean = false
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId) super.onStartCommand(intent, flags, startId)
@@ -106,6 +108,7 @@ internal class HRSService : NotificationService() {
.launchIn(lifecycleScope) .launchIn(lifecycleScope)
if (!client.isConnected) { if (!client.isConnected) {
hasBeenInitialized = true
repository.onInitComplete(device) repository.onInitComplete(device)
return@launch return@launch
} }
@@ -136,6 +139,7 @@ internal class HRSService : NotificationService() {
.onEach { repository.onHRSDataChanged(it) } .onEach { repository.onHRSDataChanged(it) }
.launchIn(lifecycleScope) .launchIn(lifecycleScope)
hasBeenInitialized = true
repository.onInitComplete(device) repository.onInitComplete(device)
} }
@@ -146,7 +150,7 @@ internal class HRSService : NotificationService() {
} }
private fun unlockUiIfDisconnected(connectionState: GattConnectionStateWithStatus, device: ServerDevice) { private fun unlockUiIfDisconnected(connectionState: GattConnectionStateWithStatus, device: ServerDevice) {
if (connectionState.state == GattConnectionState.STATE_DISCONNECTED) { if (connectionState.state == GattConnectionState.STATE_DISCONNECTED && !hasBeenInitialized) {
repository.onInitComplete(device) repository.onInitComplete(device)
} }
} }

View File

@@ -73,6 +73,8 @@ internal class HTSService : NotificationService() {
private lateinit var client: BleGattClient private lateinit var client: BleGattClient
private var hasBeenInitialized: Boolean = false
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId) super.onStartCommand(intent, flags, startId)
@@ -104,6 +106,7 @@ internal class HTSService : NotificationService() {
.launchIn(lifecycleScope) .launchIn(lifecycleScope)
if (!client.isConnected) { if (!client.isConnected) {
hasBeenInitialized = true
repository.onInitComplete(device) repository.onInitComplete(device)
return@launch return@launch
} }
@@ -130,6 +133,7 @@ internal class HTSService : NotificationService() {
.onEach { repository.onHTSDataChanged(it) } .onEach { repository.onHTSDataChanged(it) }
.launchIn(lifecycleScope) .launchIn(lifecycleScope)
hasBeenInitialized = true
repository.onInitComplete(device) repository.onInitComplete(device)
} }
@@ -140,7 +144,7 @@ internal class HTSService : NotificationService() {
} }
private fun unlockUiIfDisconnected(connectionState: GattConnectionStateWithStatus, device: ServerDevice) { private fun unlockUiIfDisconnected(connectionState: GattConnectionStateWithStatus, device: ServerDevice) {
if (connectionState.state == GattConnectionState.STATE_DISCONNECTED) { if (connectionState.state == GattConnectionState.STATE_DISCONNECTED && !hasBeenInitialized) {
repository.onInitComplete(device) repository.onInitComplete(device)
} }
} }

View File

@@ -89,6 +89,8 @@ internal class PRXService : NotificationService() {
private lateinit var alertLevelCharacteristic: BleGattCharacteristic private lateinit var alertLevelCharacteristic: BleGattCharacteristic
private var hasBeenInitialized: Boolean = false
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId) super.onStartCommand(intent, flags, startId)
@@ -173,6 +175,7 @@ internal class PRXService : NotificationService() {
.launchIn(lifecycleScope) .launchIn(lifecycleScope)
if (!client.isConnected) { if (!client.isConnected) {
hasBeenInitialized = true
repository.onInitComplete(device) repository.onInitComplete(device)
return@launch return@launch
} }
@@ -202,6 +205,7 @@ internal class PRXService : NotificationService() {
linkLossCharacteristic.write(AlertLevelInputParser.parse(AlarmLevel.HIGH)) linkLossCharacteristic.write(AlertLevelInputParser.parse(AlarmLevel.HIGH))
hasBeenInitialized = true
repository.onInitComplete(device) repository.onInitComplete(device)
} }

View File

@@ -73,6 +73,8 @@ internal class RSCSService : NotificationService() {
private lateinit var client: BleGattClient private lateinit var client: BleGattClient
private var hasBeenInitialized: Boolean = false
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId) super.onStartCommand(intent, flags, startId)
@@ -104,6 +106,7 @@ internal class RSCSService : NotificationService() {
.launchIn(lifecycleScope) .launchIn(lifecycleScope)
if (!client.isConnected) { if (!client.isConnected) {
hasBeenInitialized = true
repository.onInitComplete(device) repository.onInitComplete(device)
return@launch return@launch
} }
@@ -130,6 +133,7 @@ internal class RSCSService : NotificationService() {
.onEach { repository.onRSCSDataChanged(it) } .onEach { repository.onRSCSDataChanged(it) }
.launchIn(lifecycleScope) .launchIn(lifecycleScope)
hasBeenInitialized = true
repository.onInitComplete(device) repository.onInitComplete(device)
} }
@@ -140,7 +144,7 @@ internal class RSCSService : NotificationService() {
} }
private fun unlockUiIfDisconnected(connectionState: GattConnectionStateWithStatus, device: ServerDevice) { private fun unlockUiIfDisconnected(connectionState: GattConnectionStateWithStatus, device: ServerDevice) {
if (connectionState.state == GattConnectionState.STATE_DISCONNECTED) { if (connectionState.state == GattConnectionState.STATE_DISCONNECTED && !hasBeenInitialized) {
repository.onInitComplete(device) repository.onInitComplete(device)
} }
} }

View File

@@ -78,6 +78,8 @@ internal class UARTService : NotificationService() {
private lateinit var client: BleGattClient private lateinit var client: BleGattClient
private var hasBeenInitialized: Boolean = false
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId) super.onStartCommand(intent, flags, startId)
@@ -111,6 +113,7 @@ internal class UARTService : NotificationService() {
.launchIn(lifecycleScope) .launchIn(lifecycleScope)
if (!client.isConnected) { if (!client.isConnected) {
hasBeenInitialized = true
repository.onInitComplete(device) repository.onInitComplete(device)
return@launch return@launch
} }
@@ -144,6 +147,7 @@ internal class UARTService : NotificationService() {
.onEach { logger.log(10, "Sent: $it") } .onEach { logger.log(10, "Sent: $it") }
.launchIn(lifecycleScope) .launchIn(lifecycleScope)
hasBeenInitialized = true
repository.onInitComplete(device) repository.onInitComplete(device)
} }
@@ -162,7 +166,7 @@ internal class UARTService : NotificationService() {
} }
private fun unlockUiIfDisconnected(connectionState: GattConnectionStateWithStatus, device: ServerDevice) { private fun unlockUiIfDisconnected(connectionState: GattConnectionStateWithStatus, device: ServerDevice) {
if (connectionState.state == GattConnectionState.STATE_DISCONNECTED) { if (connectionState.state == GattConnectionState.STATE_DISCONNECTED && !hasBeenInitialized) {
repository.onInitComplete(device) repository.onInitComplete(device)
} }
} }