在Android中检测蓝牙耳机通话按钮按下

在Android中检测蓝牙耳机通话按钮按下

本文介绍了在Android中检测蓝牙耳机通话按钮按下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发呼叫应用程序.因此,我也需要从蓝牙设备接听/挂断电话.但是我只是无法从蓝牙耳机获得按键事件.我已经尝试过使用广播和音频管理器,但是只能获得播放/暂停,前和后按钮的回调.

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));
            }
        }
    }
}

清单是

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

我仅在活动处于活动状态时才需要获取事件,因此我也使用活动的onKeyDown和dispatchKeyEvent方法,但似乎无济于事.它给了我和以前相同的结果.一定有一种方法可以导致系统电话应用程序使此事件启动拨号程序.请给我建议一些有用的方法.

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 .

推荐答案

我认为您需要听KeyEvent.KEYCODE_CALLKeyEvent.KEYCODE_ENDCALL

可以在此处找到KeyEvent的完整列表: https://developer .android.com/reference/android/view/KeyEvent.html

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.

在您的dispatchKeyEvent中尝试以下操作:

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);
}

请记住,在处理静态KEYCODE_ {SOMEKEY}整数时,应使用event.getKeyCode();而不是event.getAction();.

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

这篇关于在Android中检测蓝牙耳机通话按钮按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 12:31