/examples/simple/rfcomm-client.py

http://pybluez.googlecode.com/ · Python · 48 lines · 29 code · 11 blank · 8 comment · 6 complexity · ebf333ffc94aef1fd670a1cb95f98134 MD5 · raw file

  1. # file: rfcomm-client.py
  2. # auth: Albert Huang <albert@csail.mit.edu>
  3. # desc: simple demonstration of a client application that uses RFCOMM sockets
  4. # intended for use with rfcomm-server
  5. #
  6. # $Id: rfcomm-client.py 424 2006-08-24 03:35:54Z albert $
  7. from bluetooth import *
  8. import sys
  9. if sys.version < '3':
  10. input = raw_input
  11. addr = None
  12. if len(sys.argv) < 2:
  13. print("no device specified. Searching all nearby bluetooth devices for")
  14. print("the SampleServer service")
  15. else:
  16. addr = sys.argv[1]
  17. print("Searching for SampleServer on %s" % addr)
  18. # search for the SampleServer service
  19. uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
  20. service_matches = find_service( uuid = uuid, address = addr )
  21. if len(service_matches) == 0:
  22. print("couldn't find the SampleServer service =(")
  23. sys.exit(0)
  24. first_match = service_matches[0]
  25. port = first_match["port"]
  26. name = first_match["name"]
  27. host = first_match["host"]
  28. print("connecting to \"%s\" on %s" % (name, host))
  29. # Create the client socket
  30. sock=BluetoothSocket( RFCOMM )
  31. sock.connect((host, port))
  32. print("connected. type stuff")
  33. while True:
  34. data = input()
  35. if len(data) == 0: break
  36. sock.send(data)
  37. sock.close()