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 bluetooth_chat.py 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:
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:
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 bluetooth_chat.py program that comes bundled with python for android…