Skip to content

Commit 1ab67d8

Browse files
authored
docs: add usage examples to README (#200)
1 parent 7bf5546 commit 1ab67d8

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,96 @@ Install this via pip (or your favourite package manager):
3939

4040
`pip install bluetooth-data-tools`
4141

42+
## Usage
43+
44+
### Parsing BLE GAP Advertisement Data
45+
46+
Parse raw BLE advertisement bytes into structured data:
47+
48+
```python
49+
from bluetooth_data_tools import parse_advertisement_data_bytes
50+
51+
# Parse raw GAP advertisement bytes
52+
parsed = parse_advertisement_data_bytes(raw_bytes)
53+
local_name = parsed[0] # str | None
54+
service_uuids = parsed[1] # list[str]
55+
service_data = parsed[2] # dict[str, bytes]
56+
manufacturer_data = parsed[3] # dict[int, bytes]
57+
tx_power = parsed[4] # int | None
58+
```
59+
60+
Or use the object-oriented interface:
61+
62+
```python
63+
from bluetooth_data_tools import BLEGAPAdvertisement, parse_advertisement_data
64+
65+
adv = parse_advertisement_data([raw_bytes1, raw_bytes2])
66+
print(adv.local_name)
67+
print(adv.service_uuids)
68+
print(adv.service_data)
69+
print(adv.manufacturer_data)
70+
print(adv.tx_power)
71+
```
72+
73+
### Bluetooth Address Utilities
74+
75+
```python
76+
from bluetooth_data_tools import (
77+
int_to_bluetooth_address,
78+
mac_to_int,
79+
short_address,
80+
human_readable_name,
81+
)
82+
83+
# Convert integer to MAC address
84+
int_to_bluetooth_address(0x123456789ABC)
85+
# "12:34:56:78:9A:BC"
86+
87+
# Convert MAC address to integer
88+
mac_to_int("FF:FF:FF:FF:FF:FF")
89+
# 281474976710655
90+
91+
# Get short address (last 2 octets)
92+
short_address("AA:BB:CC:DD:EE:FF")
93+
# "EEFF"
94+
95+
# Format a human-readable device name
96+
human_readable_name("My Sensor", "", "AA:BB:CC:DD:EE:FF")
97+
# "My Sensor (EEFF)"
98+
```
99+
100+
### Distance Estimation
101+
102+
Estimate distance from TX power and RSSI:
103+
104+
```python
105+
from bluetooth_data_tools import calculate_distance_meters
106+
107+
distance = calculate_distance_meters(power=-59, rssi=-60)
108+
# ~1.135 meters
109+
```
110+
111+
### Monotonic Time
112+
113+
A fast monotonic clock optimized for Bluetooth event timing. On Linux, uses `CLOCK_MONOTONIC_COARSE` via Cython for lower overhead:
114+
115+
```python
116+
from bluetooth_data_tools import monotonic_time_coarse
117+
118+
now = monotonic_time_coarse()
119+
```
120+
121+
### Private Address Resolution (RPA)
122+
123+
Resolve Bluetooth Low Energy random private addresses using an Identity Resolving Key:
124+
125+
```python
126+
from bluetooth_data_tools import get_cipher_for_irk, resolve_private_address
127+
128+
cipher = get_cipher_for_irk(irk_bytes) # 16-byte Identity Resolving Key
129+
is_match = resolve_private_address(cipher, "40:01:02:0A:C4:A6")
130+
```
131+
42132
## Contributors ✨
43133

44134
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):

0 commit comments

Comments
 (0)