How Android listens for key events on a Bluetooth headset

Source: Internet
Author: User

how Android listens for key events on a Bluetooth headset

Write in front:

Directly want the code very easy, you can directly pull the scroll bar to the bottom to be able to see. If you want to understand why, then follow the steps I have planned to understand. The following test environment with "Bluedio + red rice mobile phone"on hand.

1. Use of Bluetooth headset

The use manual of Bluetooth headset will have relevant specific use instructions, here to pick the key to explain. In addition to the power switch, there are typically three keys on the headset. For example, see the following:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqva2fuz2vhcg==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">

Each of them is a multifunction key. There are different functions in different situations. 1 the function of the number key includes: Start playing music / Stop inserting music / answer the phone / hang up the phone; 2 the function of the number key is: Add Volume / the previous song; 3 the function of the number key is to decrease the volume / next song.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqva2fuz2vhcg==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">

Note: The call mode is not included temporarily; other Bluetooth headsets are not necessarily the same.

2. Key implementation principle on Bluetooth headset

After the trial it will be clear that a button will have multiple functions. So what does the Android system say? In fact, for Android , there is only one "key" response per keystroke. It feels blurry to use, but in fact it's very clear to the system.

AVRCP full name (audio/video Remote Controlprofile). is a profile in the Bluetooth protocol . It can be seen from the name that it is mainly applied to audio/video control. Each button is not independent. Top-Song / Next song is only valid when playing music, that is, the "key value" will be sent to Android.

Based on keys from Linux to Android Analysis details for the key values:

Linux Scan Code function map string Android key value

00c8 start to play music Media_play keycode_media_play

00C9 201 Stop playing music Media_pause keycode_media_pause

00A3 163 Next song media_next keycode_media_next

00A5 165 -song media_previous keycode_media_previous

Android App Code, full test application:teskkey.

Summary: The1 number key will alternately send keycode_media_play/keycode_media_pause;2/3 The keycode_media_previous/will be sent separately when the music is played . Keycode_media_next. Suppose you want to use the keys on your Bluetooth headset. Ability to play silent music when receiving keycode_media_play to enable 2/3 number keys. This allows the complete reception of 3 kinds of key values of self-control . this detailed self-design (test code has been updated to include).

Note: This implementation is not necessarily universal, for example, I test in the depth of the custom MIUI , although the TestKey application, the system comes with the music player can still respond to the key value at the same time.


The TestKey source code has been updated to include monitoring of the Bluetooth headset keys. The implementation method is the above method of guessing, has been successfully verified. Play Music Reference "Android Multimedia Development - resource file playback ". :

It is necessary to note that the program with two music files in the/res/raw, the default is to play Lapple.mp3 a sound mp3 music file, Silence10sec.mp3 is a no sound 10 seconds music file, the actual application can use it.

Questions and Answers
1. Can this only be heard in the state of playing music?
A: Based on the above principles, these keys are also used when controlling the media. And according to the actual verification does not play music when the Bluetooth headset 2/3 number key is not sent to the Android device key value (from the bottom of Linux view). In summary, it is necessary to play music to activate it to the Android device to send a key value, in this case can play a "no sound" music file, so that can listen to the 2/3 number keys and can not affect the output of other sounds. You can play music in the foreground and stop playing in the background.

2. I am now mostly trying to listen to the key (number 1th key). Start a speech recognition feature when you're done pressing Bluetooth on key?
A: Under my test condition, the number 1th key can be heard normally. The 1th number key alternately sends the Keycode_media_play/keycode_media_pause key value.

This key does not need to be simulated to play the music can be normal to hear.

3.4 number of key monitoring methods

A: The so-called 4 1. First use testkey test application test button, test android

2. Assuming not, then use the adb shell getevent to see if the Linux Bottom can not get the key value. Then use the keys from Linux to android to determine How much of the key code is used on the Android top. Of course Suppose you really don't know how to listen, send me the Bluetooth headset. I'll make sure it's okay for you.


Note: In fact, the above article is entirely based on keys from Linux to Android test determined down. It was an article that stripped the Android shell of the process of "key" events. Method applies to all input events : various keys / touch / physical keyboards / The mouse waits for the input device. No certain Linux development experience is very ugly to understand and understand.

4. How to implement the Android background monitor button

Or: How to start an app and listen to the play key in the background since there is a scenario where the user needs to press the key to start the speech recognition function when driving.

This issue is actually beyond the scope of this article and is the issue of the distribution of keyboard events (keystrokes) for the corresponding layers of the Android system. Under normal circumstances. The key will only distribute keyboard events to the current app at the very end. That is, in the background you can not listen to the volume keys.

However, since this situation (background application monitoring button ) needs to exist, then there must be a reason for its existence.

For example, "Camera key", press the camera directly to the front desk. On the surface, the camera responds to the key, but from the implementation method, it is not operated by the key value. It has to be done by other means, such as broadcasting or whatever.

After clarifying the reason for this. Then it's good to do it. Look at this button to see if there is a broadcast, such as a radio that is sent out in the answering system; if not, then for a custom system you can add a broadcast to the system yourself. In short, the normal channel is no way in the background to listen to some should not be your supervisor to hear the keys.

MORE: Check out a bit. This button is broadcast. This allows the background to respond (no need for C/no root). Example I will not try, see the official Android sample randommusicplayer.

