0
我正在创建一个简单的应用程序,它将通过蓝牙进行通信。我已经创建了一个简单的活动,列出附近的设备与蓝牙打开,但不幸的是,我不知道如何检测当某些设备从蓝牙网络中消失(bt被关闭),以便我可以从中删除该项目列表。当Android上的蓝牙设备消失时检测到
这是我的代码,我写信给附近的BT设备添加到ListView:
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!
}
}
};
我发现的情况下不适用的行动意图,当设备被消失。也许可以使用ACTION_DISCOVERY_FINISHED,但是如何?
在此先感谢!