Billy Moon
Billy Moon

Reputation: 58531

Is it possible to program Android to act as physical USB keyboard?

What I really want to know is whether it is a hardware problem, or a software problem. Could I plug my android phone into a computer via USB and have it act as a hardware keyboard. I do not want to install anything on the computer, I want android to behave like the standard hardware.


Edit: Clarification I want to write a program/library for android that enables the device to fully emulate an ordinary keyboard, so that the operating system reports it as a standard keyboard device, and it would work in the BIOS or anywhere else that a keyboard works.

Upvotes: 94

Views: 154687

Answers (15)

Damglador
Damglador

Reputation: 1

Check out https://apt.izzysoft.de/fdroid/index/apk/me.arianb.usb_hid_client Should be able to emulate HID keyboard and mouse and even advertises itself as "For configuring BIOS/UEFI"

Upvotes: 0

Renate
Renate

Reputation: 811

Your Android already identifies with a VID/PID when plugged into a host. It already has an interface for Mass Storage. You would need to hack the driver at a low level to support a 2nd interface for 03:01 HID. Then it would just be a question of pushing scancodes to the modified driver. This wouldn't be simple, but it would be a neat hack. One use would be for typing long random passwords for logins.

Some kernels now support the hid gadget. Android configurations in rc are convoluted and require separate "recipes" for ums,adb and adb,ums. There are no recipes for anything with hid so you have to add them yourselves. Then you just configure report_desc and report_length and write your HID reports to /dev/hidg0.

Upvotes: 2

personne3000
personne3000

Reputation: 1840

Posting to an old thread but this is what came up when I searched for it, trying to install OS on a new desktop PC with only a bluetooth keyboard, and it took me a while to figure out a working solution.

I could not get the accepted answer from 2013 to work on recent hardware, but could still get something working with the resources at https://github.com/tejado/android-usb-gadget. The app is still in compiled/USB-Keyboard-app, but I could not get it to work. What worked for me is:

  1. Have a rooted phone (I used magisk). (BTW I wouldn't recommend doing this to your daily phone for no reason due to the security implications of having an unlocked bootloader, but it's needed here).
  2. Install https://github.com/tejado/android-usb-gadget
  3. Make sure the phone and PC used to setup the phone are connected to the same local network
  4. Enable adb-over-wifi (wireless debugging in developer settings). Tap the entry in developer settings to find the address and port.
  5. On the PC, adb connect [address and port], for example adb connect 192.168.1.123:45678
  6. Get the hid-gadget-test binary from https://github.com/pelya/android-keyboard-gadget/tree/master/hid-gadget-test
  7. adb push hid-gadget-test /sdcard/
  8. adb shell then su
  9. cp /sdcard/hid-gadget-test /data/local/tmp/
  10. chmod u+x /data/local/tmp/hid-gadget-test
  11. Open the app from step 2, tap the "add" icon at the top, then "mouse and keyboard", and enable it.
  12. On the PC, still using adb as root from step 8, /data/local/tmp/hid-gadget-test /dev/hidg0 keyboard
  13. Connect the phone via USB to whatever device you want to control; it should be recognized as a standard keyboard
  14. From the command line on the PC, tap any key then "enter" to send it. It also supports special keys (like "enter" or "f2"). See hid-gadget-test -h

Tested working on a Pixel 4 with a recent build, but I expect this would work on other close-to-AOSP phones in 2022 like Pixel 6 too. I could at least go into boot setup for my new desktop, install an OS and pair my bluetooth keyboard, using my old laptop to send the keys remotely through the phone. Credits still go to the owners of the 2 github repos linked above for doing all the work.

Now maybe someone could write a newer, working frontend app to use the gadget tool app, instead of using hid-gadget-test over adb ?

Upvotes: 4

Abhishek
Abhishek

Reputation: 21

I am bit late to comment in this question but it might be useful for some other people.

You can make your android phone to work like keyboard, mouse, camera, sound streaming system, tethering device. In short what ever usb gadget you see in the market and until and unless hardware doesn't limit you. Such as speed, or gadget interface not available.

USB device is of two type, host and gadget. So gadget device acts like client and usually has usb otg interface in most of the phones. So in gadget end, you can make your phone to behave like different device at all by switching between different configuration(you are already doing it when you go into usb settings and make your device as mass storage or anything else).

But for doing all these you have to modify android kernel. If you are a android device developer you can for sure do it.

