pending tracker

This commit is contained in:
callebtc
2025-07-22 14:06:33 +02:00
parent 1533d2715d
commit 632a33729f
3 changed files with 24 additions and 14 deletions
@@ -189,6 +189,9 @@ class BluetoothConnectionTracker(
Log.d(TAG, "Tracker: Connection attempt already in progress for $deviceAddress") Log.d(TAG, "Tracker: Connection attempt already in progress for $deviceAddress")
return false return false
} }
if (currentAttempt != null) {
Log.d(TAG, "Tracker: current attempt: $currentAttempt")
}
// Update connection attempt atomically // Update connection attempt atomically
val attempts = (currentAttempt?.attempts ?: 0) + 1 val attempts = (currentAttempt?.attempts ?: 0) + 1
@@ -241,14 +244,17 @@ class BluetoothConnectionTracker(
/** /**
* Clean up a specific device connection * Clean up a specific device connection
*/ */
fun cleanupDeviceConnection(deviceAddress: String) { fun cleanupDeviceConnection(deviceAddress: String, cleanupPending: Boolean = true) {
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)
} }
// CRITICAL FIX: Always remove from pending connections when cleaning up if (!cleanupPending) {
// This prevents failed connections from blocking future attempts 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")
} }
@@ -192,7 +192,7 @@ class BluetoothGattClientManager(
scanCallback = object : ScanCallback() { scanCallback = object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult) { override fun onScanResult(callbackType: Int, result: ScanResult) {
Log.d(TAG, "Scan result received: ${result.device.address}") // Log.d(TAG, "Scan result received: ${result.device.address}")
handleScanResult(result) handleScanResult(result)
} }
@@ -278,7 +278,7 @@ class BluetoothGattClientManager(
return return
} }
Log.d(TAG, "Received scan result from $deviceAddress - already connected: ${connectionTracker.isDeviceConnected(deviceAddress)}") // Log.d(TAG, "Received scan result from $deviceAddress - already connected: ${connectionTracker.isDeviceConnected(deviceAddress)}")
// Store RSSI from scan results for later use (especially for server connections) // Store RSSI from scan results for later use (especially for server connections)
connectionTracker.updateScanRSSI(deviceAddress, rssi) connectionTracker.updateScanRSSI(deviceAddress, rssi)
@@ -338,11 +338,13 @@ 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)
}
connectionScope.launch { connectionScope.launch {
delay(500) // CLEANUP_DELAY delay(500) // CLEANUP_DELAY
@@ -423,7 +425,7 @@ class BluetoothGattClientManager(
override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) { override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) {
val value = characteristic.value val value = characteristic.value
Log.d(TAG, "Client: Received packet from ${gatt.device.address}, size: ${value.size} bytes") Log.i(TAG, "Client: Received packet from ${gatt.device.address}, size: ${value.size} bytes")
val packet = BitchatPacket.fromBinaryData(value) val packet = BitchatPacket.fromBinaryData(value)
if (packet != null) { if (packet != null) {
val peerID = packet.senderID.take(8).toByteArray().joinToString("") { "%02x".format(it) } val peerID = packet.senderID.take(8).toByteArray().joinToString("") { "%02x".format(it) }
@@ -86,9 +86,7 @@ class PacketProcessor(private val myPeerID: String) {
return return
} }
// Update last seen timestamp var validPacket = true
delegate?.updatePeerLastSeen(peerID)
Log.d(TAG, "Processing packet type ${packet.type} from $peerID") Log.d(TAG, "Processing packet type ${packet.type} from $peerID")
val DEBUG_MESSAGE_TYPE = MessageType.fromValue(packet.type) val DEBUG_MESSAGE_TYPE = MessageType.fromValue(packet.type)
when (MessageType.fromValue(packet.type)) { when (MessageType.fromValue(packet.type)) {
@@ -105,9 +103,13 @@ class PacketProcessor(private val myPeerID: String) {
MessageType.DELIVERY_ACK -> handleDeliveryAck(routed) MessageType.DELIVERY_ACK -> handleDeliveryAck(routed)
MessageType.READ_RECEIPT -> handleReadReceipt(routed) MessageType.READ_RECEIPT -> handleReadReceipt(routed)
else -> { else -> {
validPacket = false
Log.w(TAG, "Unknown message type: ${packet.type}") Log.w(TAG, "Unknown message type: ${packet.type}")
} }
} }
// Update last seen timestamp
if (validPacket)
delegate?.updatePeerLastSeen(peerID)
} }
/** /**