如何知道什么时候断开连接蓝牙(How to know when bluetooth disconne

2019-09-16 22:55发布

我试图“抓”的时候,蓝牙从设备断开连接。 即时通讯正在使用此代码:

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

但是,当IM断开通过关闭遥控设备或通过电话关闭蓝牙切换蓝牙也不会,如果statment进入这个。

使用该即时通讯时:

BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)

其还好吧工作(当aconnection是astablished)。 这是为什么,我怎么能使其工作? 谢谢!

Answer 1:

您已经注册了下面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);


Answer 2:

 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;

                }
            }
        }
    };


文章来源: How to know when bluetooth disconnected