avoid duplicate conenctions to same peer, may cause infinite reconnections, not tested yet

This commit is contained in:
callebtc
2025-10-30 13:50:13 +01:00
parent 6bb3554774
commit 2fdf5d2205
3 changed files with 53 additions and 10 deletions
@@ -302,7 +302,23 @@ class BluetoothConnectionManager(
* Public: connect/disconnect helpers for debug UI
*/
fun connectToAddress(address: String): Boolean = clientManager.connectToAddress(address)
fun disconnectAddress(address: String) { connectionTracker.disconnectDevice(address) }
fun disconnectAddress(address: String) {
connectionScope.launch {
try {
val dc = connectionTracker.getDeviceConnection(address)
if (dc != null) {
if (dc.gatt != null) {
try { dc.gatt.disconnect() } catch (_: Exception) { }
} else {
// Try canceling server-side connection if present
try { serverManager.getGattServer()?.cancelConnection(dc.device) } catch (_: Exception) { }
}
}
} catch (_: Exception) { }
// Cleanup tracking regardless
connectionTracker.cleanupDeviceConnection(address)
}
}
// Optionally disconnect all connections (server and client)