How does Android monitor key events of Bluetooth headsets?

Source: Internet
Author: User

How does Android monitor key events of Bluetooth headsets?

How does Android monitor key events of Bluetooth headsets?

Preface:

You can directly pull the scroll bar to the bottom to view the code. If you want to know why, follow the steps I have planned. The following test environment uses the "Bluedio + red rice mobile phone" on hand 」.

 

1. Use of Bluetooth headsets

The usage instructions for Bluetooth headsets are described in detail here. In addition to the power switch, the headset generally has three keys. As follows:

 

Each of them is a multi-function key and has different functions under different circumstances. Key 1 functions include playing music, stopping music insertion, answering phone calls, and hanging up the phone. Key 2 functions include adding volume or upstreaming music; key 3 can be used to reduce the volume/next song.

Note: The call mode is not included for the time being. The two models of Bluetooth headsets are not necessarily the same.

 

2. Implementation of buttons on Bluetooth headsets

After the trial, I will understand that one button has multiple functions, so how is it represented in the Android system? In fact, for the Android system, each button will only have a unique "key value" response, which is confusing to use, but it is very clear to the system.

The full name of AVRCP (Audio/Video Remote Control Profile) is a profile in the Bluetooth protocol. It can be seen from the name that it is mainly used for Audio/Video control. Each button is not independent. The upper-right or lower song is valid only when the music is being played. That is, the "key value" is sent to Android 」.

 

 

Analyze the specific key values from Linux to Android based on the buttons:

Linux scan code ing string Android key value

00c8 200 starts playing the music MEDIA_PLAY KEYCODE_MEDIA_PLAY

00c9 201 stop playing music MEDIA_PAUSE KEYCODE_MEDIA_PAUSE

00a3 163 next MEDIA_NEXT KEYCODE_MEDIA_NEXT

00a5 165-qu MEDIA_PREVIOUS KEYCODE_MEDIA_PREVIOUS

 

Android application code:

Case KeyEvent. KEYCODE_VOLUME_DOWN:

PrintToast ("get Key KEYCODE_VOLUME_DOWN (KeyCode:" + keyCode + ")");

Break;

Case KeyEvent. KEYCODE_VOLUME_UP:

PrintToast ("get Key KEYCODE_VOLUME_UP (KeyCode:" + keyCode + ")");

Break;

Case KeyEvent. KEYCODE_MEDIA_PLAY:

PrintToast ("get Key KEYCODE_MEDIA_PLAY (KeyCode:" + keyCode + ")");

Break;

Case KeyEvent. KEYCODE_MEDIA_PAUSE:

PrintToast ("get Key KEYCODE_MEDIA_PAUSE (KeyCode:" + keyCode + ")");

Break;

Case KeyEvent. KEYCODE_MEDIA_PREVIOUS:

PrintToast ("get Key KEYCODE_MEDIA_PREVIOUS (KeyCode:" + keyCode + ")");

Break;

Case KeyEvent. KEYCODE_MEDIA_NEXT:

PrintToast ("get Key KEYCODE_MEDIA_NEXT (KeyCode:" + keyCode + ")");

Break;

Full test application: TeskKey.

 

Conclusion: Keys 1 send KEYCODE_MEDIA_PLAY/KEYCODE_MEDIA_PAUSE; keys 2/3 send KEYCODE_MEDIA_PREVIOUS/. KEYCODE_MEDIA_NEXT when playing music. If you want to use the buttons on the Bluetooth headset, you can play the Silent Music when receiving the KEYCODE_MEDIA_PLAY to enable the 2/3 key. In this way, three types of key values can be fully controlled.Self-designed(The test code has been updated ).

 

Note: This implementation is not necessarily universal. For example, I tested it in the in-depth custom MIUI. Even though the TestKey application was started, the music player that comes with the system can still respond to the key value at the same time.


Update:

The TestKey source code has been updated and a listener for the Bluetooth headset buttons has been added. The implementation method is the speculative method mentioned above and has been successfully verified. For more information about playing music, see Android multimedia development-resource file playback. :


It should be noted that the program carries two music files in/res/raw. the program stores lapple.mp3a audio mp3music file, and silence10sec.mp3 is a audio-free 10-second music file, which can be used in practical applications.



How does android monitor buttons on headphones,

In fact, the keys on the earphones are mapped to common keyevents.

In android, how does a service listen to Keyboard Events?

Public Class BackGroundService extends Service implements View. OnKeyListener

I don't need to talk about it later.

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