Skip to content

Commit

Permalink
fix: force use bluez with env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
ttu committed Dec 25, 2023
1 parent 147b6f1 commit ff6789f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Changelog

### [Unreleased]
* FIX: Force the use of Bluez-adapter with environment variable
* FIX: Add RSSI value to Bleson adapter payload

## [2.3.0] - 2023-09-23
Expand Down
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ RuuviTag Sensor Python Package
* [Sync-methods](#usage)
* [Observable streams](#usage)
* [Install guide](#BlueZ)
* __NOTE:__ The BlueZ-adapter implementation uses deprecated BlueZ tools that are no longer supported. Even though BlueZ is still the default adapter, it is recommended to use the Bleak-communication adapter with Linux. Bleak will be the default adapter for Linux in the next major release.
* __NOTE:__ The BlueZ-adapter implementation uses deprecated BlueZ tools that are no longer supported.
* Even though BlueZ is still the default adapter, it is recommended to use the Bleak-communication adapter with Linux. Bleak will be the default adapter for Linux in the next major release.
* Bleson-adapter supports sync-methods, but please be aware that it is not fully supported due to the alpha release status of the Bleson communication module. See [Bleson](#Bleson) for more information.
* Python 3.7+
* For Python 2.x or <3.7 support, check [installation instructions](#python-2x-and-36-and-below) for an older version

Expand Down Expand Up @@ -423,6 +425,18 @@ $ sudo apt-get install bluez bluez-hcidump

`ruuvitag-sensor` package uses internally _hciconfig_, _hcitool_ and _hcidump_. These tools are deprecated. In case tools are missing, an older version of BlueZ is required ([Issue](https://github.com/ttu/ruuvitag-sensor/issues/31))

If you wish to test the library on Windows or macOS, enable it with `RUUVI_BLE_ADAPTER` environment variable.

```sh
$ export RUUVI_BLE_ADAPTER="bluez"
```

And install ptyprocess.

```sh
python -m pip install ptyprocess
```

#### BlueZ limitations

`ruuvitag-sensor` package uses BlueZ to listen to broadcasted BL information (uses _hciconf_, _hcitool_, _hcidump_). Implementation does not handle well all unexpected errors or changes, e.g. when the adapter is busy, rebooted or powered down.
Expand Down Expand Up @@ -479,12 +493,12 @@ Bleak-adapter has a development-time generator for dummy data, which can be usef

Current state and known bugs in [issue #78](https://github.com/ttu/ruuvitag-sensor/issues/78).

Bleson works with Linux, macOS and partially with Windows.
[Bleson](https://github.com/TheCellule/python-bleson) works with Linux, macOS and partially with Windows.

Bleson is not installed automatically with `ruuvitag-sensor` package. Install it manually from GitHub.

```sh
$ pip install git+https://github.com/TheCellule/python-bleson
$ python -m pip install git+https://github.com/TheCellule/python-bleson
```

Add environment variable `RUUVI_BLE_ADAPTER` with value `bleson`. E.g.
Expand Down
33 changes: 24 additions & 9 deletions ruuvitag_sensor/adapters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,39 @@


def get_ble_adapter():
if "bleak" in os.environ.get("RUUVI_BLE_ADAPTER", "").lower():
from ruuvitag_sensor.adapters.bleak_ble import BleCommunicationBleak
forced_ble_adapter = os.environ.get("RUUVI_BLE_ADAPTER", "").lower()
use_ruuvi_nix_from_file = "RUUVI_NIX_FROMFILE" in os.environ
is_ci_env = "CI" in os.environ

return BleCommunicationBleak()
if "bleson" in os.environ.get("RUUVI_BLE_ADAPTER", "").lower():
from ruuvitag_sensor.adapters.bleson import BleCommunicationBleson
if forced_ble_adapter:
if "bleak" in forced_ble_adapter:
from ruuvitag_sensor.adapters.bleak_ble import BleCommunicationBleak

return BleCommunicationBleak()
if "bleson" in forced_ble_adapter:
from ruuvitag_sensor.adapters.bleson import BleCommunicationBleson

return BleCommunicationBleson()
if "bluez" in forced_ble_adapter:
from ruuvitag_sensor.adapters.nix_hci import BleCommunicationNix

return BleCommunicationBleson()
if "RUUVI_NIX_FROMFILE" in os.environ:
return BleCommunicationNix()

raise RuntimeError(f"Unknown BLE adapter: {forced_ble_adapter}")

if use_ruuvi_nix_from_file:
# Emulate BleCommunicationNix by reading hcidump data from a file
from ruuvitag_sensor.adapters.nix_hci_file import BleCommunicationNixFile

return BleCommunicationNixFile()
if "CI" in os.environ:
# Use BleCommunicationDummy for CI as it can't use bluez

if is_ci_env:
# Use BleCommunicationDummy for CI as it can't use BlueZ
from ruuvitag_sensor.adapters.dummy import BleCommunicationDummy

return BleCommunicationDummy()

# Use default adapter for platform
if sys.platform.startswith("win") or sys.platform.startswith("darwin"):
from ruuvitag_sensor.adapters.bleak_ble import BleCommunicationBleak

Expand Down

0 comments on commit ff6789f

Please sign in to comment.