与蓝牙设备断开连接时,Android应用程序冻结

时间:2014-04-02 18:33:54

标签: android bluetooth disconnect

我有一个连接蓝牙设备的Android应用程序接收到一些字节,并在收到所有数据后断开与设备的连接。该应用程序在诸如galaxy s3和galaxy note 2等更高级的手机上运行良好,但是当在旧型号(如s2)上运行应用程序时,应用程序在关闭输入流(即in.close()时冻结。 。应用程序冻结,之后不再继续。我在这里附加了我的代码并标记了应用程序冻结的区域。以前有人遇到过这个问题。

注意:断开发生在片段内而不是活动中。不确定这可能是个问题。


public void disconnectBTSocket() {
    try {
        BluetoothSocket btSocket= bluetooth.getBtSocket();
        InputStream in = btSocket.getInputStream();
        OutputStream out = btSocket.getOutputStream();
        Log.d("disconnect",
                "started : "
                        );
        Log.d("tag", "closing input stream");
        if (in != null) {

            in.close();
            //Gets Stuck here

        }
        Log.d("tag", "closed ");
        if (out != null) {
            out.close();
        }

        if (btSocket != null) {
            btSocket.close();
                    }
        Log.d("tag","finished disconnect");

    }

    catch (Exception e) {
        Log.d(tag,"Exception in disconnect with bluetooth socket communication : "
                        + e);
    }
    Log.d("disconnecct",
            "finished: "
                    );
}

我的logcot有这个,然后冻结。

 04-02 11:23:01.655: D/Metabolism(4900): closing inputstream
04-02 11:23:01.655: D/BluetoothSocket(4900): close(): android.bluetooth.BluetoothSocket@4078a8a0
04-02 11:23:01.655: D/BLZ20_ASOCKWRP(4900): asocket_abort [89,90,91]
04-02 11:23:01.655: I/BLZ20_WRAPPER(4900): blz20_wrp_shutdown: s 89, how 2
04-02 11:23:01.655: D/BLZ20_WRAPPER(4900): blz20_wrp_shutdown:  fd (-1:89), bta -1, rc 1, wflags     0x900, cflags 0x0, port 0
04-02 11:23:01.655: I/BLZ20_WRAPPER(4900): blz20_wrp_shutdown: shutdown socket
04-02 11:23:01.655: D/BLZ20_WRAPPER(4900): blz20_wrp_write: wrote 1 bytes out of 1 on fd 91

1 个答案:

答案 0 :(得分:0)

我想我解决了它。在新设备中,inputsocket和outputsocket为null,但在旧设备中则不是。不知道为什么但是手动使其无效。