#[cfg(not(target_vendor = "apple"))]
fn main() {
println!("restore.rs is Apple-only; target_vendor != \"apple\" on this build.");
}
#[cfg(target_vendor = "apple")]
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
use blew::central::{Central, CentralConfig};
use blew::gatt::props::{AttributePermissions, CharacteristicProperties};
use blew::gatt::service::{GattCharacteristic, GattService};
use blew::peripheral::{AdvertisingConfig, Peripheral, PeripheralConfig};
use uuid::Uuid;
const CENTRAL_RESTORE_ID: &str = "org.example.blew.central.restore";
const PERIPHERAL_RESTORE_ID: &str = "org.example.blew.peripheral.restore";
const SVC_UUID: Uuid = Uuid::from_u128(0x12345678_1234_1234_1234_123456789abc);
const CHAR_UUID: Uuid = Uuid::from_u128(0x12345678_1234_1234_1234_123456789abd);
tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.init();
let central: Central = Central::with_config(CentralConfig {
restore_identifier: Some(CENTRAL_RESTORE_ID.into()),
})
.await?;
match central.take_restored() {
Some(devices) => {
println!(
"central restored by OS: {} preserved peripheral(s)",
devices.len()
);
for device in &devices {
println!(
" - {} ({})",
device.name.as_deref().unwrap_or("<unknown>"),
device.id,
);
}
}
None => {
println!("central: fresh launch (no restoration)");
}
}
let peripheral: Peripheral = Peripheral::with_config(PeripheralConfig {
restore_identifier: Some(PERIPHERAL_RESTORE_ID.into()),
})
.await?;
match peripheral.take_restored() {
Some(service_uuids) => {
println!(
"peripheral restored by OS: {} preserved service(s)",
service_uuids.len()
);
for uuid in &service_uuids {
println!(" - {uuid}");
}
}
None => {
println!("peripheral: fresh launch (no restoration)");
peripheral
.add_service(&GattService {
uuid: SVC_UUID,
primary: true,
characteristics: vec![GattCharacteristic {
uuid: CHAR_UUID,
properties: CharacteristicProperties::READ
| CharacteristicProperties::WRITE,
permissions: AttributePermissions::READ | AttributePermissions::WRITE,
value: vec![],
descriptors: vec![],
}],
})
.await?;
peripheral
.start_advertising(&AdvertisingConfig {
local_name: "blew-restore".into(),
service_uuids: vec![SVC_UUID],
})
.await?;
}
}
println!("Ready. (In a real iOS app this task would be the long-lived main loop.)");
Ok(())
}