mirror of
https://github.com/aljazceru/Android-nRF-Toolbox.git
synced 2025-12-21 08:24:22 +01:00
Properly state handling in profiles
This commit is contained in:
@@ -77,7 +77,9 @@ fun BPSScreen() {
|
||||
.padding(16.dp)
|
||||
.verticalScroll(rememberScrollState())
|
||||
) {
|
||||
when (state.result.connectionState?.state) {
|
||||
if (state.deviceName == null) {
|
||||
DeviceConnectingView { NavigateUpButton(navigateUp) }
|
||||
} else when (state.result.connectionState?.state) {
|
||||
null,
|
||||
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
|
||||
GattConnectionState.STATE_DISCONNECTED,
|
||||
|
||||
@@ -130,6 +130,11 @@ internal class BPSViewModel @Inject constructor(
|
||||
.onEach { logAnalytics(it.state) }
|
||||
.launchIn(viewModelScope)
|
||||
|
||||
if (!client.isConnected) {
|
||||
_state.value = _state.value.copy(deviceName = device.name)
|
||||
return@launch
|
||||
}
|
||||
|
||||
client.discoverServices()
|
||||
.filterNotNull()
|
||||
.onEach { configureGatt(it) }
|
||||
|
||||
@@ -75,7 +75,6 @@ class CGMRepository @Inject constructor(
|
||||
val highestSequenceNumber = data.value.records.maxOfOrNull { it.sequenceNumber } ?: -1
|
||||
|
||||
fun launch(device: ServerDevice) {
|
||||
_data.value = _data.value.copy(deviceName = device.name)
|
||||
serviceManager.startService(CGMService::class.java, device)
|
||||
}
|
||||
|
||||
@@ -88,7 +87,6 @@ class CGMRepository @Inject constructor(
|
||||
}
|
||||
|
||||
fun onConnectionStateChanged(connectionState: GattConnectionStateWithStatus?) {
|
||||
Log.i("AAATESTAAA", "Connection state: $connectionState")
|
||||
_data.value = _data.value.copy(connectionState = connectionState)
|
||||
}
|
||||
|
||||
@@ -104,6 +102,10 @@ class CGMRepository @Inject constructor(
|
||||
_loggerEvent.tryEmit(OpenLoggerEvent())
|
||||
}
|
||||
|
||||
fun onInitComplete(device: ServerDevice) {
|
||||
_data.value = _data.value.copy(deviceName = device.name)
|
||||
}
|
||||
|
||||
fun clear() {
|
||||
_data.value = _data.value.copy(records = emptyList())
|
||||
}
|
||||
|
||||
@@ -146,13 +146,18 @@ internal class CGMService : NotificationService() {
|
||||
.onEach { stopIfDisconnected(it) }
|
||||
.launchIn(lifecycleScope)
|
||||
|
||||
if (!client.isConnected) {
|
||||
repository.onInitComplete(device)
|
||||
return@launch
|
||||
}
|
||||
|
||||
client.discoverServices()
|
||||
.filterNotNull()
|
||||
.onEach { configureGatt(it) }
|
||||
.onEach { configureGatt(it, device) }
|
||||
.launchIn(lifecycleScope)
|
||||
}
|
||||
|
||||
private suspend fun configureGatt(services: BleGattServices) {
|
||||
private suspend fun configureGatt(services: BleGattServices, device: ServerDevice) {
|
||||
val cgmService = services.findService(CGMS_SERVICE_UUID)!!
|
||||
val statusCharacteristic = cgmService.findCharacteristic(CGM_STATUS_UUID)!!
|
||||
val featureCharacteristic = cgmService.findCharacteristic(CGM_FEATURE_UUID)!!
|
||||
@@ -223,6 +228,8 @@ internal class CGMService : NotificationService() {
|
||||
if (sessionStartTime == 0L) {
|
||||
opsControlPointCharacteristic.write(CGMSpecificOpsControlPointData.startSession(secured).value!!)
|
||||
}
|
||||
|
||||
repository.onInitComplete(device)
|
||||
}
|
||||
|
||||
private fun onAccessControlPointDataReceived(data: RecordAccessControlPointData) = lifecycleScope.launch {
|
||||
|
||||
@@ -81,7 +81,9 @@ fun CGMScreen() {
|
||||
.padding(16.dp)
|
||||
.verticalScroll(rememberScrollState())
|
||||
) {
|
||||
when (state.connectionState?.state) {
|
||||
if (state.deviceName == null) {
|
||||
DeviceConnectingView { NavigateUpButton(navigateUp) }
|
||||
} else when (state.connectionState?.state) {
|
||||
null,
|
||||
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
|
||||
|
||||
|
||||
@@ -102,6 +102,11 @@ internal class CSCService : NotificationService() {
|
||||
.onEach { stopIfDisconnected(it) }
|
||||
.launchIn(lifecycleScope)
|
||||
|
||||
if (!client.isConnected) {
|
||||
repository.onInitComplete(device)
|
||||
return@launch
|
||||
}
|
||||
|
||||
client.discoverServices()
|
||||
.filterNotNull()
|
||||
.onEach { configureGatt(it, device) }
|
||||
|
||||
@@ -77,7 +77,9 @@ fun CSCScreen() {
|
||||
.padding(16.dp)
|
||||
.verticalScroll(rememberScrollState())
|
||||
) {
|
||||
when (state.connectionState?.state) {
|
||||
if (state.deviceName == null) {
|
||||
DeviceConnectingView { NavigateUpButton(navigateUp) }
|
||||
} else when (state.connectionState?.state) {
|
||||
null,
|
||||
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
|
||||
GattConnectionState.STATE_DISCONNECTED,
|
||||
|
||||
@@ -77,7 +77,9 @@ fun GLSScreen() {
|
||||
.padding(16.dp)
|
||||
.verticalScroll(rememberScrollState())
|
||||
) {
|
||||
when (state.glsServiceData.connectionState?.state) {
|
||||
if (state.deviceName == null) {
|
||||
DeviceConnectingView { NavigateUpButton(navigateUp) }
|
||||
} else when (state.glsServiceData.connectionState?.state) {
|
||||
null,
|
||||
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
|
||||
GattConnectionState.STATE_DISCONNECTED,
|
||||
|
||||
@@ -158,7 +158,7 @@ internal class GLSViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
private fun startGattClient(device: ServerDevice) = viewModelScope.launch {
|
||||
logger = NordicBlekLogger(context, stringConst.APP_NAME, "BPS", device.address)
|
||||
logger = NordicBlekLogger(context, stringConst.APP_NAME, "GLS", device.address)
|
||||
|
||||
client = device.connect(context, logger = logger)
|
||||
|
||||
@@ -168,6 +168,11 @@ internal class GLSViewModel @Inject constructor(
|
||||
.onEach { logAnalytics(it) }
|
||||
.launchIn(viewModelScope)
|
||||
|
||||
if (!client.isConnected) {
|
||||
_state.value = _state.value.copy(deviceName = device.name)
|
||||
return@launch
|
||||
}
|
||||
|
||||
client.discoverServices()
|
||||
.filterNotNull()
|
||||
.onEach { configureGatt(it, device) }
|
||||
|
||||
@@ -104,6 +104,11 @@ internal class HRSService : NotificationService() {
|
||||
.onEach { stopIfDisconnected(it) }
|
||||
.launchIn(lifecycleScope)
|
||||
|
||||
if (!client.isConnected) {
|
||||
repository.onInitComplete(device)
|
||||
return@launch
|
||||
}
|
||||
|
||||
client.discoverServices()
|
||||
.filterNotNull()
|
||||
.onEach { configureGatt(it, device) }
|
||||
|
||||
@@ -77,7 +77,9 @@ fun HRSScreen() {
|
||||
.padding(16.dp)
|
||||
.verticalScroll(rememberScrollState())
|
||||
) {
|
||||
when (state.connectionState?.state) {
|
||||
if (state.deviceName == null) {
|
||||
DeviceConnectingView { NavigateUpButton(navigateUp) }
|
||||
} else when (state.connectionState?.state) {
|
||||
null,
|
||||
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
|
||||
GattConnectionState.STATE_DISCONNECTED,
|
||||
|
||||
@@ -102,6 +102,11 @@ internal class HTSService : NotificationService() {
|
||||
.onEach { stopIfDisconnected(it) }
|
||||
.launchIn(lifecycleScope)
|
||||
|
||||
if (!client.isConnected) {
|
||||
repository.onInitComplete(device)
|
||||
return@launch
|
||||
}
|
||||
|
||||
client.discoverServices()
|
||||
.filterNotNull()
|
||||
.onEach { configureGatt(it, device) }
|
||||
|
||||
@@ -81,7 +81,9 @@ fun HTSScreen() {
|
||||
.padding(16.dp)
|
||||
.verticalScroll(rememberScrollState())
|
||||
) {
|
||||
when (state.connectionState?.state) {
|
||||
if (state.deviceName == null) {
|
||||
DeviceConnectingView { NavigateUpButton(navigateUp) }
|
||||
} else when (state.connectionState?.state) {
|
||||
null,
|
||||
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
|
||||
GattConnectionState.STATE_DISCONNECTED,
|
||||
|
||||
@@ -172,6 +172,11 @@ internal class PRXService : NotificationService() {
|
||||
.onEach { stopIfDisconnected(it.state, it.status) }
|
||||
.launchIn(lifecycleScope)
|
||||
|
||||
if (!client.isConnected) {
|
||||
repository.onInitComplete(device)
|
||||
return@launch
|
||||
}
|
||||
|
||||
client.discoverServices()
|
||||
.filterNotNull()
|
||||
.onEach { configureGatt(it, device) }
|
||||
|
||||
@@ -77,8 +77,9 @@ fun PRXScreen() {
|
||||
.padding(16.dp)
|
||||
.verticalScroll(rememberScrollState())
|
||||
) {
|
||||
Log.i("AAATESTAAA", "State: ${state.connectionState?.state}")
|
||||
when (state.connectionState?.state) {
|
||||
if (state.deviceName == null) {
|
||||
DeviceConnectingView { NavigateUpButton(navigateUp) }
|
||||
} else when (state.connectionState?.state) {
|
||||
null,
|
||||
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
|
||||
GattConnectionState.STATE_DISCONNECTED,
|
||||
|
||||
@@ -102,6 +102,11 @@ internal class RSCSService : NotificationService() {
|
||||
.onEach { stopIfDisconnected(it) }
|
||||
.launchIn(lifecycleScope)
|
||||
|
||||
if (!client.isConnected) {
|
||||
repository.onInitComplete(device)
|
||||
return@launch
|
||||
}
|
||||
|
||||
client.discoverServices()
|
||||
.filterNotNull()
|
||||
.onEach { configureGatt(it, device) }
|
||||
|
||||
@@ -77,7 +77,9 @@ fun RSCSScreen() {
|
||||
.padding(16.dp)
|
||||
.verticalScroll(rememberScrollState())
|
||||
) {
|
||||
when (state.connectionState?.state) {
|
||||
if (state.deviceName == null) {
|
||||
DeviceConnectingView { NavigateUpButton(navigateUp) }
|
||||
} else when (state.connectionState?.state) {
|
||||
null,
|
||||
GattConnectionState.STATE_CONNECTING -> DeviceConnectingView { NavigateUpButton(navigateUp) }
|
||||
GattConnectionState.STATE_DISCONNECTED,
|
||||
|
||||
@@ -97,8 +97,6 @@ internal class UARTService : NotificationService() {
|
||||
|
||||
client = device.connect(this@UARTService, logger = logger)
|
||||
|
||||
Log.d("AAATESTAAA","connect finish")
|
||||
|
||||
client.requestMtu(Mtu.max)
|
||||
|
||||
repository.loggerEvent
|
||||
@@ -111,6 +109,11 @@ internal class UARTService : NotificationService() {
|
||||
.onEach { stopIfDisconnected(it) }
|
||||
.launchIn(lifecycleScope)
|
||||
|
||||
if (!client.isConnected) {
|
||||
repository.onInitComplete(device)
|
||||
return@launch
|
||||
}
|
||||
|
||||
client.discoverServices()
|
||||
.filterNotNull()
|
||||
.onEach { configureGatt(it, device, logger) }
|
||||
@@ -140,8 +143,6 @@ internal class UARTService : NotificationService() {
|
||||
.onEach { logger.log(10, "Sent: $it") }
|
||||
.launchIn(lifecycleScope)
|
||||
|
||||
|
||||
|
||||
repository.onInitComplete(device)
|
||||
}
|
||||
|
||||
|
||||
@@ -35,8 +35,6 @@ import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.runtime.Composable
|
||||
@@ -47,7 +45,6 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import com.google.accompanist.pager.ExperimentalPagerApi
|
||||
import no.nordicsemi.android.common.theme.view.PagerView
|
||||
import no.nordicsemi.android.common.theme.view.PagerViewEntity
|
||||
import no.nordicsemi.android.common.theme.view.PagerViewItem
|
||||
@@ -57,8 +54,6 @@ import no.nordicsemi.android.common.ui.scanner.view.Reason
|
||||
import no.nordicsemi.android.kotlin.ble.core.data.GattConnectionState
|
||||
import no.nordicsemi.android.uart.R
|
||||
import no.nordicsemi.android.uart.viewmodel.UARTViewModel
|
||||
import no.nordicsemi.android.ui.view.BackIconAppBar
|
||||
import no.nordicsemi.android.ui.view.LoggerIconAppBar
|
||||
import no.nordicsemi.android.ui.view.NavigateUpButton
|
||||
import no.nordicsemi.android.ui.view.ProfileAppBar
|
||||
|
||||
@@ -85,7 +80,9 @@ fun UARTScreen() {
|
||||
Column(
|
||||
modifier = Modifier.padding(it)
|
||||
) {
|
||||
when (state.uartManagerState.connectionState?.state) {
|
||||
if (state.uartManagerState.deviceName == null) {
|
||||
DeviceConnectingView { NavigateUpButton(navigateUp) }
|
||||
} else when (state.uartManagerState.connectionState?.state) {
|
||||
null,
|
||||
GattConnectionState.STATE_CONNECTING -> PaddingBox { DeviceConnectingView { NavigateUpButton(navigateUp) } }
|
||||
GattConnectionState.STATE_DISCONNECTED,
|
||||
|
||||
Reference in New Issue
Block a user