pc-ble-driver-py
pc-ble-driver-py copied to clipboard
Unhandled KeyError Exception
In ble_adapter.py, there are several spots where the dict values are accessed without checking if they exist, that causes KeyErrors.
For e.g. in ble_adapter.py : 272:
@NordicSemiErrorCheck(expected=BLEGattStatusCode.success)
def service_discovery(self, conn_handle, uuid=None):
vendor_services = []
self.driver.ble_gattc_prim_srvc_disc(conn_handle, uuid, 0x0001)
while True:
response = self.evt_sync[conn_handle].wait( # Here sometimes, the conn_handle could not be in the evt_sync dict
evt=BLEEvtID.gattc_evt_prim_srvc_disc_rsp
)
if response["status"] == BLEGattStatusCode.success: # Here the response can be None, or it may not contain "status"
for s in response["services"]:
if s.uuid.value == BLEUUID.Standard.unknown:
I've found that every now and then the BLE service discovery will fail because of a KeyError here or sometimes a None response is returned. Adding a Key check and None check here seems to fix most of the disconnections during discovery.
I'll go ahead and submit PR with my changes. Feel free to iterate.
Thank you