btSend.py 959 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env python3
  2. import sys
  3. import bluetooth
  4. addr = None
  5. if len(sys.argv) < 2:
  6. print("No device specified. Searching all nearby bluetooth devices for "
  7. "the SampleServer service...")
  8. else:
  9. addr = sys.argv[1]
  10. print("Searching for SampleServer on {}...".format(addr))
  11. # search for the SampleServer service
  12. uuid = "00001101-0000-1000-8000-00805f9b34fb"
  13. service_matches = bluetooth.find_service(uuid=uuid, address=addr)
  14. if len(service_matches) == 0:
  15. print("Couldn't find the SampleServer service.")
  16. sys.exit(0)
  17. first_match = service_matches[0]
  18. port = first_match["port"]
  19. name = first_match["name"]
  20. host = first_match["host"]
  21. print("Connecting to \"{}\" on {}".format(name, host))
  22. # Create the client socket
  23. sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
  24. sock.connect((host, port))
  25. print("Connected. Type something...")
  26. while True:
  27. data = input()
  28. if not data:
  29. break
  30. sock.send(data)
  31. sock.close()