我正在创建一个简单的应用程序,它将通过蓝牙进行通信。我已经创建了一个简单的活动,列出了附近打开了蓝牙的设备,但不幸的是,我不知道如何检测到某些设备何时从蓝牙网络中消失(bt 被关闭),以便我可以从名单。
这是我编写的将附近的 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 意图。也许可以使用 ACTION_DISCOVERY_FINISHED,但是如何使用呢?
提前致谢!