【问题标题】:How to know when bluetooth disconnected如何知道蓝牙何时断开连接
【发布时间】:2012-05-02 09:30:41
【问题描述】:

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

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

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

当我使用这个时:

BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)

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

【问题讨论】:

    标签: android bluetooth broadcastreceiver


    【解决方案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);
    

    【讨论】:

      【解决方案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;
      
                      }
                  }
              }
          };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-03
        • 2012-10-09
        • 2018-06-25
        • 1970-01-01
        相关资源
        最近更新 更多