fix(ble): handle notification setup failures

This commit is contained in:
Zhou Xiao
2026-05-09 19:44:10 +08:00
parent 64034cbcb9
commit c6f0d4f1fd
+17 -5
View File
@@ -10,6 +10,8 @@ from bleak import BleakClient
from bleak.backends.characteristic import BleakGATTCharacteristic
from bleak.backends.service import BleakGATTService
from bleak.backends.service import BleakGATTServiceCollection
from bleak.exc import BleakGATTProtocolError
from bleak.exc import BleakGATTProtocolErrorCode
from loguru import logger
from .constants import BLE_WRITE_WITH_RESPONSE_MAX_SIZE
@@ -90,7 +92,7 @@ class BLEUARTBridge:
self.reset()
return
except Exception as e:
logger.exception(e)
logger.error(e)
self.reset()
return
@@ -147,7 +149,7 @@ class BLEUARTBridge:
await self._cleanup_connection_locked()
raise
except Exception as e:
logger.exception(e)
logger.error(e)
await self._cleanup_connection_locked()
return False
@@ -166,7 +168,7 @@ class BLEUARTBridge:
await self._cleanup_connection_locked()
raise
except Exception as e:
logger.exception(e)
logger.error(e)
await self._cleanup_connection_locked()
return False
@@ -184,8 +186,18 @@ class BLEUARTBridge:
except asyncio.CancelledError:
await self._cleanup_connection_locked()
raise
except BleakGATTProtocolError as e:
if e.code != BleakGATTProtocolErrorCode.INSUFFICIENT_AUTHENTICATION:
logger.error(e)
await self._cleanup_connection_locked()
return False
# For insufficient authentication exception, try pairing
logger.warning('Please try to pair device in system Bluetooth settings first!')
await self._cleanup_connection_locked()
return False
except Exception as e:
logger.exception(e)
logger.error(e)
await self._cleanup_connection_locked()
return False
@@ -247,7 +259,7 @@ class BLEUARTBridge:
await self._cleanup_connection()
return False
except Exception as e:
logger.exception(e)
logger.error(e)
await self._cleanup_connection()
return False
return True