bluez / bluer

BlueR — Official BlueZ Bindings for Rust
https://crates.io/crates/bluer
Other
310 stars 45 forks source link
bluetooth bluetooth-low-energy bluetooth-mesh bluez gatt-client gatt-services l2cap rfcomm

BlueR — Official BlueZ Bindings for Rust

crates.io page docs.rs page BSD-2-Clause license

This library provides the official Rust interface to the Linux Bluetooth protocol stack (BlueZ). Both publishing local and consuming remote GATT services using idiomatic Rust code is supported. L2CAP and RFCOMM sockets are presented using an API similar to Tokio networking.

The following functionality is provided:

Currently, some classic Bluetooth (BR/EDR) functionality is missing. However, pull requests and contributions are welcome!

Usage

To use BlueR as a library, run the following command in your project directory:

cargo add -F full bluer

This will add the latest version of BlueR with all features enabled as a dependency to your Cargo.toml.

Crate features

The following crate features are available.

To enable all crate features specify the full crate feature.

Requirements

The minimum support Rust version (MSRV) is 1.75.

This library has been tested with BlueZ 5.60. Older versions might work, but be aware that many bugs related to GATT handling exist. Refer to the official changelog for details.

If any bluetoothd feature is used the Bluetooth daemon must be running and configured for access over D-Bus. On most distributions this should work out of the box.

For mesh feature Bluetooth mesh daemon must be running and configured for access over D-Bus.

Configuration

The following options in /etc/bluetooth/main.conf are helpful when working with GATT services.

[GATT]
Cache = no
Channels = 1

This disables the GATT cache to avoid stale data during device discovery.

By only allowing one channel the extended attribute protocol (EATT) is disabled. If EATT is enabled, all GATT commands and notifications are sent over multiple L2CAP channels and can be reordered arbitrarily by lower layers of the protocol stack. This makes sequential data transmission over GATT characteristics more difficult.

Building

When cloning this repository make sure to use the following command. Otherwise the build will fail with file not found errors.

git clone --recursive https://github.com/bluez/bluer.git

D-Bus development headers are required for building.

Troubleshooting

The library returns detailed errors received from BlueZ.

Set the Rust log level to trace to see all D-Bus communications with BlueZ.

In some cases checking the Bluetooth system log might provide further insights. On Debian-based systems it can be displayed by executing journalctl -u bluetooth. Check the bluetoothd man page for increasing the log level.

Sometimes deleting the system Bluetooth cache at /var/lib/bluetooth and restarting bluetoothd fixes persistent issues with device connectivity.

Examples

Refer to the API documentation and examples folder for examples.

The following example applications are provided.

Use cargo run --all-features --example <name> to run a particular example application.

Tools

See the BlueR tools crate for tools that build on this library.

History

This project started as a fork of blurz but has since then become a full rewrite. It was published under the name blez before it was designated the official Rust interface to BlueZ and renamed to BlueR. Documentation has been mostly copied from the BlueZ API specification, but also adapted where it makes sense.