Skip to content Skip to sidebar Skip to footer

Android - Bluetooth Adapter - Message Handler Buffer Limit

I'm using this tutorial for building a multiplayer game that uses Bluetooth for connection: https://developer.android.com/samples/BluetoothChat/index.html Since I'm sending long st

Solution 1:

Data passed through a Bluetooth socket is abstracted as a stream, but at the transport layer is broken into packets, which have a maximum size of roughly 1000 bytes. On the sender side, your can get the output stream and write a 5 Kbyte chunk of data, but the transport layer will break it into multiple packets before sending over the air. When the receiver reads its input stream, each call to read() will return the data for one packet. The transport layer does not reassemble the packets, your code needs to do that. This means you need to devise some scheme, such as a message header that contains the length of your message, which the receiver can to know when stop assembling packets and hand the complete message off to the consuming processing.

This issue is not addressed in the BluetoothChat sample app because it (silently) assumes that all chat messages will be smaller than the packet size, so no message reassembly is needed.

Post a Comment for "Android - Bluetooth Adapter - Message Handler Buffer Limit"