Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add macOS support for MAC address instead of device-specific IDs #266

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Changelog

### [Unreleased]

* ADD: RUUVI_MACOS_USE_MAC_ADDR environment variable to enable MAC address usage on macOS

## [3.0.0] - 2025-01-13
* ADD: Install Bleak automatically on all platforms
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,16 @@ if __name__ == "__main__":

Check [get_async_bleak](https://github.com/ttu/ruuvitag-sensor/blob/master/examples/get_async_bleak.py) and other async examples from [examples](https://github.com/ttu/ruuvitag-sensor/tree/master/examples) directory.

#### Use MAC Address Instead of Device-Specific ID on macOS

Bleak has a workaround for macOS to use MAC address instead of device-specific ID. This is experimental feature from Bleak and may break with macOS updates.

Set the `RUUVI_MACOS_USE_MAC_ADDR` environment variable to `true` to enable it.

```sh
$ export RUUVI_MACOS_USE_MAC_ADDR=true
```

#### Bleak dummy BLE data

Bleak-adapter has a development-time generator for dummy data, which can be useful during development if no sensors are available. Set the `RUUVI_BLE_ADAPTER` environment variable to `bleak_dev`.
Expand Down
15 changes: 14 additions & 1 deletion ruuvitag_sensor/adapters/bleak_ble.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@

MAC_REGEX = "[0-9a-f]{2}([:])[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$"

# NOTE: macOS uses device specific IDs instead of MAC addresses
# Bleak has a workaround for this, but it's not enabled by default
# https://github.com/hbldh/bleak/pull/1073
# This is behind a env variable as macOS update can break the workaround
MACOS_USE_MAC_ADDR = os.environ.get("RUUVI_MACOS_USE_MAC_ADDR", "false").strip().lower() == "true"
MACOS_SCANNER_ARGS = dict(use_bdaddr=True) if MACOS_USE_MAC_ADDR else {}
SCANNER_ARGS = MACOS_SCANNER_ARGS if sys.platform == "darwin" else {}


def _get_scanner(detection_callback: AdvertisementDataCallback, bt_device: str = ""):
# NOTE: On Linux - bleak.exc.BleakError: passive scanning mode requires bluez or_patterns
Expand All @@ -30,9 +38,14 @@ def _get_scanner(detection_callback: AdvertisementDataCallback, bt_device: str =
detection_callback=detection_callback,
scanning_mode=scanning_mode, # type: ignore[arg-type]
adapter=bt_device,
cb=SCANNER_ARGS, # type: ignore[arg-type]
)

return BleakScanner(detection_callback=detection_callback, scanning_mode=scanning_mode) # type: ignore[arg-type]
return BleakScanner(
detection_callback=detection_callback,
scanning_mode=scanning_mode, # type: ignore[arg-type]
cb=SCANNER_ARGS, # type: ignore[arg-type]
)


queue = asyncio.Queue[Tuple[str, str]]()
Expand Down
Loading