July 26, 2013

Python 2.6 and PyBluez

My research on Python ended with Python 2.6 and PyBluez. If you haven't noticed, there are newer version of Python (currently on version 3.3) but the later versions are not compatible with PyBluez. My so-far googling skills got me to the conclusion that this is the better solution, albeit temporarily. Python does not have Bluetooth native library before version 3.0 (if I'm not mistaken) and while they have added them now, the documentations were not really thorough. PyBluez, on the contrary, is only supported for Python until version 2.6. Since I have to do some demonstration with Python and Bluetooth, this is the easier solution. I will get back to the Python native Bluetooth library later on.

This post will help you to do 2 things, assuming you're on Windows:

1. Installing Python 2.6 and PyBluez
2. Run a simple Bluetooth serial server script

Installing Python 2.6 and PyBluez
1. Get them both here : Python 2.6 PyBluez
2. Run the Python installer first, followed by the PyBluez Installer

Run the Bluetooth serial server
1. Copy the whole code below into a file named BluetoothServer.py
2. Save the file in the same folder where Python was installed previously
3. At this point, turn on your Bluetooth, set for an incomming connection (set a listening port) and pair with the other device (client)
4. Start the Windows command prompt. Navigate to the folder where Python was installed
5. Run the script : >>python BluetoothServer.py
6. Server is active and ready for an incoming connection : "Waiting for connection on RFCOMM Channel 1"

The simple server is based on the PyBluez examples here.

# file: BluetoothServer.py
# desc: Simple Bluetooth Serial Server (Python 2.6 and PyBluez)

from bluetooth import *

server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)

port = server_sock.getsockname()[1]

#change UUID accordingly
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee" 

#start the listening server
advertise_service( server_sock, "BluetoothServer",
                   service_id = uuid,
                   service_classes = [ uuid, SERIAL_PORT_CLASS ],
                   profiles = [ SERIAL_PORT_PROFILE ], 
                    )
                   
print "Waiting for connection on RFCOMM channel %d" % port

#accepting connection
client_sock, client_info = server_sock.accept()
print "Accepted connection from ", client_info

#catch data and display.
#wait for the client to cut off the connection

try:
    while True:
        data = client_sock.recv(1024)
        if len(data) == 0: break
        print "received [%s]" % data
except IOError:
    pass

print "client disconnected"

#close sockets 
client_sock.close()
server_sock.close()
print "Server is off"

The Android App (client) for sending in data is discussed separately on another post. The next step is to have this simple server ran on the Raspberry Pi and adding in some output manipulations using the Pi's GPIO pins.

Update: Here is a good read on the same topic. Link

No comments:

Post a Comment

Write a reply