1

I'm creating an app that sends and receive data with an arduino via bluetooth. Sending works fine, however when receiving data I don't get the first few characters of the string sent. I always don't get the first character, the second I sometimes get it, the third I almost always get it, etc.

So for example if the arduino sends "OK 1" I receive "K 1" or " 1" or "1", but never the complete string. An easy fix would be to add a few dummy characters, but that's a shit fix.

Here's the method which listens to incoming connections, directly copy/pasted from Android sample bluetooth code to send a simple string via bluetooth (though with a few fixes):

void beginListenForData()
{
    final Handler handler = new Handler();
    final byte delimiter = 10; //This is the ASCII code for a newline character

    final boolean stopWorker = false;
    final int readBufferPosition = 0;
    final byte[]readBuffer = new byte[1024];
    Thread workerThread = new Thread(new Runnable()
    {
        public void run()
        {
            while(!Thread.currentThread().isInterrupted() && !stopWorker)
            {

                try
                {
                    int bytesAvailable = inStream.available();
                    int readBufferPosition2 = readBufferPosition;
                    if(bytesAvailable > 0)

                    {
                        byte[] packetBytes = new byte[bytesAvailable];
                        inStream.read(packetBytes);
                        for(int i=0;i<bytesAvailable;i++)
                        {
                            byte b = packetBytes[i];
                            if(b == delimiter)
                            {
                                byte[] encodedBytes = new byte[readBufferPosition2];
                                System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
                                final String data = new String(encodedBytes);
                                readBufferPosition2 = 0;

                                handler.post(new Runnable()
                                {
                                    public void run()
                                    {
                                        result.setText(data);
                                    }
                                });
                            }
                            else
                            {
                                readBuffer[readBufferPosition2++] = b;
                            }
                        }
                    }
                }
                catch (IOException ex)
                {
                }
            }
        }
    });

    workerThread.start();
}

Here's all my code in case you want to test it (warning: lots of dummy and outdated code):

MainActivity.java http://pastebin.com/cdjW4Y1V XML layout file http://pastebin.com/Ruf5euPP

Click on the first button to connect to the arduino and click on the second button to send a string and to begin receiving data.

So yep, I have absolutely no idea why it doesn't work. It works fine with TerminalBT so it's not a problem with the arduino, it's a problem with my app, but why do I receive characters randomly?

Community
  • 1
  • 1
Zezombye
  • 321
  • 4
  • 16

1 Answers1

0

one thing i noticed is that you should not use 10 for the delimiter.

I have encountered this error before if you use 10 then some times it does not get recognized properly.

you should use standard Java Function to parse the delimiter.

System.getProperty("line.separator");

 //OR

System.lineSeparator();
Neo
  • 1,359
  • 14
  • 34
  • How should I use this function? Where do I put it in my code? – Zezombye Jan 24 '16 at 13:42
  • where you do this .byte b = packetBytes[i];. just compare with a .equals(System.lineSeparator()) function. if you need to convert the byte to a string that i cannot check .try yourself – Neo Jan 24 '16 at 15:51