如何捕捉蓝牙耳机与Android按键事件 [英] How to capture key events from bluetooth headset with android

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

问题描述

我的应用程序可以用普通的耳机来控制。它只是覆盖的onkeydown。但是,从蓝牙耳机,键盘事件是不会捕获 - 为什么?或者如何捕捉蓝牙键盘事件?

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?

日志猫显示如下,如果我的耳机preSS按钮:

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)

我也试过来处理媒体按钮动作,但是这是行不通的。我的想法是一个自由配置的按键映射:用户选择设置键我的应用程序审理的所有按键(硬件,多媒体按键,蓝牙耳机),那么用户presses一个键和事件/键code是存储配置。

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不工作答案:的 音量按钮必须由VOLUME_CHANGED_ACTION被捕获。问题是这样的意图被广播到其它应用程序和abortBroadcast()不工作(这仅适用于有序广播)。另一个问题是,对线耳机和手机触发的onReceive()两次(为什么?)蓝牙耳机触发一次键。 接下来的问题是蓝牙耳机3号键。它触发语音命令(S-声音开始在S3上),我试图捕捉对此许多不同的意图,但我不能接受这个按钮preSS,不知道是什么原因。 最后我想捕捉各种按钮和不希望他们被其他应用程序来处理(如使用的onkeydown和返回true)。

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).

推荐答案

添加广播监听器 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.

的IntentFilter 的优先级要高一些,其他的媒体播放器在手机中的优先级)

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

添加 android.permission.BLUETOOTH 许可清单,支持蓝牙耳机

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

收到后,你键入你必须手动中止使用广播 abortBroadcast()

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

但优先级和 abortBroadcast()工作得很好,只要每个应用程序只响应而 例如一些被播放。 但一些用户也期待一个默认播放器,以启动(或开始播放)按钮时preSS,如默认播放器,所以它可能发生在一个较高的优先级数量的一些应用程序不会让通过的意图来您的应用程序

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

的onReceive ,就可以得到该按钮事件

In the onReceive, you can get the button event with

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

key.getKeyAction()告诉你按钮是否被释放或pressed, key.getKey code()告诉哪个按钮是pressed。

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

如果你想处理一个按钮电缆耳机为好,也 对于关键code 键code_HEADSETHOOK

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

重写的onkeydown 方法的任何活动,并检查 在 KeyEvent.KEY code_MEDIA_KEY code_ pressed_key

例如

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

音量键集成示例 <一href="http://stackoverflow.com/questions/2874743/android-volume-buttons-used-in-my-application">Android - 在我的应用使用音量按钮
这其中可能需要批准

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" />


或者你可以通过下面的链接
尝试slimier实现    
<一href="http://android-developers.blogspot.in/2010/06/allowing-applications-to-play-nicer.html">Android开发者博客:操作遥控器的按键
      Android的故事:添加头戴式耳机按键支持Android应用程序


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天全站免登陆