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 * Clean up a specific device connection
*/ */
fun cleanupDeviceConnection(deviceAddress: String, cleanupPending: Boolean = true) { fun cleanupDeviceConnection(deviceAddress: String) {
connectedDevices.remove(deviceAddress)?.let { deviceConn -> connectedDevices.remove(deviceAddress)?.let { deviceConn ->
subscribedDevices.removeAll { it.address == deviceAddress } subscribedDevices.removeAll { it.address == deviceAddress }
addressPeerMap.remove(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") Log.d(TAG, "Cleaned up device connection for $deviceAddress")
} }
@@ -338,11 +338,8 @@ class BluetoothGattClientManager(
if (status == 147) { if (status == 147) {
Log.e(TAG, "Client: Connection establishment failed (status 147) for $deviceAddress") 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 { } else {
Log.d(TAG, "Client: Cleanly disconnected from $deviceAddress") Log.d(TAG, "Client: Cleanly disconnected from $deviceAddress")
// cleanup so we can reattempt connection later
connectionTracker.cleanupDeviceConnection(deviceAddress) connectionTracker.cleanupDeviceConnection(deviceAddress)
} }
@@ -377,7 +374,7 @@ class BluetoothGattClientManager(
gatt.discoverServices() gatt.discoverServices()
} else { } else {
Log.w(TAG, "MTU negotiation failed for $deviceAddress with status: $status. Disconnecting.") Log.w(TAG, "MTU negotiation failed for $deviceAddress with status: $status. Disconnecting.")
connectionTracker.removePendingConnection(deviceAddress) //connectionTracker.removePendingConnection(deviceAddress)
gatt.disconnect() gatt.disconnect()
} }
} }
@@ -458,13 +455,13 @@ class BluetoothGattClientManager(
val gatt = device.connectGatt(context, false, gattCallback, BluetoothDevice.TRANSPORT_LE) val gatt = device.connectGatt(context, false, gattCallback, BluetoothDevice.TRANSPORT_LE)
if (gatt == null) { if (gatt == null) {
Log.e(TAG, "connectGatt returned null for $deviceAddress") Log.e(TAG, "connectGatt returned null for $deviceAddress")
connectionTracker.removePendingConnection(deviceAddress) // connectionTracker.removePendingConnection(deviceAddress)
} else { } else {
Log.d(TAG, "Client: GATT connection initiated successfully for $deviceAddress") Log.d(TAG, "Client: GATT connection initiated successfully for $deviceAddress")
} }
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "Client: Exception connecting to $deviceAddress: ${e.message}") 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 // Wire up PacketProcessor reference for recursive handling in MessageHandler
messageHandler.packetProcessor = packetProcessor messageHandler.packetProcessor = packetProcessor
// startPeriodicDebugLogging() startPeriodicDebugLogging()
} }
/** /**