Observer connection status after successfull service discovery

This commit is contained in:
Sylwester Zieliński
2023-08-06 10:51:22 +02:00
parent 5ec41b98b0
commit 4e3d76e674

View File

@@ -92,25 +92,25 @@ internal class UARTService : NotificationService() {
private fun startGattClient(device: ServerDevice) = lifecycleScope.launch {
client = ClientBleGatt.connect(this@UARTService, device, logger = { p, s -> repository.log(p, s) })
client.requestMtu(Mtu.max)
client.connectionStateWithStatus
.filterNotNull()
.onEach { repository.onConnectionStateChanged(it) }
.onEach { stopIfDisconnected(it.state, it.status) }
.filterNotNull()
.launchIn(lifecycleScope)
if (!client.isConnected) {
return@launch
}
client.requestMtu(Mtu.max)
try {
val services = client.discoverServices()
configureGatt(services)
} catch (e: Exception) {
repository.onMissingServices()
}
client.connectionStateWithStatus
.filterNotNull()
.onEach { repository.onConnectionStateChanged(it) }
.onEach { stopIfDisconnected(it.state, it.status) }
.filterNotNull()
.launchIn(lifecycleScope)
}
private suspend fun configureGatt(services: ClientBleGattServices) {