Control Your Laptop with an Android Phone using Python, Twisted, and Django
IntroductionIt's always fun to put your Android or Python programming skills on display. A while back, I figured it'd be cool to try and control my laptop via my Android mobile device. Think about it: remote laptop access including being able to play and pause music, start and stop programming jobs or downloads, etc., all by sending messages from your phone. Neat, huh?
Before you keep on reading, please bear in mind that this is a pet project, still in its early stages-but the basic platform is there. By gluing together some mainstream tools, I was able to setup my Android phone to control my laptop via a Python interpreter.
By the way: the project is open source. You can check out the client code here, and the server code here.
The Remote Laptop Access Tool Belt: Python, Twisted, Django, and AmarokThis project involves the following technologies, some of which you may be familiar with, some of which are quite specific to the task at-hand:
- Python 2.7+
- Twisted: an excellent event-driven framework especially crafted for network hackers.
- Django: I used v1.4, so you'll have to adjust the location of some files if you want to run a lower version.
- Amarok: a D-BUS (more on this below) manageable media player. This could be subbed out for other such media players (Clementine, VLC, or anything that supports MPRIS) if you know their messaging structures. I chose Amarok because it comes with my KDE distribution by default. Plus, it's fast and easily configurable.
- An Android phone with Python for Android installed (more on this below). The process is pretty straightforward-even for Py3k!
- Remote Amarok and Remote Amarok Web.
At a High LevelAt a high level, we consider our Android phone to be the client and our laptop, the server. I'll go through this remote access architecture in-depth below, but the basic flow of the project is as follows:
- The user types some command into the Python interpreter.
- The command is sent to the Django instance.
- Django then passes the command along to Twisted.
- Twisted then parses the command sends a new command via D-Bus to Amarok.
- Amarok interacts with the actual laptop, controlling the playing/pausing of music.
Python on AndroidSo one good day, I started looking at Python interpreters that I could run on my Android phone (Droid 2, back then). Soon after, I discovered the excellent SL4A package that brought Python For Android to life. It's a really nifty package: you click a couple buttons and suddenly you have an almost fully functional Python environment on your mobile or tablet device that can both run your good ol' Python code and access the Android API (I say almost because some stuff probably is missing and the Android API isn't 100% accessible, but for most use-cases, it's sufficient).
If you prefer, you can also build your own Python distribution to run on your Android device, which has the advantage that you can then run any version of the interpreter you desire. The process involves cross-compiling Python to be run on ARM (the architecture used on Android devices and other tablets). It's not easy, but it's certainly doable. If you're up for the challenge, check here or here.
Once you have your interpreter setup, you can do basically whatever you like by combining Python with the Android API, including controlling your laptop remotely. For example, you can:
- Send and read SMS.
- Interact with third-party APIs around the Internet via urllib and other libraries.
- Display native look and feel prompts, spinning dialogs, and the like.
- Change your ringtone.
- Play music or videos.
- Interact with Bluetooth-this one in particular paves the way for a lot of opportunities. For example, I once played around with using my phone as a locker-unlocker application for my laptop (e.g., unlock my laptop via Bluetooth when my phone was nearby).
How Using Your Phone to Control Your Laptop Works
The ArchitectureOur project composition is as follows:
- A client-side application built on Twisted if you want to test the server code (below) without having to run the Django application at all.
- A server-side Django application, which reads in commands from the Android device and passes them along to Twisted. As it stands, Amarok is the only laptop application that the server can interact with (i.e., to control music), but that's a sufficient proof-of-concept, as the platform is easily extensible.
- A server-side Twisted instance' which communicates with the laptop's media player via D-Bus, sending along commands as they come in from Django (currently, I support next', previous', play', pause', stop', and mute'). Why not just pass the commands directly from Django to Amarok? Twisted's event-driven, non-blocking attributes take away all the hard work of threading (more below). If you're interested in marrying the two, see here.
- The client-side Android code, uploaded to your device with a customized URL to reach your Django application. It's worth mentioning that this particular piece of code runs on Python 2.7+, including Py3k.
What's D-Bus?I've mentioned D-Bus several times, so it's probably worth discussing it in more detail. Broadly speaking, D-Bus is a messaging bus system for communicating between applications (e.g., on a laptop computer and Android phone) easily through specially crafted messages.
It's mainly composed of two buses: the system bus, for system-wide stuff; and the session bus, for userland stuff. Typical messages to the system bus would be "Hey, I've added a new printer, notify my D-Bus enabled applications that a new printer is online", while typical Inter-Process Communication (IPC) among applications would go to the session bus.
We use the session bus to communicate with Amarok. It's very likely that most modern applications (under Linux environments, at least) will support this type of messaging and generally all the commands/functions that they can process are well documented. As any application with D-Bus support can be controlled under this architecture, the possibilities are nearly endless.
More info can be found here.
Behind the Scenes:Having set up all the infrastructure, you can fire off the Android application and it will enter into an infinite loop to read incoming messages, process them with some sanity checks, and, if valid, send them to a predefined URL (i.e., the URL of your Django app), which will in-turn process the input and act accordingly. The Android client then marks the message as read and the loop continues until a message with the exact contents "exitclient" (clever, huh?) is processed, in which case the client will exit.
On the server, the Django application picks up a command to-be processed and checks if it starts with a valid instruction. If so, it connects to the Twisted server (using Telnetlib to connect via telnet) and sends the command along. Finally, Twisted parses the input, transforms it into something suitable for Amarok, and lets Amarok do its the magic! Finally, your laptop responds by playing songs, pausing, skipping, etc.