Issue
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.
Solution
I think it's because of Implicit Broadcast Ban of Android O , to solve this you can make your targetSDKVersion be less than 26 . You can find more here https://commonsware.com/blog/2017/04/11/android-o-implicit-broadcast-ban.html
Answered By - zht2005
Answer Checked By - Robin (JavaFixing Admin)