Apply fixes

This commit is contained in:
Sylwester Zielinski
2023-04-28 10:38:09 +02:00
parent 6c0acbfba9
commit 5145266bdc
10 changed files with 55 additions and 13 deletions

View File

@@ -123,6 +123,8 @@ internal class BPSViewModel @Inject constructor(
client = device.connect(context, logger = logger)
client.waitForBonding()
client.connectionStateWithStatus
.filterNotNull()
.onEach { onDataUpdate(it) }

View File

@@ -127,7 +127,7 @@ class CGMRepository @Inject constructor(
_stopEvent.tryEmit(DisconnectAndStopEvent())
}
fun clean() {
private fun clean() {
_data.value = CGMServiceData()
}
}

View File

@@ -72,6 +72,23 @@ class CSCRepository @Inject constructor(
val isRunning = data.map { it.connectionState?.state == GattConnectionState.STATE_CONNECTED }
private var isOnScreen = false
private var isServiceRunning = false
fun setOnScreen(isOnScreen: Boolean) {
this.isOnScreen = isOnScreen
if (shouldClean()) clean()
}
fun setServiceRunning(serviceRunning: Boolean) {
this.isServiceRunning = serviceRunning
if (shouldClean()) clean()
}
private fun shouldClean() = !isOnScreen && !isServiceRunning
fun launch(device: ServerDevice) {
_data.value = _data.value.copy(deviceName = device.name)
serviceManager.startService(CSCService::class.java, device)
@@ -102,7 +119,10 @@ class CSCRepository @Inject constructor(
}
fun disconnect() {
_data.value = CSCServiceData()
_stopEvent.tryEmit(DisconnectAndStopEvent())
}
private fun clean() {
_data.value = CSCServiceData()
}
}

View File

@@ -76,6 +76,8 @@ internal class CSCService : NotificationService() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId)
repository.setServiceRunning(true)
val device = intent!!.getParcelableExtra<ServerDevice>(DEVICE_DATA)!!
startGattClient(device)
@@ -139,4 +141,10 @@ internal class CSCService : NotificationService() {
private fun disconnect() {
client.disconnect()
}
override fun onDestroy() {
super.onDestroy()
repository.setServiceRunning(false)
}
}

View File

@@ -68,6 +68,8 @@ internal class CSCViewModel @Inject constructor(
val state = repository.data
init {
repository.setOnScreen(true)
viewModelScope.launch {
if (repository.isRunning.firstOrNull() == false) {
requestBluetoothDevice()
@@ -118,4 +120,9 @@ internal class CSCViewModel @Inject constructor(
repository.disconnect()
navigationManager.navigateUp()
}
override fun onCleared() {
super.onCleared()
repository.setOnScreen(false)
}
}

View File

@@ -157,6 +157,8 @@ internal class GLSViewModel @Inject constructor(
}
private fun startGattClient(device: ServerDevice) = viewModelScope.launch {
_state.value = _state.value.copy(deviceName = device.name)
logger = NordicBlekLogger(context, stringConst.APP_NAME, "GLS", device.address)
client = device.connect(context, logger = logger)
@@ -175,7 +177,7 @@ internal class GLSViewModel @Inject constructor(
client.discoverServices()
.filterNotNull()
.onEach { configureGatt(it, device) }
.onEach { configureGatt(it) }
.launchIn(viewModelScope)
}
@@ -185,7 +187,7 @@ internal class GLSViewModel @Inject constructor(
}
}
private suspend fun configureGatt(services: BleGattServices, device: ServerDevice) {
private suspend fun configureGatt(services: BleGattServices) {
val glsService = services.findService(GLS_SERVICE_UUID)!!
glucoseMeasurementCharacteristic = glsService.findCharacteristic(GM_CHARACTERISTIC)!!
recordAccessControlPointCharacteristic = glsService.findCharacteristic(RACP_CHARACTERISTIC)!!

View File

@@ -114,11 +114,11 @@ internal class HRSService : NotificationService() {
client.discoverServices()
.filterNotNull()
.onEach { configureGatt(it, device) }
.onEach { configureGatt(it) }
.launchIn(lifecycleScope)
}
private suspend fun configureGatt(services: BleGattServices, device: ServerDevice) {
private suspend fun configureGatt(services: BleGattServices) {
val hrsService = services.findService(HRS_SERVICE_UUID)!!
val hrsMeasurementCharacteristic = hrsService.findCharacteristic(HEART_RATE_MEASUREMENT_CHARACTERISTIC_UUID)!!
val bodySensorLocationCharacteristic = hrsService.findCharacteristic(BODY_SENSOR_LOCATION_CHARACTERISTIC_UUID)!!

View File

@@ -46,6 +46,7 @@ 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.BleGattServices
import no.nordicsemi.android.kotlin.ble.core.ServerDevice
import no.nordicsemi.android.kotlin.ble.core.data.BleGattConnectOptions
import no.nordicsemi.android.kotlin.ble.core.data.BleGattConnectionStatus
import no.nordicsemi.android.kotlin.ble.core.data.BleGattPermission
import no.nordicsemi.android.kotlin.ble.core.data.BleGattProperty
@@ -161,7 +162,9 @@ internal class PRXService : NotificationService() {
private fun startGattClient(device: ServerDevice) = lifecycleScope.launch {
val logger = NordicBlekLogger(this@PRXService, stringConst.APP_NAME, "PRX", device.address)
client = device.connect(this@PRXService, logger = logger)
client = device.connect(this@PRXService, logger = logger, options = BleGattConnectOptions(autoConnect = true))
client.waitForBonding()
repository.loggerEvent
.onEach { logger.launch() }
@@ -180,7 +183,7 @@ internal class PRXService : NotificationService() {
client.discoverServices()
.filterNotNull()
.onEach { configureGatt(it, device) }
.onEach { configureGatt(it) }
.launchIn(lifecycleScope)
repository.remoteAlarmLevel
@@ -188,7 +191,7 @@ internal class PRXService : NotificationService() {
.launchIn(lifecycleScope)
}
private suspend fun configureGatt(services: BleGattServices, device: ServerDevice) {
private suspend fun configureGatt(services: BleGattServices) {
val prxService = services.findService(PRX_SERVICE_UUID)!!
alertLevelCharacteristic = prxService.findCharacteristic(ALERT_LEVEL_CHARACTERISTIC_UUID)!!
val linkLossService = services.findService(LINK_LOSS_SERVICE_UUID)!!

View File

@@ -110,11 +110,11 @@ internal class RSCSService : NotificationService() {
client.discoverServices()
.filterNotNull()
.onEach { configureGatt(it, device) }
.onEach { configureGatt(it) }
.launchIn(lifecycleScope)
}
private suspend fun configureGatt(services: BleGattServices, device: ServerDevice) {
private suspend fun configureGatt(services: BleGattServices) {
val rscsService = services.findService(RSCS_SERVICE_UUID)!!
val rscsMeasurementCharacteristic = rscsService.findCharacteristic(RSC_MEASUREMENT_CHARACTERISTIC_UUID)!!
val batteryService = services.findService(BATTERY_SERVICE_UUID)!!

View File

@@ -116,11 +116,11 @@ internal class UARTService : NotificationService() {
client.discoverServices()
.filterNotNull()
.onEach { configureGatt(it, device, logger) }
.onEach { configureGatt(it, logger) }
.launchIn(lifecycleScope)
}
private suspend fun configureGatt(services: BleGattServices, device: ServerDevice, logger: NordicBlekLogger) {
private suspend fun configureGatt(services: BleGattServices, logger: NordicBlekLogger) {
val uartService = services.findService(UART_SERVICE_UUID)!!
val rxCharacteristic = uartService.findCharacteristic(UART_RX_CHARACTERISTIC_UUID)!!
val txCharacteristic = uartService.findCharacteristic(UART_TX_CHARACTERISTIC_UUID)!!