Android 配对和取消配对蓝牙设备以进行配对/未配对时,无法获得广播接收器的回调

Android 配对和取消配对蓝牙设备以进行配对/未配对时,无法获得广播接收器的回调,android,bluetooth,android-bluetooth,Android,Bluetooth,Android Bluetooth,在下面的代码中,我发现了所有可用的BLE设备,并尝试根据请求配对和取消配对设备。但我并没有收到注册广播接收机的回调 公共类设备配置活动扩展了AppCompatActivity{ 语境; 工具栏; 回收视图回收视图; DeviceListAdapter mAdapter; 阵列列表; 相对延迟; //蓝牙 蓝牙适配器; 设置配对设备; 字符串设备名称,设备硬件地址; ProgressBar ProgressBar; 私人意图; 布尔mBound=false; 私人蓝牙串行服务; 字符串select

在下面的代码中,我发现了所有可用的BLE设备,并尝试根据请求配对和取消配对设备。但我并没有收到注册广播接收机的回调

公共类设备配置活动扩展了AppCompatActivity{
语境;
工具栏;
回收视图回收视图;
DeviceListAdapter mAdapter;
阵列列表;
相对延迟;
//蓝牙
蓝牙适配器;
设置配对设备;
字符串设备名称,设备硬件地址;
ProgressBar ProgressBar;
私人意图;
布尔mBound=false;
私人蓝牙串行服务;
字符串selectedWorkType=“”;
私有内部设备位置;
专用BTDevice实体连接设备;
私人蓝牙设备已连接到蓝牙设备;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u device\u配置);
init();
}
公共void init(){
上下文=这个;
toolbar=findviewbyd(R.id.toolbar);
recyclerView=findViewById(R.id.recycler\u视图);
rlBack=findviewbyd(R.id.rl\u back);
progressBar=findViewById(R.id.progressBar);
mList=新的ArrayList();
//方法调用
setListeners();
setUpRecyclerView();
设置蓝牙();
}
私有void setListeners(){
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
完成();
}
});
}
/**
*蓝牙设置
*/
私有无效设置蓝牙(){
progressBar.setVisibility(View.VISIBLE);
//获取蓝牙默认适配器并启动查找
bluetoothAdapter=bluetoothAdapter.getDefaultAdapter();
bluetoothAdapter.startDiscovery();
if(bluetoothAdapter==null){
Toast.makeText(上下文,“设备不支持蓝牙”,Toast.LENGTH_SHORT.show();
}
//检查蓝牙是否已启用
checkIfEnabled();
//在发现设备时注册广播
IntentFilter筛选器=新的IntentFilter(BluetoothDevice.ACTION\u已找到);
寄存器接收器(接收器、过滤器);
//注册设备状态已更改的广播
IntentFilter intent=新的IntentFilter(BluetoothDevice.ACTION\u BOND\u STATE\u已更改);
注册接收者(MPA接收者、意图);
}
私有void setUpRecyclerView(){
mAdapter=new DeviceListAdapter(上下文、mList、new DeviceListAdapter.DeviceListAdapterInterface(){
@凌驾
公共连接无效(蓝牙设备,int位置){
连接(装置、位置);
}
});
setLayoutManager(新的LinearLayoutManager(上下文));
recyclerView.setAdapter(mAdapter);
}
//检查是否启用
私有void checkIfEnabled(){
如果(!bluetoothAdapter.isEnabled()){
Intent enablebintent=新意图(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(enableBtIntent,0);
//Toast.makeText(getApplicationContext(),“已打开”,Toast.LENGTH_LONG.show();
}否则{
//Toast.makeText(getApplicationContext(),“已打开”,Toast.LENGTH_LONG.show();
}
}
专用void onConnection(蓝牙设备,内部位置){
//连接设备=设备;
btDevicePosition=位置;
if(device.getBondState()!=Constants.STATE\u已断开连接){
新建AlertDialog.Builder(上下文)
.setTitle(“连接到设备”)
.setMessage(“是否确实要连接到“+device.getName()+”?”)
.setPositiveButton(“是”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
配对设备(装置);
dialog.dismise();
}
})
.setNegativeButton(“取消”,新建DialogInterface.OnClickListener()){
@凌驾
public void onClick(DialogInterface dialog,int which){
dialog.dismise();
}
}).show();
}否则{
新建AlertDialog.Builder(上下文)
.setTitle(“断开与设备的连接”)
.setMessage(“是否确实要断开与“+设备.getName()+”?”的连接”)
.setPositiveButton(“是”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
非配对装置(装置);
dialog.dismise();
}
})
.setNegativeButton(“取消”,新建DialogInterface.OnClickListener()){
@凌驾
public void onClick(DialogInterface dialog,int which){
dialog.dismise();
}
}).show();
}
}
专用无效配对设备(蓝牙设备){
方法;
试一试{
method=device.getClass().getMethod(“createBond”,(Class[])null;
调用(设备,(对象[])null);
}捕获(NoSuchMethodException | IllegalacessException | InvocationTargetException e){
e、 printStackTrace();
}
mList.remove(btDevicePosition);
mList.add(
public BroadcastReceiver connectionCallbacks = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        switch (intent.getAction()) {
            case BluetoothCommands.STATE_CONNECTING:
                
                break;
            case BluetoothCommands.STATE_CONNECTED:
                
                break;
            
            case BluetoothCommands.STATE_DISCONNECTED:
                
                break;
            
            case BluetoothDevice.ACTION_FOUND:
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                
            //From here you can add devices to your list.

                break;

            case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
                if (intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR) == BluetoothDevice.BOND_BONDED) {
                    
                }
                break;
        }
    }
};
public IntentFilter getConnectionIntentFilter() {
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(BluetoothCommands.STATE_CONNECTING);
    intentFilter.addAction(BluetoothCommands.STATE_CONNECTED);
    intentFilter.addAction(BluetoothCommands.STATE_DISCONNECTED);
    intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
    intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
    return intentFilter;
}