1

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

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

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

当我使用这个时:

BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)

它工作正常(当建立连接时)。为什么会这样,我怎样才能让它工作?谢谢!

4

2 回答 2

6

您是否注册了以下 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);
于 2012-05-02T09:54:00.127 回答
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;

                }
            }
        }
    };
于 2019-03-08T11:00:09.977 回答