3

我正在使用从蓝牙设备读取输入流的简单线程来调整 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);
                }
            }
        }  

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

4

0 回答 0