Fix Android fragmentation for iOS/Rust compatibility

- Changed MAX_FRAGMENT_SIZE from 500 to 150 bytes to match iOS/Rust
- Fixed fragment ID generation to use 8 random bytes instead of string
- Implemented automatic fragmentation in BluetoothConnectionManager
- Added 20ms delay between fragments to match iOS/Rust implementation
- Updated fragment calculation to account for full packet overhead (34 bytes)

This ensures Android-generated fragments fit within iOS's default 185-byte BLE MTU,
fixing issues with commands like ?ls -la and ?ifconfig failing on iOS devices.
This commit is contained in:
ShilohEye
2025-07-10 19:25:52 -04:00
parent c585b5c4ac
commit 33d273f268
3 changed files with 42 additions and 16 deletions
@@ -20,7 +20,8 @@ import java.util.concurrent.CopyOnWriteArrayList
*/
class BluetoothConnectionManager(
private val context: Context,
private val myPeerID: String
private val myPeerID: String,
private val fragmentManager: FragmentManager? = null
) : PowerManagerDelegate {
companion object {
@@ -198,13 +199,38 @@ class BluetoothConnectionManager(
/**
* Broadcast packet to connected devices with connection limit enforcement
* Automatically fragments large packets to fit within BLE MTU limits
*/
fun broadcastPacket(packet: BitchatPacket) {
if (!isActive) return
// Check if we need to fragment
if (fragmentManager != null) {
val fragments = fragmentManager.createFragments(packet)
if (fragments.size > 1) {
Log.d(TAG, "Fragmenting packet into ${fragments.size} fragments")
connectionScope.launch {
fragments.forEach { fragment ->
sendSinglePacket(fragment)
// 20ms delay between fragments (matching iOS/Rust)
delay(20)
}
}
return
}
}
// Send single packet if no fragmentation needed
sendSinglePacket(packet)
}
/**
* Send a single packet (fragment or whole) to all connected devices
*/
private fun sendSinglePacket(packet: BitchatPacket) {
val data = packet.toBinaryData() ?: return
Log.d(TAG, "Broadcasting packet type ${packet.type} to ${subscribedDevices.size} server + ${connectedDevices.size} client connections")
Log.d(TAG, "Sending packet type ${packet.type} (${data.size} bytes) to ${subscribedDevices.size} server + ${connectedDevices.size} client connections")
// Send to server connections (devices connected to our GATT server)
subscribedDevices.forEach { device ->