0

I am developing a calling app. So i need to pick/hangup call from bluetooth device too . But i just can not get the key press event from bluetooth headset. I have tried with Broadcast and audio manager but only getting play/pause, pre and next button callbacks .

public class MediaButtonIntentReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
            KeyEvent event = (KeyEvent) intent .getParcelableExtra(Intent.EXTRA_KEY_EVENT);

            if (event == null) {
                return;
            }

            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                //context.sendBroadcast(new Intent(Intents.ACTION_PLAYER_PAUSE));
            }
        }
    }
}

menifest is

<receiver android:name=".net.MediaButtonIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>

I need to get the event only when my activity is alive so i have use onKeyDown and dispatchKeyEvent methods of activity too but nothing seems to work. its giving me same result and previous. There must be a way cause System phone app is getting this event starting the dialer. Pls Suggest me some useful way to do it .

Komal12
  • 3,340
  • 4
  • 16
  • 25
ADM
  • 20,406
  • 11
  • 52
  • 83

2 Answers2

0

I think you need to listen for KeyEvent.KEYCODE_CALL and KeyEvent.KEYCODE_ENDCALL

A complete list of KeyEvents can be found here: https://developer.android.com/reference/android/view/KeyEvent.html

Keep in mind that call handling for most headsets (wired or not) use the play/pause button too for ending a call.

EDIT:

Try this in your dispatchKeyEvent:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_CALL) {
        Toast.makeText(this, "CALLING!", Toast.LENGTH_LONG).show();
        return true;
    }
    return super.dispatchKeyEvent(event);
}

Keep in mind that you should use event.getKeyCode(); instead of event.getAction(); when dealing with the static KEYCODE_{SOMEKEY} integers.

Sjoerd Dal
  • 696
  • 4
  • 14
  • Thx for the reply seems like a right way to do it after reading the documentation . But I am not getting this event callback in dispatchKeyEvent . Can you please tell me how to catch these two events – ADM Apr 10 '17 at 07:15
  • Not able to get event in dispatchKeyEvent . Have you tried this code ? cause i checked it its not working – ADM Apr 11 '17 at 05:25
  • @ADM Yes, I tried this in a new project on the Android emulator using android 7.1. I simulated the call button by using `adb shell input keyevent 5`. 5 in this case is the KeyCode for the call button. – Sjoerd Dal Apr 11 '17 at 06:33
  • Yeah but what i need to get event when the call button on bluetooth device is pressed . In case of bluetooth its not working .whereas I am able to get all 3 Media buttons callbacks play,next and pre . But can't get it for call button – ADM Apr 11 '17 at 06:52
  • @ADM Have you tried setting break points in your code to see what event type you get in dispatchKeyEvent?. Does dispatchKeyEvent get called at all? – Sjoerd Dal Apr 11 '17 at 07:03
  • i debugged it . Its not getting called – ADM Apr 11 '17 at 07:19
  • You stated that pressing that button opened the system phone app. I think the system phone app is doing that by using this Intent filter: `` `` Maybe you can try to use that to call functions in your activity but unless I know what actions are bound to that button I think this is the most I can do to help you. – Sjoerd Dal Apr 11 '17 at 07:37
  • @ADM Is it possible that the phone app has a higher priority than your app? take a look at the answer of this question: http://stackoverflow.com/questions/6287116/android-registering-a-headset-button-click-with-broadcastreceiver – Sjoerd Dal Apr 11 '17 at 10:24
  • Tried that too . I am using a hbs 730 device FYI . May be i should check it on some others . – ADM Apr 11 '17 at 10:37
  • @ADM That sounds like a good idea. Sadly I can't test this with real bluetooth devices since I don't own any. If you find anything please post it as your own answer so people with the same problem can solve it too. – Sjoerd Dal Apr 11 '17 at 12:25
  • Yeah sure . Didn't get anything yet. i'll keep digging. Thx 4 help – ADM Apr 11 '17 at 13:22
  • No luck .. I kept looking for solution but didn't find any . – ADM Apr 12 '17 at 09:56
  • @ADM found any solution? I have almost identical scenario. – SoftSan Apr 13 '22 at 18:22
0

Try to get event KeyEvent.KEYCODE_HEADSETHOOK

 if (event.getKeyCode() == KeyEvent.KEYCODE_HEADSETHOOK) {
  //do somethingenter code here
}