Skip to content
Merged
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
3 changes: 2 additions & 1 deletion elements/src/proto/cedar.proto
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,7 @@ message EmptyMessage {}

message GetBluetoothNameResponse {
string name = 1;
optional string address = 2;
}

message StartBondingResponse {
Expand Down Expand Up @@ -968,4 +969,4 @@ service Cedar {
rpc StartBonding(EmptyMessage) returns (StartBondingResponse);
rpc GetBondedDevices(EmptyMessage) returns (GetBondedDevicesResponse);
rpc RemoveBond(RemoveBondRequest) returns (EmptyMessage);
}
}
236 changes: 119 additions & 117 deletions server/src/bonding_helper.rs
Original file line number Diff line number Diff line change
@@ -1,117 +1,119 @@
// Copyright (c) 2025 Omair Kamil
// See LICENSE file in root directory for license terms.

use std::{error::Error, str::FromStr, time::Duration};

use bluer::{
agent::{Agent, ReqResult, RequestConfirmation},
Address, Session,
};
use log::info;
use tokio::{sync::mpsc, time::timeout};

pub struct BluetoothDevice {
pub name: String,
pub address: String,
}

pub async fn get_adapter_alias() -> Result<String, Box<dyn Error + 'static>> {
let session = Session::new().await?;
let adapter = session.default_adapter().await?;
let alias = adapter.alias().await?;
info!("Current device alias: {}", alias);
Ok(alias)
}

pub async fn start_bonding(
) -> Result<Option<(String, u32)>, Box<dyn Error + 'static>> {
let session = Session::new().await?;
let adapter = session.default_adapter().await?;
adapter.set_powered(true).await?;

// Use a channel to pass the address back to the main thread.
let (tx, mut rx) = mpsc::channel(1);

// Clone sender for the agent closure to capture
let tx_req = tx.clone();

// Start pairing, accepting any requests.
let agent = Agent {
request_default: true,
request_confirmation: Some(Box::new(move |req| {
let tx = tx_req.clone();
Box::pin(request_confirmation(req, tx))
})),
..Default::default()
};

// Keep the handle in scope to keep the agent active
let _handle = session.register_agent(agent).await?;

adapter.set_discoverable(true).await?;
adapter.set_discoverable_timeout(55).await?;
adapter.set_pairable(true).await?;

info!("Accepting pairings - waiting up to 55 seconds...");

// Wait for the address or timeout
let result = timeout(Duration::from_secs(55), rx.recv()).await;

let paired_info = match result {
Ok(Some((address, passkey))) => {
let device = adapter.device(address)?;
let name = device.alias().await?;
info!("Paired with {}", name);
Some((name, passkey))
}
_ => {
info!("Timeout reached");
None
}
};

adapter.set_pairable(false).await?;
adapter.set_discoverable(false).await?;
Ok(paired_info)
}

async fn request_confirmation(
req: RequestConfirmation,
tx: mpsc::Sender<(Address, u32)>,
) -> ReqResult<()> {
info!("Confirming request from {} with key {}", req.device, req.passkey);
let _ = tx.send((req.device, req.passkey)).await;
Ok(())
}

pub async fn remove_bond(
address: String,
) -> Result<(), Box<dyn Error + 'static>> {
let session = Session::new().await?;
let adapter = session.default_adapter().await?;
adapter.set_powered(true).await?;

let device = Address::from_str(&address)?;
adapter.remove_device(device).await?;
info!("Removed bond: {}", address);
Ok(())
}

pub async fn get_bonded_devices(
) -> Result<Vec<BluetoothDevice>, Box<dyn Error + 'static>> {
let session = Session::new().await?;
let adapter = session.default_adapter().await?;
adapter.set_powered(true).await?;

let mut result: Vec<BluetoothDevice> = Vec::new();
let devices = adapter.device_addresses().await?;
for addr in devices {
info!("Found bonded device: {}", addr);
let device = adapter.device(addr)?;
result.push(BluetoothDevice {
name: device.alias().await?,
address: addr.to_string(),
});
}
Ok(result)
}
// Copyright (c) 2025 Omair Kamil
// See LICENSE file in root directory for license terms.

use std::{error::Error, str::FromStr, time::Duration};

use bluer::{
agent::{Agent, ReqResult, RequestConfirmation},
Address, Session,
};
use log::info;
use tokio::{sync::mpsc, time::timeout};

pub struct BluetoothDevice {
pub name: String,
pub address: String,
}

pub async fn get_adapter_info(
) -> Result<(String, String), Box<dyn Error + 'static>> {
let session = Session::new().await?;
let adapter = session.default_adapter().await?;
let alias = adapter.alias().await?;
let address = adapter.address().await?;
info!("Current device alias: {}", alias);
Ok((alias, address.to_string()))
}

pub async fn start_bonding(
) -> Result<Option<(String, u32)>, Box<dyn Error + 'static>> {
let session = Session::new().await?;
let adapter = session.default_adapter().await?;
adapter.set_powered(true).await?;

// Use a channel to pass the address back to the main thread.
let (tx, mut rx) = mpsc::channel(1);

// Clone sender for the agent closure to capture
let tx_req = tx.clone();

// Start pairing, accepting any requests.
let agent = Agent {
request_default: true,
request_confirmation: Some(Box::new(move |req| {
let tx = tx_req.clone();
Box::pin(request_confirmation(req, tx))
})),
..Default::default()
};

// Keep the handle in scope to keep the agent active
let _handle = session.register_agent(agent).await?;

adapter.set_discoverable(true).await?;
adapter.set_discoverable_timeout(55).await?;
adapter.set_pairable(true).await?;

info!("Accepting pairings - waiting up to 55 seconds...");

// Wait for the address or timeout
let result = timeout(Duration::from_secs(55), rx.recv()).await;

let paired_info = match result {
Ok(Some((address, passkey))) => {
let device = adapter.device(address)?;
let name = device.alias().await?;
info!("Paired with {}", name);
Some((name, passkey))
}
_ => {
info!("Timeout reached");
None
}
};

adapter.set_pairable(false).await?;
adapter.set_discoverable(false).await?;
Ok(paired_info)
}

async fn request_confirmation(
req: RequestConfirmation,
tx: mpsc::Sender<(Address, u32)>,
) -> ReqResult<()> {
info!("Confirming request from {} with key {}", req.device, req.passkey);
let _ = tx.send((req.device, req.passkey)).await;
Ok(())
}

pub async fn remove_bond(
address: String,
) -> Result<(), Box<dyn Error + 'static>> {
let session = Session::new().await?;
let adapter = session.default_adapter().await?;
adapter.set_powered(true).await?;

let device = Address::from_str(&address)?;
adapter.remove_device(device).await?;
info!("Removed bond: {}", address);
Ok(())
}

pub async fn get_bonded_devices(
) -> Result<Vec<BluetoothDevice>, Box<dyn Error + 'static>> {
let session = Session::new().await?;
let adapter = session.default_adapter().await?;
adapter.set_powered(true).await?;

let mut result: Vec<BluetoothDevice> = Vec::new();
let devices = adapter.device_addresses().await?;
for addr in devices {
info!("Found bonded device: {}", addr);
let device = adapter.device(addr)?;
result.push(BluetoothDevice {
name: device.alias().await?,
address: addr.to_string(),
});
}
Ok(result)
}
Loading