Simple Bluetooth Chat in Python
Bluetooth technology allows devices to communicate wirelessly over short distances. In this article, we will be discussing how to create a simple Bluetooth chat application using Python.
Setting up the environment
Before we start coding, make sure you have a Python environment set up on your computer. You will also need to install the PyBluez library, which provides support for Bluetooth communication in Python.
pip install pybluez
Creating the chat application
Now that we have the necessary libraries installed, let’s start creating our Bluetooth chat application. Below is a simple Python script that allows two devices to chat with each other over Bluetooth:
import bluetooth
server_socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
port = 1
server_socket.bind(("", port))
server_socket.listen(1)
client_socket, address = server_socket.accept()
print("Accepted connection from", address)
while True:
message = input("You: ")
client_socket.send(message)
data = client_socket.recv(1024)
print("Friend:", data.decode("utf-8"))
In this script, we create a server socket and listen for incoming connections. Once a client connects, we enter a loop where we continuously send and receive messages between the two devices.
Running the application
To run the chat application, run the script on one device and then run the script again on another device. Make sure the devices are paired and have Bluetooth enabled. Once the connection is established, you can start chatting with each other through the terminal.
Conclusion
Creating a simple Bluetooth chat application in Python is easy with the help of the PyBluez library. You can further customize the application by adding features such as encryption, file transfer, or group chat. Have fun experimenting with Bluetooth communication in Python!
btw if anyone has issues with socket.AF_BLUETOOTH. downlod the latest version of python (worked on 3.12.2 for me)
Love your videos! Another way to grab your Mac is ipconfig /all in cmd 😊
What happend to all the bluetooth stuff ilke GATT and ATTR, and bluetooth profiles?
Does this mean just treat the bluetooth connection like a socket then forget all the Bluetooth documentation?
great tutorial.
how to implement that into a real project like communicating python script in computer with android phone Bluetooth?
After 1 or 2 days of searching and trials, I finally found this gentleman and succeed in a few minutes. I'm almost crying. Million thanks.
this is an really cool project. I tried it
is the user interface from the mac a terminal or did he code anything extra to make a UI?
I am using react native as client. I am able to connect but not able to send data, also connection does not happen through python server. how to send data to server with native. any help is highly appreciated. Thanks
its awesome dude. can you also make a tutorial on sending text with mobiles
Can I transfer also simulation from PyGame?
client=socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
AttributeError: module 'socket' has no attribute 'AF_BLUETOOTH'
how can I solve it?
Great tutorial man, which kind of data can we transfer through a Bluetooth connection?
I was wondering, is it possible to put the client script on phone?
this is insanely cool
hey man can you make video about to discover nearby bluetooth devices without using pybluez(or pybluez2) or make video to discover devices in pybluez2 & transmit the data from one device to another device(like pc to mobile phone)
where is the code so I can test it? i mean the github link to your python chat code so i can modify? I searched your github and cant find the chat script you mentioned can you reply this with a working link to it?
Will this work between a raspberry pi and Android mobile??
how to print the product of integers between two integers
Is it possible to make a whatsapp chatbot in python???
Can this work on mobile devices?