Fixed reconnection

This commit is contained in:
hiar
2025-08-27 15:50:56 +02:00
committed by Himali Aryal
parent 9aadf84e60
commit 0b39290019

View File

@@ -12,7 +12,6 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@@ -135,17 +134,13 @@ internal class ProfileService : NotificationService() {
StateReason(reason as ConnectionState.Disconnected.Reason) StateReason(reason as ConnectionState.Disconnected.Reason)
) )
_devices.update { it - peripheral.address } _devices.update { it - peripheral.address }
handleDisconnection(peripheral.address)
} }
else -> { else -> {
// Handle connecting/disconnecting states if needed // Handle connecting/disconnecting states if needed
} }
} }
}.onCompletion {
handleDisconnection(peripheral.address)
managedConnections[peripheral.address]?.cancel()
managedConnections.remove(peripheral.address)
stopServiceIfNoDevices()
}.launchIn(this) }.launchIn(this)
} }
@@ -215,6 +210,9 @@ internal class ProfileService : NotificationService() {
private fun handleDisconnection(address: String) { private fun handleDisconnection(address: String) {
_devices.update { it - address } _devices.update { it - address }
_isMissingServices.update { it - address } _isMissingServices.update { it - address }
managedConnections[address]?.cancel()
managedConnections.remove(address)
stopServiceIfNoDevices()
} }
private fun stopServiceIfNoDevices() { private fun stopServiceIfNoDevices() {