My first Android project -- in Python!

I have a lot of books.  For years I've been planning to inventory them.  At one point I was going to sign out a barcode scanner from the university and write some software that would allow me to scan them, but I got busy with other things.

With barcode scanning now possible on the Android phones, I decided to sit down and do it.  Less than one hour later, I was done!  That's the beauty of Python.

To develop in Python for the Android phones, you'll need to install ASE (the Android Scripting Environment).  If you've installed the free Barcode Scanner app from the Android Marketplace (the one from ZXing, which I highly recommend!) you can just point it at this QR code to get ASE:

Otherwise, you can download ASE from here (on your phone, of course).  You'll have to allow external apps by going to Menu, Settings, Applications and turning on the Unknown sources option.  Eventually, ASE will be in the Android Marketplace and you won't have to do that extra step.

Once you launch ASE, you need to tell it to install Python by going to Menu, Add interpreter, and choosing Python.

And that's it -- you're all set.  You can now start programming your phone in PYthon.

If you like, you can actually write Python programs on the phone itself, but that's pretty awkward.  A better option is to use adb (which I discussed in a previous post).  If you have Python 2.6 installed on your PC, you can run the app on the PC side and have it talk to the phone over the USB cable (as described in the ASE project pages).  That's neat, but you have to keep telling adb to use a different port on the phone each time because ASE randomly chooses a new port each time.

What I do is just use adb to copy the script to the phone ("adb push bookInventory.py /sdcard/ase/scripts/bookInventory.py").  I put that one line in a .bat file to save typing.  I then run it from ASE by just tapping on bookInventory.py in the list of scripts.

To create the actual scripts on Windows I just use notepad, and on Linux as I use vi.  You can also use a Python IDE like Idle (which gives you syntax checking and highlighting).

My scanner is very straightforward to use.  I point the camera at a barcode on a book, it scans it, looks it up in Google Books, and says either "okay" or "book not found".  It also writes the ISBN and other information to a file on the SD card, so I can mount it on the PC and access it later.  When you're done scanning books, you just scan this special QR code:

which tells the program to stop scanning and exit.  I recommend printing it out on a little piece of paper and having it handy when you're scanning.  It's a lot easier than lugging your computer monitor along with you.   :-)

The program uses the Barcode Scanner I mentioned earlier, and the text-to-speech service from the Android Marketplace.

And here's the entire program:

# Book scanner for the Android phone
# Written by Bernie Roehl, July 2009

# Scans book barcodes and looks them up by ISBN in Google Books.
# Writes entries to a booklist file, storing the ISBN and title
# for books it finds and just the ISBN for books it doesn't find.
#
# Scan a QR code that says "Stop scanning" to exit

import android
import urllib
import xml.dom.minidom

droid = android.Android()

def nodeValue(doc, tagname):  # get the text inside a node
	return str(doc.getElementsByTagName(tagname)[0].firstChild.nodeValue)

out = open("/sdcard/booklist.txt", "a")

while True:
	code = droid.scanBarcode()
	isbn = code['result']['SCAN_RESULT']
	if isbn == "Stop scanning":
		droid.speak("Shutting down")
		break
	url = "http://books.google.com/books/feeds/volumes?q=" + isbn
	doc = xml.dom.minidom.parse(urllib.urlopen(url))
	found = int(nodeValue(doc, "openSearch:totalResults"))
	if found:
		title = nodeValue(doc, "dc:title")
		droid.speak("Okay")
		print >>out, "found %s %s" % (isbn, title)
	else:
		droid.speak("Sorry, book not found")
		print >>out, "not_found %s" % isbn

out.close()

And that's all there is to it. You can add a shortcut to your program on the desktop by going into ASE, long-holding on the name of your app, and selecting "Add shortcut".

Very cool.