From 1155359dd5b807ad9c710985e3449e816f85ccb9 Mon Sep 17 00:00:00 2001 From: Sylwester Zielinski Date: Wed, 26 Apr 2023 15:32:59 +0200 Subject: [PATCH] Prevent calling init after disconnect --- .../no/nordicsemi/android/cgms/repository/CGMService.kt | 6 +++++- .../nordicsemi/android/csc/repository/CSCRepository.kt | 9 +++++++-- .../no/nordicsemi/android/csc/repository/CSCService.kt | 7 ++++++- .../java/no/nordicsemi/android/csc/view/CSCScreen.kt | 3 +++ .../java/no/nordicsemi/android/hrs/service/HRSService.kt | 6 +++++- .../no/nordicsemi/android/hts/repository/HTSService.kt | 6 +++++- .../no/nordicsemi/android/prx/repository/PRXService.kt | 4 ++++ .../no/nordicsemi/android/rscs/repository/RSCSService.kt | 6 +++++- .../no/nordicsemi/android/uart/repository/UARTService.kt | 6 +++++- 9 files changed, 45 insertions(+), 8 deletions(-) diff --git a/profile_cgms/src/main/java/no/nordicsemi/android/cgms/repository/CGMService.kt b/profile_cgms/src/main/java/no/nordicsemi/android/cgms/repository/CGMService.kt index 6f5658b5..e43cf74f 100644 --- a/profile_cgms/src/main/java/no/nordicsemi/android/cgms/repository/CGMService.kt +++ b/profile_cgms/src/main/java/no/nordicsemi/android/cgms/repository/CGMService.kt @@ -104,6 +104,8 @@ internal class CGMService : NotificationService() { private lateinit var recordAccessControlPointCharacteristic: BleGattCharacteristic + private var hasBeenInitialized: Boolean = false + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { super.onStartCommand(intent, flags, startId) @@ -148,6 +150,7 @@ internal class CGMService : NotificationService() { .launchIn(lifecycleScope) if (!client.isConnected) { + hasBeenInitialized = true repository.onInitComplete(device) return@launch } @@ -230,6 +233,7 @@ internal class CGMService : NotificationService() { opsControlPointCharacteristic.write(CGMSpecificOpsControlPointData.startSession(secured).value!!) } + hasBeenInitialized = true repository.onInitComplete(device) } @@ -314,7 +318,7 @@ internal class CGMService : NotificationService() { } private fun unlockUiIfDisconnected(connectionState: GattConnectionStateWithStatus, device: ServerDevice) { - if (connectionState.state == GattConnectionState.STATE_DISCONNECTED) { + if (connectionState.state == GattConnectionState.STATE_DISCONNECTED && !hasBeenInitialized) { repository.onInitComplete(device) } } diff --git a/profile_csc/src/main/java/no/nordicsemi/android/csc/repository/CSCRepository.kt b/profile_csc/src/main/java/no/nordicsemi/android/csc/repository/CSCRepository.kt index 0f009ec8..94544f15 100644 --- a/profile_csc/src/main/java/no/nordicsemi/android/csc/repository/CSCRepository.kt +++ b/profile_csc/src/main/java/no/nordicsemi/android/csc/repository/CSCRepository.kt @@ -32,13 +32,13 @@ package no.nordicsemi.android.csc.repository import android.content.Context +import android.util.Log import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.map 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.SpeedUnit import no.nordicsemi.android.kotlin.ble.core.ServerDevice @@ -78,7 +78,11 @@ class CSCRepository @Inject constructor( } 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) { @@ -106,6 +110,7 @@ class CSCRepository @Inject constructor( } fun release() { + Log.d("AAATESTAAA", "release: ${data.value}") _data.value = CSCServiceData() _stopEvent.tryEmit(DisconnectAndStopEvent()) } diff --git a/profile_csc/src/main/java/no/nordicsemi/android/csc/repository/CSCService.kt b/profile_csc/src/main/java/no/nordicsemi/android/csc/repository/CSCService.kt index 8cb71746..5e21dd1d 100644 --- a/profile_csc/src/main/java/no/nordicsemi/android/csc/repository/CSCService.kt +++ b/profile_csc/src/main/java/no/nordicsemi/android/csc/repository/CSCService.kt @@ -73,6 +73,8 @@ internal class CSCService : NotificationService() { private lateinit var client: BleGattClient + private var hasBeenInitialized: Boolean = false + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { super.onStartCommand(intent, flags, startId) @@ -104,6 +106,7 @@ internal class CSCService : NotificationService() { .launchIn(lifecycleScope) if (!client.isConnected) { + hasBeenInitialized = true repository.onInitComplete(device) return@launch } @@ -131,6 +134,7 @@ internal class CSCService : NotificationService() { .onEach { repository.onCSCDataChanged(it) } .launchIn(lifecycleScope) + hasBeenInitialized = true repository.onInitComplete(device) } @@ -141,7 +145,8 @@ internal class CSCService : NotificationService() { } 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) } } diff --git a/profile_csc/src/main/java/no/nordicsemi/android/csc/view/CSCScreen.kt b/profile_csc/src/main/java/no/nordicsemi/android/csc/view/CSCScreen.kt index e8967219..7c454235 100644 --- a/profile_csc/src/main/java/no/nordicsemi/android/csc/view/CSCScreen.kt +++ b/profile_csc/src/main/java/no/nordicsemi/android/csc/view/CSCScreen.kt @@ -31,6 +31,7 @@ package no.nordicsemi.android.csc.view +import android.util.Log import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.padding import androidx.compose.foundation.rememberScrollState @@ -57,6 +58,8 @@ fun CSCScreen() { val viewModel: CSCViewModel = hiltViewModel() val state = viewModel.state.collectAsState().value + Log.d("AAATESTAAA", "State: ${state}") + val navigateUp = { viewModel.onEvent(NavigateUp) } Scaffold( diff --git a/profile_hrs/src/main/java/no/nordicsemi/android/hrs/service/HRSService.kt b/profile_hrs/src/main/java/no/nordicsemi/android/hrs/service/HRSService.kt index f7331c13..2a6588e8 100644 --- a/profile_hrs/src/main/java/no/nordicsemi/android/hrs/service/HRSService.kt +++ b/profile_hrs/src/main/java/no/nordicsemi/android/hrs/service/HRSService.kt @@ -75,6 +75,8 @@ internal class HRSService : NotificationService() { private lateinit var client: BleGattClient + private var hasBeenInitialized: Boolean = false + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { super.onStartCommand(intent, flags, startId) @@ -106,6 +108,7 @@ internal class HRSService : NotificationService() { .launchIn(lifecycleScope) if (!client.isConnected) { + hasBeenInitialized = true repository.onInitComplete(device) return@launch } @@ -136,6 +139,7 @@ internal class HRSService : NotificationService() { .onEach { repository.onHRSDataChanged(it) } .launchIn(lifecycleScope) + hasBeenInitialized = true repository.onInitComplete(device) } @@ -146,7 +150,7 @@ internal class HRSService : NotificationService() { } private fun unlockUiIfDisconnected(connectionState: GattConnectionStateWithStatus, device: ServerDevice) { - if (connectionState.state == GattConnectionState.STATE_DISCONNECTED) { + if (connectionState.state == GattConnectionState.STATE_DISCONNECTED && !hasBeenInitialized) { repository.onInitComplete(device) } } diff --git a/profile_hts/src/main/java/no/nordicsemi/android/hts/repository/HTSService.kt b/profile_hts/src/main/java/no/nordicsemi/android/hts/repository/HTSService.kt index 01a4d47b..107aef99 100644 --- a/profile_hts/src/main/java/no/nordicsemi/android/hts/repository/HTSService.kt +++ b/profile_hts/src/main/java/no/nordicsemi/android/hts/repository/HTSService.kt @@ -73,6 +73,8 @@ internal class HTSService : NotificationService() { private lateinit var client: BleGattClient + private var hasBeenInitialized: Boolean = false + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { super.onStartCommand(intent, flags, startId) @@ -104,6 +106,7 @@ internal class HTSService : NotificationService() { .launchIn(lifecycleScope) if (!client.isConnected) { + hasBeenInitialized = true repository.onInitComplete(device) return@launch } @@ -130,6 +133,7 @@ internal class HTSService : NotificationService() { .onEach { repository.onHTSDataChanged(it) } .launchIn(lifecycleScope) + hasBeenInitialized = true repository.onInitComplete(device) } @@ -140,7 +144,7 @@ internal class HTSService : NotificationService() { } private fun unlockUiIfDisconnected(connectionState: GattConnectionStateWithStatus, device: ServerDevice) { - if (connectionState.state == GattConnectionState.STATE_DISCONNECTED) { + if (connectionState.state == GattConnectionState.STATE_DISCONNECTED && !hasBeenInitialized) { repository.onInitComplete(device) } } diff --git a/profile_prx/src/main/java/no/nordicsemi/android/prx/repository/PRXService.kt b/profile_prx/src/main/java/no/nordicsemi/android/prx/repository/PRXService.kt index 7d137bef..96f668d3 100644 --- a/profile_prx/src/main/java/no/nordicsemi/android/prx/repository/PRXService.kt +++ b/profile_prx/src/main/java/no/nordicsemi/android/prx/repository/PRXService.kt @@ -89,6 +89,8 @@ internal class PRXService : NotificationService() { private lateinit var alertLevelCharacteristic: BleGattCharacteristic + private var hasBeenInitialized: Boolean = false + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { super.onStartCommand(intent, flags, startId) @@ -173,6 +175,7 @@ internal class PRXService : NotificationService() { .launchIn(lifecycleScope) if (!client.isConnected) { + hasBeenInitialized = true repository.onInitComplete(device) return@launch } @@ -202,6 +205,7 @@ internal class PRXService : NotificationService() { linkLossCharacteristic.write(AlertLevelInputParser.parse(AlarmLevel.HIGH)) + hasBeenInitialized = true repository.onInitComplete(device) } diff --git a/profile_rscs/src/main/java/no/nordicsemi/android/rscs/repository/RSCSService.kt b/profile_rscs/src/main/java/no/nordicsemi/android/rscs/repository/RSCSService.kt index d0d26e12..0056160d 100644 --- a/profile_rscs/src/main/java/no/nordicsemi/android/rscs/repository/RSCSService.kt +++ b/profile_rscs/src/main/java/no/nordicsemi/android/rscs/repository/RSCSService.kt @@ -73,6 +73,8 @@ internal class RSCSService : NotificationService() { private lateinit var client: BleGattClient + private var hasBeenInitialized: Boolean = false + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { super.onStartCommand(intent, flags, startId) @@ -104,6 +106,7 @@ internal class RSCSService : NotificationService() { .launchIn(lifecycleScope) if (!client.isConnected) { + hasBeenInitialized = true repository.onInitComplete(device) return@launch } @@ -130,6 +133,7 @@ internal class RSCSService : NotificationService() { .onEach { repository.onRSCSDataChanged(it) } .launchIn(lifecycleScope) + hasBeenInitialized = true repository.onInitComplete(device) } @@ -140,7 +144,7 @@ internal class RSCSService : NotificationService() { } private fun unlockUiIfDisconnected(connectionState: GattConnectionStateWithStatus, device: ServerDevice) { - if (connectionState.state == GattConnectionState.STATE_DISCONNECTED) { + if (connectionState.state == GattConnectionState.STATE_DISCONNECTED && !hasBeenInitialized) { repository.onInitComplete(device) } } diff --git a/profile_uart/src/main/java/no/nordicsemi/android/uart/repository/UARTService.kt b/profile_uart/src/main/java/no/nordicsemi/android/uart/repository/UARTService.kt index 50b60814..ae63213c 100644 --- a/profile_uart/src/main/java/no/nordicsemi/android/uart/repository/UARTService.kt +++ b/profile_uart/src/main/java/no/nordicsemi/android/uart/repository/UARTService.kt @@ -78,6 +78,8 @@ internal class UARTService : NotificationService() { private lateinit var client: BleGattClient + private var hasBeenInitialized: Boolean = false + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { super.onStartCommand(intent, flags, startId) @@ -111,6 +113,7 @@ internal class UARTService : NotificationService() { .launchIn(lifecycleScope) if (!client.isConnected) { + hasBeenInitialized = true repository.onInitComplete(device) return@launch } @@ -144,6 +147,7 @@ internal class UARTService : NotificationService() { .onEach { logger.log(10, "Sent: $it") } .launchIn(lifecycleScope) + hasBeenInitialized = true repository.onInitComplete(device) } @@ -162,7 +166,7 @@ internal class UARTService : NotificationService() { } private fun unlockUiIfDisconnected(connectionState: GattConnectionStateWithStatus, device: ServerDevice) { - if (connectionState.state == GattConnectionState.STATE_DISCONNECTED) { + if (connectionState.state == GattConnectionState.STATE_DISCONNECTED && !hasBeenInitialized) { repository.onInitComplete(device) } }