diff --git a/ch-13/2.3-bluetooth/find_devices.py b/ch-13/2.3-bluetooth/find_devices.py index 316c65b..44c2468 100644 --- a/ch-13/2.3-bluetooth/find_devices.py +++ b/ch-13/2.3-bluetooth/find_devices.py @@ -9,8 +9,7 @@ async def run(): print([device.name for device in devices]) matching_devices = [device for device in devices if device.name==ble_name] if len(matching_devices) == 0: - print(f"Could not find robot at {ble_name}") - return + raise RuntimeError("Could not find robot") ble_device_info = matching_devices[0] print(f"Found robot {ble_device_info.name}...") diff --git a/ch-13/2.3-bluetooth/robot_ble_connection.py b/ch-13/2.3-bluetooth/robot_ble_connection.py index 59953bd..91d37b1 100644 --- a/ch-13/2.3-bluetooth/robot_ble_connection.py +++ b/ch-13/2.3-bluetooth/robot_ble_connection.py @@ -22,9 +22,10 @@ class BleConnection: devices = await bleak.BleakScanner.discover( service_uuids=[self.ble_uuid] ) - print(f"Found {len(devices)} devices") - print([device.name for device in devices]) - ble_device_info = [device for device in devices if device.name==self.ble_name][0] + matching_devices = [device for device in devices if device.name==self.ble_name] + if len(matching_devices) == 0: + raise RuntimeError("Could not find robot") + ble_device_info = matching_devices[0] print(f"Connecting to {ble_device_info.name}...") self.ble_client = bleak.BleakClient(ble_device_info.address) await self.ble_client.connect() diff --git a/ch-13/3.1-displaying-a-pose/robot_ble_connection.py b/ch-13/3.1-displaying-a-pose/robot_ble_connection.py index 66dd100..91d37b1 100644 --- a/ch-13/3.1-displaying-a-pose/robot_ble_connection.py +++ b/ch-13/3.1-displaying-a-pose/robot_ble_connection.py @@ -19,10 +19,13 @@ class BleConnection: async def connect(self): print("Scanning for devices...") - devices = await bleak.BleakScanner.discover(service_uuids=[self.ble_uuid]) - print(f"Found {len(devices)} devices") - print([device.name for device in devices]) - ble_device_info = [device for device in devices if device.name==self.ble_name][0] + devices = await bleak.BleakScanner.discover( + service_uuids=[self.ble_uuid] + ) + matching_devices = [device for device in devices if device.name==self.ble_name] + if len(matching_devices) == 0: + raise RuntimeError("Could not find robot") + ble_device_info = matching_devices[0] print(f"Connecting to {ble_device_info.name}...") self.ble_client = bleak.BleakClient(ble_device_info.address) await self.ble_client.connect() diff --git a/ch-13/3.2-collision-avoidance/robot_ble_connection.py b/ch-13/3.2-collision-avoidance/robot_ble_connection.py index 66dd100..91d37b1 100644 --- a/ch-13/3.2-collision-avoidance/robot_ble_connection.py +++ b/ch-13/3.2-collision-avoidance/robot_ble_connection.py @@ -19,10 +19,13 @@ class BleConnection: async def connect(self): print("Scanning for devices...") - devices = await bleak.BleakScanner.discover(service_uuids=[self.ble_uuid]) - print(f"Found {len(devices)} devices") - print([device.name for device in devices]) - ble_device_info = [device for device in devices if device.name==self.ble_name][0] + devices = await bleak.BleakScanner.discover( + service_uuids=[self.ble_uuid] + ) + matching_devices = [device for device in devices if device.name==self.ble_name] + if len(matching_devices) == 0: + raise RuntimeError("Could not find robot") + ble_device_info = matching_devices[0] print(f"Connecting to {ble_device_info.name}...") self.ble_client = bleak.BleakClient(ble_device_info.address) await self.ble_client.connect() diff --git a/ch-13/3.3-moving-with-encoders/robot_ble_connection.py b/ch-13/3.3-moving-with-encoders/robot_ble_connection.py index 66dd100..91d37b1 100644 --- a/ch-13/3.3-moving-with-encoders/robot_ble_connection.py +++ b/ch-13/3.3-moving-with-encoders/robot_ble_connection.py @@ -19,10 +19,13 @@ class BleConnection: async def connect(self): print("Scanning for devices...") - devices = await bleak.BleakScanner.discover(service_uuids=[self.ble_uuid]) - print(f"Found {len(devices)} devices") - print([device.name for device in devices]) - ble_device_info = [device for device in devices if device.name==self.ble_name][0] + devices = await bleak.BleakScanner.discover( + service_uuids=[self.ble_uuid] + ) + matching_devices = [device for device in devices if device.name==self.ble_name] + if len(matching_devices) == 0: + raise RuntimeError("Could not find robot") + ble_device_info = matching_devices[0] print(f"Connecting to {ble_device_info.name}...") self.ble_client = bleak.BleakClient(ble_device_info.address) await self.ble_client.connect() diff --git a/ch-13/4.1-generating-weights/robot_ble_connection.py b/ch-13/4.1-generating-weights/robot_ble_connection.py index 66dd100..91d37b1 100644 --- a/ch-13/4.1-generating-weights/robot_ble_connection.py +++ b/ch-13/4.1-generating-weights/robot_ble_connection.py @@ -19,10 +19,13 @@ class BleConnection: async def connect(self): print("Scanning for devices...") - devices = await bleak.BleakScanner.discover(service_uuids=[self.ble_uuid]) - print(f"Found {len(devices)} devices") - print([device.name for device in devices]) - ble_device_info = [device for device in devices if device.name==self.ble_name][0] + devices = await bleak.BleakScanner.discover( + service_uuids=[self.ble_uuid] + ) + matching_devices = [device for device in devices if device.name==self.ble_name] + if len(matching_devices) == 0: + raise RuntimeError("Could not find robot") + ble_device_info = matching_devices[0] print(f"Connecting to {ble_device_info.name}...") self.ble_client = bleak.BleakClient(ble_device_info.address) await self.ble_client.connect()