如何使用android从蓝牙耳机中捕获关键事件 [英] How to capture key events from bluetooth headset with android

查看:47
本文介绍了如何使用android从蓝牙耳机中捕获关键事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用可以通过普通耳机控制.它只是覆盖onKeyDown".但是没有捕获来自蓝牙耳机的关键事件 - 为什么?或者如何捕捉蓝牙按键事件?

如果我按下耳机上的按钮,日志猫"会显示以下内容:

蓝牙 AT recv(3043): AT+VGS=15AudioPolicyManagerBase(13654):FM 收音机录音关闭AudioService(2261):sendVolumeUpdate,isKeyguardLocked...不更新音量面板.VolumePanel(2261):通过 MSG_VOLUME_CHANGED 改变音量VolumePanel(2261):onVolumeChanged(streamType:6,标志:0)VolumePanel(2261): 调用 setChangeSeekbarColor(false)

我也尝试处理媒体按钮操作,但这不起作用.我的想法是一个免费的可配置键映射:用户选择设置键",我的应用程序在所有键(硬件、媒体按钮、蓝牙耳机)上听到,然后用户按下一个键,事件/键代码存储在配置中.

总结不工作答案:音量按钮必须由VOLUME_CHANGED_ACTION"捕获.问题是这个意图被广播到其他应用程序并且 abortBroadcast() 不起作用(它仅适用于有序"广播).另一个问题是有线耳机和电话上的按键触发 onReceive() 两次(为什么?)蓝牙耳机触发一次.下一个问题是蓝牙耳机的第三个键.它会触发语音命令(s-voice 从 s3 开始),我试图捕捉到许多关于此的不同意图,但我无法接收"此按钮按下并且不知道为什么.最后,我想捕获各种按钮,不希望它们被其他应用程序处理(例如使用 onKeyDown 并返回 true).

解决方案

MEDIA_BUTTON 添加广播监听器:

<action android:name="android.intent.action.MEDIA_BUTTON"/></意图过滤器>

您应该在您的应用程序中注册您的广播接收器(而不是在清单文件中).否则,Google 音乐播放器将接收您的广播并播放.

您的 IntentFilter 优先级应该高于您手机中其他媒体播放器的优先级)

在manifest中添加android.permission.BLUETOOTH权限以支持蓝牙耳机

收到您的密钥后,您必须使用 abortBroadcast() 手动中止广播.

然而,优先级和 abortBroadcast() 工作正常,只要每个应用程序只在例如有东西在玩.但是一些用户还希望在按下按钮时启动(或开始播放)默认播放器",就像默认播放器一样,因此可能会发生某些具有更高优先级编号的应用程序不会让意图通过您的应用程序

onReceive中,可以用

获取按钮事件

KeyEvent key = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);

key.getKeyAction() 告诉您按钮是释放还是按下,key.getKeyCode() 告诉您按下了哪个按钮.

如果您还想处理单按钮有线耳机,也可以看关键代码KEYCODE_HEADSETHOOK

覆盖任何活动中的 onKeyDown 方法并检查KeyEvent.KEYCODE_MEDIA_KEYCODE_pressed_key

