Exit cleanly when no BT adapter found instead of crash-looping

Wrap BleakScanner context manager in try-except to detect adapter
initialization errors. If 'No Bluetooth adapters found' or similar, log
warning and set shutdown event to trigger graceful exit with code 0.
Prevents systemd restart loop on hardware without BT adapter (e.g. OPi).
This commit is contained in:
Cobra
2026-04-14 13:29:29 -04:00
parent 0116cb974a
commit e0086bce6f
+17 -8
View File
@@ -275,14 +275,23 @@ async def scan_loop():
await on_arrival(name, mac, rssi) await on_arrival(name, mac, rssi)
# Active scanner — explicitly required; bleak 0.20 BlueZ backend defaults to passive # 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:
try: async with BleakScanner(detection_callback=detection_callback, scanning_mode="active") as scanner:
while not shutdown_event.is_set(): try:
await asyncio.sleep(1) while not shutdown_event.is_set():
except asyncio.CancelledError: await asyncio.sleep(1)
logging.info("BLE scanner cancelled") except asyncio.CancelledError:
except Exception as e: logging.info("BLE scanner cancelled")
logging.error(f"BLE scanner exception: {e}") 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(): async def timeout_checker():