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,6 +275,7 @@ 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
|
||||||
|
try:
|
||||||
async with BleakScanner(detection_callback=detection_callback, scanning_mode="active") as scanner:
|
async with BleakScanner(detection_callback=detection_callback, scanning_mode="active") as scanner:
|
||||||
try:
|
try:
|
||||||
while not shutdown_event.is_set():
|
while not shutdown_event.is_set():
|
||||||
@@ -283,6 +284,14 @@ async def scan_loop():
|
|||||||
logging.info("BLE scanner cancelled")
|
logging.info("BLE scanner cancelled")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"BLE scanner exception: {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():
|
||||||
|
|||||||
Reference in New Issue
Block a user