例如

 boolean onKeyDown(int keyCode, KeyEvent event) {AudibleReadyPlayer abc;开关(键码){案例 KeyEvent.KEYCODE_MEDIA_FAST_FORWARD://快进代码返回真;案例 KeyEvent.KEYCODE_MEDIA_NEXT://下一个代码返回真;案例 KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE://播放/暂停代码返回真;案例 KeyEvent.KEYCODE_MEDIA_PREVIOUS://之前的代码返回真;案例 KeyEvent.KEYCODE_MEDIA_REWIND://倒带代码返回真;案例 KeyEvent.KEYCODE_MEDIA_STOP://停止代码返回真;}返回假;}

音量键集成示例Android - 我的应用程序中使用的音量按钮
这个可能需要权限

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>


或者您可以通过以下链接尝试更纤薄的实现

Android 开发者博客:处理遥控器按钮
Android Tales:为您的 Android 应用程序添加耳机按钮支持

My app can be controlled by normal headset. It simply overrides "onKeyDown". But key events from bluetooth headset are not captured - why? Or how to capture bluetooth key events?

the "log cat" shows the following if i press button on headset:

Bluetooth AT recv(3043): AT+VGS=15
AudioPolicyManagerBase(13654): FM radio recording off
AudioService(2261): sendVolumeUpdate, isKeyguardLocked...Not to update Volume Panel.
VolumePanel(2261): change volume by MSG_VOLUME_CHANGED
VolumePanel(2261): onVolumeChanged(streamType: 6, flags: 0)
VolumePanel(2261): Call setChangeSeekbarColor(false)

i also tried to handle media button actions but this isn't working. my idea is a free configurable key mapping: the user chooses "set key" my app hears on all keys (hardware, media buttons, bluetooth headset) then the user presses a key and the event/key code is stored in config.

Summerizing not working Answers: Volume buttons must be captured by "VOLUME_CHANGED_ACTION". The problem is this intents are broadcasted to other apps and abortBroadcast() doesn't work (it works only for "ordered" Broadcasts). Another problem is that keys on cable headset and on phone trigger onReceive() twice (why?) the bluetooth headset trigger it once. The next Problem is the 3rd key on Bluetooth headset. It triggers voice-command (s-voice starts on s3), i tried to capture many different intents regarding this but i can't "receive" this button press and don't know why. At the end i want capture all kinds of buttons and don't want them handled by other apps (like using onKeyDown and returning true).

解决方案

Add a broadcast listener to MEDIA_BUTTON:

<intent-filter android:priority="<some number>"> 
   <action android:name="android.intent.action.MEDIA_BUTTON" /> 
</intent-filter> 

You should register your broadcast receiver inside your application (not in manifest file). Otherwise Google Music player will catch your broadcast and aboard it.

Your IntentFilter priority should be higher that other media players priorities in your phone)

Add android.permission.BLUETOOTH permission in manifest to support Bluetooth headset

After received you key you have to manually abort the broadcast using abortBroadcast().

However priorities and abortBroadcast() work fine as long as each app only responds while e.g. something is played. But several users also expect a "default player" to be launched (or start playing) upon button press, like the default player, so it might happen some app with a higher priority number won't let the intent come through to your app

In the onReceive, you can get the button event with

KeyEvent key = (KeyEvent) 
intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); 

key.getKeyAction() tells you whether the button was released or pressed, key.getKeyCode() tells which button is pressed.

If you want to handle single button cable headsets as well, also regard the key code KEYCODE_HEADSETHOOK

Override the onKeyDown method in any activity and check for the KeyEvent.KEYCODE_MEDIA_KEYCODE_pressed_key

e.g.

 boolean onKeyDown(int keyCode, KeyEvent event) { 
            AudibleReadyPlayer abc; 
            switch (keyCode) { 
            case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: 
                    // code for fast forward 
                    return true; 
            case KeyEvent.KEYCODE_MEDIA_NEXT: 
                    // code for next 
                    return true; 
            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: 
                    // code for play/pause 
                    return true; 
            case KeyEvent.KEYCODE_MEDIA_PREVIOUS: 
                    // code for previous 
                    return true; 
            case KeyEvent.KEYCODE_MEDIA_REWIND: 
                    // code for rewind 
                    return true; 
            case KeyEvent.KEYCODE_MEDIA_STOP: 
                    // code for stop 
                    return true; 
            } 
            return false; 
    } 

Volume key integration example Android - Volume Buttons used in my application
This one may need permission

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />


Or you can try slimier implementations over the following link

Android Developer Blog : Handling remote control buttons
Android Tales : Add Headset button support to your Android application

这篇关于如何使用android从蓝牙耳机中捕获关键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