错误与Android中通过蓝牙接收XML字符串 [英] Error with receiving xml strings via bluetooth in Android

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

问题描述

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

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.

下面是我的code,其中我收到字符串

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'/&GT;&LT; HRG V = '75'** ** LT; /头&GT; **

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

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

感谢名单提前

推荐答案

我用下面的code这样做成功...

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

}

希望这有助于...

Hope this helps...

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

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