在 Android 中通过蓝牙接收 xml 字符串时出错 [英] Error with receiving xml strings via bluetooth in Android

查看:22
本文介绍了在 Android 中通过蓝牙接收 xml 字符串时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,它通过蓝牙(来自 Arduino 和安卓手机)接收 xml 作为字符串.

i am developing an App which receives xml as String over bluetooth (from Arduino and android phones).

我从蓝牙收到无效/不完整的字符串.蓝牙被定义为 Android 服务.每次我收到一个字符串时,它都不是原始形式,无论我是从 arduino 还是其他 android 手机发送的.xml 解析函数正在工作,我已经检查过了.

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;

                    }
                };
            };

这是我正在使用的示例 xml 字符串

Here is a sample xml String i am using

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

我总是得到字符串但不完整 **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

提前谢谢

推荐答案

我用下面的代码成功地做到了...

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

  ....

然后在我从arduino发送的文本中检索到我想要的所有信息后:

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

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

来自我的 adruino 的字符串如下所示:::

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

}

希望这有帮助...

这篇关于在 Android 中通过蓝牙接收 xml 字符串时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