错误与Android中通过蓝牙接收XML字符串(Error with receiving xml s

2019-10-17 17:45发布

我正在开发通过蓝牙接收XML作为字符串(从Arduino的和Android手机)的应用程序。

我正在从蓝牙无效/不完整的字符串。 蓝牙被定义为一个Android服务。 每当我收到一个字符串,它不是以原来的形式没有母校,我从Arduino的或其他android手机发送。 XML解析功能工作我检查了这一点。

这里是我的代码,其中我收到的字符串

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;

                    }
                };
            };

下面是我使用的样本XML字符串

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

我总是得到字符串,但不能完成这样**v='100'/><hrg v='75'****</head>**

如果这个问题不明确告诉我任何事情,我将更新

thanx提前

Answer 1:

我用下面的代码这样做成功...

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));

  ....

我检索到的所有信息,然后后,我从我所谓的Arduino的发送的文本想要的东西:

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

从我adruino来的字符串看起来像下面:::

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

  .....

  Serial.println();

}

希望这可以帮助...



Answer 2:

尝试使用类似的线92,而(真)语句代码在这里给出: http://code.google.com/p/android-arduino/source/browse/garduino/android/src/net/morrildl/ garduino / CommThread.java



文章来源: Error with receiving xml strings via bluetooth in Android