Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
306bfc6
BLE fixes; bleak to 1.1.1
jeremiah-k Sep 28, 2025
715d1dd
Fix BLE interface hang on exit by improving shutdown logic.
google-labs-jules[bot] Sep 28, 2025
4ef6302
feat: Add auto-reconnect to BLE interface
google-labs-jules[bot] Sep 28, 2025
cb53e4b
fix: Robustly handle BLE shutdown and add reconnect hook
google-labs-jules[bot] Sep 28, 2025
28a5880
fix: Address BLE interface race conditions and improve error handling
jeremiah-k Sep 28, 2025
342037c
fix: Resolve critical BLE interface issues and improve pub/sub consis…
jeremiah-k Sep 28, 2025
fb630f1
fix: Address race condition and dependency compatibility issues
jeremiah-k Sep 28, 2025
ee88b61
Replace magic number with RECEIVE_THREAD_JOIN_TIMEOUT constant
jeremiah-k Sep 28, 2025
3343670
fix: Improve exception handling in reconnect example to prevent resou…
jeremiah-k Sep 28, 2025
d2ea3cc
fix: Prevent tearing down live client for stale disconnect events
jeremiah-k Sep 28, 2025
ae4ace8
refactor: Simplify exception handling in reconnect example
jeremiah-k Sep 28, 2025
af6d03e
fix: Improve BLE auto-reconnect behavior and refactor test setup
jeremiah-k Sep 28, 2025
7beabd1
fix: Complete BLE interface refactoring with code quality improvements
jeremiah-k Sep 28, 2025
f4dbb47
fix: Address final code review feedback for BLE interface refactoring
jeremiah-k Sep 28, 2025
f693669
fix: Resolve critical auto-reconnect and disconnect notification issues
jeremiah-k Sep 29, 2025
ce706f8
feat: Improve BLE reconnection efficiency and example usability
jeremiah-k Sep 29, 2025
bfb5f1d
style: Fix pylint too-many-positional-arguments warning
jeremiah-k Sep 29, 2025
fba8148
refactor: Extract common BLE disconnection logic to improve maintaina…
jeremiah-k Sep 29, 2025
c58c187
style: Fix docstring and improve formatting across BLE interface files
jeremiah-k Sep 29, 2025
2674cc4
docs: Add comprehensive documentation for BLEInterface __init__ param…
jeremiah-k Sep 29, 2025
b369300
style: Implement comprehensive code review improvements
jeremiah-k Sep 29, 2025
26de6b5
docs: Add detailed comment explaining BLE write polling mechanism
jeremiah-k Sep 29, 2025
8746ee0
Revert "docs: Add detailed comment explaining BLE write polling mecha…
jeremiah-k Sep 29, 2025
53e463e
fix(ble): improve BLE disconnection handling
jeremiah-k Sep 29, 2025
e2230c7
fix(ble): address code review comments for BLE interface refactoring
jeremiah-k Sep 29, 2025
c5af8fc
fix(ble): optimize reconnection wait and shutdown handling
jeremiah-k Sep 29, 2025
24b09b7
fix(ble): improve BLE shutdown and pubsub handling
jeremiah-k Sep 29, 2025
659146c
fix(examples): correct variable scope in reconnect_example.py
jeremiah-k Sep 29, 2025
ac1368b
fix(ble): prevent BLEClient thread leak in read loop disconnect
jeremiah-k Sep 29, 2025
2dac3bb
refactor(ble): apply code quality improvements and fix duplication
jeremiah-k Sep 29, 2025
7b1bbd0
fix(ble): improve BLE shutdown and thread safety
jeremiah-k Sep 29, 2025
09fbf06
fix(ble): improve BLE shutdown and service discovery
jeremiah-k Sep 29, 2025
0d409ec
fix(ble): add exception handling to prevent silent receive thread death
jeremiah-k Sep 29, 2025
5c04ac3
fix(ble): address additional code review issues
jeremiah-k Sep 29, 2025
23bbc7c
fix(ble): resolve race condition in disconnect handling and improve t…
jeremiah-k Sep 29, 2025
cc187a5
Replace hardcoded timeout/delay values with named constants
jeremiah-k Sep 29, 2025
0b9f078
Fix BLE scanning logic for bleak 1.1.1
jeremiah-k Sep 29, 2025
3ad2e8b
fix(ble): address code review feedback for BLE interface refactoring
jeremiah-k Sep 29, 2025
e5e3baf
feat(ble): implement comprehensive exception handling improvements
jeremiah-k Sep 29, 2025
cd4c18b
fix(ble): address code review feedback for robustness and compatibility
jeremiah-k Sep 29, 2025
23e3505
fix(ble): complete code review feedback implementation
jeremiah-k Sep 29, 2025
13a7b91
Update docstring
jeremiah-k Sep 29, 2025
bb12978
fix(ble): address code review feedback for exception specificity and …
jeremiah-k Sep 29, 2025
35567e2
Fix BLE interface code review feedback
jeremiah-k Sep 29, 2025
deaf285
Move pubsub import to top of mesh_interface.py for PEP 8 compliance
jeremiah-k Sep 29, 2025
d86ea84
Add BLEInterface.BLEError to receive loop exception handler
jeremiah-k Sep 29, 2025
c3b419c
Combine duplicate RuntimeError and OSError exception handlers
jeremiah-k Sep 29, 2025
728e194
Improve exception handling consistency in BLE interface
jeremiah-k Sep 29, 2025
261571d
Fix unreachable TimeoutError catch in disconnect handling
jeremiah-k Sep 29, 2025
cf99268
Fix critical GATT services issue and improve error message consistency
jeremiah-k Sep 29, 2025
2986ceb
refactor: consolidate exception handling in BLE interface
jeremiah-k Sep 29, 2025
286b7a1
fix: move pubsub import to correct PEP 8 order
jeremiah-k Sep 29, 2025
1b8b2e1
refactor: improve BLE interface maintainability and robustness
jeremiah-k Sep 29, 2025
149a0c1
fix: resolve critical AttributeError and improve code clarity
jeremiah-k Sep 29, 2025
a7a6c0c
fix: resolve race condition in BLE disconnect notification handling
jeremiah-k Sep 30, 2025
a62d3ec
fix: prevent resource leak in BLE connection setup
jeremiah-k Sep 30, 2025
9843917
refactor: improve code clarity and performance in BLE interface
jeremiah-k Sep 30, 2025
21aa720
refactor: improve type hints and docstring formatting in BLE interface
jeremiah-k Sep 30, 2025
9616795
refactor: improve disconnect notification flag state management
jeremiah-k Sep 30, 2025
3e641ab
fix: correct test assertion to match actual log message
jeremiah-k Sep 30, 2025
c8f0dc0
fix: add timeout handling for BLE event thread shutdown
jeremiah-k Sep 30, 2025
12769a7
fix: improve exception handling robustness in BLE interface
jeremiah-k Sep 30, 2025
54b3062
Add test for BLE auto_reconnect functionality
jeremiah-k Sep 30, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions examples/reconnect_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"""
Example demonstrating a robust client-side reconnection loop for a
long-running application that uses the BLE interface.

The key is to instantiate the BLEInterface with `auto_reconnect=True` (the default).
This prevents the library from calling `close()` on the entire interface when a
disconnect occurs. Instead, it cleans up the underlying BLE client and notifies
listeners via the `onConnection` event with a `connected=False` payload.

The application can then listen for this event and attempt to create a new
BLEInterface instance to re-establish the connection, as shown in this example.
"""
import argparse
import logging
import threading
import time

