import sys
import bluetooth

class GSV4BT():
  def __init__(self, addr):
    self.addr = addr
    self.uuid = None
    self.sock = None

  def connect(self):
    if self.sock:
      return True

    service_matches = bluetooth.find_service(address=self.addr)
    if len(service_matches) == 0:
      print("BT device {} not found.".format(self.addr))
      return False
    
    first_match = service_matches[0]
    self.uuid = first_match["uuid"]
    self.port = first_match["port"]
    self.name = first_match["name"]
    self.host = first_match["host"]

    print("Connecting to \"{}\" on {}".format(self.name, self.host))

    self.sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
    return self.sock.connect((self.host, self.port))
  
  def sendRaw(self, data):
    self.sock.send(data)

  def recvRaw(self):
    return self.sock.recv(1024)

  def getForces(self):
    return (0, 0, 0)

  def close(self):
    self.sock.close()