From 7a45e7d51ea979180c329c6a8f66936a2074b515 Mon Sep 17 00:00:00 2001 From: klonfish Date: Mon, 26 Dec 2016 23:53:30 +0100 Subject: [PATCH] Added exception handling on timeout --- wii-pair/wiiboard.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/wii-pair/wiiboard.py b/wii-pair/wiiboard.py index 7be6711..6e3997d 100644 --- a/wii-pair/wiiboard.py +++ b/wii-pair/wiiboard.py @@ -70,9 +70,7 @@ class Wiiboard: try: self.datasocket = bluetooth.BluetoothSocket(bluetooth.L2CAP) - self.datasocket.settimeout(2) self.controlsocket = bluetooth.BluetoothSocket(bluetooth.L2CAP) - self.controlsocket.settimeout(2) except ValueError: raise Exception("Error: Bluetooth not found") @@ -88,7 +86,9 @@ class Wiiboard: print("Non existant address") return self.datasocket.connect((address, 0x13)) + self.datasocket.settimeout(2) self.controlsocket.connect((address, 0x11)) + self.controlsocket.settimeout(2) if self.datasocket and self.controlsocket: print("Connected to Wiiboard at address " + address) self.status = "Connected" @@ -181,7 +181,7 @@ class Wiiboard: # Thread that listens for incoming data def receivethread(self): while self.status == "Connected": - if True: + try: data = self.datasocket.recv(25) intype = data[1] if intype == INPUT_STATUS: @@ -202,6 +202,9 @@ class Wiiboard: else: print("ACK to data write received") + except bluetooth.btcommon.BluetoothError: + print('Error! Disconnecting') + self.disconnect() self.status = "Disconnected" self.disconnect()