Upvotes: 2

Martze
Martze

Reputation: 921

Some others figured out that this is wrong. In the meantime i share their opinion. I'm sorry.

Old WRONG answer:

In my opinion this is barely possible.

Your Computer identifies any USB device with the USB device descriptor or the usb interface descriptor. To be able to use your android device as a keyboard, you would have to change these. Actually i think these are saved on a ROM in the device, so you would have to change hardware. The device needs to identifiy itself with the host even if its only charging in turned off state (has to tell usb host about the power consumption, otherwise only a few mA max). For me this points into the direction, that you would have to change hardware

"Easiest" way would proabably be assemble an adapter containing a usb host chip with a µC that converts the received data (which you still had to send via usb) to ps/2 or usb-client signals that u send to the computer.

In my opinion the easiest way would be: Buy one of these Keyboards you can roll and put them in your bag too.

Upvotes: 2

pelya
pelya

Reputation: 4494

I've modified kernel on Nexus 7 to act like standard HID keyboard and mouse, without losing MTP/ADB/other USB functionality.

You can use usb-gadget-test commandline utility to send keystrokes and mouse movements to your PC. I want to create a remote admin app later, which will send key events and receive video from camera.

Kernel patch, binaries and instructions: https://github.com/pelya/android-keyboard-gadget

Edit: I've published a proper app to Google Play, if your Nexus 7 is rooted you can flash kernel right from the app, and send keypresses with it.

Upvotes: 51

JavaMan
JavaMan

Reputation: 5034

Seems someone have done it by patching the kernel. I just came across a paper titled "Exploiting Smart-Phone USB Connectivity For Fun And Profit" by Angelos Stavrou, Zhaohui Wang, Computer Science Department George Mason University, Fairfax, VA. (available freely by googling the above title). Here the two researchers are investigating the possibility of a compromised android device controlling the attached PC by having the android device presenting itself as an HID device (keyboard). As a proof of concept, it seems that they have successfully patched a kernel doing exactly what you want. They didn't provide detailed steps but anyway I just quote what they said they've done:

.....we developed a special USB gadget driver in addition to existing USB composite interface on the Android Linux kernel using the USB Gadget API for Linux[4]. The UGAL framework helped us implement a simple USB Human Interface Driver (HID) functionality (i.e. device driver) and the glue code between the various kernel APIs. Using the code provided in: “drivers/usb/gadget/composite.c”, we created our own gadget driver as an additional composite USB interface. This driver simulates a USB keyboard device. We can also simulate a USB mouse device sending pre-programmed input command to the desktop system. Therefore, it is straightforward to pose as a normal USB mouse or keyboard device and send predefined command stealthily to simulate malicious interactive user activities. To verify this functionality, in our controlled experiments, we send keycode sequences to perform non-fatal operations and show how such a manipulated device can cause damages In particular, we simulated a Dell USB keyboard (vendorID=413C, productID=2105) sending ”CTRL+ESC” key combination and ”U” and ”Enter” key sequence to reboot the machine. Notice that this only requires USB connection and can gain the ”current user” privilege on the desktop system. With the additional local or remote exploit sent as payload, the malware can escalate the privilege and gain full access of the desktop system.

Upvotes: 5

Ivanz
Ivanz

Reputation: 21

This is possible, without any additional drivers needed.

You can emulate PC's USB keyboard with small USB dongle-sized device and then use your Android device to send keyboard (and/or mouse) data over Bluetooth.

Take a look on descriptive video in Indiegogo campaign: http://igg.me/at/hiDBLUE/x/3400885

BTW: The product technical documents is available here: http://www.flyfish-tech.com/hiDBLUE

Upvotes: 2

Orwellophile
Orwellophile

Reputation: 13933

Looks like someone finally did it, it is a tiny bit ugly - but here it is:

http://forum.xda-developers.com/showthread.php?t=1871281

It involves some kernel recompiling, and a bit of editing, and you loose partial functionality (the MDC?) .. but it's done.

Personally though, now that I see the "true cost", I would probably put together a little adapter on a Teency or something - assuming that Android can talk to serial devices via USB. But that's based on the fact that I have a samsung, and would require a special cable to make a USB connection anyway - no extra pain to have a little device on the end, if I have to carry the damn cable around anyway.

Upvotes: 12

Orwellophile
Orwellophile

