【问题标题】:Error with receiving xml strings via bluetooth in Android在 Android 中通过蓝牙接收 xml 字符串时出错
【发布时间】:2013-02-25 02:42:33
【问题描述】:

我正在开发一个应用程序,它通过蓝牙接收 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'/&gt;&lt;hrg v='75'****&lt;/head&gt;**

如果问题不清楚,请告诉我任何我会更新的内容

提前感谢

【问题讨论】:

    标签: android xml bluetooth arduino


    【解决方案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();
    
    }
    

    希望这会有所帮助...

    【讨论】:

      【解决方案2】:

      【讨论】:

        猜你喜欢
        • 2018-03-14
        • 2013-08-02
        • 1970-01-01
        • 1970-01-01
        • 2013-03-11
        • 1970-01-01
        • 2015-11-24
        • 2015-10-21
        • 2014-11-15
        相关资源
        最近更新 更多