如何知道蓝牙断开连接的时间

时间:2012-05-02 09:30:42

标签: android bluetooth broadcastreceiver

当蓝牙与设备断开连接时,我正试图“捕获”。 我正在使用此代码:

if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)){
            deleteNotification();
            setWarningState(WarningState.RedWarning);
            showNotification("You are parked");

但是当我通过关闭远程设备或通过关闭手机中的蓝牙切换来断开蓝牙连接时,它将不会进入此状态。

当我使用它时:

BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)

它的工作正常(当连接完成时)。 为什么这样,我怎么能让它发挥作用? 谢谢!

2 个答案:

答案 0 :(得分:5)

您是否注册了以下IntenFilters

IntentFilter f1 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
IntentFilter f2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
this.registerReceiver(mReceiver, f1);
this.registerReceiver(mReceiver, f2);

答案 1 :(得分:0)

 IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
        registerReceiver(mBluetoothReceiver, filter);

 private final BroadcastReceiver mBluetoothReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();

            if (action!=null && action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                        BluetoothAdapter.ERROR);
                switch (state) {
                    case BluetoothAdapter.STATE_OFF:

                        break;
                    case BluetoothAdapter.STATE_ON:

                        break;

                }
            }
        }
    };