我正在编写一个使用蓝牙设备发现的Android应用程序。以下是我用来启动发现的代码。
try {
myBluetoothAdapter.startDiscovery();
Log.d("Bluetooth Started successfully","yes");
} catch (Error e) {
Log.d("FAILED","Ya failed mate");
e.printStackTrace();
}
然后我注册了一个BroadcastReceiver来监听设备被发现的情况。以下是我的代码:
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
final ArrayList<String> stringArrayList = new ArrayList<>();
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getApplicationContext(),android.R.layout.simple_list_item_1,stringArrayList);
final ListView listView = findViewById(R.id.listView);
listView.setAdapter(arrayAdapter);
BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d("ACTION RECEIVED","Action was received");
Log.d("Device Name", String.valueOf(intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)));
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
stringArrayList.add(device.getName());
arrayAdapter.notifyDataSetChanged();
listView.invalidateViews();
}
}
};
registerReceiver(myReceiver,intentFilter);
listView、arrayAdapter和stringArrayList只是我“记录”的东西。
问题在于,每次运行这段代码时,我都会收到此错误,并且我的代码无法正常工作。我假设它不起作用的原因是由于这个错误。
W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.bluetooth.adapter.action.DISCOVERY_STARTED flg=0x10 } to com.verizon.messaging.vzmsgs/com.verizon.vzmsgs.receiver.DevicePairingListener
有人能告诉我这个错误是什么意思以及如何修复吗?
我还在Stack Overflow上找到其他类似的错误问题;例如,不是蓝牙,而是在BOOT_COMPLETED、ACTION_POWER_DISCONECTED或BATTERY_LOW的情况下。它们与此类错误有何相似之处。
android.bluetooth.adapter.action.DISCOVERY_STARTED
的<intent-filter>
的<receiver>
? - David Wasser