mirror of
https://github.com/kawasaki/pyscrlink.git
synced 2025-09-07 18:20:07 +02:00
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 <kawasaki@juno.dti.ne.jp>
This commit is contained in:
@@ -335,6 +335,8 @@ class BLESession(Session):
|
|||||||
0x2: "adtype incomplete 16b",
|
0x2: "adtype incomplete 16b",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MAX_SCANNER_IF = 3
|
||||||
|
|
||||||
class BLEThread(threading.Thread):
|
class BLEThread(threading.Thread):
|
||||||
"""
|
"""
|
||||||
Separated thread to control notifications to Scratch.
|
Separated thread to control notifications to Scratch.
|
||||||
@@ -494,14 +496,20 @@ class BLESession(Session):
|
|||||||
err_msg = None
|
err_msg = None
|
||||||
|
|
||||||
if self.status == self.INITIAL and method == 'discover':
|
if self.status == self.INITIAL and method == 'discover':
|
||||||
scanner = Scanner()
|
found_ifaces = 0
|
||||||
|
for i in range(self.MAX_SCANNER_IF):
|
||||||
|
scanner = Scanner(iface=i)
|
||||||
try:
|
try:
|
||||||
devices = scanner.scan(10.0)
|
devices = scanner.scan(10.0)
|
||||||
for dev in devices:
|
for dev in devices:
|
||||||
if self.matches(dev, params['filters']):
|
if self.matches(dev, params['filters']):
|
||||||
self.found_devices.append(dev)
|
self.found_devices.append(dev)
|
||||||
|
found_ifaces += 1
|
||||||
|
logger.debug(f"BLE device found with iface #{i}");
|
||||||
except BTLEManagementError as e:
|
except BTLEManagementError as e:
|
||||||
logger.error(e);
|
logger.debug(f"BLE iface #{i}: {e}");
|
||||||
|
|
||||||
|
if found_ifaces == 0:
|
||||||
err_msg = "Can not scan BLE devices. Check BLE controller."
|
err_msg = "Can not scan BLE devices. Check BLE controller."
|
||||||
logger.error(err_msg);
|
logger.error(err_msg);
|
||||||
res["error"] = { "message": err_msg }
|
res["error"] = { "message": err_msg }
|
||||||
|
Reference in New Issue
Block a user