@@ -106,13 +106,16 @@ STATIC uint32_t queue_next_write(bleio_packet_buffer_obj_t *self) {
106106
107107STATIC bool packet_buffer_on_ble_client_evt (ble_evt_t * ble_evt , void * param ) {
108108 const uint16_t evt_id = ble_evt -> header .evt_id ;
109+ bleio_packet_buffer_obj_t * self = (bleio_packet_buffer_obj_t * )param ;
110+ if (evt_id == BLE_GAP_EVT_DISCONNECTED && self -> conn_handle == ble_evt -> evt .gap_evt .conn_handle ) {
111+ self -> conn_handle = BLE_CONN_HANDLE_INVALID ;
112+ }
109113 // Check if this is a GATTC event so we can make sure the conn_handle is valid.
110114 if (evt_id < BLE_GATTC_EVT_BASE || evt_id > BLE_GATTC_EVT_LAST ) {
111115 return false;
112116 }
113117
114118 uint16_t conn_handle = ble_evt -> evt .gattc_evt .conn_handle ;
115- bleio_packet_buffer_obj_t * self = (bleio_packet_buffer_obj_t * )param ;
116119 if (conn_handle != self -> conn_handle ) {
117120 return false;
118121 }
@@ -298,11 +301,14 @@ mp_int_t common_hal_bleio_packet_buffer_write(bleio_packet_buffer_obj_t *self, u
298301 if (len + self -> pending_size > outgoing_packet_length ) {
299302 // No room to append len bytes to packet. Wait until we get a free buffer,
300303 // and keep checking that we haven't been disconnected.
301- while (self -> pending_size != 0 && self -> conn_handle != BLE_CONN_HANDLE_INVALID ) {
304+ while (self -> pending_size != 0 &&
305+ self -> conn_handle != BLE_CONN_HANDLE_INVALID &&
306+ !mp_hal_is_interrupted ()) {
302307 RUN_BACKGROUND_TASKS ;
303308 }
304309 }
305- if (self -> conn_handle == BLE_CONN_HANDLE_INVALID ) {
310+ if (self -> conn_handle == BLE_CONN_HANDLE_INVALID ||
311+ mp_hal_is_interrupted ()) {
306312 return -1 ;
307313 }
308314
0 commit comments