diff --git a/adafruit_ble_broadcastnet.py b/adafruit_ble_broadcastnet.py index 0738ba0..569e90f 100644 --- a/adafruit_ble_broadcastnet.py +++ b/adafruit_ble_broadcastnet.py @@ -46,8 +46,8 @@ def broadcast(measurement, *, broadcast_time=0.1, extended=False): """Broadcasts the given measurement for the given broadcast time. If extended is False and the - measurement would be too long, it will be split into multiple measurements for transmission. - """ + measurement would be too long, it will be split into multiple measurements for transmission. + """ global _sequence_number # pylint: disable=global-statement,invalid-name for submeasurement in measurement.split(252 if extended else 31): submeasurement.sequence_number = _sequence_number @@ -61,14 +61,17 @@ def broadcast(measurement, *, broadcast_time=0.1, extended=False): if not hasattr(os, "environ") or ( "GITHUB_ACTION" not in os.environ and "READTHEDOCS" not in os.environ ): - device_address = "{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}".format( # pylint: disable=invalid-name - *reversed( - list( - _ble._adapter.address.address_bytes # pylint: disable=protected-access + if _ble._adapter.address: # pylint: disable=protected-access + device_address = "{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}".format( # pylint: disable=invalid-name + *reversed( + list( + _ble._adapter.address.address_bytes # pylint: disable=protected-access + ) ) ) - ) - """Device address as a string.""" + else: + device_address = "000000000000" # pylint: disable=invalid-name + """Device address as a string.""" _MANUFACTURING_DATA_ADT = const(0xFF) _ADAFRUIT_COMPANY_ID = const(0x0822) @@ -79,7 +82,10 @@ class AdafruitSensorMeasurement(Advertisement): # This prefix matches all match_prefixes = ( - struct.pack("".format(self.__class__.__name__, " ".join(parts)) + def __bytes__(self): + """The raw packet bytes.""" + # Must reorder the ManufacturerData contents so the sequence number field is always first. + # Necessary to ensure that match_prefixes works right to reconstruct on the receiver. + self.data_dict[255].data.move_to_end(3, last=False) + return super().__bytes__() + def split(self, max_packet_size=31): """Split the measurement into multiple measurements with the given max_packet_size. Yields - each submeasurement.""" + each submeasurement.""" current_size = 8 # baseline for mfg data and sequence number if current_size + len(self.manufacturer_data) < max_packet_size: yield self diff --git a/examples/ble_broadcastnet_scan_test.py b/examples/ble_broadcastnet_scan_test.py new file mode 100644 index 0000000..9a43919 --- /dev/null +++ b/examples/ble_broadcastnet_scan_test.py @@ -0,0 +1,13 @@ +"""This example merely scans for broadcastnet packets to check that something is sending them.""" + +import adafruit_ble +import adafruit_ble_broadcastnet + +ble = adafruit_ble.BLERadio() + +print("scanning") +# By providing Advertisement as well we include everything, not just specific advertisements. +for advert in ble.start_scan( + adafruit_ble_broadcastnet.AdafruitSensorMeasurement, interval=0.5 +): + print(advert)