micropython-lib icon indicating copy to clipboard operation
micropython-lib copied to clipboard

aioble: Raise correct error if l2cap disconnects in middle of send.

Open andrewleech opened this issue 8 months ago • 0 comments

When using aioble/l2cap to send data from micropython to a cental like a phone, if the ble disconnects in the middle of a send process it can currently fail with the exception:

Traceback (most recent call last):
  File "aioble/l2cap.py", line 147, in send
TypeError: can't convert NoneType to int

This is because the send function is operating in a loop sending the data in chunks. At the top of the function self._assert_connected() is already called which will raise a L2CAPDisconnectedError if the connection has already been dropped (as detected by self._cid is None however the chunk send loop can run for some time, a disconnect in the middle of this results in self._cid = None being passed to ble.l2cap_send() resulting in the exception above.

This PR simply adds the correct connection check immediately before each loop of l2cap_send().

andrewleech avatar May 07 '25 11:05 andrewleech