How to connect ANY gamepad to Android smartphone. Plug&Play native support (Root required)

in #android7 years ago (edited)

Brief
Many of you might know about possibility of connecting the gamepads to the Andriod smartphone via USB OTG or Bluetooth.
But some of the gamepads are not recognized by the device properly or not recognized at all.
I know the reason why this happens and would like to share information on how to connect almost ANY gamepad and provide it with native support.

The main point of getting the native support
Here is an example I've experienced. If the device is recognized, but the buttons sending keyboard character keys - that means device has no key map and uses generic map for keyboard.
I could not use such gamepad for Grand Theft Auto, Max Payne or other games that supports gamepad due to this problem.
The only use case is retro-console emulator with custom mapping in the application's settings.

PS3 controller is supported natively by any Android device running above 3.0\4.0 and it let's you control the phone's menu and the games pretty naturally without sending keyboard characters. That's because this gamepad already has the key map in the Android system, as well as Xbox360 and some other popular controllers.
But actually we can replicate that behavior for any gamepad.

In my case I've used Motorola DroidMaxx (Android 4.0.4) to pair with random cheap USB gamepads connected via USB OTG and with bluetooth gamepad IPEGA.

Recognizing connected device
As you might remember from using Windows and installing drivers for unrecognized devices:
"vid_xxxx pid_xxxx" or "VEN_xxx&DEV_xxxx" is the ID of the device.

So the same thing is used in the Android Linux.
Folder /system/usr/keylayout contains a lot of files with the following names Vendor_xxxx_Product_xxxx.kl
You can use them as examples for your custom key maps for your controllers.
We need to determine the ID of the device and easiest way for it is just to connect it to Windows PC and look for it's IDs in the Device Manager.

The list of all connected input devices (Bluetooth as well) can be found in the following file in Android device's filesystem:
/proc/bus/input/devices
Once you've found the IDs, create a file with the name Vendor_xxxx_Product_xxxx.kl
I was able to determine IPEGA PG-9017 as Vendor_05ac_Product_3232.kl

Getting key codes of the buttons
To determine the key codes that we need to write in that text file, we can use the free APK such as Keytest (available on GitHub).

To find all available key codes, search on the Internet for Android KeyEvent

Making key map
Grab an example from the folder /system/usr/keylayout to make your custom key map.
Here is what I've made for IPEGA bluetooth gamepad:

key 99 BUTTON_X
key 100 BUTTON_Y
key 96 BUTTON_A
key 97 BUTTON_B
key 102 BUTTON_L1
key 103 BUTTON_R1
key 108 BUTTON_START
axis 0x00 X
axis 0x01 Y

Saving custom key map to device
It's time to put our custom key map to /system/usr/keylayout
Note that right permissions must be the same as for other files in that folder:
-rw-r--r-- or 644
Owner/Group:
root root

Conclusion
Now any game that supports gamepad should work with your gamepad directly without any additional applications, also you should be able to navigate through menu of the Android device with DPAD and buttons, everything like if it's the PS3 controller.

Feel free to ask your questions in the comments

Sort:  

Very informative post!

Thanks! Glad to know it's useful