We have an app that scans for Bluetooth devices. The code responsible for scanning should only run when bluetooth is enabled. Also we want to disable/enable this feature at any point in time.
We choose to implement a BroadcastReceiver that registers for the BluetoothAdapter.ACTION_STATE_CHANGED broadcast.
Here some of the problems we encountered:
Programmatically enable the BroadcastReceiver:
public void registerForBroadcasts(Context context) {
IntentFilter bluetooth = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
context.registerReceiver(this, bluetooth);
}
Declare the BroadcastReceiver in the Manifest
<receiver android:name="com.mypackage.BroadcastReceiver">
<intent-filter>
<action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
</intent-filter>
</receiver>
Declare the BroadcastReceiver in the Manifest as disabled + enable it programmatically
<receiver android:name="com.mypackage.BroadcastReceiver"
android:enabled="false" >
<intent-filter>
<action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
</intent-filter>
</receiver>
Then enable the component if you need it.
public void registerForBroadcasts(Context context) {
ComponentName component = new ComponentName(context, BroadcastReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(
component,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
My tests have shown, the state is persisted with the system, so the BroadcastReceiver will stay enabled. It combines the advantages of both methods.
Am I missing something, does this method seem legit?
A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.
Android BroadcastReceiver is a dormant component of android that listens to system-wide broadcast events or intents. When any of these events occur it brings the application into action by either creating a status bar notification or performing a task.
You register this broadcast receiver either in the manifest file or in the code. These events are intents. So, whenever these kinds of events happen, an intent is triggered.
The solution to this problem is a Broadcast Receiver and it will listen in on changes you tell it to. A broadcast receiver will always get notified of a broadcast, regardless of the status of your application. It doesn't matter if your application is currently running, in the background or not running at all.
The BroadcastReceiver cannot be disabled.
Sure it can. Use PackageManager
and setComponentEnabledSetting()
, as you did in your third scenario.
Am I missing something, does this method seem legit?
It is very legit, at least in terms of managing the BroadcastReceiver
. I don't know if there are any Bluetooth-specific hiccups, though I would doubt it. This technique is used for various broadcasts, such as only listening to ACTION_BOOT_COMPLETED
when you really do have something that needs to be done at boot time.
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