Skip to content

Commit 342037c

Browse files
committed
fix: Resolve critical BLE interface issues and improve pub/sub consistency
- Fix disconnect callback to properly handle BleakClient and close BLEClient wrapper - Add meshtastic.connection.status messages with connected boolean to align with example - Improve reconnect_example.py exception handling to continue loop instead of exiting - Prevent thread leaks by properly closing BLE client wrapper in background thread - Fix AttributeError in disconnect callback by using correct client.address attribute Critical fixes that ensure the BLE reconnection feature works correctly and prevents resource leaks during disconnection events.
1 parent 28a5880 commit 342037c

3 files changed

Lines changed: 16 additions & 5 deletions

File tree

examples/reconnect_example.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ def main():
7474
print(f"An unexpected error occurred: {e}")
7575
if iface:
7676
iface.close()
77-
raise
77+
print("Retrying in 5 seconds...")
78+
time.sleep(5)
7879

7980
if __name__ == "__main__":
8081
main()

meshtastic/ble_interface.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,17 @@ def __repr__(self):
106106
rep += ")"
107107
return rep
108108

109-
def _on_ble_disconnect(self, client: "BLEClient") -> None:
109+
def _on_ble_disconnect(self, client) -> None:
110110
"""Disconnected callback from Bleak."""
111-
logger.debug(f"BLE client {client.bleak_client.address} disconnected.")
111+
address = getattr(client, "address", repr(client))
112+
logger.debug(f"BLE client {address} disconnected.")
112113
if self.auto_reconnect:
113-
# We only cleanup the client, but do not call close()
114-
# This allows the application to handle reconnection.
114+
previous_client = self.client
115115
self.client = None
116+
if previous_client:
117+
Thread(
118+
target=previous_client.close, name="BLEClientClose", daemon=True
119+
).start()
116120
self._disconnected()
117121
else:
118122
Thread(target=self.close, name="BLEClose", daemon=True).start()

meshtastic/mesh_interface.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,9 @@ def _disconnected(self):
11321132
publishingThread.queueWork(
11331133
lambda: pub.sendMessage("meshtastic.connection.lost", interface=self)
11341134
)
1135+
publishingThread.queueWork(
1136+
lambda: pub.sendMessage("meshtastic.connection.status", interface=self, connected=False)
1137+
)
11351138

11361139
def sendHeartbeat(self):
11371140
"""Sends a heartbeat to the radio. Can be used to verify the connection is healthy."""
@@ -1165,6 +1168,9 @@ def _connected(self):
11651168
"meshtastic.connection.established", interface=self
11661169
)
11671170
)
1171+
publishingThread.queueWork(
1172+
lambda: pub.sendMessage("meshtastic.connection.status", interface=self, connected=True)
1173+
)
11681174

11691175
def _startConfig(self):
11701176
"""Start device packets flowing"""

0 commit comments

Comments
 (0)