Python Bluetooth
Python Bluetooth
In modern society, Bluetooth technology has become one of the standards for wireless communications. It is widely used in various devices, such as headphones, keyboards, mice, and speakers. Python, as a powerful programming language, also provides libraries to support Bluetooth communication. This article will detail how to use Bluetooth technology for communication in Python.
What is Bluetooth?
Bluetooth is a short-range wireless communication technology that allows data to be transmitted between devices without the need for cables. Bluetooth technology is commonly used to connect devices, such as between a mobile phone and headphones, or between a computer and a keyboard and mouse.
Bluetooth technology has many advantages, such as low power consumption, low cost, and ease of use. It is suitable for a variety of scenarios, including home entertainment, smart homes, healthcare, and more. In Python, we can use related libraries to implement Bluetooth communication.
Python Bluetooth Libraries
Several libraries are available for implementing Bluetooth communication in Python, such as pybluez
and bluetooth
. pybluez
is a Python library based on the Bluetooth specification that provides a rich set of functions for communication between Bluetooth servers and clients.
Installing the pybluez Library
To use the pybluez library in Python, you first need to install it. You can install it using the pip command:
pip install pybluez
Once installed, you can import the pybluez library into your Python code to begin using Bluetooth.
Bluetooth Server
First, let’s implement a simple Bluetooth server to demonstrate how to use the pybluez library. The following is a simple server-side code:
import bluetooth
server_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
server_sock.bind(("", bluetooth.PORT_ANY))
server_sock.listen(1)
port = server_sock.getsockname()[1]
uuid = "00001101-0000-1000-8000-00805F9B34FB"
bluetooth.advertise_service(server_sock, "SampleServer",
service_id=uuid,
service_classes=[uuid, bluetooth.SERIAL_PORT_CLASS],
profiles=[bluetooth.SERIAL_PORT_PROFILE])
print("Waiting for connection on RFCOMM channel", port)
client_sock, client_info = server_sock.accept()
print("Accepted connection from", client_info)
try:
while True:
data = client_sock.recv(1024)
if not data:
break
print("Received", data)
except OSError:
pass
print("Disconnected.")
client_sock.close()
server_sock.close()
In the above code, we first create a Bluetooth server socket and bind it to an available port. Then, we set the server’s UUID and call the advertise_service
function to advertise the Bluetooth service. Finally, we wait for client connections using the accept
function and print out any client data received.
You can save the above code as server.py
and run it from the command line. When the client connects to the server, you will see the received data printed out.
Bluetooth Client
Now, let’s implement a simple Bluetooth client to connect to the server implemented above. Below is the simple client code:
import bluetooth
server_mac = "AA:BB:CC:DD:EE:FF"
port = 3
uuid = "00001101-0000-1000-8000-00805F9B34FB"
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((server_mac, port))
while True:
data = input("Enter your message: ")
sock.send(data)
sock.close()
In the above code, we first set the server’s MAC address and port number, then create a Bluetooth client socket and connect to the server. Afterwards, we loop and wait for the user to enter data, then send the data to the server using the send
function.
Save the above code as client.py
and run it from the command line. Once the client connects to the server, you can enter a message and send it to the server.
Conclusion
Through this article, you’ve learned how to use the pybluez library to implement Bluetooth communication in Python. You can further expand and improve the above code to implement more complex Bluetooth functionality based on your needs. Bluetooth technology is widely used in modern society. I believe that mastering Python Bluetooth communication will greatly facilitate your project development.