在Android上检测蓝牙设备何时消失
我正在创建一个通过蓝牙通信的简单应用程序。我已经创建了一个简单的活动,列出了蓝牙已打开的附近设备,但不幸的是,我不知道如何检测某些设备何时从蓝牙网络中消失(bt已关闭),以便从列表中删除该项目 这是我为将附近的BT设备添加到ListView而编写的代码:在Android上检测蓝牙设备何时消失,android,listview,bluetooth,android-arrayadapter,Android,Listview,Bluetooth,Android Arrayadapter,我正在创建一个通过蓝牙通信的简单应用程序。我已经创建了一个简单的活动,列出了蓝牙已打开的附近设备,但不幸的是,我不知道如何检测某些设备何时从蓝牙网络中消失(bt已关闭),以便从列表中删除该项目 这是我为将附近的BT设备添加到ListView而编写的代码: mNewDevicesArrayAdapter = new BluetoothDeviceArrayAdapter(this, 0, new ArrayList<BluetoothDevice>()); lvDiscovered
mNewDevicesArrayAdapter = new BluetoothDeviceArrayAdapter(this, 0, new ArrayList<BluetoothDevice>());
lvDiscovered = (ListView)findViewById(R.id.bt_dev_discovered_list);
lvDiscovered.setAdapter(mNewDevicesArrayAdapter);
...
// The BroadcastReceiver that listens for discovered devices and
// changes the title when discovery is finished
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// If it's already paired, skip it, because it's been listed already
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
mNewDevicesArrayAdapter.add(device);
}
// When discovery is finished, change the Activity title
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
// TODO show no devices found!
}
}
};
mNewDevicesArrayAdapter=new BluetoothDeviceArrayAdapter(这个,0,new ArrayList());
lvDiscovered=(ListView)findViewById(R.id.bt_dev_discovered_list);
lvDiscovered.setAdapter(mNewDevicesArrayAdapter);
...
//侦听发现的设备和设备的广播接收器
//在发现完成时更改标题
专用最终广播接收器mReceiver=新广播接收器(){
@凌驾
公共void onReceive(上下文、意图){
String action=intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(ACTION)){
//从Intent获取BluetoothDevice对象
BluetoothDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA\u设备);
//如果它已配对,请跳过它,因为它已列出
if(device.getBondState()!=BluetoothDevice.BOND\u BOND){
mNewDevicesArrayAdapter.add(设备);
}
//发现完成后,更改活动标题
}else if(BluetoothAdapter.ACTION\u DISCOVERY\u FINISHED.equals(ACTION)){
//TODO显示未找到任何设备!
}
}
};
当一个设备消失时,我没有发现适用的行动意图。也许可以使用“发现”完成的动作,但如何使用
提前谢谢 我找到了一种从以前发现的列表中删除这些设备的简单方法 扩展上面的代码后,我引入了一个
更新列表
,用于存储新发现的设备。当操作\u发现\u完成时
出现,我将使用此更新列表更新列表视图
...
private ArrayList<BluetoothDevice> btDevicesUpdateList;
...
// The BroadcastReceiver that listens for discovered devices and
// changes the title when discovery is finished
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// If it's already paired, skip it, because it's been listed already
// in the paired devices list
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
if(mNewDevicesArrayAdapter.getCount() == 0){
// if the list is empty we add the device immediately to it
mNewDevicesArrayAdapter.add(device);
}
btDevicesUpdateList.add(device);
}
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
mNewDevicesArrayAdapter.setItems(btDevicesUpdateList);
btDevicesUpdateList.clear();
mBtAdapter.startDiscovery();
}
}
}
// BluetoothDeviceArrayAdapter.java
public void setItems(ArrayList<BluetoothDevice> items){
this.items.clear();
this.items.addAll(items);
}
。。。
私有ArrayList btDevicesUpdateList;
...
//侦听发现的设备和设备的广播接收器
//在发现完成时更改标题
专用最终广播接收器mReceiver=新广播接收器(){
@凌驾
公共void onReceive(上下文、意图){
String action=intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(ACTION)){
//从Intent获取BluetoothDevice对象
BluetoothDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA\u设备);
//如果它已配对,请跳过它,因为它已列出
//在配对设备列表中
if(device.getBondState()!=BluetoothDevice.BOND\u BOND){
如果(mNewDevicesArrayAdapter.getCount()==0){
//如果列表为空,我们会立即将设备添加到其中
mNewDevicesArrayAdapter.add(设备);
}
btDevicesUpdateList.add(设备);
}
}
else if(BluetoothAdapter.ACTION\u DISCOVERY\u FINISHED.equals(ACTION)){
mNewDevicesArrayAdapter.setItems(btDevicesUpdateList);
btDevicesUpdateList.clear();
mBtAdapter.startDiscovery();
}
}
}
//BluetoothDeviceArrayaAdapter.java
公共无效集合项(ArrayList项){
this.items.clear();
this.items.addAll(items);
}
不可用的设备将不在列表中