Basic Bluetooth Communication in Python

Posted by

Simple Bluetooth Chat in Python

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!

0 0 votes
Article Rating
37 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@zeren5005
4 months ago

btw if anyone has issues with socket.AF_BLUETOOTH. downlod the latest version of python (worked on 3.12.2 for me)

@abra866
4 months ago

Love your videos! Another way to grab your Mac is ipconfig /all in cmd 😊

@GoForwardPs34
4 months ago

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?

@pirate9-cb7jb1gr9j
4 months ago

great tutorial.
how to implement that into a real project like communicating python script in computer with android phone Bluetooth?

@user-bz9ly7tl3r
4 months ago

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.

@udageethdias
4 months ago

this is an really cool project. I tried it

@francisvincejaca4077
4 months ago

is the user interface from the mac a terminal or did he code anything extra to make a UI?

@dharikarsath8852
4 months ago

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

@ajuvinaju
4 months ago

its awesome dude. can you also make a tutorial on sending text with mobiles

@filippocaregnato4098
4 months ago

Can I transfer also simulation from PyGame?

@g.s.3389
4 months ago

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?

@7jonathan
4 months ago

Great tutorial man, which kind of data can we transfer through a Bluetooth connection?

@talshrem7286
4 months ago

I was wondering, is it possible to put the client script on phone?

@michalbotor
4 months ago

this is insanely cool

@kabalikali7439
4 months ago

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)

@JNET_Reloaded
4 months ago

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?

@girishjv1078
4 months ago

Will this work between a raspberry pi and Android mobile??

@yalcni2638
4 months ago

how to print the product of integers between two integers

@killerhakie
4 months ago

Is it possible to make a whatsapp chatbot in python???

@Bsoul_music
4 months ago

Can this work on mobile devices?