Java Android Bluetooth设备通知远程未绑定更改状态

Java Android Bluetooth设备通知远程未绑定更改状态,java,android,bluetooth,broadcastreceiver,bluecove,Java,Android,Bluetooth,Broadcastreceiver,Bluecove,问题:蓝牙设备配对或配对时会通知我。但是,我不知道如何在设备未配对时得到通知当我的应用程序与我的设备不配对时,我如何让它识别? 示例:假设我通过代码将surface平板电脑与android手机配对。我的广播接收器将注册操作\u绑定\u状态\u更改意图,并允许我以任何方式处理它现在,如果我想取消我的surface平板电脑的配对,我的手机不会通知我该设备已取消配对。如果我在解除配对后为设备检查,device.getBondState(),它等于BOND\u BOND(它显然不是)我可以让它在尝试连接

问题:蓝牙设备配对或配对时会通知我。但是,我不知道如何在设备未配对时得到通知当我的应用程序与我的设备不配对时,我如何让它识别?

示例:假设我通过代码将surface平板电脑与android手机配对。我的广播接收器将注册
操作\u绑定\u状态\u更改
意图,并允许我以任何方式处理它现在,如果我想取消我的surface平板电脑的配对,我的手机不会通知我该设备已取消配对。如果我在解除配对后为设备检查,
device.getBondState()
,它等于
BOND\u BOND
(它显然不是)
我可以让它在尝试连接到未配对的设备后识别它再次解除配对,然后应用程序崩溃,然后当我再次打开应用程序时,只有这样,它才会承认它是不成对的

场景:我注册了一个
BroadcastReceiver
来监听蓝牙设备引起的意图

getActivity().registerReceiver(broadcastReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
getActivity().registerReceiver(broadcastReceiver, new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED));
getActivity().registerReceiver(broadcastReceiver, new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED));
getActivity().registerReceiver(broadcastReceiver, new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED));
我使用此
广播接收器
处理这些操作:

final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        System.out.println("ACTION: "+ action);
        // When discovery finds a device

    if (BluetoothDevice.ACTION_FOUND.equals(action)) {
        list();

        // Get the BluetoothDevice object from the intent
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        System.out.println("FOUND: " + device.getName() + " STATE: " + device.getBondState());
        mapInput(device);

        // Add the name and the mac address of the object to the array adapter
        BTSimpleAdapter.notifyDataSetChanged();
    } else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        System.out.println("UPDATE Name " + device.getName() + " Value " + device.getAddress() + " Bond state " + device.getBondState());
        for (Map<String, String> entry : data) {
            if (entry.get("Name").equals(device.getName()) && entry.get("Value").equals(device.getAddress())) {
                if (device.getBondState() == BluetoothDevice.BOND_NONE) {
                    entry.put("Paired", "Unpaired");
                } else if (device.getBondState() == BluetoothDevice.BOND_BONDING) {
                    entry.put("Paired", "Pairing");
                } else {
                    entry.put("Paired", "Paired");
                }
                BTSimpleAdapter.notifyDataSetChanged();
            }
        }
    }
}
};
final BroadcastReceiver=new BroadcastReceiver(){
@凌驾
公共void onReceive(上下文、意图){
String action=intent.getAction();
System.out.println(“操作:+操作”);
//当发现发现发现设备时
if(BluetoothDevice.ACTION_FOUND.equals(ACTION)){
list();
//从intent获取BluetoothDevice对象
BluetoothDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA\u设备);
System.out.println(“找到:”+device.getName()+“状态:”+device.getBondState());
mapInput(设备);
//将对象的名称和mac地址添加到阵列适配器
btsimpledapter.notifyDataSetChanged();
}else if(蓝牙设备。动作\键\状态\更改。等于(动作)){
BluetoothDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA\u设备);
System.out.println(“更新名称”+device.getName()+“值”+device.getAddress()+“绑定状态”+device.getBondState());
用于(地图条目:数据){
if(entry.get(“Name”).equals(device.getName())和&entry.get(“Value”).equals(device.getAddress()){
if(device.getBondState()==BluetoothDevice.BOND\u NONE){
分录。卖出(“成对”、“未成对”);
}else if(device.getBondState()==BluetoothDevice.BOND\u BONDING){
输入。输入(“配对”、“配对”);
}否则{
输入。输入(“配对”、“配对”);
}
btsimpledapter.notifyDataSetChanged();
}
}
}
}
};

如何取消绑定设备:编程方式还是外部方式?@I从外部取消绑定这有助于我监控设备的配对。收到此广播后,我只需使用
BluetoothAdapter.getDefaultAdapter().getBondedDevices()
检查绑定设备的列表。