Using the Android Scripting Environment to find out if a book is in the Library
Being a developer type I couldn't wait to start experimenting with coding on my new HTC Desire. Being Android powered it is very friendly and coding mainly takes place in Java, which I'm familiar with. This class of mobile device is incredibly powerful of course, armed not just with portability and web access but also decent processing power and a variety of sensors. We're still learning about what exactly this means and what opportunities it can bring and so it is very important to be able to experiment, try out ideas and build rough prototypes. This is where the Android Scripting Environment (ASE) comes in, and I used it to build a prototype application to scan a barcode on a book and use that code to see if it is in a library. Handy when deciding whether to buy a book!
First of all it is important to point out that the ASE is absolutely not for making applications for end users. Instead it is a handy way to experiment with the possibilities of your device. In some ways it reminds me a little of the Quickly system on Ubuntu, it has a lower entry barrier in terms of the skills you need to learn to try writing a program than developing a full blown application and has an element of enabling fun programming (not that programming in Java is not fun of course). It also should be a quicker way to get a prototype. You can also write the script on the phone itself. This is a bit awkward in practice, but it can be done.
A little while ago I read the blog post: “Android barcode scanner in 6 lines of Python code” and got intrigued. The script used the camera on the device to scan a bar code on the back of a book and then pass the ISBN number to Google Books to find out information about it. As I work in a university I am lucky enough to have access to an academic library so wondered about adapting the script to search our library's catalogue instead which would be handy in situations where I am about to buy the book. It seems things have changed slightly with ASE since the script was written, so it did not work as it was, but fortunately there were some helpful hints in the comments that provided fixes.
Installing ASE and Python
Strangely, the Android Scripting Environment is not available in the Android Market. It still can be installed with the minimum of fuss though. I got the Barcode Scanner app from the Zxing team from the Android Market. Then from the home screen I pressed the Menu key then Settings → Applications and ticked the Unknown Sources entry. This allows you to install apps from places other than the market. Now go into your applications menu and start up the Barcode Scanner. You should be able to scan the barcode on the ASE project page (http://code.google.com/p/android-scripting/), when you open this URL it will install. When you have ASE installed, you should untick the Unknown sources entry to keep your phone secure.
You should now be able to start up ASE from your applications menu. When you do you will notice things look a bit sparse, this is because ASE only ships with a Shell interpreter, you have to tell it what other interpreters you would like to install. To install Python press Menu then View → Interpreters. Only Shell will appear in this list at the moment. Click Menu again then Add and select Python. The new interpreter will now be downloaded and you will shortly be ready to write some scripts in this great language.
Is that book in my library?
A lot of libraries have on line catalogues that can be searched over the web. You can enter many bits of information to find a book, but one that is especially useful is the ISBN code. Why? Because this is the code you get when you scan the barcode on the back of the book. You could adapt the script to search any catalogue online that accepted ISBN codes. Note that this script is not meant to be an exhaustive way to search you library, it is more of a use case for ASE. If you chat to a Librarian you might be able to get a different edition of the book or something very similar – matches that will be missed with this technique.
The script works by invoking the bar code scanner function of ASE. This function return a structure that contains the ISBN from the scanned bar code which is extracted this is inserted into the template URL to replace the “%s” characters and this is passed to a web browser. To establish this URL I did a manual search on our catalogue and noted down the URL generated to get the query results.
The final script is here:
# -*- coding: utf-8 -*-
droid = android.Android()
(id, result, error) = droid.scanBarcode()
isbn = str(result['extras']['SCAN_RESULT'])
url = "http://voyager.open.ac.uk/vwebv/search?searchArg=%s&searchCode=GKEY^*&searchType=0" % isbn
With only a slight modification to a code sample I found on the web I could create a short script that could potentially help me utilise the library I am a member of much more effectively. It also lets me try out an idea quickly to see if the concept is any good before needing to commit to writing a full end user targeted Java application. ASE is great for experimentation and having a bit of fun with your device. Is it ok to have fun while programming? Absolutely.