Android Bluetooth key value adaptation (the most popular article)

catalogue

  1. Obtain the scan code of the key

      1. getevent displays three valid information in the result:

2.hid-input.c

3.KeyLayoutFile  --> .kl

    kl file format simple analysis (detailed explanation and other use methods, self search):

Summary:

      Take the two buttons of TV VOD in the figure below as an example. Only the file name is written for the files involved in the article. Because the paths of each scheme are different, please search the files globally by yourself.

  1. Obtain the scan code of the key

      Execute getevent - L (lowercase L) under android device serial port or adb shell, and then press the key (take VOD key as an example). The print results are as follows:

      1. getevent displays three valid information in the result:

            1)000c006b     The first four digits 000C are the key value type (please skip to 2 and read hid. H for details), and the last four digits are the Bluetooth key value;

            2)KEY_BLUE   If in Linux_ This key is defined in key. H, which will be displayed here. If Linux_ If there is no definition of this key in key. H, key will be displayed_ UNKNOWN.

            3)/dev/input/event4:   For the device node, you can view the KeyLayoutFile (i.e. the. kl file that maps the key value) used by this node through the dumpsys input command

console:/ # getevent -l                                                        
add device 1: /dev/input/event4
  name:     "Voice assistant"
could not get driver version for /dev/input/mouse2, Not a typewriter
add device 2: /dev/input/event3
  name:     "Hi Keypad"
could not get driver version for /dev/input/mouse1, Not a typewriter
add device 3: /dev/input/event1
  name:     "Hi mouse"
could not get driver version for /dev/input/mice, Not a typewriter
add device 4: /dev/input/event0
  name:     "Hi keyboard"
could not get driver version for /dev/input/mouse0, Not a typewriter
add device 5: /dev/input/event2
  name:     "qwerty"

/dev/input/event4: EV_MSC       MSC_SCAN             000c006b            
/dev/input/event4: EV_KEY       KEY_BLUE             DOWN                
/dev/input/event4: EV_SYN       SYN_REPORT           00000000            
/dev/input/event4: EV_MSC       MSC_SCAN             000c006b            
/dev/input/event4: EV_KEY       KEY_BLUE             UP                  
/dev/input/event4: EV_SYN       SYN_REPORT           00000000    

2.hid-input.c

    From getevent -l, it is known that the VOD key is 000c006b. For the detailed definition of the first four digits, please refer to hid.h under the kernel. Here are only a few:

#define HID_UP_KEYBOARD         0x00070000
#define HID_UP_LED              0x00080000
#define HID_UP_BUTTON           0x00090000
#define HID_UP_ORDINAL          0x000a0000
#define HID_UP_TELEPHONY        0x000b0000
#define HID_UP_CONSUMER         0x000c0000

  There is a function hidinput in HID input. C_ configure_ Usage: different codes will be executed according to different hid. When it is 0007(HID_UP_KEYBOARD), hid will be processed_ The array of keyboard [256] is the subscript of the array after converting the scan code to hexadecimal. The array element of the subscript of the array is the linux key value. My key value is 000c in front of it, so skip the above, and case goes to HID_UP_CONSUMER, 0x6b has been processed under this case, which means that this key represents key in linux_ BLUE.

static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_field *field,
				     struct hid_usage *usage)
{

	/**Omit**/
	switch (usage->hid & HID_USAGE_PAGE) {
	case HID_UP_UNDEFINED:
		goto ignore;
    /**Omit**/
    /**Omit**/
	case HID_UP_KEYBOARD:
		set_bit(EV_REP, input->evbit);

		if ((usage->hid & HID_USAGE) < 256) {
			if (!hid_keyboard[usage->hid & HID_USAGE]) goto ignore;
			map_key_clear(hid_keyboard[usage->hid & HID_USAGE]);
		} else
			map_key(KEY_UNKNOWN);

		break;
    

	case HID_UP_CONSUMER:	/* USB HUT v1.12, pages 75-84 */
		switch (usage->hid & HID_USAGE) {
		case 0x06a: map_key_clear(KEY_GREEN);		break;
		case 0x06b: map_key_clear(KEY_BLUE);		break;
		case 0x06c: map_key_clear(KEY_YELLOW);		break;
		case 0x06d: map_key_clear(KEY_ZOOM);		break;

    /**Omit**/
    /**Omit**/
}

That's key_ What is the key value of blue?

{"KEY_BLUE", 0x191},

Let's look at linux_key.h, search KEY_BLUE, key in my sdk_ The value of blue is 0x191, which is converted to decimal value 401.

3.KeyLayoutFile  --> .kl

    When getevent -l is used, it is known that the device node is / dev/input/event4. Then we execute dumpsys input. The results are as follows:

5: Voice assistant
      Classes: 0x8000012b
      Path: /dev/input/event4
      Enabled: true
      Descriptor: e0cab5a5e7db45d100777208b12fb1c21978adca
      Location: 
      ControllerNumber: 0
      UniqueId: 18:11:18:24:33:18
      Identifier: bus=0x0005, vendor=0x2b54, product=0x1600, version=0x0101
      KeyLayoutFile: /vendor/usr/keylayout/Vendor_2b54_Product_1600.kl
      KeyCharacterMapFile: /vendor/usr/keychars/Generic.kcm
      ConfigurationFile: 
      HaveKeyboardLayoutOverlay: false

    It can be seen from the results that the vendor of my Bluetooth remote control is 2b54 and product is 1600, so the system gives priority to loading KL (please Baidu for KL naming rules and loading priority). Some may not be found and will load Generic.kl and other files. Please modify the mapping relationship according to the file in the path after KeyLayoutFile:. If it is the default Generic.kl, it is recommended not to modify this file, You can create a new file according to vendor and product rules.

    Then we open the kl file pointed to, and the contents are as follows:

#key 222 "KEY_RED"                                                   
#key 107 "KEY_GREEN"                                               
# key 400 "KEY_YELLOW"                                                    
#key 401 KEY_BLUE                                                   
#key 401 KEY_TVSETUP            
key 402   CHANNEL_UP
key 403   CHANNEL_DOWN

    kl file format simple analysis (detailed explanation and other use methods, self search):

      key   It can be understood as a fixed format  

      222, 107 and other numbers correspond to the key values of linux

      KEY_BLUE   This corresponds to the key value of the Android layer

    Now that everything is clear, isn't it easy to handle the keys?

      Known linux_ The linux key value of VOD in key. H is 401. If you want this key to take effect, remove the previous # number and restart the device to make its mapping relationship take effect. If you want to change the function of the key value, you can directly put the key of android layer corresponding to 401 in kl_ Change blue to the key you want. (of course, you can also modify the key value of linux, but it is not recommended.)

Summary:

  1. Get the key value device node through getevent -l;

  2. Add or modify your key value in HID input. C;

  3. Find the corresponding kl file through the device node;

  4. Add or modify mapping relationship;

Tags: Android

Posted on Wed, 01 Dec 2021 07:16:39 -0500 by Visualant