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

14

91

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.

Fuddyduddy answered 21/3, 2012 at 13:35 Comment(2)
The Linux kernel includes this functionality, but I have no idea what the status of HID gadget support is like in Android kernels.Varanasi
Have you succeed? Is your app available in play store?Oneirocritic
A
51

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.

Ailing answered 17/12, 2013 at 21:15 Comment(8)
Is this at all possible with iOS?Ruth
Very unlikely, as you will need to jailbreak and install custom XNU kernel with your own patches - my patches won't work, as it's not Linux kernel. Practically it's much cheaper to buy second-hand Nexus 4/5/7 rather than tweak and possibly brick your iPad.Ailing
where can I learn more about xnu kernel and how to make my own patches? I am not too familiar with it. Let's assume the iOS device is jailbrokenRuth
hi @Ailing i was trying to compile the kernel for my android mtk 6589 based. But seems something is worng. can you help ? I have seen you have wrote lots of such thing and making android almost a universal pc gadget :DBarraza
Unfortunately, the app has since been removed from the play store.Clinician
Play Store started to refuse apps that use superuser privileges for security reasons, so the app was unlisted. You still can get the same .apk file from the Github page.Ailing
camera is in theory very easy : you can just get a 30$ hdmi capture card for usb c and install a usb kamera appPyrrhic
That would defeat the intention of repurposing an old broken Nexus 7 tablet which I already own for $0. You can buy a proper KVM switch for $30Ailing
O
15

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)

Overline answered 21/3, 2012 at 14:1 Comment(1)
I awarded you the bounty, because although I doubt your answer can lead to a solution, you provided the most solution orientated answer here, that tries to tackle the problem with some useful resources. Thanks ,~)Fuddyduddy
B
12

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.

Bigham answered 28/9, 2012 at 4:29 Comment(3)
Yes I did loose all other functionality with USB and this is far from perfect but it does what. I can still use adb over wifi and transfer files via SD card. If i have time at some stage i may try and refine it to restore the original functionality, idealy adding another option to the usb menu allowing you to select the keyboardGradient
Sadly, it seems to be SOP that devices always seem to ship kernels that don't support the full compliment of [gadget] modules. whether it be a kindle, android, or even an evaluation board. i've been trying to build a combined Mass Storage/HID solution for BitLocker usage on a tablet.. damn frustrating. PS: damn fine work you did.Bigham
Im interested in adding mass storage at a later date but will finalise this first. Once i am happy with it i will post a tutorial on how i did everything. Im currently working on getting an Android app to talk to the driver via a small app written in C. Hope to be done in the next week or soGradient
P
5

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.

Prolegomenon answered 10/10, 2013 at 18:18 Comment(0)
C
3

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

Cheerless answered 21/3, 2012 at 13:46 Comment(4)
Could you explain a little more about the firmware modification option, for example, what are the limitations? What problems does that create (can you run existing software/update the phone)? Where can I get information about how to do this kind of thing? Cheers.Fuddyduddy
Well, I don't think that by default (i.e., on stock firmware) the USB capabilities (also mentioned by Slothsberry) will give you access to the USB hardware at a low enough level to control the output in a necessary way to implement output you'd need to make this happen. (I.e., you need to output the signal, not accept it..)Cheerless
What about blutooth? I don't mean to use android as blutooth keyboard, but maybe there is an existing usb device that receives blutooth keyboard signal, and relays it to computer without driver necessary on computer... I think I am maybe being hopeful, but that would work for me.Fuddyduddy
This would theoretically be possible. You'd have to get a Bluetooth receiver that can communicate with the Android device, and then make a little circuit that interprets the BT signal from the phone, and then converts it into a valid keyboard signal. You can buy all the parts on SparkFun, but it's not going to be super simple (i.e., probably a weekend hack?). I would do it if I had a lot of money / time, :-), as I'm sure it's possible, but I don't think there's one out of the box.Cheerless
S
2

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.

Shaffer answered 30/3, 2012 at 16:20 Comment(0)
C
2

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.

Cu answered 13/7, 2012 at 20:3 Comment(5)
I expect this would require the phone to be rooted. Do you expect there would be any other pre-requisites?Fuddyduddy
Yes, you'd probably have to root. The interface that you'd want to use would be the Linux "Gadget" API. link I just noticed Greg's comment on the question, that is the answer, i.e. Gadget.Cu
Did you read the answer I ticked? Do you agree? Do you have any information that confirms or denies that it is not possible due to ANdroid storing interface descriptor on the rom - and therefore not programmable?Fuddyduddy
My Android can respond with at least two VID/PIDs, one for FastBoot and on for normal Android operation. In normal Android operation it supports two interfaces, USB Mass Storage and ADB. How exactly one adds additional interfaces, I don't know, but it's clearly not hardware limited. USB Gadgets seems the right direction.Cu
Thanks @Cu - that looks promising!Fuddyduddy
B
2

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.

Bigham answered 11/8, 2012 at 18:23 Comment(2)
I will try not to give up - but it seems like a large undertaking if it might be completely impossible from the get-go. You have given me lots to think about/research in this post. Thanks.Fuddyduddy
I just found an app for jailbroken iPhone, which turns it into a USB Mass Storage Device with multiple selectable "Virtual Disks" and changes the USB devId so it doesn't launch iTunes/Photo Import. (No help to you, but it's amazing stuff for iPhone). I have also found a USB library for my Texas TI84+ SE calculator that would do what you wanted (it also unlocks Playstations). Anyway, my Samsung Galaxy Note arrived today, so I will actually have an Android device.Bigham
U
2

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.

Us answered 15/1, 2013 at 16:51 Comment(1)
For the record, this isn't a blog, and shouldn't be treated as such. Check out the FAQ to find out more. EDIT: On that note, consider checking this question on meta out.Pretentious
E
2

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

Emu answered 13/6, 2013 at 0:37 Comment(0)
J
2

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 ?

Judenberg answered 11/8, 2022 at 2:53 Comment(0)
C
1

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

Cheriecherilyn answered 21/3, 2012 at 13:40 Comment(1)
I don't want to change my plans, because I want to develop a library that allows android to operate as a hardware keyboard for standard computer devices. As a developer, or as a user, it is clearly preferable than having to get one's swede around some kind of abstracted communication protocol requiring non-standard set-up.Fuddyduddy
G
1

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.

Ghirlandaio answered 30/3, 2012 at 10:31 Comment(3)
Can you point to a resource that confirms your suspicion about the ROM identification of Android USB connections?Fuddyduddy
I ticked your answer, as it looks right - although I would like to have some confirmation of the Android descriptor being hardcoded. ,~)Fuddyduddy
Is is wrong. Most USB devises on Android are implemented in software.Lovelace
M
0

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/

Mcquiston answered 29/3, 2012 at 4:13 Comment(1)
Thanks for your feedback, but I was actually looking for an on-phone software only, with no software installed on the computer.Fuddyduddy

© 2022 - 2024 — McMap. All rights reserved.