Я реализую поддержку Bluetooth через выполнение задачи, которая ищет устройства в фоновом режиме и предоставляет список, когда поиск завершен. Однако этот список иногда содержит запись No devices found
(добавляется только в том случае, если ACTION_DISCOVERY_FINISHED
получено, когда в списке нет устройств) перед перечислением других устройств!
private BroadcastReceiver mBlueToothInfoDiscoveryListener = new BroadcastReceiver() {
/**
* This is an overridden function called by the framework whenever there
* is some broadcast message for Bluetooth info discovery listener
*/
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
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) {
mNewBtDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
// When discovery is finished, change the Activity title
setProgressBarIndeterminateVisibility(false);
setTitle("device list");
if (mNewBtDevicesArrayAdapter.getCount() == 0) {
String noDevices = "No devices found";
mNewBtDevicesArrayAdapter.add(noDevices);
}
}
}
};
Я не ожидаю, что ACTION_DISCOVERY_FINISHED
когда-либо придет до события ACTION_FOUND
, так почему бы строку No devices found
добавить в список до того, как будет найдено устройство?