android – Simulate USB/Bluetooth HID device locally in order to inject events?-ThrowExceptions

Exception or error:

Is it possible to simulate a USB or Bluetooth device connected to Android?

I would like to make an app which is able to simulate an HID device locally. What I mean by this is: the app should make Android believe that an USB/Bluetooth HID device is connected. This would allow my app to inject touch events globally, I hope. Is there anyway to simulate a (virtual) device? Note that I don’t really care about the device, I just want to use the built-in support for HID input.

I know a lot of people already asked about touch event injections but this approach seems to be a little bit different. I do not want to use this for testing purpose, so InstrumentationTestCase and the like won’t help.

Rooting the device might be an option, although I can also imagine to ask my endusers to install a specifically signed app manually (according to https://stackoverflow.com/a/16737083/2923406 and https://stackoverflow.com/a/22902808/2923406 this works, but it would be device specific).

I am aware of:

  1. Simulate a mouse input on android – This guy seems to do it in his own app, although his questions wasn’t answered 😉

  2. http://www.pocketmagic.net/2013/01/programmatically-injecting-events-on-android-part-2/#.U58SqfldVHV – Needs Root. Did anyone succeeded in using this? Also, the solution seems very device-specific again.

  3. Bluetooth + simulating mouse – That’s not what I want. The phone itself should not be an HID device, but use the (virtually created) one itself. Same for this:
    https://stackoverflow.com/a/8174973/2923406

Note that I do not want to turn my phone into an HID device of any kind.

How to solve:

Simulating is a broad term. When I created my Bluetooth app I “simulated” a Bluetooth connection in two ways.

Way 1: Use a serial port UART converter and hook it up to a Bluetooth module transmitter.

Then you can use a terminal program like CoolTerm, to send your data.

Like so. In this case I coded in a string to send on successful connection with the device however you can make a infinite loop for testing purposes effectively not requiring your phone to be turned into a HID device.

Way 2 (not easy): Use your computers bluetooth in a server/client relashionship model.

This is harder to do. What you can do is convert your pc/mac into a server and the Android phone into a client or vice-versa. For this you will need to write external code which will need to be compiled separately on a jvm(java virtual machine). The procedure to do this can be found here. If you are using a Linux machine you have to separately download the Bluez module. I have not tried this on any other operating sytem other than Ubuntu, and it was a pain to get functional.

Hopefully that helped.

###

Yes, it’s quite easy using the AOA2 protocol check this & this links for details ( you’ll need to switch your device to the Accessory mode )

###

This may be possible (or at least be easier) using the Robolectric library, which simulates a full Android device locally. Although it is intended primarily for testing, the fact that it simulates a whole device locally – including Bluetooth and USB – means adding to it may be an easier approach.

In other words, you may be able to modify the classes it uses to simulate these abilities locally (i.e. in the IDE itself without an emulator or device) in order to simulate them on the device itself. After all, it does provide full simulations of these functions. You could simply change these Bluetooth and USB simulating classes to load onto the device itself rather than onto the local Robolectric test “device.”

This is just an idea though – I can’t confirm this will work – it just might be a good place for you to start.

Leave a Reply

Your email address will not be published. Required fields are marked *