### Install BlueZ on Debian/Ubuntu Source: https://docs.rs/bluetooth-rust On Debian-based systems like Ubuntu, install the BlueZ package using apt. This is a prerequisite for using the bluetooth-rust library on Linux. ```bash sudo apt install bluez # Debian / Ubuntu ``` -------------------------------- ### Start Bluetooth Daemon on Linux Source: https://docs.rs/bluetooth-rust Ensure the Bluetooth daemon (bluetoothd) is running on your Linux system. This command starts the daemon if it is not already active, which is necessary for the bluetooth-rust library to function. ```bash sudo systemctl start bluetooth # start the daemon ``` -------------------------------- ### Handle Bluetooth Pairing Events Source: https://docs.rs/bluetooth-rust Sets up a Tokio task to listen for and handle Bluetooth pairing messages, such as displaying or confirming passkeys. Automatically confirms passkey entry in this example. ```rust use bluetooth_rust::{MessageToBluetoothHost, ResponseToPasskey}; use tokio::sync::mpsc; let (tx, mut rx) = mpsc::channel::(8); tokio::spawn(async move { while let Some(msg) = rx.recv().await { match msg { MessageToBluetoothHost::DisplayPasskey(passkey, reply_tx) => { println!("Pairing passkey: {:06}", passkey); // Automatically confirm — replace with real UI logic let _ = reply_tx.send(ResponseToPasskey::Yes).await; } MessageToBluetoothHost::ConfirmPasskey(passkey, reply_tx) => { println!("Confirm passkey: {:06}?", passkey); let _ = reply_tx.send(ResponseToPasskey::Yes).await; } MessageToBluetoothHost::CancelDisplayPasskey => { println!("Pairing canceled"); } } } }); ``` -------------------------------- ### Register RFCOMM Profile Source: https://docs.rs/bluetooth-rust Registers a custom RFCOMM profile with specified settings. This example registers a Serial Port Profile (SPP). ```rust use bluetooth_rust::{BluetoothRfcommProfileSettings, AsyncBluetoothAdapterTrait}; let settings = BluetoothRfcommProfileSettings { uuid: "00001101-0000-1000-8000-00805F9B34FB".to_string(), // SPP name: Some("My Serial Port".to_string()), service_uuid: None, channel: Some(1), psm: None, authenticate: Some(false), authorize: Some(false), auto_connect: Some(false), sdp_record: None, sdp_version: None, sdp_features: None, }; let profile = adapter .register_rfcomm_profile(settings) .await .expect("Failed to register RFCOMM profile"); ``` -------------------------------- ### Build Bluetooth Adapter (Linux/Windows) Source: https://docs.rs/bluetooth-rust Builds a Bluetooth adapter for Linux or Windows. Requires a sender channel to receive pairing events. ```rust use bluetooth_rust::{BluetoothAdapterBuilder, MessageToBluetoothHost}; use tokio::sync::mpsc; #[tokio::main] async fn main() { // Channel for receiving passkey / pairing messages from the stack let (tx, mut rx) = mpsc::channel::(8); let adapter = BluetoothAdapterBuilder::new() .with_sender(tx) // required so the library can forward pairing events .async_build() .await .expect("Failed to build Bluetooth adapter"); println!("Adapter built successfully"); } ``` -------------------------------- ### Build Bluetooth Adapter (Android) Source: https://docs.rs/bluetooth-rust Builds a Bluetooth adapter for Android. Requires an AndroidApp context. ```rust use bluetooth_rust::BluetoothAdapterBuilder; use winit::platform::android::activity::AndroidApp; fn start_bluetooth(app: AndroidApp) { let mut builder = BluetoothAdapterBuilder::new(); builder.with_android_app(app); let adapter = builder.build().expect("Failed to build Bluetooth adapter"); } ``` -------------------------------- ### Add bluetooth-rust to Cargo.toml Source: https://docs.rs/bluetooth-rust Add the bluetooth-rust crate and tokio to your project's Cargo.toml file to include it as a dependency. Ensure you are using a compatible version of bluetooth-rust. ```toml [dependencies] bluetooth-rust = "0.3" tokio = { version = "1", features = ["full"] } ``` -------------------------------- ### List Paired Bluetooth Devices Source: https://docs.rs/bluetooth-rust Retrieves and prints details of all paired Bluetooth devices. Assumes an adapter has already been obtained. ```rust use bluetooth_rust::AsyncBluetoothAdapterTrait; // `adapter` is a BluetoothAdapter obtained from BluetoothAdapterBuilder let devices = adapter.get_paired_devices(); for device in devices { println!("Device: {:?}", device.get_name()); println!("Address: {:?}", device.get_address()); println!("UUIDs: {:?}", device.get_uuids()); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.