SL4A + Python 4 Android : Bluetooth and Webviews example

Most of the other non-native frameworks that I’ve used so far are often very good, but lack low-level connectivity modules such as bluetooth (though they pretty much do anything else…). So while hunting around for what I might be able to use for developing a bluetooth app, I ran across SL4A – scripting language for android, and it’s corresponding python module: Py4A – python for android. It’s quite an underrated environment – for something that can do so much, there is less “buzz” around it than I expected. It really can do wonders. Probably the main reason that it hasnt gone mainstream is the inability to package the resulting scripts you write into standalone apps. However, in some situations it is extremely useful – like when you are building a personal app, or, as is my case, your distribution platform is a controlled one and you can pre-install and setup the SL4A environment.


Installation is a breeze: you simply each link in turn and download the .APK (make sure your device allows installation of apps from unknown sources). On installation, the installer creates a “scripts” folder located at “/mnt/sdcard/sl4a/scripts“. Any python script you place there can be launched via the SL4A app. I recommend downloading an FTP server for your android device for easy transferring of files to and from your development machine. Then it’s playtime…

For a developer coming from a python and web development background, getting up and running is easy. However, you should still have a once-over the API, which can be found here:

In this article, i’ll post basically an amalgamation of two concepts: the bluetooth chat and webviews. We should end up with the following:

1. A bluetooth “client” device that will display the data sent to it over bluetooth in a webview.

2. The bluetooth server can be anything really, but in my case it will be another android device, so i’ll provide the code for the server too.

3. We’ll show off some SL4A UI capabilites…

Note the bluetooth communication is one-way (from server -> client). For two way communication, have a look at the script that comes included with python for android (and include a little fix I posted about here). Before continuing, if you’d like to know the details of what i’m doing in webviews (especially when it comes to communication between webview and python script) I suggest you have a once over the following excellent article, since I’ll skim over details already posted elsewhere:

Ok, so let’s start with the setup. As I mentioned before, I used a FTP server and client to create a new folder “daveTest”, inside /mnt/sdcard/sl4a/scripts into which I placed “css” and “js” subfolders. In here, I placed bootstrap + jquery css and javascript files. I’ll be using these within my webview. Then place the main .py script into /mnt/sdcard/sl4a/scripts (i called it in my case). Looking at the contents of we see:

I included some comments in bold and red to illustrate what’s going on… Now, onto the webview. One thing to note, is that unlike normal HTML, webviews do not allow you to use relative filenames. So when linking to external scripts, make sure to use the absolute file name. The index.html file I’ve used for the webview is quite simple:

Again, comments in bold / red font color. That’s all there is to it – you can’t get much easier than that i’m sure!!! Think of the possibilities this opens up – you can use your already well-known HTML / Javascript constructs and techniques, to get really low level communication to your android device….

Here’s a video of it in action:)

PS, here’s the code for the bluetooth “server” – though again this can be any bluetooth device that emits messages. It’s basically a modified version of the program that comes bundled with python for android…

One response

  1. […] a follow up to my recent post I tried to program a very basic launcher. The idea is to pass commands from my android phone to my […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 296 other followers

%d bloggers like this: