简体   繁体   English

Android和Arduino之间的蓝牙数据丢失

[英]Bluetooth data loss between Android and Arduino

Straight to the point - my application due to some mysterious reason looses part of a data (String) when receiving an InputStream. 直截了当-我的应用程序由于某种神秘的原因而在接收InputStream时失去了部分数据(字符串)。 I am talking about Bluetooth connection here. 我在这里谈论蓝牙连接。 Most of the time, I am receiving correct string but once in while it is shortened. 大多数时候,我会收到正确的字符串,但有时会缩短它。 Weirdest thing in here is that I am printing every InputStream into my ScrollView and I can tell that the whole string is there... Nevertheless here is the code: 这里最奇怪的是,我正在将每个InputStream打印到ScrollView中,并且可以说出整个字符串都在里面...不过,这里是代码:

@Override
public void run() {
    InputStream inputStream;

    try {
        inputStream = mBTSocket.getInputStream();
        while (!bStop) {
            byte[] buffer = new byte[256];
            int bytes;
            if (inputStream.available() > 0) {
                bytes = inputStream.read(buffer);
                final String strInput = new String(buffer, 0, bytes);;

                mTxtReceive.post(new Runnable() {
                    @Override
                    public void run() {
                        mTxtReceive.append(strInput);

                        int txtLength = mTxtReceive.getEditableText().length();
                        if (txtLength > mMaxChars) {
                            mTxtReceive.getEditableText().delete(0, txtLength - mMaxChars);
                        }

                        scrollView.post(new Runnable() {
                            @Override
                            public void run() {
                                scrollView.fullScroll(View.FOCUS_DOWN);
                            }
                        });
                        }
                    });

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        weather = strInput.split(",");
                        mTxtHumidity.setText(weather[0]);
                        mTxtTemperatureDHT.setText(weather[1]);
                        mTxtPressure.setText(weather[2]);
                        mTxtLux.setText(weather[3]);
                        mTxtRainMM.setText(weather[4]);
                        mTxtRainDaily.setText(weather[5]);
                        mTxtWSKPH.setText(weather[6]);
                        mTxtWGKPH.setText(weather[7]);
                        mTxtWSAVG2.setText(weather[8]);
                        mTxtWGAVG10.setText(weather[9]);

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

}

The problem in here is null exception on array weather as according to my app I am sometimes accessing items out of boundry ( weather length is 6/7/8 most of the time once it errors). 这里的问题是数组weather为空异常,因为根据我的应用程序,我有时会越界访问项目(一旦出错,大多数时候weather长度为6/7/8)。 App crashes in 10% of the time. 应用程序在10%的时间内崩溃。

Any reason behind it? 背后有什么原因吗?

EDIT: while receiving InputStream sometimes instead of receiving 56 bytes I get 33 and 22 separetely 编辑:有时接收InputStream而不是接收56个字节,我分别得到33和22

Answer to be found here: Android InputStream dropping first two bytes (modified BluetoothChat) 在此处找到答案: Android InputStream删除前两个字节(修改后的BluetoothChat)

Adding 新增中

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

solves only for non-dynamic data exchange between Android and Arduino due to sleep method. 由于sleep方法,仅解决了Android与Arduino之间非动态数据交换的问题。 It turns out that even sleep(50) works in this condition. 事实证明,即使sleep(50)在这种情况下也可以工作。 For some reason after this short sleep buffer is never divided nor lost. 由于某种原因,此短暂的sleep缓冲区永远不会被分割或丢失。 If it is bad coding please explain before downvoting. 如果编码不正确,请在投票之前解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM