
Martin Chikilian
Martin is a full-stack engineer and has worked as a professional Python developer since 2007.
It’s always fun to put your 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: being able to play and pause music, start and stop programming jobs or downloads, etc., all by sending messages from your phone. Neat, huh?
It’s always fun to put your 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: being able to play and pause music, start and stop programming jobs or downloads, etc., all by sending messages from your phone. Neat, huh?
Martin is a full-stack engineer and has worked as a professional Python developer since 2007.
It’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.
This project involves the following technologies, some of which you may be familiar with, some of which are quite specific to the task at-hand:
At 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:
Now, lets dig in.
So 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:
Our 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.
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.
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.
Everything looks really simple so far, huh? You may be asking yourself: “Can this be extended to support nifty feature [X]?” The answer is: Yes (probably)! Given that you know how to interact with your computer using your phone properly, you can supplement the server-side code to do whatever you like. Before you know it, you’ll be shooting off lengthy processes on your computer remotely. Or, if you can cope with the electronics, you could build an interface between your computer and your favorite appliance, controlling that via SMS instructions (“Make me coffee!” comes to mind).
I will keep on playing with my Android laptop control toy for a while, improving on its features, fixing bugs, etc. But if you’re interested, I’d love to see you help out! Just fork the repository and send over any pull requests.
If you have any questions, please shoot me an email at martin@toptal.com and I’ll be happy to help out.
World-class articles, delivered weekly.
World-class articles, delivered weekly.
Join the Toptal® community.