diff options
Diffstat (limited to 'examples/bluetooth/ble_advertising.py')
-rw-r--r-- | examples/bluetooth/ble_advertising.py | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/examples/bluetooth/ble_advertising.py b/examples/bluetooth/ble_advertising.py index 3a06beb74..3fb1281f6 100644 --- a/examples/bluetooth/ble_advertising.py +++ b/examples/bluetooth/ble_advertising.py @@ -26,9 +26,12 @@ def advertising_payload(limited_disc=False, br_edr=False, name=None, services=No def _append(adv_type, value): nonlocal payload - payload += struct.pack('BB', len(value) + 1, adv_type) + value + payload += struct.pack("BB", len(value) + 1, adv_type) + value - _append(_ADV_TYPE_FLAGS, struct.pack('B', (0x01 if limited_disc else 0x02) + (0x00 if br_edr else 0x04))) + _append( + _ADV_TYPE_FLAGS, + struct.pack("B", (0x01 if limited_disc else 0x02) + (0x00 if br_edr else 0x04)), + ) if name: _append(_ADV_TYPE_NAME, name) @@ -44,7 +47,7 @@ def advertising_payload(limited_disc=False, br_edr=False, name=None, services=No _append(_ADV_TYPE_UUID128_COMPLETE, b) # See org.bluetooth.characteristic.gap.appearance.xml - _append(_ADV_TYPE_APPEARANCE, struct.pack('<h', appearance)) + _append(_ADV_TYPE_APPEARANCE, struct.pack("<h", appearance)) return payload @@ -54,32 +57,36 @@ def decode_field(payload, adv_type): result = [] while i + 1 < len(payload): if payload[i + 1] == adv_type: - result.append(payload[i + 2:i + payload[i] + 1]) + result.append(payload[i + 2 : i + payload[i] + 1]) i += 1 + payload[i] return result def decode_name(payload): n = decode_field(payload, _ADV_TYPE_NAME) - return str(n[0], 'utf-8') if n else '' + return str(n[0], "utf-8") if n else "" def decode_services(payload): services = [] for u in decode_field(payload, _ADV_TYPE_UUID16_COMPLETE): - services.append(bluetooth.UUID(struct.unpack('<h', u)[0])) + services.append(bluetooth.UUID(struct.unpack("<h", u)[0])) for u in decode_field(payload, _ADV_TYPE_UUID32_COMPLETE): - services.append(bluetooth.UUID(struct.unpack('<d', u)[0])) + services.append(bluetooth.UUID(struct.unpack("<d", u)[0])) for u in decode_field(payload, _ADV_TYPE_UUID128_COMPLETE): services.append(bluetooth.UUID(u)) return services def demo(): - payload = advertising_payload(name='micropython', services=[bluetooth.UUID(0x181A), bluetooth.UUID('6E400001-B5A3-F393-E0A9-E50E24DCCA9E')]) + payload = advertising_payload( + name="micropython", + services=[bluetooth.UUID(0x181A), bluetooth.UUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")], + ) print(payload) print(decode_name(payload)) print(decode_services(payload)) -if __name__ == '__main__': + +if __name__ == "__main__": demo() |