Reputation: 10822
I'm looking over the Bluetooth Chat sample application from Google and they write to BluetoothSocket
's OutputStream
on the UI thread. Is that correct? Normally, streams block until the data is sent out.
In my tests (with that app), as long as the devices were close enough, the communication was prompt. When they got farther, the connection was dropped. Is this correct way to send data over Bluetooth? My message size will by 100-500 bytes.
Upvotes: 4
Views: 1057
Reputation: 8345
https://developer.android.com/guide/topics/connectivity/bluetooth#ManageAConnection
There are, of course, implementation details to consider. In particular, you should use a dedicated thread for reading from the stream and writing to it. This is important because both the read(byte[]) and write(byte[]) methods are blocking calls. The read(byte[]) method blocks until there is something to read from the stream. The write(byte[]) method doesn't usually block, but it can block for flow control if the remote device isn't calling read(byte[]) quickly enough and the intermediate buffers become full as a result. So, your main loop in the thread should be dedicated to reading from the InputStream. A separate public method in the thread can be used to initiate writes to the OutputStream.
Upvotes: 1
Reputation: 1476
I have wondered about this as well, because as far as I can tell BluetoothChat writes Bluetooth data on the UI thread, as does every other Android Bluetooth example I've found - all of which appear to be based on BluetoothChat.
I have done some more tests along the lines of those Oliv did. Using a Samsung T113 running 4.4.4, I found that for writing 60 character strings, the write generally takes 12-14ms. However, there were also cases where the write took much longer - 35-45ms. Further, if the device being written to is not reading the messages sent to it, eventually the buffer on the sending device will fill and the write operation will block indefinitely (see Android BluetoothSocket OutputStream write blocks infinitely). For these reasons, I think that a well-behaved app that writes Bluetooth will need to do so off the main thread.
(According to Mark Murphy of Commonsware, "all I/O on the main application thread is a bad idea" and BluetoothChat's use of the main thread for writing is "probably just a bug.")
Upvotes: 2
Reputation: 10822
Since the documentation does not say anything about this, I did my own test: I tried to send the following amounts of data and measured, how long does the write last.
Old Android 2.3 device Recent Android 5.0 device
1kB 12ms 2ms
4kB 15-20ms 2ms
64kB 25-35ms 7ms
128kB 10-17ms 6ms
256kB 2000-3000ms 3000ms
Since I will send amounts of less than 1kB, I'll do it on the UI thread. They do the same in the "official" sample chat app.
Seems that Android has some internal buffer of at least 128kB, so short messages can be written without bothering yourself with a background thread.
However, to read 128kB took second or two on the other device. I've used 4kB read buffer. When I read byte-by-byte, it was maybe a minute.
Upvotes: 2