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:

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. 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: increase the volume / the previous song; 3 the function of the number key is to decrease the 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 will understand that a button will have multiple functions, then the android How is it expressed in the system? In fact, for android system, each keystroke will only have a unique The "key value" response, the use of the feeling will be vague, but in fact, for the system is very clear.

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

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

Complete 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. 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 is specifically designed by itself.

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.

How Android listens for key events on a Bluetooth headset

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.