diff --git a/net_alerter/ble_alerter.py b/net_alerter/ble_alerter.py index 0e52a56..9378544 100755 --- a/net_alerter/ble_alerter.py +++ b/net_alerter/ble_alerter.py @@ -275,14 +275,23 @@ async def scan_loop(): await on_arrival(name, mac, rssi) # Active scanner — explicitly required; bleak 0.20 BlueZ backend defaults to passive - async with BleakScanner(detection_callback=detection_callback, scanning_mode="active") as scanner: - try: - while not shutdown_event.is_set(): - await asyncio.sleep(1) - except asyncio.CancelledError: - logging.info("BLE scanner cancelled") - except Exception as e: - logging.error(f"BLE scanner exception: {e}") + try: + async with BleakScanner(detection_callback=detection_callback, scanning_mode="active") as scanner: + try: + while not shutdown_event.is_set(): + await asyncio.sleep(1) + except asyncio.CancelledError: + logging.info("BLE scanner cancelled") + except Exception as e: + logging.error(f"BLE scanner exception: {e}") + except Exception as e: + # Catch no-adapter or other initialization errors + if "No Bluetooth adapters found" in str(e) or "No default Bluetooth adapter" in str(e): + logging.warning("No Bluetooth adapter found — BLE scanning disabled.") + shutdown_event.set() + else: + logging.error(f"Failed to initialize BLE scanner: {e}") + raise async def timeout_checker():