Skip to content

iOS → Android messages over 157 characters are not received; Android → iOS works up to 352 bytes #90

Open

Description

@hamzaozturk

Checklist

  • I am able to reproduce the bug with the latest version given here: CLICK THIS LINK.
  • I made sure that there are no existing issues - open or closed - which I could contribute my information to.
  • I have taken the time to fill in all the required details. I understand that the bug report will be dismissed otherwise.
  • This issue contains only one bug.
  • I have read and understood the contribution guidelines.

Affected version

0.6

Steps to reproduce the bug

  1. Open the iOS app.
  2. Send a message with more than 157 characters to an Android device.
  3. Observe that nothing is received on Android.
  4. On iOS, the message appears to be sent successfully.

Expected behavior

Android should receive the full message from iOS, regardless of length (within BLE limits). If chunking is required, either iOS should handle fragmentation, or Android should support reassembly.

Actual behavior

When sending a message from iOS to Android that exceeds 157 characters, the Android app does not receive the message at all. It silently fails or is possibly discarded.

Meanwhile, Android can send messages up to 352 bytes to iOS without any issue.

Working cases:

  • ✅ Android → iOS: up to 352 bytes = works fine
  • ✅ iOS → Android: up to 157 characters = works fine

Broken case:

  • ❌ iOS → Android: over 157 characters = not received at all

Screenshots/Screen recordings

No response

Affected Android/Custom ROM version

Android 14.0.11

Affected device model

Redmi Note 10S

Additional information

No response

Activity

  1. added
    bug
    on Jul 12, 2025
  2. hamzaozturk commented on Jul 13, 2025

    @hamzaozturk
    ContributorAuthor

    🔍 Additional Technical Insight:

    I’ve debugged the Android side further and noticed the following:

    • onCharacteristicWriteRequest() is correctly triggered.
    • The preparedWrite flag is true — which indicates that a Reliable Write (long write) transaction is in progress.
    • However, only the chunk with offset = 0 is being processed.
    • Subsequent chunks (with non-zero offsets) are being ignored, so the full message is never assembled.

    This is likely the root cause of the issue when iOS sends a message longer than the MTU size (512+ chars in this case).

    👉 Suggestion: Android should store all incoming chunks (when preparedWrite == true) and finalize the write in onExecuteWrite() to properly support long writes.

    Let me know if you'd like help testing a potential fix!

    Image

    **Example Code: **

    // Temporary buffer to store incoming chunks per device
    val writeBuffer = mutableMapOf<BluetoothDevice, MutableList<Pair<Int, ByteArray>>>()
    
    val gattServerCallback = object : BluetoothGattServerCallback() {
    
        override fun onCharacteristicWriteRequest(
            device: BluetoothDevice,
            requestId: Int,
            characteristic: BluetoothGattCharacteristic,
            preparedWrite: Boolean,
            responseNeeded: Boolean,
            offset: Int,
            value: ByteArray
        ) {
            if (preparedWrite) {
                // Buffer the chunk with its offset
                val buffer = writeBuffer.getOrPut(device) { mutableListOf() }
                buffer.add(offset to value)
            } else {
                // Handle short write directly
                characteristic.value = value
            }
    
            if (responseNeeded) {
                gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value)
            }
        }
    
        override fun onExecuteWrite(device: BluetoothDevice, execute: Boolean) {
            if (execute) {
                val chunks = writeBuffer[device]
                if (!chunks.isNullOrEmpty()) {
                    // Sort by offset and merge
                    val completeData = chunks
                        .sortedBy { it.first }
                        .fold(ByteArray(0)) { acc, pair -> acc + pair.second }
    
                    // Now you can process the complete message
                    // For example, assign to characteristic or parse message
                    Log.d("BLE", "Complete message received: ${String(completeData)}")
    
                    // Clear buffer after use
                    writeBuffer.remove(device)
                }
            } else {
                // Cancel write
                writeBuffer.remove(device)
            }
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions