GSV4BT.py 934 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import sys
  2. import bluetooth
  3. class GSV4BT():
  4. def __init__(self, addr):
  5. self.addr = addr
  6. self.uuid = None
  7. self.sock = None
  8. def connect(self):
  9. if self.sock:
  10. return True
  11. service_matches = bluetooth.find_service(address=self.addr)
  12. if len(service_matches) == 0:
  13. print("BT device {} not found.".format(self.addr))
  14. return False
  15. first_match = service_matches[0]
  16. self.uuid = first_match["uuid"]
  17. self.port = first_match["port"]
  18. self.name = first_match["name"]
  19. self.host = first_match["host"]
  20. print("Connecting to \"{}\" on {}".format(self.name, self.host))
  21. self.sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
  22. return self.sock.connect((self.host, self.port))
  23. def sendRaw(self, data):
  24. self.sock.send(data)
  25. def recvRaw(self):
  26. return self.sock.recv(1024)
  27. def getForces(self):
  28. return (0, 0, 0)
  29. def close(self):
  30. self.sock.close()