how to receive bluetooth neckband play pause event in my VOIP app?
I've implemented below broadcast receiver in app.
Manifest file
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<receiver android:name=".MediaButtonReceiver"
android:exported="true"
android:priority="10000" >
<intent-filter >
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
public class MediaButtonReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
android.util.Log.d("TAG", "Bluetooth onPlay: "+intent.getAction());
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event != null && event.getAction() == KeyEvent.ACTION_DOWN) {
int keyCode = event.getKeyCode();
if (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY || keyCode == KeyEvent.KEYCODE_MEDIA_PAUSE) {
// Handle play/pause event
}
}
}
}
}
and register into calling activity like this
MediaButtonReceiver r = new MediaButtonReceiver();
registerReceiver(r, new IntentFilter(Intent.ACTION_MEDIA_BUTTON));
and also implemented below broadcast receiver in app
public class BluetoothHeadsetReceiver extends BroadcastReceiver implements BluetoothProfile.ServiceListener {
private static final String TAG = "BluetoothHeadsetReceiver";
private BluetoothHeadset bluetoothHeadset;
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (profile == BluetoothProfile.HEADSET) {
bluetoothHeadset = (BluetoothHeadset) proxy;
Log.d(TAG, "Bluetooth Headset connected");
}
}
@Override
public void onServiceDisconnected(int profile) {
if (profile == BluetoothProfile.HEADSET) {
bluetoothHeadset = null;
Log.d(TAG, "Bluetooth Headset disconnected");
}
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG, "Bluetooth Headset onReceive : "+action);
if (BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
int state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_DISCONNECTED);
if (state == BluetoothHeadset.STATE_CONNECTED) {
Log.d(TAG, "Bluetooth Headset connected");
} else if (state == BluetoothHeadset.STATE_DISCONNECTED) {
Log.d(TAG, "Bluetooth Headset disconnected");
}
} else if (BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED.equals(action)) {
int state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_AUDIO_DISCONNECTED);
if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED) {
Log.d(TAG, "Bluetooth Headset audio connected");
// Handle button click events or other actions here
} else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED) {
Log.d(TAG, "Bluetooth Headset audio disconnected");
}
}else{
Log.d(TAG, "Bluetooth Headset onReceive else : ");
}
}
public void register(Context context) {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null) {
bluetoothAdapter.getProfileProxy(context, this, BluetoothProfile.HEADSET);
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
filter.addAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
context.registerReceiver(this, filter);
} else {
Log.e(TAG, "Bluetooth is not supported on this device");
}
}
public void unregister(Context context) {
context.unregisterReceiver(this);
if (bluetoothHeadset != null) {
BluetoothAdapter.getDefaultAdapter().closeProfileProxy(BluetoothProfile.HEADSET, bluetoothHeadset);
}
}
}
Registered below code in Calling activity OnCreate() method.
MediaButtonReceiver r = new MediaButtonReceiver();
registerReceiver(r, new IntentFilter(Intent.ACTION_MEDIA_BUTTON));
if(gc.hasBTPermission(this, ConstantStrings.BLUETOOTH_PERMISSION)) {
if (BluetoothManager.getInstance().isBluetoothHeadsetAvailable()) {
headsetReceiver = new BluetoothHeadsetReceiver();
headsetReceiver.register(this);
BluetoothManager.getInstance().initBluetooth();
}
}
But not receive Bluetooth headset play, pause click event only receive bluetooth connected and disconnected event in app.
I want to do based on bluetooth click event call accept and hangup call but not receive event in app.
The Bluetooth HID profile/service is always handled by the underlying operating system of your device. Simply subscribe to the key events of your operating system. In Android an app can override the default behaviour by overriding
MediaSession.Callback.onMediaButtonEvent(Intent). In such a case the app can/needs to handle all API specifics on its own.