btRecv.py 959 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/usr/bin/env python3
  2. import bluetooth
  3. server_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
  4. server_sock.bind(("", bluetooth.PORT_ANY))
  5. server_sock.listen(1)
  6. port = server_sock.getsockname()[1]
  7. uuid = "00001101-0000-1000-8000-00805f9b34fb"
  8. bluetooth.advertise_service(server_sock, "SampleServer", service_id=uuid,
  9. service_classes=[uuid, bluetooth.SERIAL_PORT_CLASS],
  10. profiles=[bluetooth.SERIAL_PORT_PROFILE],
  11. # protocols=[bluetooth.OBEX_UUID]
  12. )
  13. print("Waiting for connection on RFCOMM channel", port)
  14. client_sock, client_info = server_sock.accept()
  15. print("Accepted connection from", client_info)
  16. try:
  17. while True:
  18. data = client_sock.recv(1024)
  19. if not data:
  20. break
  21. print("Received", data)
  22. except OSError:
  23. pass
  24. print("Disconnected.")
  25. client_sock.close()
  26. server_sock.close()
  27. print("All done.")