mirror of
https://github.com/permissionlesstech/bitchat-android.git
synced 2026-07-25 01:45:22 +00:00
Merge branch 'main' into adjust-scan-intervals
This commit is contained in:
@@ -9,6 +9,8 @@ import android.os.ParcelUuid
|
||||
import android.util.Log
|
||||
import androidx.core.app.ActivityCompat
|
||||
import com.bitchat.android.protocol.BitchatPacket
|
||||
import com.bitchat.android.protocol.SpecialRecipients
|
||||
import com.bitchat.android.model.RoutedPacket
|
||||
import kotlinx.coroutines.*
|
||||
import java.util.*
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
@@ -53,6 +55,7 @@ class BluetoothConnectionManager(
|
||||
// Simplified connection tracking - reduced memory footprint
|
||||
private val connectedDevices = ConcurrentHashMap<String, DeviceConnection>()
|
||||
private val subscribedDevices = CopyOnWriteArrayList<BluetoothDevice>()
|
||||
public val addressPeerMap = ConcurrentHashMap<String, String>()
|
||||
|
||||
// Connection attempt tracking with automatic cleanup
|
||||
private val pendingConnections = ConcurrentHashMap<String, ConnectionAttempt>()
|
||||
@@ -199,47 +202,106 @@ class BluetoothConnectionManager(
|
||||
powerManager.setAppBackgroundState(inBackground)
|
||||
}
|
||||
|
||||
// Function to send data to a single device (server side)
|
||||
private fun notifyDevice(device: BluetoothDevice, data: ByteArray): Boolean {
|
||||
return try {
|
||||
characteristic?.let { char ->
|
||||
char.value = data
|
||||
val result = gattServer?.notifyCharacteristicChanged(device, char, false) ?: false
|
||||
result
|
||||
} ?: false
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "Error sending to server connection ${device.address}: ${e.message}")
|
||||
connectionScope.launch {
|
||||
delay(CLEANUP_DELAY)
|
||||
subscribedDevices.remove(device)
|
||||
addressPeerMap.remove(device.address)
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
// Function to send data to a single device (client side)
|
||||
private fun writeToDeviceConn(deviceConn: DeviceConnection, data: ByteArray): Boolean {
|
||||
return try {
|
||||
deviceConn.characteristic?.let { char ->
|
||||
char.value = data
|
||||
val result = deviceConn.gatt?.writeCharacteristic(char) ?: false
|
||||
result
|
||||
} ?: false
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "Error sending to client connection ${deviceConn.device.address}: ${e.message}")
|
||||
connectionScope.launch {
|
||||
delay(CLEANUP_DELAY)
|
||||
cleanupDeviceConnection(deviceConn.device.address)
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Broadcast packet to connected devices with connection limit enforcement
|
||||
*/
|
||||
fun broadcastPacket(packet: BitchatPacket) {
|
||||
fun broadcastPacket(routed: RoutedPacket) {
|
||||
val packet = routed.packet
|
||||
|
||||
if (!isActive) return
|
||||
|
||||
val data = packet.toBinaryData() ?: return
|
||||
|
||||
if (packet.recipientID != SpecialRecipients.BROADCAST) {
|
||||
val recipientID = packet.recipientID?.let {
|
||||
String(it).replace("\u0000", "").trim()
|
||||
} ?: ""
|
||||
|
||||
// Try to find the recipient in server connections (subscribedDevices)
|
||||
val targetDevice = subscribedDevices.firstOrNull { addressPeerMap[it.address] == recipientID }
|
||||
// If found, send directly
|
||||
if (targetDevice != null) {
|
||||
Log.d(TAG, "Send packet type ${packet.type} directly to target device for recipient $recipientID: ${targetDevice.address}")
|
||||
if (notifyDevice(targetDevice, data))
|
||||
return // Sent, no need to continue
|
||||
}
|
||||
|
||||
// Try to find the recipient in client connections (connectedDevices)
|
||||
val targetDeviceConn = connectedDevices.values.firstOrNull { addressPeerMap[it.device.address] == recipientID }
|
||||
// If found, send directly
|
||||
if (targetDeviceConn != null) {
|
||||
Log.d(TAG, "Send packet type ${packet.type} directly to target client connection for recipient $recipientID: ${targetDeviceConn.device.address}")
|
||||
if (writeToDeviceConn(targetDeviceConn, data))
|
||||
return // Sent, no need to continue
|
||||
}
|
||||
}
|
||||
|
||||
// Else, continue with broadcasting to all devices
|
||||
Log.d(TAG, "Broadcasting packet type ${packet.type} to ${subscribedDevices.size} server + ${connectedDevices.size} client connections")
|
||||
|
||||
|
||||
val senderID = String(packet.senderID).replace("\u0000", "")
|
||||
// Send to server connections (devices connected to our GATT server)
|
||||
subscribedDevices.forEach { device ->
|
||||
try {
|
||||
characteristic?.let { char ->
|
||||
char.value = data
|
||||
gattServer?.notifyCharacteristicChanged(device, char, false)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "Error sending to server connection ${device.address}: ${e.message}")
|
||||
// Clean up failed connection
|
||||
connectionScope.launch {
|
||||
delay(CLEANUP_DELAY)
|
||||
subscribedDevices.remove(device)
|
||||
}
|
||||
if (device.address == routed.relayAddress) {
|
||||
Log.d(TAG, "Skipping broadcast back to relayer: ${device.address}")
|
||||
return@forEach
|
||||
}
|
||||
if (addressPeerMap[device.address] == senderID) {
|
||||
Log.d(TAG, "Skipping broadcast back to sender: ${device.address}")
|
||||
return@forEach
|
||||
}
|
||||
notifyDevice(device, data)
|
||||
}
|
||||
|
||||
// Send to client connections
|
||||
connectedDevices.values.forEach { deviceConn ->
|
||||
if (deviceConn.isClient && deviceConn.gatt != null && deviceConn.characteristic != null) {
|
||||
try {
|
||||
deviceConn.characteristic.value = data
|
||||
deviceConn.gatt.writeCharacteristic(deviceConn.characteristic)
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "Error sending to client connection ${deviceConn.device.address}: ${e.message}")
|
||||
// Clean up failed connection
|
||||
connectionScope.launch {
|
||||
delay(CLEANUP_DELAY)
|
||||
cleanupDeviceConnection(deviceConn.device.address)
|
||||
}
|
||||
if (deviceConn.device.address == routed.relayAddress) {
|
||||
Log.d(TAG, "Skipping broadcast back to relayer: ${deviceConn.device.address}")
|
||||
return@forEach
|
||||
}
|
||||
if (addressPeerMap[deviceConn.device.address] == senderID) {
|
||||
Log.d(TAG, "Skipping broadcast back to sender: ${deviceConn.device.address}")
|
||||
return@forEach
|
||||
}
|
||||
writeToDeviceConn(deviceConn, data)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -909,6 +971,7 @@ class BluetoothConnectionManager(
|
||||
private fun cleanupDeviceConnection(deviceAddress: String) {
|
||||
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
|
||||
@@ -937,6 +1000,7 @@ class BluetoothConnectionManager(
|
||||
private fun clearAllConnections() {
|
||||
connectedDevices.clear()
|
||||
subscribedDevices.clear()
|
||||
addressPeerMap.clear()
|
||||
pendingConnections.clear()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user