i’m having a broadcast reciever:
private final BroadcastReceiver mediaReceiver = new MediaChangeReceiver(this);
and i’m adding filters to it
IntentFilter filter = new IntentFilter();
filter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
filter.addAction(Intent.ACTION_HEADSET_PLUG);
context.registerReceiver(mediaReceiver, filter);
and I have listener to listen for that calls
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case Intent.ACTION_HEADSET_PLUG:
//do something
Log.e ("test", "headset plug has been called");
break;
case BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED:
//do something
Log.e ("test", "ACTION_CONNECTION_STATE_CHANGED has been called");
break;
case AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED:
//do something
Log.e ("test", "ACTION_SCO_AUDIO_STATE_UPDATED has been called");
break;
default:
//do something
Log.e ("test", "default been called");
break;
}
}
but when I’m connecting BT headphones (after the broadcast receiver has been called)
I don’t get any of the broadcast receiver intents called
here is the full broadcast receiver class
public class MediaChangeReceiver extends BroadcastReceiver {
private static final String LOG_TAG = MediaChangeReceiver.class.getName();
private MediaChangeListener mListener;
public MediaChangeReceiver(MediaChangeListener listener) {
super();
mListener = listener;
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case Intent.ACTION_HEADSET_PLUG:
//do something
Log.e ("test", "headset plug has been called");
break;
case BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED:
//do something
Log.e ("test", "ACTION_CONNECTION_STATE_CHANGED has been called");
break;
case AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED:
//do something
Log.e ("test", "ACTION_SCO_AUDIO_STATE_UPDATED has been called");
break;
default:
//do something
Log.e ("test", "default been called");
break;
}
}
}
here is my intent in the manifest
<receiver android:name=".MediaChangeReceiver"
android:exported="false">
<intent-filter>
<action android:name="BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED" />
<action android:name="Intent.ACTION_HEADSET_PLUG" />
</intent-filter>
</receiver>
what am I doing wrong? can anyone help?
2
Answers
i was missing the
and the app needs to ask that permission I couldn't find a way to make that work without getting that Bluetooth permission
if someone found a way to check when Bluetooth is connected without asking for Bluetooth permission please write in this question thread
The issue seems to be with the way the intent filters are defined in the manifest file. You need to remove the package name prefix from the action names in the tag.
Try modifying the intent filter like this:
Merry coding!