#![allow(unused_imports)]
use futures_executor::block_on;
use std::sync::atomic::{AtomicU8, Ordering};
use std::sync::Arc;
use std::time::Duration;
#[cfg(peripheral_role)]
use webbluetooth::peripheral::{
Advertising, AttError, Characteristic, Descriptor, Peripheral, Request, Service,
};
use webbluetooth::prelude::*;
use webbluetooth::uuid::{characteristics, services};
use webbluetooth::Result;
#[cfg(peripheral_role)]
static BATTERY: AtomicU8 = AtomicU8::new(87);
#[cfg(not(peripheral_role))]
fn main() {
eprintln!(
"The peripheral role is unavailable on this platform: CoreBluetooth's \
CBPeripheralManager initialisers are API_UNAVAILABLE on watchOS, tvOS \
and visionOS."
);
}
#[cfg(peripheral_role)]
fn main() -> Result<()> {
block_on(run())
}
#[cfg(peripheral_role)]
async fn run() -> Result<()> {
let (peripheral, mut requests) = Peripheral::new();
if let Err(why) = peripheral.availability().await {
eprintln!("Bluetooth unavailable: {why} — try the `doctor` example.");
std::process::exit(1);
}
let level = Characteristic::new(characteristics::BATTERY_LEVEL)?
.read()
.notify()
.descriptor(Descriptor::user_description("Battery level, percent"));
let control = Characteristic::new("6e400002-b5a3-f393-e0a9-e50e24dcca9e")?
.write()
.write_without_response();
let published = peripheral
.publish(
Service::new(services::BATTERY_SERVICE)?
.characteristic(level)
.characteristic(control),
)
.await?;
println!("published {}", published.uuid());
peripheral
.start_advertising(
Advertising::new()
.local_name("Rust Battery")
.service(services::BATTERY_SERVICE)?,
)
.await?;
println!("advertising as \"Rust Battery\" — ^C to stop\n");
let battery_level = published
.characteristic(characteristics::BATTERY_LEVEL)
.expect("just published")
.clone();
let ticker = Arc::new(battery_level.clone());
std::thread::spawn({
let ticker = ticker.clone();
move || loop {
std::thread::sleep(Duration::from_secs(5));
let next = BATTERY.fetch_sub(1, Ordering::Relaxed).saturating_sub(1);
if next == 0 {
BATTERY.store(100, Ordering::Relaxed);
}
if !ticker.subscribers().is_empty() && ticker.try_notify(&[next]) {
println!(" notified {next}%");
}
}
});
while let Some(request) = requests.next().await {
match request {
Request::Read(read) => {
let value = BATTERY.load(Ordering::Relaxed);
println!(
"read {} from {}",
read.characteristic(),
read.central().id()
);
read.respond(&[value])?;
}
Request::Write(write) => {
for w in write.writes() {
println!("write {} <- {:02x?}", w.characteristic, w.value);
}
if write.writes().iter().all(|w| w.value.len() == 1) {
write.accept();
} else {
write.reject_all(AttError::InvalidAttributeValueLength);
}
}
Request::Subscribed {
central,
characteristic,
} => {
println!(
"subscribe {characteristic} by {} (mtu {})",
central.id(),
central.max_notification_length()
);
battery_level
.notify(&[BATTERY.load(Ordering::Relaxed)])
.await?;
}
Request::Unsubscribed {
central,
characteristic,
} => {
println!("unsubscribe {characteristic} by {}", central.id());
}
#[cfg(l2cap)]
Request::ChannelOpened(channel) => {
println!(
"L2CAP open on PSM 0x{:04x} from {}",
channel.psm(),
channel.peer_id()
);
let Some(mut incoming) = channel.take_incoming() else {
continue;
};
std::thread::spawn(move || {
block_on(async move {
while let Some(chunk) = incoming.next().await {
println!(" L2CAP {} bytes in", chunk.len());
if channel.send(&chunk).is_err() {
break;
}
}
println!(" L2CAP closed: {:?}", channel.closed());
});
});
}
Request::ReadyToNotify => {}
}
}
Ok(())
}