Skip to content

Instantly share code, notes, and snippets.

@kevindoran
Last active March 23, 2021 18:42
Show Gist options
  • Save kevindoran/5428390 to your computer and use it in GitHub Desktop.
Save kevindoran/5428390 to your computer and use it in GitHub Desktop.
A simple Python script to receive messages from a client over Bluetooth using Python sockets (with Python 3.3 or above).
"""
A simple Python script to receive messages from a client over
Bluetooth using Python sockets (with Python 3.3 or above).
"""
import socket
hostMACAddress = '00:1f:e1:dd:08:3d' # The MAC address of a Bluetooth adapter on the server. The server might have multiple Bluetooth adapters.
port = 3 # 3 is an arbitrary choice. However, it must match the port used by the client.
backlog = 1
size = 1024
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s.bind((hostMACAddress,port))
s.listen(backlog)
try:
client, address = s.accept()
while 1:
data = client.recv(size)
if data:
print(data)
client.send(data)
except:
print("Closing socket")
client.close()
s.close()
@mitjad
Copy link

mitjad commented May 28, 2018

on windows 10, python 3.6.5 - error
AttributeError: module 'socket' has no attribute 'AF_BLUETOOTH'

@maxcoldson
Copy link

Hi mitjad:

socket.AF_BLUETOOTH looks like only for Linux only.

@reold
Copy link

reold commented Nov 28, 2020

Hi, maxcoldson and mitjad,
Well, people say there is the same available for python3.9. I don't know about it. But you can check.

@Digital-Rookie4
Copy link

Hi, maxcoldson and mitjad,
Well, people say there is the same available for python3.9. I don't know about it. But you can check.

I can definately tell you from doing it myself that python 3.9 is the solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment