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 0a8d6a2019
commit 6a5d1f0670
+17 -8
View File
@@ -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():