| 1 | # Bluetooth Data Tools |
| 2 | |
| 3 | <p align="center"> |
| 4 | <a href="https://github.com/bluetooth-devices/bluetooth-data-tools/actions/workflows/ci.yml?query=branch%3Amain"> |
| 5 | <img src="https://img.shields.io/github/actions/workflow/status/bluetooth-devices/bluetooth-data-tools/ci.yml?branch=main&label=CI&logo=github&style=flat-square" alt="CI Status" > |
| 6 | </a> |
| 7 | <a href="https://bluetooth-data-tools.readthedocs.io"> |
| 8 | <img src="https://img.shields.io/readthedocs/bluetooth-data-tools.svg?logo=read-the-docs&logoColor=fff&style=flat-square" alt="Documentation Status"> |
| 9 | </a> |
| 10 | <a href="https://codecov.io/gh/Bluetooth-Devices/bluetooth-data-tools"> |
| 11 | <img src="https://img.shields.io/codecov/c/github/Bluetooth-Devices/bluetooth-data-tools.svg?logo=codecov&logoColor=fff&style=flat-square" alt="Test coverage percentage"> |
| 12 | </a> |
| 13 | <a href="https://codspeed.io/Bluetooth-Devices/bluetooth-data-tools"><img src="https://img.shields.io/endpoint?url=https://codspeed.io/badge.json" alt="CodSpeed Badge"/></a> |
| 14 | </p> |
| 15 | <p align="center"> |
| 16 | <a href="https://python-poetry.org/"> |
| 17 | <img src="https://img.shields.io/badge/packaging-poetry-299bd7?style=flat-square&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAASCAYAAABrXO8xAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAJJSURBVHgBfZLPa1NBEMe/s7tNXoxW1KJQKaUHkXhQvHgW6UHQQ09CBS/6V3hKc/AP8CqCrUcpmop3Cx48eDB4yEECjVQrlZb80CRN8t6OM/teagVxYZi38+Yz853dJbzoMV3MM8cJUcLMSUKIE8AzQ2PieZzFxEJOHMOgMQQ+dUgSAckNXhapU/NMhDSWLs1B24A8sO1xrN4NECkcAC9ASkiIJc6k5TRiUDPhnyMMdhKc+Zx19l6SgyeW76BEONY9exVQMzKExGKwwPsCzza7KGSSWRWEQhyEaDXp6ZHEr416ygbiKYOd7TEWvvcQIeusHYMJGhTwF9y7sGnSwaWyFAiyoxzqW0PM/RjghPxF2pWReAowTEXnDh0xgcLs8l2YQmOrj3N7ByiqEoH0cARs4u78WgAVkoEDIDoOi3AkcLOHU60RIg5wC4ZuTC7FaHKQm8Hq1fQuSOBvX/sodmNJSB5geaF5CPIkUeecdMxieoRO5jz9bheL6/tXjrwCyX/UYBUcjCaWHljx1xiX6z9xEjkYAzbGVnB8pvLmyXm9ep+W8CmsSHQQY77Zx1zboxAV0w7ybMhQmfqdmmw3nEp1I0Z+FGO6M8LZdoyZnuzzBdjISicKRnpxzI9fPb+0oYXsNdyi+d3h9bm9MWYHFtPeIZfLwzmFDKy1ai3p+PDls1Llz4yyFpferxjnyjJDSEy9CaCx5m2cJPerq6Xm34eTrZt3PqxYO1XOwDYZrFlH1fWnpU38Y9HRze3lj0vOujZcXKuuXm3jP+s3KbZVra7y2EAAAAAASUVORK5CYII=" alt="Poetry"> |
| 18 | </a> |
| 19 | <a href="https://github.com/ambv/black"> |
| 20 | <img src="https://img.shields.io/badge/code%20style-black-000000.svg?style=flat-square" alt="black"> |
| 21 | </a> |
| 22 | <a href="https://github.com/pre-commit/pre-commit"> |
| 23 | <img src="https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white&style=flat-square" alt="pre-commit"> |
| 24 | </a> |
| 25 | </p> |
| 26 | <p align="center"> |
| 27 | <a href="https://pypi.org/project/bluetooth-data-tools/"> |
| 28 | <img src="https://img.shields.io/pypi/v/bluetooth-data-tools.svg?logo=python&logoColor=fff&style=flat-square" alt="PyPI Version"> |
| 29 | </a> |
| 30 | <img src="https://img.shields.io/pypi/pyversions/bluetooth-data-tools.svg?style=flat-square&logo=python&logoColor=fff" alt="Supported Python versions"> |
| 31 | <img src="https://img.shields.io/pypi/l/bluetooth-data-tools.svg?style=flat-square" alt="License"> |
| 32 | </p> |
| 33 | |
| 34 | Tools for converting bluetooth data and packets |
| 35 | |
| 36 | ## Installation |
| 37 | |
| 38 | Install this via pip (or your favourite package manager): |
| 39 | |
| 40 | `pip install bluetooth-data-tools` |
| 41 | |
| 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 | |
| 132 | ## Contributors ✨ |
| 133 | |
| 134 | Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)): |
| 135 | |
| 136 | <!-- prettier-ignore-start --> |
| 137 | <!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --> |
| 138 | <!-- markdownlint-disable --> |
| 139 | <!-- markdownlint-enable --> |
| 140 | <!-- ALL-CONTRIBUTORS-LIST:END --> |
| 141 | <!-- prettier-ignore-end --> |
| 142 | |
| 143 | This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! |
| 144 | |
| 145 | ## Credits |
| 146 | |
| 147 | This package was created with |
| 148 | [Cookiecutter](https://github.com/audreyr/cookiecutter) and the |
| 149 | [browniebroke/cookiecutter-pypackage](https://github.com/browniebroke/cookiecutter-pypackage) |
| 150 | project template. |