Android BluetoothSocket OutputStream write blocks infinitely

后端 未结 1 704
一个人的身影
一个人的身影 2021-01-02 20:23

I need to programmatically write data of say 1 to 100 MB in chunks of 1024 bytes to the remote Bluetooth device. Both are android devices. Here is a sample code snippet in m

相关标签:
1条回答
  • 2021-01-02 20:44

    The Bluetooth socket operates in blocking mode for both reads and writes.

    If you fill up the send buffer, then the only thing that .write() can do to stop you trying to send any more data is to block. The alternative to it blocking would be to return an "operation would block!" error code, just like TCP sockets can do when placed in non-blocking mode. But the Bluetooth socket doesn't provide any such non-blocking mode.

    You state that the remote Bluetooth device is not reading from its socket. With this being the case, the local sending buffer and remote receive buffer, with each only being of a certain finite size, will eventually fill up. At this point, your .write() operation is going to block until the remote end reads something from its socket. You can't just keep pumping in megabytes of data and expect it to just buffer it all somewhere.

    The differences you experience between different Android platforms are probably down to the different amounts of buffer space available in the related Bluetooth stacks.

    0 讨论(0)
提交回复
热议问题