Bluetooth Programming With Python 3: The Two Options

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Bluetooth Programming with Python 3

written Apr 21st, 2013

This post presents basic techniques for communicating over Bluetooth with Python 3.3
and above (using Python sockets). Some simple examples of communicating over
Bluetooth with sockets will be shown. PyBluez examples will also be presented for
comparison.

The two options


Currently, the most widely documented way to communicate with Python over
Bluetooth is to use PyBluez. Previously, PyBluez only supported Python 2. In January
2014, they released a Python 3 version.

Python 3.3’s native Python sockets support Bluetooth communication. Unfortunately,


there is very little documentation available describing how to use Python sockets to
communicate over Bluetooth. While using Bluetooth with these sockets might be easy
for someone who already knows how to use Python sockets, the lack of documentation
leaves many people unaware that this method of using Bluetooth even exists. Since
PyBluez was ported to Python 3, the use of native Python sockets has limited use.

Required skill: finding the MAC address of a bluetooth


adapter
To run the examples, the MAC address of the Bluetooth adapter used by the server must
be known. The client application uses this address to connect to the server. On Linux,
you can get a list of all available Bluetooth devices and their MAC addresses using the
command hciconfig, like so:
Client sever messaging
This application connects two devices over Bluetooth and allows one to send messages
to the other. The sending device runs socketClient.py, and the receiving device runs
socketServer.py. These scripts are shown below, first using Python sockets, then
using PyBluez:

Python sockets
Client (socketClient.py)

1 """
2 A simple Python script to send messages to a sever over Bluetooth using
3 Python sockets (with Python 3.3 or above).
4 """
5
6 import socket
7
8 serverMACAddress = '00:1f:e1:dd:08:3d'
9 port = 3
10 s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
11 s.connect((serverMACAddress,port))
12 while 1:
13 text = input()
14 if text == "quit":
15 break
16 s.send(bytes(text, 'UTF-8'))
17 s.close()

Server (socketServer.py)

1 """
2 A simple Python script to receive messages from a client over
3 Bluetooth using Python sockets (with Python 3.3 or above).
4 """
5
6 import socket
7
8 hostMACAddress = '00:1f:e1:dd:08:3d' # The MAC address of a Bluetooth adapter on the s
9 port = 3 # 3 is an arbitrary choice. However, it must match the port used by the clien
10 backlog = 1
11 size = 1024
12 s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
13 s.bind((hostMACAddress,port))
14 s.listen(backlog)
15 try:
16 client, address = s.accept()
17 while 1:
18 data = client.recv(size)
19 if data:
20 print(data)
21 client.send(data)
22 except:
23 print("Closing socket")
24 client.close()
25 s.close()

As an aside, this code is almost identical to code required to create a client-server


application over the internet. All that needs to be changed is the two lines:

1 # For the Server


2 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
3 s.bind(("192.168.1.17",50001)) # The Bluetooth MAC Address and RFCOMM port is replaced
4
5 # For the Client
6 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
7 s.connect(("192.168.1.17",50001))
8
9 # Note: these are arbitrary IP addresses and TCP ports.

PyBluez
To compare, below is the functionally identical application written using the PyBluez
library.

Client (PyBluezClient.py)

1 """
2 A simple Python script to send messages to a sever over Bluetooth
3 using PyBluez (with Python 2).
4 """
5
6 import bluetooth
7
8 serverMACAddress = '00:1f:e1:dd:08:3d'
9 port = 3
10 s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
11 s.connect((serverMACAddress, port))
12 while 1:
13 text = raw_input() # Note change to the old (Python 2) raw_input
14 if text == "quit":
15 break
16 s.send(text)
17 sock.close()

Server (PyBluezServer.py)
1 """
2 A simple Python script to receive messages from a client over
3 Bluetooth using PyBluez (with Python 2).
4 """
5
6 import bluetooth
7
8 hostMACAddress = '00:1f:e1:dd:08:3d' # The MAC address of a Bluetooth adapter on the s
9 port = 3
10 backlog = 1
11 size = 1024
12 s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
13 s.bind((hostMACAddress, port))
14 s.listen(backlog)
15 try:
16 client, clientInfo = s.accept()
17 while 1:
18 data = client.recv(size)
19 if data:
20 print(data)
21 client.send(data) # Echo back to client
22 except:
23 print("Closing socket")
24 client.close()
25 s.close()

Conclusion
PyBluez is the most effective way of communicating over Bluetooth using Python.
Python sockets can now be used for Bluetooth communication (since Python 3.3). For a
simple application, the code is almost identical. For some tasks, however, such as
device discovery and Bluetooth service advertisements, it does not seem possible to
carry them out using Python sockets. Consequently, PyBluez surpassed Python sockets
in most regards. This Stackoverflow question discusses some of the limitations of
Python sockets for Bluetooth.

Further reading
There is little to no information on Bluetooth programming with Python sockets. There is
plenty of information on PyBluez. The following are some useful resources:

Bluetooth Essentials for Programmers:


Covers many programming languages and uses PyBluez with Python. Great for
getting started fast and gaining understanding along the way.

PyBluez examples:
Example code covering various Bluetooth tasks.

« Previous: How to Create Dynamic Menus in JSF Next: Solr Tutorial »

Comments
18 Comments blog.kevindoran.co 🔒 Disqus' Privacy Policy 
1 Login

 Recommend 11 t Tweet f Share Sort by Best

Join the discussion…

LOG IN WITH
OR SIGN UP WITH DISQUS ?

Name

Leopold • 5 years ago


Thanks for the post. It helped me decide with way to choose as my server needs to advertise its service I
will need to use Pybluez. I cant manage to install the Pybluez module I need for my program to work. (I
assume its another one than Pybluez for python 2, as when I download that one seems to be only
downloading for python 2 and not 3). I have searched online but I find nothing with Pybluez for Python 3 ...
Thanks a lot in advance
2 R l Sh
H om e

Power ed by Octopr ess. D esign for k ed fr om Adr ia n Ar tiles.

You might also like