37

I have a game without built-in controller support, with touch-based controls. I would like to use a gamepad with it and get some hands-on programming experience in Android.

I want to figure out if I can have my app listen for controller events, and forward them to my games.

I learned how to process controller events. Then I found out I cannot forward them due to security - programmatically sending touch events is now allowed for good reasons.

According to this blog (its SSL cert expired), there are three ways to inject touch events:

  1. Using internal APIs (prone to change, need root to send to another app)
  2. Using the public API, Instrumentation (need root to send to another app)
  3. Write directly to /dev/input/eventX (need root to change permissions)

Other methods I have found include building your own platform, so you can set your application as a system app and sign it with the platform certificate, to provide it with the required permissions.

Or wrapping another application within your own, but using Instrumentation to send touch events there still require root.

I have also read about the Accessibility API, but it looks like it is considered a misuse of the API and maybe impossible in the future.

I would rather not root my device, as many games have issues if they detect root.

I was about to give up, but then I found apps like these:

  1. Touch4Gamepad - This one makes use of hardware called the Arduino Board. Demo of Touch4Gamepad
  2. Octopus - This one seems to be the approach I want to try, but it has pretty polarizing comments. It also does not work with Google Play service. Demo of Octopus
  3. DRO-CON - This one seems to take an entirely different approach by doing it via a connected PC.
  4. TeamViewer - It seems to be able to remote control Android including sending touch events. According to this this thread, TeamViewer struck a deal with phone manufacturers to get the required permissions. Supported Manufacturers for Remote Control

Despite Android security, options 2 seem to be able to do it regardless. How is it doing it?

EDIT: I downloaded Octopus' APK and took a look inside. It includes a file named libinject.so... so that's where it is doing its magic. I have a new direction to research now, but if anyone has links to resources related to this I would really appreciate it.

W. Churchill
  • 346
  • 1
  • 7
  • 28
KC Wong
  • 2,410
  • 1
  • 18
  • 26
  • 2
    managed to get figure out this? – hepizoj Mar 19 '19 at 05:50
  • 1
    @hepizoj: Sadly, no. Octopus doesn't play nice with Google Play, so it is useless. TeamViewer got it done by making deals with manufacturers. I tried asking about it on Arduino Board forum, but no one replied. Unsure if it will work, I don't want to not buy an Arduino Board just yet. – KC Wong Mar 21 '19 at 08:07
  • 1
    Yea, but what about libinject.so? How octopus doing that magic? Octopus doesn't require root access, system app privileges or ROM signature. How?) – StayCool Dec 11 '19 at 09:01
  • 1
    I didn't downloaded Octupus, but if it uses Accessibility permission, then it is using an API that already is becoming more restricted and some apps with bad intentions using the API are being removed from Play Store. – Marcos Vasconcelos Jan 28 '20 at 22:56
  • Although it's been late, did you try it? https://stackoverflow.com/a/51706446/16146250 – Ananta Raha Jul 10 '21 at 05:12

1 Answers1

1

HOW TO HANDLE CONTROLLER ACTION [ALSO API 16+]

At the system level, Android reports input event codes from game controllers as Android key codes and axis values. In your game, you can receive these codes and values and convert them to specific in-game actions.

When players physically connect or wirelessly pair a game controller to their Android-powered devices, the system auto-detects the controller as an input device and starts reporting its input events. Your game can receive these input events by implementing the following callback methods in your active Activity or focused View (you should implement the callbacks for either the Activity or View, but not both).

More in detail you can find in this documentation.


SUPPORT CONTROLLERS ACROSS ANDROID VERSIONS [API 12+]

If you are supporting game controllers in your game, it's your responsibility to make sure that your game responds to controllers consistently across devices running on different versions of Android. This lets your game reach a wider audience, and your players can enjoy a seamless gameplay experience with their controllers even when they switch or upgrade their Android devices.

More in detail you can find in this documentation.


SINGLE TOUCH EVENTS

For single touch events that you want to controll you can use AccessibilityService combined with dispatchGesture. But I don't think that is what you are really looking for. You don't want to handle the Touch itself, but the parameters that a controller is using for. That's why I added the 2 upper paragraphs, to achieve what you you want to have.


REASON WHY I ADDED THIS ANSWER

I know it's kinda late but till yet there is now acceptable answer out there. And in terms of reaching higher API level, it would be useful to know how to handle these stuff. Cheers :)

Ole Pannier
  • 3,208
  • 9
  • 22
  • 33