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")
return false
}
if (currentAttempt != null) {
Log.d(TAG, "Tracker: current attempt: $currentAttempt")
}
// Update connection attempt atomically
val attempts = (currentAttempt?.attempts ?: 0) + 1
@@ -241,14 +244,17 @@ class BluetoothConnectionTracker(
/**
* Clean up a specific device connection
*/
fun cleanupDeviceConnection(deviceAddress: String) {
fun cleanupDeviceConnection(deviceAddress: String, cleanupPending: Boolean = true) {
connectedDevices.remove(deviceAddress)?.let { deviceConn ->
subscribedDevices.removeAll { it.address == deviceAddress }
addressPeerMap.remove(deviceAddress)
}
// CRITICAL FIX: Always remove from pending connections when cleaning up
// This prevents failed connections from blocking future attempts
pendingConnections.remove(deviceAddress)
if (!cleanupPending) {
Log.d(TAG, "Skipped cleanup of pending connection for $deviceAddress")
}
if (cleanupPending) {
pendingConnections.remove(deviceAddress)
}
Log.d(TAG, "Cleaned up device connection for $deviceAddress")
}
@@ -192,7 +192,7 @@ class BluetoothGattClientManager(
scanCallback = object : ScanCallback() {
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)
}
@@ -278,7 +278,7 @@ class BluetoothGattClientManager(
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)
connectionTracker.updateScanRSSI(deviceAddress, rssi)
@@ -338,12 +338,14 @@ 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)
}
connectionTracker.cleanupDeviceConnection(deviceAddress)
connectionScope.launch {
delay(500) // CLEANUP_DELAY
try {
@@ -423,7 +425,7 @@ class BluetoothGattClientManager(
override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) {
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)
if (packet != null) {
val peerID = packet.senderID.take(8).toByteArray().joinToString("") { "%02x".format(it) }
@@ -85,10 +85,8 @@ class PacketProcessor(private val myPeerID: String) {
Log.d(TAG, "Packet failed security validation from $peerID")
return
}
// Update last seen timestamp
delegate?.updatePeerLastSeen(peerID)
var validPacket = true
Log.d(TAG, "Processing packet type ${packet.type} from $peerID")
val DEBUG_MESSAGE_TYPE = 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.READ_RECEIPT -> handleReadReceipt(routed)
else -> {
validPacket = false
Log.w(TAG, "Unknown message type: ${packet.type}")
}
}
// Update last seen timestamp
if (validPacket)
delegate?.updatePeerLastSeen(peerID)
}
/**