Reputation: 13933

Don't give up. Linux can do it with the right hardware, via "USB Gadgets." And giving the following facts:

  1. My old Nokia N95 could use it's USB to be a "Mass Storage Device", a "Media Player", "a GSM modem", or to print photos.
  2. I can plug an iPhone into an iPad via a the Apple USB-Camera passive adapter, and they transfer pictures.
  3. iPhone can obvious present as a number of things, e.g. when they go into DFU.

Why is all this relevant?

Because if I was writing a linux phone I know what it would do, and how it would do it. And the answer would involve USB Gadgets.

Reading one of the links that was posted here,

It's the Linux kernel, the code is in drivers/usb/gadget/ in the kernel.org tree if you are interested. Android does have a few specific gadget patches that are not in mainline, but it's not all that much. You can see all of this by just checking out their kernel git tree, no need to bother their developers.

I would guess that you would have a shot at it - but it would involve recompiling the android kernel/operating system - or at least having a build environment in which you /could/ rebuild the kernel if you wanted.

BTW, I have an Atmel NGW100mkII, which support USB gadgets, but doesn't ship with the HID module. And I'll be having to do the above and more.

Upvotes: 2

gfour
gfour

Reputation: 999

I believe that you can do it if you have a rooted device with a recent Android. For example, the Asus Eee Pad Transformer running Android 4 has the libraries /system/lib/libusb.so and /system/lib/libusbhost.so, so you can write a Java application that calls them using JNI to emulate a USB keyboard. This means that you must write some glue C code that emulates the way a USB keyboard is communicating with a PC (=you must study the way the USB protocol works).

I say "rooted", because some permissions are usually needed to use these libraries.

Edit: The above is true when programming an Android device to act as a USB host, in your case you need to be a "gadget". I don't know how much of the Linux gadget functionality is contained in the kernel of your Android device. See this for a similar question.

Upvotes: 2

JasonG
JasonG

Reputation: 5962

I have some experience here as a user. The most obvious solution is via tcp/ip via a client/server model. Many of the tools out there like airkeyboard (http://www.freenew.net/iPhone/air-keyboard-111/171415.htm) utilize this method for creating a keyboard/mouse replacement using a smartphone os. Note that there are some security issues that become apparent in the implementation. For instance, you must be logged in to utilize the server componenents.

Other cross platform tools (ie windows/mac controlling another windows/mac instance) utilize a similar approach. See synergy: http://synergy-foss.org/

Upvotes: 0

Chris Bye
Chris Bye

Reputation: 1048

Most USB keyboards need drivers to run. Any keyboard functionality (non-standard buttons) beyond the capabilities of the standard HID drivers will need to install some software on the computer.

That being said, It may be possible to use Android's USB capabilities, as well as writing a custom driver if default HID is not sufficient, to achieve your goal. It is likely a very non-trivial undertaking.

Edit: I think KristopherMicinski is right that the level of control you get with the stock Android USB API is inadequate for this purpose. His two solutions of modifying the firmware to communicate using HID standards, as well as a hardware middleman that translates from the Android Accessory protocol to HID both seem valid to me. If installing drivers on the computer is out of the question, these may be the only two options.

However, if you're open to installing a driver for this behavior, It should be possible to write a custom driver that can handle Android USB protocol, and correctly translate to the correct calls/interrupts for keyboard functionality. If memory serves, every peripheral keyboard I've used in the last 10 years has needed to install a driver for full functionality, so this may not be considered non-standard behavior. (The though just occurs that this approach will only allow the device to function as a keyboard inside windows, not during the boot process)

Upvotes: 15

Kristopher Micinski
Kristopher Micinski

Reputation: 7672

The only way I could see this being possible is if you:

  • modified the Android firmware to give you usb level access at a low enough level that you could operate using the necessary protocol

or

  • Made some sort of special hardware level converter that you attached to the device.

(So I suppose, depending on how much work you want to do, it could be a hardware or software problem.)

Upvotes: 3

Bondax
Bondax

Reputation: 3163

You have to establish some kind of connection to do that android-out-of-the-box, like via tcp/ip and adb, so no not w/o installing at least adb and a listener on the computer.

But if you have an activity that sends the hardware keyboard like data via usb then why not? Won't be easy i guess. At this point the usuas forum answer comes right away: "Why don't you change your plans and ...." :)

Upvotes: 1

Related Questions