5

我有一个BluetoothSocket正在与自定义设备(双向)通信。有时(在以 300 字节块传输 8 Mb 数据时,大约有 4 次发生这种情况),在文件传输过程中,蓝牙停止传输数据。对于大约 100-200 块数据(我没有确切的数量),OutputStream.write(byte[], int, int)每次调用后继续正常返回。没有生成异常。然后,调用OutputStream.write(byte[], int, int)锁。

该代码在华硕 eee 变压器 Pad TF101 上进行了测试。

我无权访问低OutputStream代码来查看正在发生的事情并进行调试。我的猜测是有一个缓冲区被填满但没有被清空。当缓冲区满时,write就会锁定。

有人知道发生了什么吗?

这是示例代码:

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

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

        setName( "BT Connected Thread" );

        // Get the BluetoothSocket input and output streams
        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } 
        catch (IOException e) 
        {
            Log.e( "BT GetStreams", e.getMessage() );
            e.printStackTrace();
        }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    public void run() {

        byte[] buffer = new byte[1024];

        int bytes;

        // Keep listening to the InputStream while connected
        while (true) {
            try {
                // Read from the InputStream
                bytes = mmInStream.read(buffer);

                // Send the obtained bytes to the UI Activity
                mHandler.obtainMessage( FirmwareHandler.COMMAND_READ, bytes, -1, buffer.clone() ).sendToTarget();

            } catch (IOException e) 
            {
                connectionLost();
                break;
            }
        }
    }

    public void write(byte[] buffer, int size) {
        try {
            Log.d(this.getClass().getSimpleName(), "Before Write, size = " + size + ", mmOutStream.toString() = " + mmOutStream.toString() + ", mmSocket..toString() = " + mmSocket.toString());

            mmOutStream.write(buffer, 0, size); // This is the call that locks.
            Log.d(this.getClass().getSimpleName(), "After Write");
            mmOutStream.flush(); // Make sure the command is sent entirely immediately
            Log.d(this.getClass().getSimpleName(), "After Flush");


        } catch (IOException e) 
        {
            e.printStackTrace();
        }
    }

    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
}

编辑:

一旦锁定发生,如果我关闭,然后打开远程Bluetooth设备,套接字就会解锁并出现 IO 异常。然后,当我尝试连接时,回到Bluetooth设备,我得到一个Bluetooth java.IOException:Service discovery failed. 恢复的唯一方法Bluetooth是重新启动平板电脑。

编辑#2:

如果我在有这样的锁定后手动断开套接字,它会毫无例外地返回。远程设备的连接状态保持连接。但是,如果我尝试连接到任何设备(不一定是锁定的设备,即使设备已连接已断电),我会收到以下异常:

09-13 09:34:45.342: E/BT Connection Assert(1864): Service discovery failed
09-13 09:34:45.342: W/System.err(1864): java.io.IOException: Service discovery failed
09-13 09:34:45.342: W/System.err(1864):     at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:397)
09-13 09:34:45.342: W/System.err(1864):     at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:207)
09-13 09:34:45.342: W/System.err(1864):     at com.brioconcept.hit001.communication.bluetooth.BluetoothService$ConnectThread.run(BluetoothService.java:177)

恢复的唯一方法似乎是重新启动平板电脑。

4

0 回答 0