from pubsub import pub

import meshtastic
import meshtastic.ble_interface

# Retry delay in seconds when connection fails
RETRY_DELAY_SECONDS = 5

logger = logging.getLogger(__name__)

# A thread-safe flag to signal disconnection
disconnected_event = threading.Event()


def on_connection_change(interface, connected):
"""Callback for connection changes."""
iface_label = getattr(interface, "address", repr(interface))
logger.info(
"Connection changed for %s: %s", iface_label, "Connected" if connected else "Disconnected"
)
if not connected:
# Signal the main loop that we've been disconnected
disconnected_event.set()


def main():
"""Main function."""
logging.basicConfig(level=logging.INFO)
parser = argparse.ArgumentParser(
description="Meshtastic BLE interface automatic reconnection example."
)
parser.add_argument("address", help="The BLE address of your Meshtastic device.")
args = parser.parse_args()
address = args.address

# Subscribe to the connection change event
pub.subscribe(on_connection_change, "meshtastic.connection.status")

while True:
iface = None
try:
disconnected_event.clear()
logger.info("Attempting to connect to %s...", address)
# Set auto_reconnect=True to prevent the interface from closing on disconnect.
# This allows us to handle the reconnection here.
iface = meshtastic.ble_interface.BLEInterface(
address,
noProto=True, # Set to False in a real application
auto_reconnect=True,
)

logger.info("Connection successful. Waiting for disconnection event...")
# Wait until the on_connection_change callback signals a disconnect
disconnected_event.wait()
logger.info("Disconnected normally.")

except KeyboardInterrupt:
logger.info("Exiting...")
break
except meshtastic.ble_interface.BLEInterface.BLEError:
logger.exception("Connection failed")
except Exception:
logger.exception("An unexpected error occurred")
finally:
if iface:
iface.close()
logger.info("Interface closed.")

logger.info("Retrying in %d seconds...", RETRY_DELAY_SECONDS)
time.sleep(RETRY_DELAY_SECONDS)


if __name__ == "__main__":
main()
Loading