【问题标题】:Bluetooth connection timed out too soon?蓝牙连接超时太快?
【发布时间】:2011-10-14 23:09:06
【问题描述】:

大家好!

当我通过蓝牙连接嵌入式设备时遇到问题。 当我使用此代码连接时:

public void run() {
        Looper.prepare();
        try {
            // Connect the device through the socket. This will block
            // until it succeeds or throws an exception

                mmSocket.connect();
            if(!cancel){
                toMain = threadhandler.obtainMessage();
                toMain.arg1 = 1;
                threadhandler.sendMessage(toMain);
            }
        } catch (SecurityException e) {
            if(!cancel){
                Log.e("SecurityExcep", "Oh noes" , e);
                toMain = threadhandler.obtainMessage();
                toMain.arg1 = 2;
                threadhandler.sendMessage(toMain);
                Log.w("MESSAGE", e.getMessage());
            }
        }catch (IOException e) {
            //Bad connection, let's get the hell outta here
            try {
                Log.e("IOExcep", "Oh noes" , e);
                Log.w("MESSAGE", e.getMessage());
                if(!cancel){
                    mmSocket.close();
                    toMain = threadhandler.obtainMessage();
                    toMain.arg1 = 2;
                    threadhandler.sendMessage(toMain);
                }
                return;
            } catch (IOException e1) {
                Log.e("IOExcep2", "Oh noes" , e);
            }
        }
        try {
            mmSocket.close();
        } catch (IOException e) {
            Log.d("CONNECT_CONSTRUCTOR", "Unable to close the socket", e);
        }
        if(!cancel){
            toMain = threadhandler.obtainMessage();
            toMain.arg1 = 3;
            threadhandler.sendMessage(toMain);
        }
        Looper.loop();
        return;
        // Now it should be paired.. only thing to do now is let the user commit to the rest
    }

我搜索设备,找到它,然后通过mmSocket.connect() 连接它。之后,请求被发送到我输入PIN 的嵌入式设备。之后,如果我收到通知说设备正在尝试配对等,请求就会发送到手机。

首先,它不应该是通知!它应该是一个对话,我 猜猜这不是我的错。

其次,为什么我在收到通知约 20 秒后收到以下异常:

07-28 11:13:00.537: ERROR/IOExcep(1064): Oh noes
07-28 11:13:00.537: ERROR/IOExcep(1064): java.io.IOException: Connection timed out
07-28 11:13:00.537: ERROR/IOExcep(1064):     at android.bluetooth.BluetoothSocket.connectNative(Native Method)
07-28 11:13:00.537: ERROR/IOExcep(1064):     at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:204)
07-28 11:13:00.537: ERROR/IOExcep(1064):     at com.se.mainActivity.ConnectThread.run(ConnectThread.java:61)
07-28 11:13:00.537: WARN/MESSAGE(1064): Connection timed out
07-28 11:13:00.537: INFO/MESSAGEHANDLER(1064): Message 2 recieved, on ConnectThread
07-28 11:13:00.567: ERROR/BluetoothEventLoop.cpp(234): event_filter: Received signal org.bluez.Adapter:PropertyChanged from /org/bluez/377/hci0
07-28 11:13:00.567: ERROR/BluetoothEventLoop.cpp(234): event_filter: Received signal org.bluez.Adapter:DeviceRemoved from /org/bluez/377/hci0
07-28 11:13:00.567: DEBUG/BluetoothService(234): 00:0E:9F:88:A3:C1 bond state 11 -> 10 (9)
07-28 11:13:00.577: VERBOSE/BluetoothEventRedirector(1111): Received android.bluetooth.device.action.BOND_STATE_CHANGED
07-28 11:13:00.577: WARN/CachedBluetoothDeviceManager(1111): showUnbondMessage: Not displaying any message for reason:9
07-28 11:13:00.577: INFO/BluetoothEventLoop.cpp(234): agent_event_filter: Received method org.bluez.Agent:Cancel
07-28 11:13:00.627: VERBOSE/BluetoothEventRedirector(1111): Received android.bluetooth.device.action.PAIRING_CANCEL

【问题讨论】:

    标签: android timeout connection bluetooth


    【解决方案1】:

    发现是嵌入式设备在关闭套接字并发送异常之前有 20 秒的等待时间,从而终止了配对进程

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-22
      • 2020-01-06
      • 1970-01-01
      • 2019-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-18
      相关资源
      最近更新 更多