Skip to content

Commit 9f015f4

Browse files
committed
wow Bleak now seems pretty buggy to me (compared to 2 yrs ago)
1 parent dc50a60 commit 9f015f4

1 file changed

Lines changed: 22 additions & 13 deletions

File tree

meshtastic/ble_interface.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ class BLEInterface(MeshInterface):
2626

2727
class BLEError(Exception):
2828
"""An exception class for BLE errors."""
29-
30-
def __init__(self, message):
31-
self.message = message
32-
super().__init__(self.message)
29+
pass
3330

3431
class BLEState: # pylint: disable=C0115
3532
THREADS = False
@@ -118,15 +115,21 @@ def scan() -> list[BLEDevice]:
118115

119116
def find_device(self, address: Optional[str]) -> BLEDevice:
120117
"""Find a device by address."""
121-
addressed_devices = BLEInterface.scan()
122118

123-
if address:
124-
addressed_devices = list(
125-
filter(
126-
lambda x: address == x.name or address == x.address,
127-
addressed_devices,
119+
# Bleak scan is buggy (only on linux?) Try a few times
120+
for _ in range(5):
121+
addressed_devices = BLEInterface.scan()
122+
123+
if address:
124+
addressed_devices = list(
125+
filter(
126+
lambda x: address == x.name or address == x.address,
127+
addressed_devices,
128+
)
128129
)
129-
)
130+
# We finally found something?
131+
if len(addressed_devices) > 0:
132+
break
130133

131134
if len(addressed_devices) == 0:
132135
raise BLEInterface.BLEError(
@@ -158,7 +161,10 @@ def _receiveFromRadioImpl(self):
158161
self.should_read = False
159162
retries = 0
160163
while True:
161-
b = bytes(self.client.read_gatt_char(FROMRADIO_UUID))
164+
try:
165+
b = bytes(self.client.read_gatt_char(FROMRADIO_UUID))
166+
except Exception as e:
167+
raise BLEInterface.BLEError("Error reading BLE") from e
162168
if not b:
163169
if retries < 5:
164170
time.sleep(0.1)
@@ -175,7 +181,10 @@ def _sendToRadioImpl(self, toRadio):
175181
b = toRadio.SerializeToString()
176182
if b:
177183
logging.debug(f"TORADIO write: {b.hex()}")
178-
self.client.write_gatt_char(TORADIO_UUID, b, response=True)
184+
try:
185+
self.client.write_gatt_char(TORADIO_UUID, b, response=False)
186+
except Exception as e:
187+
raise BLEInterface.BLEError("Error writing BLE") from e
179188
# Allow to propagate and then make sure we read
180189
time.sleep(0.1)
181190
self.should_read = True

0 commit comments

Comments
 (0)