软件导致连接中止 - Java InputStream读取缓冲区 - 蓝牙

时间:2013-08-15 09:16:45

标签: java android bluetooth serial-port

我正在调整Google的BluetoothChat示例中的代码,其中一个简单的线程是从蓝牙设备读取输入流。

大多数时候,我似乎永远都会不断地从设备中读取字节。但是,有时几轮之后读数就会停止。

08-15 17:09:28.123: E/BluetoothCommService(19749): disconnected
08-15 17:09:28.123: E/BluetoothCommService(19749): java.io.IOException: Software caused connection abort
08-15 17:09:28.123: E/BluetoothCommService(19749):  at android.bluetooth.BluetoothSocket.readNative(Native Method)
08-15 17:09:28.123: E/BluetoothCommService(19749):  at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:388)
08-15 17:09:28.123: E/BluetoothCommService(19749):  at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
08-15 17:09:28.123: E/BluetoothCommService(19749):  at java.io.InputStream.read(InputStream.java:163)
08-15 17:09:28.123: E/BluetoothCommService(19749):  at org.projectproto.yuscope.BluetoothCommService$ConnectedThread.run(BluetoothCommService.java:406)

从蓝牙设备读取数据的线程:

public class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;

        public ConnectedThread(BluetoothSocket socket) {
            Log.d(TAG, "create ConnectedThread");
            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) {
                Log.e(TAG, "temp sockets not created", e);
            }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }

        public void run() {
            Log.i(TAG, "BEGIN mConnectedThread");

            int bufSize = 5;
            byte[] buffer = new byte[bufSize];

            //Number of bytes read into the buffer return by mmInStream.read()
            int bytesReceived = 0;

            // Keep listening to the InputStream while connected
            while (true) {
                try {
                    // Read from the InputStream
                    bytesReceived = mmInStream.read(buffer);//this is line 406 where the exception is caught                    
                    // Send the obtained bytes to the UI Activity
                    if (bytesReceived>-1) {
                        mHandler.obtainMessage(MyUIAactivity.MESSAGE_READ, bytesReceived, -1, buffer).sendToTarget();
                    }

                } catch (IOException e) {
                    Log.e(TAG, "disconnected", e);
                    connectionLost();
                    // Start the service over to restart listening mode
                    BluetoothCommService.this.start();
                    break;

                } catch (Exception e) {
                    Log.e(TAG, "some weird exception thrown", e);
                }
            }
        }  

更新
在发布我自己之前,我显然已经阅读了this question。我的问题有一个非常具体的背景(Android手机和一些支持蓝牙2.0的健康设备之间的通信),这与其他问题不同。此外,微软的文章提到那里根本没有帮助我,更不用说Stackoverflow本身的答案对我来说不清楚。

0 个答案:

没有答案