Bluetooth Programming With Python 3: The Two Options
Bluetooth Programming With Python 3: The Two Options
Bluetooth Programming With Python 3: The Two Options
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.
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()
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:
PyBluez examples:
Example code covering various Bluetooth tasks.
Comments
18 Comments blog.kevindoran.co 🔒 Disqus' Privacy Policy
1 Login
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name