从Android平板电脑和HC-05无线电发射器的蓝牙输入流中提取数据存在问题。

huangapple go评论84阅读模式
英文:

Problems with extracting data from Bluetooth input stream, using android tablet and HC-05 radio transmitter

问题

以下是翻译好的部分:

我有一个项目,其中我有一个HC-05无线电每1秒传输约500到800个字符的XML格式数据。我在一个Android应用程序中接收它,将其转换为字符串,然后使用XmlPullParserFactory处理XML。一切工作得很好,除了每5到10秒只有部分传输的数据被接收。然后其余部分与下一个数据集一起在一秒钟后接收到。我有一些理论,但没有一个被证实。

... 目前正在研究这个理论 >

我不知道HC-05无线电的内部工作原理,所以我的问题可能听起来很愚蠢,问题是,无线电是否会在字符到达时立即通过蓝牙传输字符,还是会等待数据停止,然后将所有数据一次性发送?我想它可能有一个一次性传输多少字符的限制,因此会导致数据传输中断。但这不是一致的,所以这几乎不可能是问题。

关于这个问题的任何帮助都将非常感激。在需要澄清的地方问我。

以下是将输入流转换为字符串的代码片段,如果我有错误,请告诉我。

@Override
public void run() {
    InputStream inputStream;

    try {
        inputStream = mBTSocket.getInputStream();
        while (!bStop) {
            byte[] buffer = new byte[1200];
            if (inputStream.available() > 0) {
                inputStream.read(buffer);
                int i = 0;
                for (i = 0; i < buffer.length && buffer[i] != 0; i++) {
                }
                final String strInput = new String(buffer, 0, i);
            }

            Thread.sleep(100);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
英文:

I have a project where I have an HC-05 radio transmitting about 500 to 800 characters of XML formatted data every 1 second. I receive it in an android app, convert it to a String then process the XML using XmlPullParserFactory. Everything works great except every 5 to 10 seconds only a portion of the transmitted data gets received. then the rest of it gets received along with the next dataset a second later. I have a few theories but none prooven.

... Currently working on this theory >

I do not know how the HC-05 radio works internally so my commend might sound stupid, question is, does the radio instantly transmit the characters over Bluetooth as they arrive, or does it wait for data to stop then send all the data in one "packet"? I was thinking it might have a limit to how many characters it can transmit at once hence causing a break in data transmission. But it is not consistent so that can hardly be the issue.

Any help on the matter would be greatly appreciated. Ask me for clarity where needed.

Below is a snip from the code that converts the input stream to String, let me know if I have an error.

    @Override
    public void run() {
        InputStream inputStream;

        try {
            inputStream = mBTSocket.getInputStream();
            while (!bStop) {
                byte[] buffer = new byte[1200];
                if (inputStream.available() &gt; 0) {
                    inputStream.read(buffer);
                    int i = 0;
                    for (i = 0; i &lt; buffer.length &amp;&amp; buffer[i] != 0; i++) {
                    }
                    final String strInput = new String(buffer, 0, i);
                }

                Thread.sleep(100);
            }
        } catch (IOException e) {
// TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
// TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

答案1

得分: 0

好的,所以我过了一段时间回来解决了这个问题。我以250毫秒的间隔发送数据包,每次传输(根据数据长度)需要大约100毫秒的时间来完成。因此,使用Thread.sleep(100)时,新的数据字符串到达时它并不总是准备好。将它简单地减少到10就使得一切正常工作了。

英文:

Ok, so I got back to this a while later and solved to. I am sending packets out at 250ms intervals, and each transmit (depending on data length) takes a bit 100ms to compete. So with the Thread.sleep(100) it was not always ready when the new data string came in. Simply reducing it to 10 made it all work.

huangapple
  • 本文由 发表于 2020年10月27日 08:31:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64546798.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定