I wrecked my phone the other day:
Doesn’t look so bad, but in fact the touch screen is completely dead. So not only do I now have to decide whether to get a new phone or try to replace the screen myself, I also had to find a way to regain control over the phone so I could backup my data.
A bluetooth mouse would be one way to control the phone without the touch screen, but I don’t have one – and even if I did, I didn’t have bluetooth turned on (though that could apparently be accomplished through the console).
A USB keyboard would also be of help if the Nexus 4 had USB Host support. Sadly, unlike the Nexus 5 (and the Nexus 1, if I recall correctly), doesn’t.
What else then? A series of internet searches yielded the following results:
- There’s a ton of remote control apps for Android (as was to be expected). Most require root though. Those that don’t generally can’t control the touchscreen/mouse. And even they generally require some setup steps before they can be used – back to the start.
- adb (the Android Debug Bridge, part of the SDK tools) provides shell access to the phone via USB. And the shell has commands to simulate key presses and touch events. Touch events require the exact screen coordinates as numbers, so as a standalone solution that’s still rather tedious.
- There are ways to get screenshots from an Android phone through adb – obviously; after all, even the official Eclipse plug-ins provide a screenshot feature.
- A combination of points 2 and 3 would be a way to actually simulate touch events in a useful manner. Astonishingly though, nobody seems to have written such a tool.
So what does a programmer to when the obvious tool doesn’t yet exist? Write it himself, of course.
I spent yesterday evening hacking together a crude Java tool to serve as a primitive remote control through an Android phone. I present it here in case someone else might face the same problem I did. It requires no extra software installed on the phone, just a USB connection and the Java tool (Java 7 required) and SDK tools installed on the computer. It uses adb access to the phone – meaning that you have to have the SDK tools installed and you have to have already confirmed the USB debug connection on your phone earlier. If you didn’t, connecting the phone will open a confirmation dialogue on the phone – and without a touchscreen, you obviously won’t be able to confirm it. But if you’re a programmer like me and have already access the phone via adb before, all you have to do is fire up the tool on the computer. I won’t get into how to setup and configure adb here – please check the official docs if necessary.
The tool does the following:
- Repeatedly grab a screenshot from the phone and display it in a Window
- Capture keyboard events and forward them to the phone
- Capture mouse clicks and drags and forward them to the phone
It’s slow as hell (everything is being done through shell calls, which makes the screenshot updates particularly slow), but it works.
Before you first start it, make sure you have adb installed and modify the config file provided with the download. It contains four settings:
- adbCommand – the full path to the adb tool as a shell command. Linux users note that Java doesn’t seem like the ~ character in file paths to access the home directory.
- screenshotDelay – the delay (in milliseconds) between displaying a screenshot and grabbing the next one. Note that the actual grabbing process will probably take considerably longer than this delay, so the update rate will be slower.
- localImageFilePath – the tool has to download the image file from the phone. This path tells it where to store the file on the local system before displaying it.
- phoneImageFilePath – the location on the phone where the screenshot file is stored before downloading it.
Once the configuration is done, simply run the tool like this:
java -jar adbcontrol.jar
If you put the config file in a different directory, you can add the path to it as an option after the jar file name.
Here’s what it looks like:
In the window, you can single click to send a tap event or click-drag to send a swipe event to the phone. Pressing a number or letter key (or anything else that produces an ASCII character between 33 and 127) will send that key press to the phone. Additionally, the following keys are forwarded:
- Escape – as the Back key on Android
- Cursor keys
The download includes the full source code and is provided under the Eclipse Public Licence.
In case you find this tool useful, please consider donating – after all, I only wrote it because I wrecked my phone.