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:
@@ -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():
|
||||
|
||||
Reference in New Issue
Block a user