bluetooth-rust
A cross-platform Bluetooth communication library for Rust. bluetooth-rust provides a unified API for discovering adapters, scanning for devices, managing pairing, and establishing RFCOMM and L2CAP connections across Linux, Windows, and Android.
Platform Support
| Platform | Backend | Async API | Sync API |
|---|---|---|---|
| Linux | BlueZ (bluer) |
✅ | ❌ |
| Windows | Windows API | ✅ | ✅ |
| Android | JNI (Android SDK) | ❌ | ✅ |
Features
- Adapter discovery — enumerate Bluetooth adapters on the host system
- Device discovery — scan for nearby Bluetooth devices
- Paired device listing — retrieve bonded/paired devices
- RFCOMM profiles — register and accept RFCOMM connections
- L2CAP profiles — register and accept L2CAP connections
- Passkey / pairing — display and confirm passkeys during the pairing process
- Discoverability — make the local adapter discoverable
- Well-known UUIDs — built-in enum of standard Bluetooth service UUIDs
Installation
Add the crate to your Cargo.toml:
[]
= "0.3"
= { = "1", = ["full"] }
Linux Prerequisites
On Linux the library relies on BlueZ via the bluer crate. Make sure BlueZ is installed and the bluetoothd daemon is running:
Windows Prerequisites
No additional runtime setup is required. The library uses the built-in Windows Bluetooth APIs via the windows crate.
Android Prerequisites
The library uses JNI to call into the Android Bluetooth SDK. You must integrate it with an Android activity that provides an AndroidApp handle (via winit with the android-native-activity feature) and grant the appropriate Bluetooth permissions in your AndroidManifest.xml.
Quick Start
Building an Adapter (Linux / Windows)
use ;
use mpsc;
async
Building an Adapter (Android)
use BluetoothAdapterBuilder;
use AndroidApp;
Listing Paired Devices
use AsyncBluetoothAdapterTrait;
// `adapter` is a BluetoothAdapter obtained from BluetoothAdapterBuilder
let devices = adapter.get_paired_devices;
for device in devices
Registering an RFCOMM Profile
use ;
let settings = BluetoothRfcommProfileSettings ;
let profile = adapter
.register_rfcomm_profile
.await
.expect;
Handling Passkey / Pairing Events
use ;
use mpsc;
let = ;
spawn;
Well-Known UUIDs
The BluetoothUuid enum covers a wide range of standard Bluetooth profiles:
| Variant | UUID | Profile |
|---|---|---|
SPP |
00001101-... |
Serial Port Profile |
A2dpSource |
0000110a-... |
A2DP Source |
A2dpSink |
0000110b-... |
A2DP Sink |
HspHs |
00001108-... |
Headset (HS) |
HspAg |
00001112-... |
Headset Audio Gateway |
HfpAg |
0000111f-... |
Hands-Free Audio Gateway |
HfpHs |
0000111e-... |
Hands-Free Headset |
ObexOpp |
00001105-... |
OBEX Object Push |
ObexFtp |
00001106-... |
OBEX File Transfer |
ObexMas |
00001132-... |
OBEX Message Access |
ObexMns |
00001133-... |
OBEX Message Notification |
ObexPse |
0000112f-... |
OBEX Phone Book Access |
ObexSync |
00001104-... |
OBEX Sync |
AvrcpRemote |
0000110e-... |
AVRCP Remote Control |
NetworkingNap |
00001116-... |
Bluetooth NAP |
AndroidAuto |
4de17a00-... |
Android Auto |
Base |
00000000-... |
Bluetooth Base |
Unknown(String) |
any | Unrecognized UUID |
UUIDs can be parsed from strings with str::parse::<BluetoothUuid>() and converted back with .as_str().
Key Types and Traits
| Type / Trait | Description |
|---|---|
BluetoothAdapterBuilder |
Builder for constructing a BluetoothAdapter |
BluetoothAdapter |
Platform-dispatched adapter (Linux/Windows/Android) |
AsyncBluetoothAdapterTrait |
Async adapter operations (Linux, Windows) |
SyncBluetoothAdapterTrait |
Sync adapter operations (Android) |
BluetoothDevice |
A discovered or paired remote device |
BluetoothDeviceTrait |
Query device name, address, UUIDs, sockets, and pair state |
BluetoothStream |
Active async or sync communication stream |
BluetoothRfcommProfileSettings |
Configuration for an RFCOMM profile |
BluetoothL2capProfileSettings |
Configuration for an L2CAP profile |
BluetoothUuid |
Well-known Bluetooth service UUIDs |
PairingStatus |
NotPaired / Pairing / Paired / Unknown |
MessageToBluetoothHost |
Pairing events forwarded to the application |
ResponseToPasskey |
Application's response to a pairing challenge |
License
Licensed under either of:
at your option.
Acknowledgements
Android portions adapted from android-bluetooth-serial-rs.