20

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.

Community
  • 1
  • 1
Rolf
  • 1,129
  • 11
  • 27
  • No - as a design principle an app may not impersonate a user to the system or other apps. And if it were accidentally possible without external hardware, that would be considered a security hole and patched. – Chris Stratton Nov 11 '14 at 15:28

3 Answers3

2

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.

Community
  • 1
  • 1
SeahawksRdaBest
  • 868
  • 5
  • 17
  • This does not answer the question asked, as it involves actual (rather than simulated) hardware. – Chris Stratton Nov 13 '14 at 13:25
  • @ChrisStratton I answered the question best I could. I believe I had a chat discussion with Rolf and helped him tackle the issue. You are more then welcome to propose your solution (if you know one). As per your comment above in the Question, that is exactly what I told Rolf and gave him these alternate methods. – SeahawksRdaBest Nov 13 '14 at 17:24
  • The outcome of your comment chat (attached to the deleted non-answer) was that you told Rolf that "it can not be done the way you want" which is what you should have posted from the start. Your visible answer here gives a very different impression, as it addresses a question substantially different than any one which was ever asked. The whole reason you got into that long discussion was that it took Rolf a while to figure out that you weren't actually answering his question, and were not proposing anything that could actually solve his problem. – Chris Stratton Nov 13 '14 at 17:29
  • @ChrisStratton The answer Rolf posted himself (incorrectly) had comments in it. In these comments I told him, if I remember correctly, explicitly that it can not be done the way he wants. I think I also remember quoting security reasons as-well. – SeahawksRdaBest Nov 13 '14 at 17:36
0

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 )

NadavRub
  • 2,520
  • 27
  • 63
  • The question asks how to simulate hardware, not how to use actual hardware. – Chris Stratton Nov 12 '14 at 15:25
  • Using AOA2 in the above mentioned manner is exactly the same as using the BluZ BlueTooth stack mentioned in the accepted answer, the only different is that with AOA2 HID is carried over USB where with the BlueZ solution it is carried over BT – NadavRub Nov 13 '14 at 07:10
  • No. The question is not about actual hardware at all, but about simulating it. – Chris Stratton Nov 13 '14 at 13:23
  • Using either BluZ or AOA2 U can simulate a device by a Desktop machine and not a physical device such as USB/BT HW Keyboard – NadavRub Nov 13 '14 at 13:40
  • First, no, it can't - the data comes into android differently and is treated differently. But more importantly, it is irrelevant because having one piece of hardware pretend to be another still requires having a piece of hardware. The question is about circumventing the phone's security by having **software on the phone** pretend to be hardware, and that is (for the obvious reason) blocked by the security model. – Chris Stratton Nov 13 '14 at 13:46
  • Both options of the accepted answer ( the one with +50 ) suggest an external device, either a "serial port UART converter" or "your computers bluetooth", I don't see how this converge with "software on the phone". – NadavRub Nov 13 '14 at 13:52
  • The asker has explained why SeahawksRdaBest's proposal does not match their goal either. Unfortunately, they did this by misusing the answer form to post commentary, so it was deleted and therefore only visible above a reputation threshold. Reread the question though and you will see that it seeks a software, not hardware solution. – Chris Stratton Nov 13 '14 at 13:52
  • OK... BTW, per your response: "it can't - the data comes into android differently and is treated differently", while with the AOA USB protocol indeed such a simulation is not possible, with AOA2 it is supported... – NadavRub Nov 13 '14 at 13:58
  • And no, SeahawksRdaBest's proposal is not accepted as an answer, despite getting a bounty from misunderstanding or default. – Chris Stratton Nov 13 '14 at 13:58
0

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.

Andrew Faulkner
  • 3,662
  • 3
  • 21
  • 24