From 81266e756b2920d2f2f428fdc3d6ed03049de43c Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Sun, 23 Jun 2024 13:52:32 -0700 Subject: [PATCH] fixes to make Bleak (BLE) work better --- meshtastic/__main__.py | 4 ++-- meshtastic/ble_interface.py | 16 ++++++---------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index fb12a0c..e6c2362 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -1122,11 +1122,11 @@ def addConnectionArgs(parser: argparse.ArgumentParser) -> argparse.ArgumentParse action="store_true", ) - group.add_argument( + outer.add_argument( "--ble-dest", help="The BLE device address or name to connect to", default=None, - ) + ) return parser diff --git a/meshtastic/ble_interface.py b/meshtastic/ble_interface.py index 4b8ddeb..0624db6 100644 --- a/meshtastic/ble_interface.py +++ b/meshtastic/ble_interface.py @@ -51,9 +51,9 @@ class BLEInterface(MeshInterface): self.client = self.connect(address) self.state.BLE = True logging.debug("BLE connected") - # except BLEInterface.BLEError as e: - finally: + except BLEInterface.BLEError as e: self.close() + raise e logging.debug("Mesh init starting") MeshInterface.__init__(self, debugOut = debugOut, noProto = noProto, noNodes = noNodes) @@ -112,17 +112,13 @@ class BLEInterface(MeshInterface): .replace(":", "") \ .lower() - def connect(self, address): + def connect(self, address: Optional[str] = None): "Connect to a device by address" + + # Bleak docs recommend always doing a scan before connecting (even if we know addr) device = self.find_device(address) client = BLEClient(device.address) client.connect() - try: - client.pair() - except NotImplementedError: - # Some bluetooth backends do not require explicit pairing. - # See Bleak docs for details on this. - pass return client @@ -212,7 +208,7 @@ class BLEClient(): def __enter__(self): return self - + def __exit__(self, _type, _value, _traceback): self.close()