Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stream Byte Array Fragmentation in Flutter Platform Channel #23

Open
saurabh-ifmt opened this issue Dec 30, 2024 · 5 comments
Open

Stream Byte Array Fragmentation in Flutter Platform Channel #23

saurabh-ifmt opened this issue Dec 30, 2024 · 5 comments

Comments

@saurabh-ifmt
Copy link

I am experiencing an issue with byte array fragmentation when receiving data from the native side to the Flutter side via a platform channel. The data sent from the native side is received in chunks, breaking the expected byte array into fragments. Below is an example of the
Received From Native: [104]
Received From Native: [105, 32, 115, 97, 117, 114, 97, 98, 104, 13, 10]

@farellsujanto
Copy link
Owner

hi @saurabh-ifmt, can you share what device did you use for inbound & outbound data?

@saurabh-ifmt
Copy link
Author

Device used - Arduino
InBound
[0xAA,0x01, 0x00, 0x00, 0x00, 0x00, 0x00,0xBB]
OutBound
I/flutter (31817): Received From Native: [170, 1]
I/flutter (31817): Received From Native: [0, 0, 0, 0, 0, 187]

@farellsujanto
Copy link
Owner

no I mean what kind of arduino device did you use (be detailed)? and what android device did you use?

@farellsujanto
Copy link
Owner

temporary solution is that you can pool your data within specific time frame, so for like in a 50ms scope all data will be combined into one:

0ms: [170, 1]
30ms: [0, 0, 0, 0, 0, 187]
50ms: combine both data -> [170, 1, 0, 0, 0, 0, 0, 187]

@DominikStarke
Copy link
Contributor

This behavior is normal and expected. On a bare serial port, there is no concept of "message boundaries".
Data is sent and received as a continuous stream of bytes. The fragmentation you're seeing (e.g., [0x01, 0x02] being received as [0x01] and [0x02]) happens because the serial driver delivers data as it becomes available, often in chunks of arbitrary size.

To handle this, you'll need to implement your own logic to reconstruct messages. The easiest is using a "Delimiter-Based Protocol" (\n aka newline in this case)

final serialStream = _flutterSerialCommunicationPlugin
    .getSerialMessageListener()
    .receiveBroadcastStream();

await for (final message in serialStream
    .toStringStream() // Convert to string
    .transform(const LineSplitter())) {  // Split on newlines
  print(message);  // Process each message as a complete unit
}

On the Arduino, you can use println("Hello World") to append a newline character (\n) automatically to your messages. This will allow the Dart code to correctly separate them.

Otherwise look into protocols for example SLIP or COBS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants