fixes to make Bleak (BLE) work better

This commit is contained in:
Kevin Hester
2024-06-23 13:52:32 -07:00
parent 62f16d34d4
commit 81266e756b
2 changed files with 8 additions and 12 deletions

View File

@@ -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

View File

@@ -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()