首页 > 解决方案 > bluetooth InputStream in android gets corrupted after sending with OutputStream

问题描述

So, I am using my arduino to collect some data and send it to my android app , so that I can store this data in a file, making my android a sort of datalogger. I am using an hC-06 for this, working at 115200 bauds/sec. Seems to be allright when the arduino sends the data chunks ( 60bytes every chunk) every 100ms aprox. The problem begins when I "query" the arduino for some special data, by sending a single byte with the OutputStream method. From the moment the app uses the "mmOutStream.write(buffer);" the data received by InputStream becomes unstable, varying the chunk size with random values ( 60 bytes, 45 butes, 100 bytes, etc...) It seems like using OutputStream kind of corrupts the InputStream buffer... Anyone has been through this? Thaks in advance below the code:

    // It handles all incoming and outgoing transmissions.
    private class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;
        private boolean send_request=false;
        private byte[] send_buffer;

        public ConnectedThread(BluetoothSocket socket, String socketType) {
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            // Get the BluetoothSocket input and output streams
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) { }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }

        public void run() {
            byte[] buffer;
            ArrayList<Integer> arr_byte = new ArrayList<Integer>();

            // Keep listening to the InputStream while connected
            while (true) {

                   try {
                        int data = mmInStream.read();
                        if(data == 0x0A) {

                        }
                        else if(data == 0x0D) {

                            buffer = new byte[arr_byte.size()];
                            for(int i = 0 ; i < arr_byte.size() ; i++) {
                                buffer[i] = arr_byte.get(i).byteValue();
                            }
                            // Send the obtained bytes to the UI Activity
                            mHandler.obtainMessage(BluetoothState.MESSAGE_READ
                                    , buffer.length, -1, buffer).sendToTarget();
                            arr_byte = new ArrayList<Integer>();
                        }
                        else {
                            arr_byte.add(data);
                         }
                    } catch (IOException e) {
                        connectionLost();
                        // Start the service over to restart listening mode
                        BluetoothService.this.start(BluetoothService.this.isAndroid);
                        break;
                    }


            }
        }

        // Write to the connected OutStream.
        // @param buffer  The bytes to write
        public void write(byte[] buffer) {
           try {

               mmOutStream.write(buffer);

             //  mmOutStream.close();  //vdv , close after writing to see if liberates memeory . uncommenting this causes the phone not to connect
            //TODO: investigate and solve why a single writing in the outstream causes the inputstream to get corrupted after a while.
                // Share the sent message back to the UI Activity
                mHandler.obtainMessage(BluetoothState.MESSAGE_WRITE
                        ,  -1, -1, buffer).sendToTarget();

            } catch (IOException e) {
               Log.d(TAG,"write exception");   //vdv
           }

        }

标签: androidbluetoothinputstreamoutputstream

解决方案


推荐阅读