Skip to content

Commit 81266e7

Browse files
committed
fixes to make Bleak (BLE) work better
1 parent 62f16d3 commit 81266e7

2 files changed

Lines changed: 8 additions & 12 deletions

File tree

meshtastic/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,11 +1122,11 @@ def addConnectionArgs(parser: argparse.ArgumentParser) -> argparse.ArgumentParse
11221122
action="store_true",
11231123
)
11241124

1125-
group.add_argument(
1125+
outer.add_argument(
11261126
"--ble-dest",
11271127
help="The BLE device address or name to connect to",
11281128
default=None,
1129-
)
1129+
)
11301130

11311131
return parser
11321132

meshtastic/ble_interface.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ def __init__(self, address: Optional[str], noProto: bool = False, debugOut = Non
5151
self.client = self.connect(address)
5252
self.state.BLE = True
5353
logging.debug("BLE connected")
54-
# except BLEInterface.BLEError as e:
55-
finally:
54+
except BLEInterface.BLEError as e:
5655
self.close()
56+
raise e
5757

5858
logging.debug("Mesh init starting")
5959
MeshInterface.__init__(self, debugOut = debugOut, noProto = noProto, noNodes = noNodes)
@@ -112,17 +112,13 @@ def _sanitize_address(address): # pylint: disable=E0213
112112
.replace(":", "") \
113113
.lower()
114114

115-
def connect(self, address):
115+
def connect(self, address: Optional[str] = None):
116116
"Connect to a device by address"
117+
118+
# Bleak docs recommend always doing a scan before connecting (even if we know addr)
117119
device = self.find_device(address)
118120
client = BLEClient(device.address)
119121
client.connect()
120-
try:
121-
client.pair()
122-
except NotImplementedError:
123-
# Some bluetooth backends do not require explicit pairing.
124-
# See Bleak docs for details on this.
125-
pass
126122
return client
127123

128124

@@ -212,7 +208,7 @@ def close(self): # pylint: disable=C0116
212208

213209
def __enter__(self):
214210
return self
215-
211+
216212
def __exit__(self, _type, _value, _traceback):
217213
self.close()
218214

0 commit comments

Comments
 (0)