blew 0.1.0-alpha.2

Cross-platform async BLE library for Rust (Apple, Linux, Android)
Documentation
use super::types::{AdvertisingConfig, PeripheralEvent};
use crate::error::BlewResult;
use crate::gatt::service::GattService;
use crate::l2cap::{L2capChannel, types::Psm};
use futures_core::Stream;
use std::future::Future;
use uuid::Uuid;

pub(crate) mod private {
    pub trait Sealed {}
}

/// Trait implemented by each platform backend for the peripheral (advertiser/GATT server) role.
///
/// This trait is **sealed**: external crates cannot implement it. See [`CentralBackend`](crate::central::backend::CentralBackend)
/// for the sealing rationale.
pub trait PeripheralBackend: private::Sealed + Send + Sync + 'static {
    /// The concrete `Stream` type yielded by [`events`](Self::events).
    type EventStream: Stream<Item = PeripheralEvent> + Send + Unpin + 'static;

    /// Construct and initialise the backend.
    fn new() -> impl Future<Output = BlewResult<Self>> + Send
    where
        Self: Sized;

    /// Returns `true` if the local Bluetooth adapter is powered on.
    fn is_powered(&self) -> impl Future<Output = BlewResult<bool>> + Send;

    /// Register a GATT service. Must be called before [`start_advertising`](Self::start_advertising).
    fn add_service(&self, service: &GattService) -> impl Future<Output = BlewResult<()>> + Send;

    /// Begin advertising. Returns [`BlewError::AlreadyAdvertising`](crate::error::BlewError::AlreadyAdvertising)
    /// if already active.
    fn start_advertising(
        &self,
        config: &AdvertisingConfig,
    ) -> impl Future<Output = BlewResult<()>> + Send;

    /// Stop advertising.
    fn stop_advertising(&self) -> impl Future<Output = BlewResult<()>> + Send;

    /// Push a characteristic value update to all subscribed centrals.
    fn notify_characteristic(
        &self,
        char_uuid: Uuid,
        value: Vec<u8>,
    ) -> impl Future<Output = BlewResult<()>> + Send;

    /// Publish an L2CAP CoC channel and return the OS-assigned PSM together with
    /// a stream of incoming [`L2capChannel`] connections.
    ///
    /// The caller should advertise the returned PSM (e.g. via a readable GATT
    /// characteristic) so that centrals know which PSM to connect on.
    ///
    /// Returns [`BlewError::NotSupported`](crate::error::BlewError::NotSupported) until
    /// the platform backend implements L2CAP.
    fn l2cap_listener(
        &self,
    ) -> impl Future<
        Output = BlewResult<(
            Psm,
            impl Stream<Item = BlewResult<L2capChannel>> + Send + 'static,
        )>,
    > + Send;

    /// Subscribe to peripheral-role events. Each call returns an independent stream.
    fn events(&self) -> Self::EventStream;
}