From dfe97777fd8cbe7043454bc09635f6c331a69440 Mon Sep 17 00:00:00 2001 From: Shin'ichiro Kawasaki Date: Sat, 18 Jul 2020 16:44:20 +0900 Subject: [PATCH] BLESession: Support multiple Bluetooth adapters It was reported that bluepy-scratch-link fails to connect to BLE devices with the PC has multiple Bluetooth adapters if the first adapter does not support BLE. To support such a case, scan Bluetooth adapters up to 3. Signed-off-by: Shin'ichiro Kawasaki --- scratch_link.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/scratch_link.py b/scratch_link.py index 4ee7615..788dca2 100755 --- a/scratch_link.py +++ b/scratch_link.py @@ -335,6 +335,8 @@ class BLESession(Session): 0x2: "adtype incomplete 16b", } + MAX_SCANNER_IF = 3 + class BLEThread(threading.Thread): """ Separated thread to control notifications to Scratch. @@ -494,14 +496,20 @@ class BLESession(Session): err_msg = None if self.status == self.INITIAL and method == 'discover': - scanner = Scanner() - try: - devices = scanner.scan(10.0) - for dev in devices: - if self.matches(dev, params['filters']): - self.found_devices.append(dev) - except BTLEManagementError as e: - logger.error(e); + found_ifaces = 0 + for i in range(self.MAX_SCANNER_IF): + scanner = Scanner(iface=i) + try: + devices = scanner.scan(10.0) + for dev in devices: + if self.matches(dev, params['filters']): + self.found_devices.append(dev) + found_ifaces += 1 + logger.debug(f"BLE device found with iface #{i}"); + except BTLEManagementError as e: + logger.debug(f"BLE iface #{i}: {e}"); + + if found_ifaces == 0: err_msg = "Can not scan BLE devices. Check BLE controller." logger.error(err_msg); res["error"] = { "message": err_msg }