Error with receiving xml strings via bluetooth in Android

你说的曾经没有我的故事 提交于 2019-12-11 10:13:25

问题


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 defined as an Android service. Every time i receive a string its not in its original form no mater i send it from arduino or other android phone. the xml parsing function is working i have checked that.

Here is my code where i am receiving the string

mConnectedThread = new ConnectedThread(btSocket);
            mConnectedThread.start();

            h = new Handler() {
                public void handleMessage(android.os.Message msg) {
                    switch (msg.what) {
                    case RECIEVE_MESSAGE:                                                   // if receive massage
                        byte[] readBuf = (byte[]) msg.obj;
                        String strIncom = new String(readBuf, 0, msg.arg1);                 // create string from bytes array
                        sb.append(strIncom);                                                // append string
                        int endOfLineIndex = sb.indexOf("\n");                          // determine the end-of-line
                        if (endOfLineIndex > 0) {                                           // if end-of-line,
                            String sbprint = sb.substring(0, endOfLineIndex);               // extract string
                            sendXML(sbprint); // this method is for sending the xml string
                            sb.delete(0, sb.length());                                      // and clear


                        }
                        Log.d(TAG, "...String:"+ sb.toString() +  "Byte:" + msg.arg1 + "...");
                        Log.d("IncString", strIncom);

                        break;

                    }
                };
            };

Here is a sample xml String i am using

<head><hbt v='100'/><hrg v='75'/></head>

i always get the string but not complete like this **v='100'/><hrg v='75'** or **</head>**

If the question is not clear tell me any thing i will update

thanx in advance


回答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...




回答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



来源:https://stackoverflow.com/questions/15053358/error-with-receiving-xml-strings-via-bluetooth-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!