简体   繁体   中英

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. 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:

@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). App crashes in 10% of the time.

Any reason behind it?

EDIT: while receiving InputStream sometimes instead of receiving 56 bytes I get 33 and 22 separetely

Answer to be found here: Android InputStream dropping first two bytes (modified 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. It turns out that even sleep(50) works in this condition. For some reason after this short sleep buffer is never divided nor lost. If it is bad coding please explain before downvoting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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