Android 蓝牙连接错误(未找到 bt sock,scn 1)-无限循环

标签 android sockets bluetooth infinite-loop

我面临与上一个(未回答的)问题相同的问题,我在犹豫是否要对其进行编辑,因为我想添加更多信息。所以这是相关的问题:Android bluetooth connection fails (Error: no bt sock found, scn 1) .

尝试建立蓝牙连接时发生错误。 I 并不总是出现(为什么?),但它非常频繁。我正在使用 Eclipse Indigo 为 Android 2.3 编写此应用程序。

这是我在出现错误之前进行连接时得到的日志(似乎在错误发生之前连接成功):

03-29 15:14:52.205: D/BLZ20_ASOCKWRP(22027): asocket_connect
03-29 15:14:52.205: I/BLZ20_WRAPPER(22027): blz20_wrp_connect: s 53
03-29 15:14:52.205: D/BLZ20_WRAPPER(22027): blz20_wrp_connect:  fd (-1:53), bta -1, rc 0, wflags 0x0, cflags 0x0, port 0
03-29 15:14:52.205: I/BLZ20_WRAPPER(22027): __connect_prot_rfcomm: connecting to... 0440201b0800, rc chan 1
03-29 15:14:52.205: I/BLZ20_WRAPPER(22027): __connect_prot_rfcomm: non blocking mode
03-29 15:14:52.205: I/BTL_IFC(22027): send_ctrl_msg: [BTL_IFC CTRL] send BTLIF_BTS_RFC_CON_REQ (BTS) 23 pbytes (hdl 52)
03-29 15:14:52.205: D/BLZ20_WRAPPER(22027): blz20_wrp_connect: success
03-29 15:14:52.205: I/BLZ20_WRAPPER(22027): blz20_wrp_poll: nfds 2, timeout -1 ms
03-29 15:14:52.205: D/BLZ20_WRAPPER(22027): blz20_wrp_poll: pending connect fd (-1:53), bta -1, rc 1, wflags 0x0, cflags 0x1, port 0
03-29 15:14:52.205: D/BLZ20_WRAPPER(22027): btlif_wait_response: id(s) |BTLIF_BTS_RFC_CON_RSP|BTLIF_BTS_RFC_DISC_IND|
03-29 15:14:52.920: I/BTL_IFC(22027): btl_ifc_ctrl_rx: [BTL_IFC CTRL] recv BTLIF_BTS_RFC_CON_RSP (BTS) 22 pbytes (hdl 52)
03-29 15:14:52.920: I/BLZ20_WRAPPER(22027): btlif_ctrl_callback: btlif_ctrl_callback : msg id BTLIF_BTS_RFC_CON_RSP
03-29 15:14:52.920: E/BLZ20_WRAPPER(22027): ##### ERROR : btlif_process_con_rsp: no bt sock found, scn 1#####

这是我在连接失败时得到的日志(但没有错误)

03-29 15:20:37.100: D/BLZ20_ASOCKWRP(22027): asocket_abort [44,45,46]
03-29 15:20:37.100: I/BLZ20_WRAPPER(22027): blz20_wrp_shutdown: s 44, how 2
03-29 15:20:37.100: D/BLZ20_WRAPPER(22027): blz20_wrp_shutdown:  fd (-1:44), bta -1, rc 0, wflags 0x0, cflags 0x0, port 0
03-29 15:20:37.100: I/BLZ20_WRAPPER(22027): blz20_wrp_shutdown: shutdown socket
03-29 15:20:37.100: D/BLZ20_WRAPPER(22027): blz20_wrp_write: wrote 1 bytes out of 1 on fd 46
03-29 15:20:37.100: D/BLZ20_ASOCKWRP(22027): asocket_destroy
03-29 15:20:37.100: D/BLZ20_ASOCKWRP(22027): asocket_abort [44,45,46]
03-29 15:20:37.100: I/BLZ20_WRAPPER(22027): blz20_wrp_shutdown: s 44, how 2
03-29 15:20:37.100: D/BLZ20_WRAPPER(22027): blz20_wrp_shutdown:  fd (-1:44), bta -1, rc 0, wflags 0x0, cflags 0x0, port 0
03-29 15:20:37.100: I/BLZ20_WRAPPER(22027): blz20_wrp_shutdown: shutdown socket
03-29 15:20:37.100: D/BLZ20_WRAPPER(22027): blz20_wrp_write: wrote 1 bytes out of 1 on fd 46
03-29 15:20:37.100: I/BLZ20_WRAPPER(22027): blz20_wrp_close: s 46
03-29 15:20:37.100: D/BLZ20_WRAPPER(22027): blz20_wrp_close: std close (46)
03-29 15:20:37.100: I/BLZ20_WRAPPER(22027): blz20_wrp_close: s 45
03-29 15:20:37.100: D/BLZ20_WRAPPER(22027): blz20_wrp_close: std close (45)
03-29 15:20:37.100: I/BLZ20_WRAPPER(22027): blz20_wrp_close: s 44
03-29 15:20:37.100: D/BLZ20_WRAPPER(22027): blz20_wrp_close:  fd (-1:44), bta -1, rc 0, wflags 0x0, cflags 0x0, port 0
03-29 15:20:37.100: I/BLZ20_WRAPPER(22027): __close_prot_rfcomm: fd 44
03-29 15:20:37.100: I/BTL_IFC(22027): send_ctrl_msg: [BTL_IFC CTRL] send BTLIF_BTS_RFC_CLOSE (BTS) 8 pbytes (hdl 52)
03-29 15:20:37.100: D/BTL_IFC_WRP(22027): wrp_close_s_only: wrp_close_s_only [44] (44:-1) []
03-29 15:20:37.100: D/BTL_IFC_WRP(22027): wrp_close_s_only: data socket closed
03-29 15:20:37.100: D/BTL_IFC_WRP(22027): wsactive_del: delete wsock 44 from active list [ad439320]
03-29 15:20:37.100: D/BTL_IFC_WRP(22027): wrp_close_s_only: wsock fully closed, return to pool
03-29 15:20:37.100: D/BLZ20_WRAPPER(22027): btsk_dump_list:  fd (-1:53), bta -1, rc 1, wflags 0x900, cflags 0x0, port 0
03-29 15:20:37.100: D/BLZ20_WRAPPER(22027): btsk_free: success

