← index #6796PR #471
Related · medium · value 2.207
QUERY · ISSUE

extmod/bluetooth: handling of disconnect in ble_simple_central.py example is misleading

openby tveopened 2021-01-25updated 2021-01-25

The handling of the disconnect event in ble_simple_central.py is misleading. The condition if conn_handle == self._conn_handle:
(line 108) makes it look as if a different value for conn_handle would mean that there's nothing to do, but this is not the case. In particular, a disconnect can happen during the connection process, at least with nimble on the esp32, in which case self._conn_handle is still None and conn_handle != self._conn_handle but calling self._reset() is necessary.

https://github.com/micropython/micropython/blob/203e1d2a65273db3f6ff063ba1124a89c3482c0f/examples/bluetooth/ble_simple_central.py#L108

CANDIDATE · PULL REQUEST

aioble/peripheral: Handle immediate disconnect.

mergedby andrewleechopened 2021-12-07updated 2024-09-06

If the client disconnects immediately after connection, the irq can be run before the initial connect handler has finished.

2 comments
puppet13th · 2022-09-01

+1

jimmo · 2022-09-02

Thanks @andrewleech

I think maybe this could just be simplified by having DeviceConnection::__init__ always unconditionally create the event. I'm not sure I can see any code paths now that don't end up initialising self._event anyway. i.e. construction of a DeviceConnection for either a central or a peripheral will always end up needing to initialise the event. So we'll always have to do this allocation regardless, might as well do it up-front.

Then we can get rid of all the various places that create-or-reuse the event...less code overall.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied