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