Setting up and using pyBluez on the Nokia 770

From CENS Urban Sensing

Contents

Prereqs

  • python2.4 installed on 770

Installation Procedure


Copy bluetooth.py and _blutooth.so to the python directory on your 770

  • (should be /var/lib/install/usr/lib/python2.4/site-packages)


All set!

Using PyBlueZ

There is a good tutorial at: http://people.csail.mit.edu/albert/bluez-intro/c209.html

Finding Nearby Devices

Instead of using "hcitool scan" from the command line, you can use bluetooth.discover_devices() from within your python program. It will return a list of the addresses (in string form) or all bluetooth devices it can find:

import bluetooth

# Look for devices
addresses = bluetooth.discover_devices()
if len(addresses) == 0:
	print "No devices found!"
else:
	print addresses

Reading Data from a Bluetooth Device

You can use bluetooth.BluetoothSocket() to get a socket for communicating with a bluetooth device. This socket acts just like any normal TCP/IP socket for the most part, so you can do socket.send() and socket.recv() to send and receive data (as strings). The following code will connect to a GPS unit (or any bluetooth serial port device for that matter), and then read 200 bytes from it. (make sure to change gpsAddress to the address for your own GPS unit)

import bluetooth

# Connect to the GPS (by hardware address)
gpsAddress = '00:08:1B:0C:FD:D9'
sock = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((gpsAddress, 1))

# Read 200 bytes
print sock.recv(200)

Finding the Device Name from its Address

When there are multiple devices within range, you may want to find out which ones are, for example, GPS units. To do this you can query the device for its name. (All of our GPS units are named "HOLUX GR-231") (i think)

import sys, bluetooth

addresses = bluetooth.discover_devices()
if len(addresses) == 0:
	print "No devices found!"
	sys.exit()

print "Devices found:"
print "===================================="
import bluetooth
gpsAddress = None
for addr in addresses:
	name = bluetooth.lookup_name(addr)
	print addr, "(" + name + ")"


Example Program for finding and Reading from a GPS Unit

pybluez_example.py

Personal tools