在调试的过程中,我发现用于建立连接的ConnectThread在错误的情况下一直在运行,这意味着它进入了一个死循环。因此,如果另一个 Thread 试图取消并完成此 ConnectThread,应用程序将崩溃。

请在此处查看此 ConnectThread 的源代码,它是 Service 的内部类(与 BluetoothChat 示例非常相似):

/**
 * This thread runs while attempting to make an outgoing connection
 * with a device. It runs straight through; the connection either
 * succeeds or fails.
 */
private class ConnectThread extends Thread
{
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;

    public ConnectThread(BluetoothDevice device)
    {
        mmDevice = device;
        // Use a temporary object that is later assigned to mmSocket,
        // because mmSocket is final
        BluetoothSocket tmp = null;

        // Get a BluetoothSocket to connect with the given BluetoothDevice
        try
        {
            // SerialPortServiceClass_UUID is the app's UUID string, also
            // used by the server code
            tmp = device.createRfcommSocketToServiceRecord(SerialPortServiceClass_UUID);
        } catch (IOException e)
        {
            if (D) Log.e(TAG, "create RFCOMM failed", e);
            // here I do something to notify the UI that the connection has failed

            // Reset the ConnectThread because the connection has failed
            synchronized (DataReceiverService.this)
            {
                mConnectThread = null;
            }
        }
        mmSocket = tmp;
    }

    public void run()
    {
        if (D) Log.i(TAG, "BEGIN mConnectThread");
        setName("ConnectThread");

        // Cancel discovery because it will slow down the connection
        mBtAdapter.cancelDiscovery();

        try
        {
            // Connect the device through the socket. This will block
            // until it succeeds or throws an exception
            mmSocket.connect();
        } catch (IOException connectException)
        {
            // Unable to connect, close the socket and get out
            try {
                mmSocket.close();
            } catch (IOException closeException)
            {
                if (D) Log.e(TAG, "unable to close() socket during connection failure", closeException);
            }
            // here I do something to notify the UI that the connection has fqiled
            return;
        }

        // Reset the ConnectThread because we're done
        synchronized (DataReceiverService.this)
        {
            mConnectThread = null;
        }

        // Do work to manage the connection (in the ConnectedThread)
        connected(mmSocket, mmDevice);
    }

    /** Will cancel an in-progress connection, and close the socket */
    public void cancel()
    {
        try {
            mmSocket.close();
        } catch (IOException e)
        {
            if (D) Log.e(TAG, "close() of connect socket failed", e);
        }
    }
}

最佳答案

检查这个http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html

蓝牙设备

代表一个远程蓝牙设备。使用它通过 BluetoothSocket 请求与远程设备的连接或查询有关设备的信息,例如设备的名称、地址、类别和绑定(bind)状态。

关于Android 蓝牙连接错误(未找到 bt sock,scn 1)-无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9922328/

相关文章:

android - 为什么我的应用程序不调用电话?

android - 打开android图片库,不在选择模式。是否可以?

android - OpenNLP 的模型文件路径是什么?

java - 在线程中调用地理编码器构造函数时,连接崩溃

c - 使用 C 进行客户端服务器编程速度慢

Cocoa-如何使用 ARC 制作 TCP 套接字?

ios - iOS 中的 WebBLE 支持

android - 是否可以使用Android SDK或NDK重启手机?

iphone - HID 报告描述符(使用 iPhone)

.net - Windows上的蓝牙开发