| 1 | (usage)= |
| 2 | |
| 3 | # Usage |
| 4 | |
| 5 | Assuming that you've followed the {ref}`installations steps <installation>`, you're now ready to use this package. |
| 6 | |
| 7 | Start by importing it: |
| 8 | |
| 9 | ```python |
| 10 | import bluetooth_data_tools |
| 11 | ``` |
| 12 | |
| 13 | TODO: Document usage |
| 14 | |
| 15 | ## Parsed results are cached and shared |
| 16 | |
| 17 | `parse_advertisement_data`, `parse_advertisement_data_bytes` and |
| 18 | `parse_advertisement_data_tuple` are all memoized. Two callers parsing the same |
| 19 | payload receive the _same_ `service_uuids` list, `service_data` dict and |
| 20 | `manufacturer_data` dict — not copies. Advertisements that carry none of a given |
| 21 | AD type get a shared module-level empty container, so the aliasing also spans |
| 22 | unrelated payloads. |
| 23 | |
| 24 | **Treat every parsed result as read-only.** Mutating one corrupts the cache for |
| 25 | every subsequent caller in the process: |
| 26 | |
| 27 | ```python |
| 28 | uuids = parse_advertisement_data_bytes(flags_only_ad)[1] |
| 29 | uuids.append("dead") # don't |
| 30 | parse_advertisement_data_bytes(other_ad)[1] |
| 31 | # ['dead'] — an unrelated advertisement now reports a UUID it never sent |
| 32 | ``` |
| 33 | |
| 34 | Copy before you modify: |
| 35 | |
| 36 | ```python |
| 37 | uuids = list(parse_advertisement_data_bytes(ad)[1]) |
| 38 | ``` |