feat: Implement iOS-compatible selective padding for BLE messages #501
+26
−9
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
iOS Compatibility: Selective Padding Policy for BLE Transmission
Objective
Fix BLE communication failure between Android and iOS by implementing iOS-compatible selective padding policy. Android was applying PKCS#7 padding to all packet types over BLE, while iOS only pads encrypted messages. This caused iOS to receive invalid packet headers (0x54 padding bytes instead of version bytes 0x01/0x02) and drop all incoming Android packets.
Root Cause
MessagePadding.pad()to all packet types during BLE transmission, resulting in 256-byte padded packetspadPolicy(for:)that returnsfalsefor ANNOUNCE/MESSAGE/SYNC andtrueonly for NOISE_ENCRYPTED/NOISE_HANDSHAKENotificationStreamAssemblerrejected all Android packets with "Dropping byte from BLE stream (unexpected prefix 54)" where 0x54 (84 decimal) is the PKCS#7 padding length byteChanges
Android (BluetoothPacketBroadcaster.kt)
Added selective padding method:
Modified broadcast methods:
Android (BinaryProtocol.kt)
Modified encoding signature:
Maintained robust decoding:
iOS Compatibility
This change aligns Android implementation with iOS's existing padPolicy(for:) in BLEService.swift:
Message Type Behavior:
Testing Areas
Suggested Test Scenarios:
Security & Privacy
Padding Purpose: PKCS#7 padding obscures actual message length to prevent traffic analysis attacks. This is critical for encrypted messages where message size could reveal sensitive information.
Why Selective Padding:
Security Assessment:
Performance Impact
Positive Impacts:
Example:
Benchmarks (estimated):
Backward Compatibility
✅ Fully Compatible: Android's decode() method already handles both padded and unpadded packets:
Migration Path:
Summary: This PR implements iOS-compatible selective padding policy for BLE transmission in Android, fixing cross-platform communication while improving performance and maintaining security guarantees for encrypted messages.