Dusting my blog with Bluetooth API !
Hi Folks !
Its been quite a long time since I wrote a blog entry ( few months perhaps ?! ). So refreshing it again with something I wanted to write for quite some time now.
Not many good tutorials on developing a bluetooth application. And It doesn't get any easier with our Python. I am not gonna brief here about the Bluetooth protocol as such. I believe users are already familiar with it.
If not, nothing to worry. Its quite similar to our traditional Internet programming. If you need to brush on Internet programming basics, Beej's guide is the best place to start !
Quite a few dependencies here. I am using CentOS. You can use your distro's package installer to install the same packages.
yum install bluez bluez-libs bluez-libs-devel
Bluez is the official bluetooth protocol stack of GNU/Linux.
Apart from that you also need to install python wrappers for Bluez. wget http://pybluez.googlecode.com/files/PyBluez-0.18.tar.gz tar -xvzf PyBluez-0.18.tar.gz cd PyBluez-0.18 python setup.py install
Your Bluetooth app can use an underlying RFCOMM/L2CAP protocols. They are similar to TCP/UDP protocols. However L2CAP also has a 'reliable' version that helps to retransmit infinitely. If this infinite retransmit option is suppressed ( for some real-time applications), its analogous to our UDP.
First lets write a simple code to discover devices around you. As usual, you need to ensure all devices are in "discoverable" mode.
https://gist.github.com/5211281
Here, the discover_devices() method returns list of bluetooth addresses. Bluetooth addresses are 48-bit addresses ( similar to MAC ). However, unlike MAC (used only upto data-link), they are used upto the application layer. This address uniquely identifies a device.
We need to perform a "lookup" ( similar to DNS ) to convert this 48-bit address to a human-readable format ( This is the name that we give to our device , say "My Phone" )
So , in my situation above snippet prints :
device address: 08:ED:B9:9F:EF:46 Name: FRIEND-PC device address: 38:16:D1:1F:90:C3 Name: Aravindh phone
Usually a bluetooth application involves both writing a server and client; However I am gonna focus only on writing a client for some well defined services available in most mobiles supporting bluetooth ( say OBEX File transfer service).
Now lets write a script to list out all the services supported by the device.Once we get to know that , we can query the required port.
https://gist.github.com/5211695
The above code snippet returns the port on which the OBEX file transfer service runs.
Once we have obtained the port, we can send a simple file to the device. For this we require the PyOBEX wrapper.
https://gist.github.com/5211721
The above snippet sends a simple text file to the device which will be save in the device in the name - 'date.txt' with contents as 'todays date'.
Its as simple as that. Hope you find it useful !
Cheers !! :-)
Resources :
1) http://people.csail.mit.edu/albert/bluez-intro/ 2) python "help" feature ;-)
















