使用OutputStream发送后,Android中的蓝牙InputStream被损坏

问题描述

因此,我正在使用arduino收集一些数据并将其发送到android应用程序,以便可以将这些数据存储在文件中,从而使android成为一种数据记录器。 我为此使用hC-06,工作速度为115200波特/秒。当arduino每隔100毫秒发送一次数据块(每个块60字节)时,似乎一切正常。 当我通过使用OutputStream方法发送单个字节来“查询” arduino的某些特殊数据时,问题就开始了。从应用开始使用“ mmOutStream.write(buffer);”开始InputStream接收到的数据变得不稳定,以随机值(60字节,45个分支,100字节等)改变块大小 似乎使用OutputStream会损坏InputStream缓冲区... 有人经历过这个吗?提前解冻 代码下方:

    // 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,buffer).sendToTarget();

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

        }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)