Android 蓝牙配对中未调用广播接收器

Android 蓝牙配对中未调用广播接收器,android,bluetooth,Android,Bluetooth,很简单。我唯一的代码是: final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTI

很简单。我唯一的代码是:

        final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)){
                mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                Log.d(TAG,"Device "+mDevice.getName()+" Was Found");
            }
        }
    };

但当我配对一个设备时,这不被调用。为什么?

请参阅您将发现如何连接以列出蓝牙相关事件

您是否在清单中添加了权限

<uses-permission android:name="android.permission.BLUETOOTH" />

您好,您需要为以下过滤器注册广播接收器

ACTION_BOND_STATE_CHANGED
然后在onReceive中

像这样添加它们

    if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)){
            mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (mDevice .getBondState() == BluetoothDevice.BOND_BONDED) {
            //means device paired
        }

   }
在这里阅读更多

编辑:在最后一条评论之后

您还需要添加

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(mReceiver, filter);

在安卓6.0及更高版本中,为了获取列表,您需要检查额外的权限访问位置。
如果没有此选项,您将不会调用带有“找到”操作的onReceive方法。

您是否在清单文件中注册了广播接收器?@Avi no。如何执行此操作?确定然后打印您检索的操作,并确保您检索的操作名称与“找到”操作相同,并确保您已注册您的广播,,示例:IntentFilter filter1=新的IntentFilter(BluetoothAdapter.ACTION\u STATE\u已更改);寄存器接收器(MBroadCastReceiver 1,filter1);我的IDE(android studio)似乎找不到这些与蓝牙相关的操作,例如:Hi。我得到的唯一动作是ACL\u已连接,然后是ACL\u已断开。再次,出于某种原因,我没有得到状态更改动作,只有ACL\u已连接,然后是ACL\u已断开。你误解了我。我在onReceive中记录我收到的操作,我收到的唯一操作是我提到的操作。意思是你的答案中的“如果”永远都不是真的哦,对不起。是否将筛选器添加到广播接收器。如此IntentFilter筛选器=新建IntentFilter(BluetoothAdapter.ACTION\u BOND\u STATE\u CHANGED);registerReceiver(mrReceiver,过滤器);在意图过滤器中,我认为BluetoothAdapter.ACTION\u BOND\u STATE\u CHANGED实际上应该是BluetoothDevice.ACTION\u BOND\u STATE\u CHANGED
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(mReceiver, filter);