BluetoothSocket在OutputStream.write()上停止传输

时间:2012-09-06 19:54:24

标签: android bluetooth

我有一个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();
        }
    }
}

修改

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

修改#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)

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

0 个答案:

没有答案