#![deny(missing_docs)]
#![deny(clippy::missing_docs_in_private_items)]
#![warn(unused_extern_crates)]
#[cfg(not(any(feature = "sync", feature = "async")))]
compile_error!("Must enable either the `sync` or `async` feature");
#[cfg(all(feature = "sync", feature = "async"))]
compile_error!("Cannot enable both `sync` and `async` features simultaneously");
#[cfg(target_os = "android")]
pub mod android;
#[cfg(target_os = "android")]
pub use android::Bluetooth;
#[cfg(target_os = "android")]
use winit::platform::android::activity::AndroidApp;
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "windows")]
mod windows;
mod bluetooth_uuid;
pub use bluetooth_uuid::BluetoothUuid;
#[cfg(not(target_os = "android"))]
mod sdp;
#[cfg(not(target_os = "android"))]
pub use sdp::run_sdp;
#[cfg(target_os = "android")]
type ServiceRecord = ();
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub enum BluetoothCommand {
DetectAdapters,
QueryNumAdapters,
}
pub enum MessageToBluetoothHost {
#[cfg(feature = "async")]
DisplayPasskey(u32, tokio::sync::mpsc::Sender<ResponseToPasskey>),
#[cfg(feature = "async")]
ConfirmPasskey(u32, tokio::sync::mpsc::Sender<ResponseToPasskey>),
CancelDisplayPasskey,
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub enum MessageFromBluetoothHost {
PasskeyMessage(ResponseToPasskey),
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub enum ResponseToPasskey {
Yes,
No,
Cancel,
Waiting,
}
pub enum BluetoothResponse {
Adapters(usize),
}
#[derive(Clone, Debug)]
pub struct BluetoothRfcommProfileSettings {
pub uuid: String,
pub name: Option<String>,
pub service_uuid: Option<String>,
pub channel: Option<u16>,
pub psm: Option<u16>,
pub authenticate: Option<bool>,
pub authorize: Option<bool>,
pub auto_connect: Option<bool>,
pub sdp_record: Option<String>,
pub sdp_version: Option<u16>,
pub sdp_features: Option<u16>,
}
#[derive(Clone)]
pub struct BluetoothL2capProfileSettings {
pub uuid: String,
pub name: Option<String>,
pub service_uuid: Option<String>,
pub channel: Option<u16>,
pub psm: Option<u16>,
pub authenticate: Option<bool>,
pub authorize: Option<bool>,
pub auto_connect: Option<bool>,
pub sdp_record: Option<String>,
pub sdp_version: Option<u16>,
pub sdp_features: Option<u16>,
}
#[cfg(target_os = "android")]
pub type BluetoothDiscovery = android::BluetoothDiscovery;
#[cfg(target_os = "linux")]
pub type BluetoothDiscovery = linux::BluetoothDiscovery;
#[cfg(target_os = "windows")]
pub type BluetoothDiscovery = windows::BluetoothDiscovery;
pub enum BluetoothAdapterAddress {
String(String),
Byte([u8; 6]),
}
pub enum PairingStatus {
NotPaired,
Pairing,
Paired,
Unknown,
}
#[cfg(target_os = "android")]
pub type BluetoothDevice = android::BluetoothDevice;
#[cfg(target_os = "linux")]
pub type BluetoothDevice = linux::LinuxBluetoothDevice;
#[cfg(target_os = "windows")]
pub type BluetoothDevice = windows::BluetoothDevice;
#[cfg(target_os = "android")]
pub type BluetoothAdapter = android::Bluetooth;
#[cfg(target_os = "linux")]
pub type BluetoothAdapter = linux::BluetoothHandler;
#[cfg(target_os = "windows")]
pub type BluetoothAdapter = windows::BluetoothHandler;
pub struct BluetoothAdapterBuilder {
#[cfg(target_os = "android")]
app: Option<AndroidApp>,
#[cfg(feature = "async")]
s: Option<tokio::sync::mpsc::Sender<MessageToBluetoothHost>>,
}
impl Default for BluetoothAdapterBuilder {
fn default() -> Self {
Self::new()
}
}
impl BluetoothAdapterBuilder {
pub fn new() -> Self {
Self {
#[cfg(target_os = "android")]
app: None,
#[cfg(feature = "async")]
s: None,
}
}
#[cfg(target_os = "android")]
pub fn with_android_app(&mut self, app: AndroidApp) {
self.app = Some(app);
}
#[cfg(feature = "async")]
pub fn with_sender(&mut self, s: tokio::sync::mpsc::Sender<MessageToBluetoothHost>) {
self.s = Some(s);
}
#[cfg(all(target_os = "android", feature = "sync"))]
pub fn build<'a>(self) -> Result<BluetoothAdapter, String> {
return Ok(android::Bluetooth::new(self.app.unwrap()));
}
#[cfg(all(target_os = "android", feature = "async"))]
pub async fn build(self) -> Result<BluetoothAdapter, String> {
return self.build();
}
#[cfg(all(not(target_os = "android"), feature = "async"))]
pub async fn build(self) -> Result<BluetoothAdapter, String> {
#[cfg(target_os = "linux")]
{
return Ok(
linux::BluetoothHandler::new(self.s.unwrap()).await?,
);
}
#[cfg(target_os = "windows")]
{
return Ok(
windows::BluetoothHandler::new(self.s.unwrap()).await?,
);
}
}
}
#[cfg(target_os = "linux")]
pub type BluetoothStream = linux::BluetoothStream;
#[cfg(target_os = "android")]
pub type BluetoothStream = android::RfcommStream;
#[cfg(target_os = "windows")]
pub type BluetoothStream = windows::WindowsRfcommStream;
#[cfg(target_os = "android")]
pub type BluetoothRfcommConnectable = android::BluetoothRfcommConnectable;
#[cfg(target_os = "linux")]
pub type BluetoothRfcommConnectable = linux::BluetoothRfCommConnectable;
#[cfg(target_os = "windows")]
pub type BluetoothRfcommConnectable = windows::BluetoothRfcommConnectable;
#[cfg(all(target_os = "android", feature = "async"))]
pub type BluetoothL2capConnectable = android::BluetoothRfcommConnectable;
#[cfg(all(target_os = "linux", feature = "async"))]
pub type BluetoothL2capConnectable = linux::BluetoothL2capConnectable;
#[cfg(all(target_os = "linux", feature = "async"))]
pub type BluetoothRfcommProfile = linux::BluetoothRfcommProfile;
#[cfg(all(target_os = "windows", feature = "async"))]
pub type BluetoothRfcommProfile = windows::BluetoothRfcommProfile;
#[cfg(all(target_os = "android", feature = "sync"))]
pub type BluetoothRfcommProfile = android::BluetoothRfcommProfile;
#[cfg(all(target_os = "linux", feature = "async"))]
pub type BluetoothL2capProfile = linux::BluetoothL2capProfile;
#[cfg(all(target_os = "android", feature = "sync"))]
pub type BluetoothL2capProfile = android::BluetoothRfcommProfile;
#[cfg(target_os = "windows")]
pub type BluetoothL2capProfile = ();
#[cfg(target_os = "android")]
pub type BluetoothSocket = android::BluetoothSocket;
#[cfg(target_os = "linux")]
pub type BluetoothSocket = linux::BluetoothRfcommSocket;
#[cfg(target_os = "windows")]
pub type BluetoothSocket = windows::BluetoothRfcommSocket;