def pair(): """ Function to try to pair with a phone. Note that this function accepts all pair request that come in """ logger.log("Trying to pair with a device", logger.LOG_DEBUG) # only continue if there is no network if wifi.ConnectedToTheNetwork(): return # wait unitl we pair with a devices AutoPair = pairable.BtAutoPair() logger.log("Configuring discoverability settings") AutoPair.enable_pairing() logger.log("Done configuring bluetooth settings") # check if we paired with a new device has_connected = subprocess.check_output("bluetoothctl info | head -n1", shell=True).decode("utf-8") logger.log(has_connected) while "Missing" in has_connected: has_connected = subprocess.check_output("bluetoothctl info | head -n1", shell=True).decode("utf-8") time.sleep(0.1) logger.log("Connected with device: " + has_connected) return has_connected.split(" ")[1]
def test_IsConnectedToWifi(self): # check if a network connection and is in fact real connected = True try: socket.setdefaulttimeout(1) socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect( ("1.1.1.1", 53)) except socket.error as ex: connected = False self.assertTrue(wifi.ConnectedToTheNetwork()) self.assertTrue(connected)
def try_connect(self): """ Trying to connect to the network """ logger.log("Trying to connect to the network {}".format(self.ssid)) # TODO: try to connect to the network using the given credentials cli = wpa.wpa( ["ssid {}".format(self.ssid), "psk {}".format(self.password)]) cli.execute() # give time to connect time.sleep(config.WIFI_WAIT_UNTIL_CONNECTION) return wifi.ConnectedToTheNetwork()
def getWifiData(client, clientInfo, server): """ Comminicate with the phone over bluetooth to gather the wifi data here """ if wifi.ConnectedToTheNetwork(): return logger.log("Receiving wifi credentials", logger.LOG_DEBUG) connection = None while 1: data = client.recv(size).decode("utf-8") if data: if "TYPE:" in data: type = extractData("TYPE", data) if type == "wpa2": connection = wifiConnection() elif type == "mchap": connection = wifiMCHAPConnection() else: client.send("ERROR:2 - Server doesn't recognize wifi type") if "SSID:" in data: if connection: connection.ssid = extractSSID(data) else: client.send("ERROR:1 - No connection specified") elif "PWD:" in data: if connection: connection.password = extractPassword(data) else: client.send("ERROR:1 - No connection specified") elif "USER:"******"ERROR:4 cannot set property that is not part of the connection type" ) else: client.send("ERROR:1 - No connection specified") elif "TRY:1" in data: if connection: if connection.try_connect( ): # try to connect to the network client.send("SUCCESS:1 - connected to a network") else: client.send("ERROR:3 - Network credentials are wrong") else: client.send("ERROR:1 - No connection specified") else: client.send(data) # Echo back to client
def idle(server): """ Wait until a bluetooth connection is made @server is the bluetooth connection server """ logger.log("Waiting for a bluetooth connection", logger.LOG_DEBUG) if wifi.ConnectedToTheNetwork(): return None, None try: client, clientInfo = server.accept() except: print("Closing socket") client.close() server.close() return client, clientInfo
def startup(server): """ Initialize a bluetooth server so that we can communicate with the client phone @server is the bluetooth connection server """ logger.log("Starting up the bluetooth module", logger.LOG_DEBUG) logger.log("Connected to bluetooth device: {} ".format(pair()), logger.LOG_DEBUG) if wifi.ConnectedToTheNetwork(): return server.bind((hostMACAddress, port)) server.listen(backlog) uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee" bluetooth.advertise_service( sock=server, name="Bluetooth Speaker", service_id=uuid, service_classes=[bluetooth.SERIAL_PORT_CLASS], profiles=[bluetooth.SERIAL_PORT_PROFILE], ) return server
def test_IsConnectedToWifi(self): # check if a network connection exists self.assertTrue(wifi.ConnectedToTheNetwork())
def isConnectedTOWifi(): return wifi.ConnectedToTheNetwork()