How Android listens for key events on a Bluetooth headset (go)

Source: Internet
Author: User

Source: How Android listens for key events on a Bluetooth headset

Write in front:

Just want the code very simple, you can directly pull the scroll bar to the bottom to see. If you want to understand why, then follow the steps I have planned to understand. The following test environments have "Bluedio + red rice phones" on hand.

1. Use of Bluetooth headset

The use manual of the Bluetooth headset will have the relevant detailed use instructions, here to pick the key to explain. In addition to the power switch, there are typically three keys on the headset. As shown below:

Each of them is a multifunction key and has different functions in different situations. The 1 key features include: Start playing music / stop inserting music / answer phone /hang up phone,2 number key function: Increase volume / previous song;3 the function of the number key is reduced volume / next song.

Note: The call mode is not included temporarily; other models of Bluetooth headsets are not necessarily identical.

2. Key implementation principle on Bluetooth headset

After the trial, you will understand that a button will have multiple functions, then How is the Android system represented? In fact , for the Android system, each keystroke will only have a unique "key value" response, the use of the feeling will be vague, but in fact, the system is very clear.

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

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

Linux Scan Code function map string Android key value

00c8 began 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, complete test application:teskkey.

Summary: the 1 key will alternately send keycode_media_play/keycode_media_pause;2/3 number will be sent separately when playing music Keycode_ media_previous/. Keycode_media_next. If you want to use the keys on the Bluetooth headset, you can play silent music when you receive the Keycode_media_play to enable the 2/3 number keys. This allows the complete reception of 3 kinds of key values of self-control. This specific self-design (the test code has been updated to include).

Note: This implementation is not necessarily generic, for example, I test in a deep custom MIUI, even though the TestKey application is launched, the system's own music player can still respond to the key values at the same time.

Update:

has updated TestKey source code, add to the Bluetooth headset button monitoring, the implementation method is the above-mentioned method, has been successfully verified. Play Music Reference "Android Multimedia Development -resource file playback ". :

One thing to note is 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.

Q and a 1. This can only be heard in the state of playing music? A: According to the above principle, these keys are only used in the control of the media, and according to the actual verification does not play music when the Bluetooth headset 2/3 key is not sent to the Android device key value (from the underlying Linux view). In summary, you need to play music to activate it to the Android device to send key values, in this case can play a "no sound" music file, so that you can listen to the 2/3 number keys and can not affect the output of other sounds. You can play music while in the foreground and stop playing in the background.
2. I am now mainly want to listen to get open key (1th number 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

Answer: The so-called4The number of the Bluetooth headset on the tested model in this article is not available, but it is possible to have other models on the Bluetooth headset. I have no way to test the verification, so here is a brief description of the "New button" key values to determine the idea: First use testkey test application test button, test android If the upper layer can get the corresponding key value. If you don't get it, use adb shell geteventlinux android To determine What is the key value code used in the upper layer? (Of course, if you really don't know how to listen, send me the Bluetooth headset, I'll make sure it's OK for you.) :)

Note: In fact, the above article is completely based on the key from Linux to Android test determined down. It's an article about the process of stripping the Android shell of the "key" event, which applies to all input events : various key / Touch / physical keyboard / mouse waiting input devices. It's hard to read and understand without a certain amount of Linux development experience.

4. How to implement the Android background monitor button

Or: How to start an app and listen to the play key all the time in the background because 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 Android System's distribution of keyboard events (keystrokes) on the application layer. Under normal circumstances, the key will only distribute keyboard events to the most current app, which means you can't listen to the volume keys in the background.

However, since this situation (background application of the Listening 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 button, but from the implementation method, it is not operated by key values, but by other means, such as broadcasting or other.

After understanding the reason, then want to achieve is good to run. Look at this button to see if there is a broadcast, such as the answer to the system sent out of the broadcast; if not, 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 of the keys should not be heard by your supervisor.

MORE: Check, this button is broadcast. This allows the background to respond (no need for C/no root). Example I will not try, see Android Official example 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 about the background monitoring media-related keys method. No updates on the interface, view printing information via LogCat.

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

The first step is to compile and run the Randommusicplayer directly, and then find that you do not receive the broadcast, and then judge the system version of the problem, it is possible that Randommusicplayer is not updated to fit 4.0 or more. The Capture Media button on the Android >=4.0 (works on 2.3) found here where the problem was, adding success. It is then integrated into the TestKey, specifically changing which content is viewed through the commit ID on Github.

20150108 MORE:

About the test program source code, is hosted on GitHub, the article found the "TestKey" link, opened following download:

APK download

Update the source code, add the compiled apk file.

Direct: https://github.com/kangear/TestKey/raw/master/apk/TestKey.apk

20150109 MORE:

rooted Android device gets the key value method If the device is already root, you can get the key value at the bottom. 1. Download the terminal emulator http://shouji.baidu.com/soft/item?docid=7309820 2. Run Su-c getevent and press the key you want to test. Note:There are spaces in the middle of su-c getevent. 3. The output is as follows, where the second-to-last line of the output data after each press is the second-lowest value of the key 4. Tell me about this key, and I'll tell you how to do it.

Note:

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. In general, 4 lines of information are output each time the key is pressed.

3. The final output of 4 lines is the output of the vol-. If your test does not achieve this effect, you have to compare yourself to where the problem occurs.

4. The following examples of listening Bluetooth keys

20150112 MORE:

Which key values will be passed over by Bluetooth

1. When Bluetooth is connected, if you press a key on the Bluetooth headset directly, your phone will play music automatically, then it means that the key value is transferred from the Bluetooth headset.

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

3. When playing music, if you can just pass a key on the Bluetooth headset, your phone screen display increases or decreases 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, That means that the button only has the ability to control 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.

Case:

A. A 4.0 version of the Bluetooth headset is more focused on power-saving, when the volume control is only 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 user experience is not very different, but the principle of implementation is very distinct .

Additional knowledge:

Play volume/audio calls are two different specifications in a Bluetooth headset, usually supported at the same time, but sometimes only support the latter (also known as mono Bluetooth headphones), in order to save power. Of course, some features will be further compressed.

20150113 More single channel headphones cannot use the above method to detect key analysis Bluetooth standard specification list: Https://en.wikipedia.org/wiki/List_of_Bluetooth_profiles Here are a few common specifications: (the specification can also be understood as a channel) 1._ Normal _ play Music

2._ Normal _ key when playing music

3. Keys for voice calls/and voice calls

Note: for mono headphones (which can only be answered by default) 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 is a simple speculation on the principle, the 3rd case of the "key" is included in the call voice signal, should be directly by the phone application analysis and the corresponding operation. The Android4.2 version will have a built-in no-interface application named BLUETOOTH.APK, which is the core of the Android system's support for Bluetooth devices, all of which are parsed by the specification (profile). So it's a bit of a look at what's going on between it and the phone app to know how.  ;) 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. Another point is that the study of mono headphone keys is now only a pit for digging. When to bury the temporary not to say.

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

Text description: The process has been found, and speculation is consistent. bluetooth resolves a specific "key" after the direct processing, there is no notification to the outside world. It is also possible to guess the meaning according to the highlighted English. The first picture is a variety of "key" events during voice calls, and the second picture deals with 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. Headsetstatemachine.java (another: I am based on 4.2.2 analysis), this is a simple analysis process, for how to listen in the app, next.

Reference documentation for voice calls:

1.GSM 07.07_v5.0.pdf

2. bluetooth.apk Source HFP

How Android listens for key events on a Bluetooth headset (go)

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: info-contact@alibabacloud.com 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.