keep pending connections on errors

This commit is contained in:
callebtc
2025-07-22 14:43:14 +02:00
parent 632a33729f
commit b82d325deb
3 changed files with 6 additions and 14 deletions
@@ -244,17 +244,12 @@ class BluetoothConnectionTracker(
/**
* Clean up a specific device connection
*/
fun cleanupDeviceConnection(deviceAddress: String, cleanupPending: Boolean = true) {
fun cleanupDeviceConnection(deviceAddress: String) {
connectedDevices.remove(deviceAddress)?.let { deviceConn ->
subscribedDevices.removeAll { it.address == deviceAddress }
addressPeerMap.remove(deviceAddress)
}
if (!cleanupPending) {
Log.d(TAG, "Skipped cleanup of pending connection for $deviceAddress")
}
if (cleanupPending) {
pendingConnections.remove(deviceAddress)
}
pendingConnections.remove(deviceAddress)
Log.d(TAG, "Cleaned up device connection for $deviceAddress")
}
@@ -338,11 +338,8 @@ class BluetoothGattClientManager(
if (status == 147) {
Log.e(TAG, "Client: Connection establishment failed (status 147) for $deviceAddress")
}
// cleanup without removing the pending connection so we don't reattempt
connectionTracker.cleanupDeviceConnection(deviceAddress, cleanupPending = false)
} else {
Log.d(TAG, "Client: Cleanly disconnected from $deviceAddress")
// cleanup so we can reattempt connection later
connectionTracker.cleanupDeviceConnection(deviceAddress)
}
@@ -377,7 +374,7 @@ class BluetoothGattClientManager(
gatt.discoverServices()
} else {
Log.w(TAG, "MTU negotiation failed for $deviceAddress with status: $status. Disconnecting.")
connectionTracker.removePendingConnection(deviceAddress)
//connectionTracker.removePendingConnection(deviceAddress)
gatt.disconnect()
}
}
@@ -458,13 +455,13 @@ class BluetoothGattClientManager(
val gatt = device.connectGatt(context, false, gattCallback, BluetoothDevice.TRANSPORT_LE)
if (gatt == null) {
Log.e(TAG, "connectGatt returned null for $deviceAddress")
connectionTracker.removePendingConnection(deviceAddress)
// connectionTracker.removePendingConnection(deviceAddress)
} else {
Log.d(TAG, "Client: GATT connection initiated successfully for $deviceAddress")
}
} catch (e: Exception) {
Log.e(TAG, "Client: Exception connecting to $deviceAddress: ${e.message}")
connectionTracker.removePendingConnection(deviceAddress)
// connectionTracker.removePendingConnection(deviceAddress)
}
}
@@ -64,7 +64,7 @@ class BluetoothMeshService(private val context: Context) {
// Wire up PacketProcessor reference for recursive handling in MessageHandler
messageHandler.packetProcessor = packetProcessor
// startPeriodicDebugLogging()
startPeriodicDebugLogging()
}
/**