Skip to content

Commit c936f92

Browse files
Fix broken class matching on receive
Restructured match_prefixes to correctly match sequence number field Added insurance that sequence number comes first to do so Also caught the (macOS) case where adapter has no address (WIP) Fixes adafruit#15
1 parent 82899ad commit c936f92

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

adafruit_ble_broadcastnet.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,16 @@ def broadcast(measurement, *, broadcast_time=0.1, extended=False):
6161
if not hasattr(os, "environ") or (
6262
"GITHUB_ACTION" not in os.environ and "READTHEDOCS" not in os.environ
6363
):
64-
device_address = "{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}".format( # pylint: disable=invalid-name
65-
*reversed(
66-
list(
67-
_ble._adapter.address.address_bytes # pylint: disable=protected-access
64+
if _ble._adapter.address:
65+
device_address = "{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}".format( # pylint: disable=invalid-name
66+
*reversed(
67+
list(
68+
_ble._adapter.address.address_bytes # pylint: disable=protected-access
69+
)
6870
)
6971
)
70-
)
72+
else:
73+
device_address = "000000000000"
7174
"""Device address as a string."""
7275

7376
_MANUFACTURING_DATA_ADT = const(0xFF)
@@ -79,7 +82,8 @@ class AdafruitSensorMeasurement(Advertisement):
7982

8083
# This prefix matches all
8184
match_prefixes = (
82-
struct.pack("<BBH", 3, _MANUFACTURING_DATA_ADT, _ADAFRUIT_COMPANY_ID),
85+
# Matches the sequence number field header (length+ID)
86+
struct.pack("<BHBH", _MANUFACTURING_DATA_ADT, _ADAFRUIT_COMPANY_ID, 0x03, 0x0003),
8387
)
8488

8589
manufacturer_data = LazyObjectField(
@@ -174,6 +178,13 @@ def __str__(self):
174178
if value is not None:
175179
parts.append("{}={}".format(attr, str(value)))
176180
return "<{} {} >".format(self.__class__.__name__, " ".join(parts))
181+
182+
def __bytes__(self):
183+
"""The raw packet bytes."""
184+
# Must reorder the ManufacturerData contents so the sequence number field is always first.
185+
# Necessary to ensure that match_prefixes works right to reconstruct on the receiver.
186+
self.data_dict[255].data.move_to_end(3, last=False)
187+
return super().__bytes__()
177188

178189
def split(self, max_packet_size=31):
179190
"""Split the measurement into multiple measurements with the given max_packet_size. Yields
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""This example merely scans for broadcastnet packets as a quick check that something is sending them."""
2+
3+
from adafruit_ble.advertising.standard import ManufacturerDataField
4+
import adafruit_ble
5+
import adafruit_ble_broadcastnet
6+
7+
ble = adafruit_ble.BLERadio()
8+
9+
print("scanning")
10+
# By providing Advertisement as well we include everything, not just specific advertisements.
11+
for advert in ble.start_scan(
12+
adafruit_ble_broadcastnet.AdafruitSensorMeasurement, interval=0.5
13+
):
14+
print(advert)

0 commit comments

Comments
 (0)