The focus is on this broadcast Android.intent.action.MEDIA_BUTTON.

20141029 MORE:

Originally did not want to update that Testkey , OK, I still updated a bit. A method for monitoring media-related keystrokes in the background. Not updated on the interface. View information printing through LogCat.

The above content is " fish ", the bottom of the " fishing " also provides:

The first step is to compile the execution randommusicplayerdirectlyand then discover that the broadcast is not actually received. Then inference is the system version number problem, it is possible that Randommusicplayer is not updated to fit 4.0 or more.

The problem is found here in Capture media button on the Android >=4.0 (works on 2.3) . The success of joining. And then integrate it into the testkey. The details of what was changed are viewed through the commit ID on Github .

20150108 MORE:

The source code for the test program is hosted on GitHub. This article finds the "TestKey" link, which is opened after the following example:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqva2fuz2vhcg==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

APK download

Update the source code and add the compiled APK file.


20150109 MORE:

Root-rooted Android device gets the key-value method
Assuming that the device is already root, you can get the key value at a lower level.
1. Download terminal emulator Http://

2. Execute the su-c getevent and press the key that needs to be tested Note:There are spaces in the middle of the su-c getevent.
3. There will be an output such as the following, in which the second-to-last line of the output data is followed by the second countdown is the original key value of the key
4. Let me know this key, and I'll figure out how to implement the corresponding function.


1. This command will listen to all input events touch screen/key/bluetooth Headset/mouse keyboard will be detected, so after the output command do not click on the touch screen or there will be a lot of output.

2. Normally, each keystroke will output 4 lines of information.

3. The last output of 4 lines is the output of the vol-. Let's say that your test did not achieve this effect, you have to control where the problem occurs.

4. Listen for examples of Bluetooth keys below

20150112 MORE:

Which key values will be passed over by Bluetooth

1. After connecting Bluetooth, assuming that you press one of the keys on the Bluetooth headset, your hand is actively playing the music, then the key value is transferred from the Bluetooth headset.

2. After playing the music, assuming that you can only pass a key on the Bluetooth headset, your phone will switch music, then the key value is transferred from the Bluetooth headset.

3. When playing music, assume that you can only pass a key on the Bluetooth headset, the screen of your phone to increase or decrease the volume, then the key value is transferred from the Bluetooth headset. Conversely, if the internal volume of the Bluetooth headset is changed, Android has no response whatsoever, Then it is the function that controls the volume of the Bluetooth headset, and does not escalate the key value to Android.

4. Connect the Bluetooth call, if you can hang up the phone by pressing a key, then it means that the key is transmitted from the Bluetooth headset.


A. A 4.0 version of the Bluetooth headset is more focused on power-saving, when the volume is controlled only by the volume of its own speakers increase or decrease, not to the Android system to report the need to increase or decrease the volume, then this button has never reported to Android the key value. (No button upload)

B. Some models of Bluetooth headphones do not control the volume itself, and when the volume key is pressed, the key value is passed to the Android device, which requires Android to increase or decrease the volume of the audio source to achieve volume control. (with button upload)

In both cases, the implementation principle is very different , although the user experience is not quite the same.

Additional knowledge:

Play volume/audio calls are two different specifications for Bluetooth headsets, which are normally supported at the same time, but sometimes only support the latter (also known as Mono Bluetooth headsets) to save power. Of course, some features will be further compressed.

20150113 more single channel headphones cannot use the above method to detect the key analysis
Bluetooth standard specification list: Https://
Here are a few frequently used specifications: (The specification can also be understood as a channel)
1._ Normal _ Play Music

2._ Normal _ key when playing music

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqva2fuz2vhcg==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
3. Keys for voice calls/and voice calls

Note: For mono headphones (the default is only to answer the phone) the principle of playing music through some software is to transfer the "music signal" through the "Voice call channel" to the headset. The headset essentially works in voice call mode.

Say 1000 10,000, for the third case of the non-key button how to detect, and listen to tell.


Here's a simple guess of the principle. In the 3rd case, the "key" is included in the voice signal of the call, it should be directly resolved by the phone application and the corresponding operation. Android4.2 The version number above will have a built-in no-interface application called BLUETOOTH.APK, which is the Android system to the Bluetooth device support core, all the specifications (profile) are resolved through it. So look at what it does with the phone app. ;)
Of course, it is easy to listen to the volume changes can also achieve curve monitoring, and so on, but this is not the focus of my research.

The other point is that the study of mono headphone keys is now just a pit to dig. When to bury the temporary difficult to say.

Write here first ("key" process during voice call)

Descriptive narrative: The process has been found. And guess the same. bluetooth resolves a specific "key" after it is processed directly. Nothing was notified to the outside world. The meaning can be guessed according to the highlighted English. The first picture is a variety of "key" events during voice calls. The second picture is the processing of the dial/redial key. Highlight the last number in the call log when you redial.

If you can access Google then you can also view my two snippets of code directly online. (another: I am based on 4.2.2 analysis), this is a simple analysis process, for how to listen in the app, next.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqva2fuz2vhcg==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

Voice Call related documentation:

1.GSM 07.07_v5.0.pdf

2. bluetooth.apk Source Code HFP

How Android listens for key events on a Bluetooth headset

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.