Control Your Laptop with Android, Python, Twisted, and Django

Introduction


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?
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 Tool Belt

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:
  • 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 Level

At a high level, we consider our phone to be the client and our laptop, the server. I’ll go through this architecture in-depth below, but the basic flow of the project is as follows:
  1. The user types some command into the Python interpreter.
  2. The command is sent to the Django instance.
  3. Django then passes the command along to Twisted.
  4. Twisted then parses the command sends a new command via D-Bus to Amarok.
  5. Amarok interacts with the actual laptop, controlling the playing/pausing of music.

Now, lets dig in.

Python on Android

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

Comments

Popular posts from this blog

Reliance to Launch 4G Smartphones Under 'Lyf' Brand

How to Make Your Computer Faster – 10 Proven tips