About these ads

David Vassallo's Blog

If at first you don't succeed; call it version 1.0

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.

sl4a

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:

https://code.google.com/p/android-scripting/wiki/ApiReference

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:

http://www.pythoncentral.io/python-for-android-using-webviews-sl4a/

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 daveDemo.py in my case). Looking at the contents of daveDemo.py 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 bluetooth_chat.py program that comes bundled with python for android…

About these ads

One response to “SL4A + Python 4 Android : Bluetooth and Webviews example

  1. Pingback: Android (SL4A) bluetooth to Linux (ubuntu) | David Vassallo's Blog

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Follow

Get every new post delivered to your Inbox.

Join 162 other followers

%d bloggers like this: