Skip to content Skip to sidebar Skip to footer

Error With Receiving Xml Strings Via Bluetooth In Android

i am developing an App which receives xml as String over bluetooth (from Arduino and android phones). I am getting invalid/incomplete strings from the bluetooth . the Bluetooth is

Solution 1:

I did this successfully with the following code...

byte[] buffer = new byte[128];  // buffer store for the stream
        int bytes; // bytes returned from read()

        // Keep listening to the InputStream until an exception occurs
        while (true) {
            try {


                bytes = mmInStream.read(buffer);
                byte[] readBuf = (byte[]) buffer;
                String strIncom = new String(readBuf, 0, bytes);  // create string from bytes array
                sb.append(strIncom);     // append string
                int endOfLineIndex = sb.indexOf("\r\n"); // determine the end-of-line
                if (endOfLineIndex > 0) {  
                    // add the current string to eol to a local string
                    String sbprint = sb.substring(0, endOfLineIndex);

                    // get the start and end indexes of the heading
                    int startHeading = sb.indexOf("HE");
                    int endHeading = sb.indexOf("/HE");

                    // set the heading
                    blahblah.setCurrentHeading(sb.substring((startHeading + 2), endHeading));

  ....

Then after I retrieved all the information I wanted from the text sent by the arduino I called:

sb.delete(0, sb.length());

The string coming from my adruino looks like the following:::

void taskTransmitData(void)
{
  // start the xml
  Serial.print("HE");
  Serial.print(pMyMemory->currentHeading);
  Serial.print("/HE");

  .....

  Serial.println();

}

Hope this helps...


Solution 2:

Try to use something like the while(true) statement in line 92 given in the code here: http://code.google.com/p/android-arduino/source/browse/garduino/android/src/net/morrildl/garduino/CommThread.java


Post a Comment for "Error With Receiving Xml Strings Via Bluetooth In Android"