pub mod backend;
pub mod types;
pub use types::{AdvertisingConfig, PeripheralEvent, ReadResponder, WriteResponder};
use crate::error::BlewResult;
use crate::gatt::service::GattService;
use crate::l2cap::{L2capChannel, types::Psm};
use crate::platform::PlatformPeripheral;
use crate::util::event_stream::EventStream;
use backend::PeripheralBackend;
use uuid::Uuid;
pub struct Peripheral<B: PeripheralBackend = PlatformPeripheral> {
pub(crate) backend: B,
}
impl<B: PeripheralBackend> Peripheral<B> {
pub async fn new() -> BlewResult<Self> {
Ok(Self {
backend: B::new().await?,
})
}
pub async fn is_powered(&self) -> BlewResult<bool> {
self.backend.is_powered().await
}
pub async fn add_service(&self, service: &GattService) -> BlewResult<()> {
self.backend.add_service(service).await
}
pub async fn start_advertising(&self, config: &AdvertisingConfig) -> BlewResult<()> {
self.backend.start_advertising(config).await
}
pub async fn stop_advertising(&self) -> BlewResult<()> {
self.backend.stop_advertising().await
}
pub async fn notify_characteristic(&self, char_uuid: Uuid, value: Vec<u8>) -> BlewResult<()> {
self.backend.notify_characteristic(char_uuid, value).await
}
pub async fn l2cap_listener(
&self,
) -> BlewResult<(
Psm,
impl futures_core::Stream<Item = BlewResult<L2capChannel>> + Send + 'static,
)> {
self.backend.l2cap_listener().await
}
pub fn events(&self) -> EventStream<PeripheralEvent, B::EventStream> {
EventStream::new(self.backend.events())
}
}