I used code I found on the Internet that implemented a handler for Bluetooth communication with an Arduino and the HC-05 module. It worked for turning on and off the built-in led on the Arduino board.
The data became sporadic when I tried to send more than a few bytes. The ‘ Led On/Off’ functionality seemed to work fine but I couldn’t expand on the example past very small data packets.
On the Android side I saw the handler was moving the data from the incoming buffer, doing a little processing, and passing the results onto the UI thread.
That seemed reasonable enough but after eliminating all other possibilities I suspected the Android application was busy processing data causing it to miss part or all of the next data burst.
The clue was that the problem seemed to get worse with larger data packets. The issue was it didn’t fail in a predictable way. The data could make it through intact but it was more often garbled.
There were two issues I addressed. First and foremost, doing a little processing, and secondly, running the UI update from within the connection thread.
The task of processing the data was moved into a separate thread started on the creation of the Activity. The second thread checks for data in the ‘processed’ buffer. Since it is a separate thread it can run as long as it takes. It calls the handler to interact with the UI thread so the ‘connection thread’ is completely dedicated to watching for incoming data.