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

AndroidKeyboardUsb

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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)

Solution 3 - Android

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.

Solution 4 - Android

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.

Solution 5 - Android

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.)

Solution 6 - Android

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.

Solution 7 - Android

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.

Solution 8 - Android

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.

Solution 9 - Android

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

Solution 10 - Android

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 ...." :)

Solution 11 - Android

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.

Solution 12 - Android

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.

Solution 13 - Android

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/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBilly MoonView Question on Stackoverflow
Solution 1 - AndroidpelyaView Answer on Stackoverflow
Solution 2 - AndroidChris ByeView Answer on Stackoverflow
Solution 3 - AndroidOrwellophileView Answer on Stackoverflow
Solution 4 - AndroidJavaManView Answer on Stackoverflow
Solution 5 - AndroidKristopher MicinskiView Answer on Stackoverflow
Solution 6 - AndroidgfourView Answer on Stackoverflow
Solution 7 - AndroidRenateView Answer on Stackoverflow
Solution 8 - AndroidOrwellophileView Answer on Stackoverflow
Solution 9 - AndroidIvanzView Answer on Stackoverflow
Solution 10 - AndroidBondaxView Answer on Stackoverflow
Solution 11 - AndroidMartzeView Answer on Stackoverflow
Solution 12 - AndroidAbhishekView Answer on Stackoverflow
Solution 13 - AndroidJasonGView Answer on Stackoverflow