From 0bf96a187f0eb1e56da120ca7f3d62e285d3d14d Mon Sep 17 00:00:00 2001 From: wsidl Date: Mon, 2 Sep 2019 17:36:15 -0700 Subject: [PATCH] Adding a retry effort in device scan c3V6a2Vy/pyanova#4 Adding a retry loop to allow resetting the BLE adapter so it can perform another scan --- pyanova/pyanova.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/pyanova/pyanova.py b/pyanova/pyanova.py index 3510e4a..d9e31e7 100644 --- a/pyanova/pyanova.py +++ b/pyanova/pyanova.py @@ -185,7 +185,14 @@ def discover(self, list_all = False, dev_mac_pattern=DEFAULT_DEV_MAC_PATTERN, ti e.g: [{'name': 'ffs', 'address': '01:02:03:04:05:10'}, {'name': 'rlx', 'address': '01:02:03:04:05:21'}] """ - devices = self._adapter.scan(run_as_root=True, timeout=timeout) + retries = 0 + while retries < 5: + try: + devices = self._adapter.scan(run_as_root=True, timeout=timeout) + except pygatt.exceptions.BLEError: + self._logger.info('Resetting BLE Adapter, retrying scan') + self._adapter.reset() + retries += 1 if list_all: return devices return list(filter(lambda dev: dev_mac_pattern.match(dev['address']), devices)) @@ -326,4 +333,4 @@ def set_unit(self, unit, handle=DEVICE_NOTIFICATION_CHAR_HANDLE, timeout=DEFAULT errmsg = 'Expected unit to be either \'c\' or \'f\', found: %s'%unit self._logger.error(errmsg) raise ValueError(errmsg) - return self._write_strcmd(SET_TEMP_UNIT%unit, handle, timeout) \ No newline at end of file + return self._write_strcmd(SET_TEMP_UNIT%unit, handle, timeout)