javaandroidarduinobluetoothhc-05

Problems with extracting data from Bluetooth input stream, using android tablet and HC-05 radio transmitter


I have a project where I have an HC-05 radio transmitting about 500 to 800 characters of XML formatted data every 1 second. I receive it in an android app, convert it to a String then process the XML using XmlPullParserFactory. Everything works great except every 5 to 10 seconds only a portion of the transmitted data gets received. then the rest of it gets received along with the next dataset a second later. I have a few theories but none prooven.

... Currently working on this theory >

I do not know how the HC-05 radio works internally so my commend might sound stupid, question is, does the radio instantly transmit the characters over Bluetooth as they arrive, or does it wait for data to stop then send all the data in one "packet"? I was thinking it might have a limit to how many characters it can transmit at once hence causing a break in data transmission. But it is not consistent so that can hardly be the issue.

Any help on the matter would be greatly appreciated. Ask me for clarity where needed.

Below is a snip from the code that converts the input stream to String, let me know if I have an error.

    @Override
    public void run() {
        InputStream inputStream;

        try {
            inputStream = mBTSocket.getInputStream();
            while (!bStop) {
                byte[] buffer = new byte[1200];
                if (inputStream.available() > 0) {
                    inputStream.read(buffer);
                    int i = 0;
                    for (i = 0; i < buffer.length && buffer[i] != 0; i++) {
                    }
                    final String strInput = new String(buffer, 0, i);
                }

                Thread.sleep(100);
            }
        } catch (IOException e) {
// TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
// TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

Solution

  • Ok, so I got back to this a while later and solved to. I am sending packets out at 250ms intervals, and each transmit (depending on data length) takes a bit 100ms to compete. So with the Thread.sleep(100) it was not always ready when the new data string came in. Simply reducing it to 10 made it all work.