So I'm programming an Android app that uses Bluetooth discovery of devices. Here is the code I use to start discovery.
try {
myBluetoothAdapter.startDiscovery();
Log.d("Bluetooth Started successfully","yes");
} catch (Error e) {
Log.d("FAILED","Ya failed mate");
e.printStackTrace();
}
I then register a BroadcastReceiver to watch for when devices are found. Here is my code for that
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);
The listView, arrayAdapter, and stringArrayList are just things I'm "logging" to.
The problem is that whenever I run that code I get this error and my code doesn't function. I'm assuming that the reason it doesn't function is because of this error.
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
Can someone tell me what this error means as well as how to fix it?
I also find other questions on Stack Overflow with errors that look very similar; like, instead of Bluetooth, it will be in the context of BOOT_COMPLETED, ACTION_POWER_DISCONECTED, or BATTERY_LOW. How are those similar to this.
In my case - Android 9 (API level 28) I had to define my BroadcastReceiver inside the code only to make it work.
Like this (added inside a service in my case - its not matter)
private MyReceiver myReceiver;
@Override
public void onCreate() {
myReceiver = new MyReceiver();
this.registerReceiver(myReceiver, new IntentFilter("android.intent.action.myreceiver"));
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(myReceiver);
}
private class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
try {
if (intent.getAction().equals("android.intent.action.myreceiver")) {
//logic goes here
}
} catch (Exception e) {
//some log goes here
}
}
}
Send the broadcast like this
Intent intentMy = new Intent();
intentMy.setAction("android.intent.action.myreceiver");
intentMy.putExtra("whatever", true);
sendBroadcast(intentMy);
Implicit broadcasts are not allowed anymore in Android Oreo and later.
An implicit broadcast is a broadcast that does not target that app specifically.
Instead of just using the intent-filter action name for constructing the Intent, search the package manager for the component providing the broadcast receiver:
Intent intent = new Intent("the intent-filter action name in the different app").setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
PackageManager packageManager = getPackageManager();
List<ResolveInfo> infos = packageManager.queryBroadcastReceivers(intent, 0);
for (ResolveInfo info : infos) {
ComponentName cn = new ComponentName(info.activityInfo.packageName,
info.activityInfo.name);
intent.setComponent(cn);
sendBroadcast(intent);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With