Skip to content

Instantly share code, notes, and snippets.

@ReactorboY
Created April 16, 2023 05:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ReactorboY/891c78358cbbbb1c4c4b84bf31da3a4c to your computer and use it in GitHub Desktop.
Save ReactorboY/891c78358cbbbb1c4c4b84bf31da3a4c to your computer and use it in GitHub Desktop.
import bluetooth
# host machine bluetooth mac address
bt_mac_address = "74:E5:F9:97:F8:E1"
server_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
server_sock.bind((bt_mac_address, bluetooth.PORT_ANY))
server_sock.listen(1)
port = server_sock.getsockname()[1]
uuid = "00001101-0000-1000-8000-00805F9B34FB"
bluetooth.advertise_service(server_sock, "BluetoothServer", 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)
msg = data.decode("utf-8")
if msg == "quit":
break
print("Received from client: ", msg)
client_sock.send(msg)
except OSError:
pass
print("Disconnected.")
client_sock.close()
server_sock.close()
print("All done.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment