Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ objc2-core-bluetooth = { version = "0.2.2", default-features = false, features =
] }

[target.'cfg(target_os = "windows")'.dependencies]
windows = { version = "0.61", features = ["Devices_Bluetooth", "Devices_Bluetooth_GenericAttributeProfile", "Devices_Bluetooth_Advertisement", "Devices_Radios", "Foundation_Collections", "Foundation", "Storage_Streams"] }
windows = { version = "0.61", features = ["Devices_Enumeration", "Devices_Bluetooth", "Devices_Bluetooth_GenericAttributeProfile", "Devices_Bluetooth_Advertisement", "Devices_Radios", "Foundation_Collections", "Foundation", "Storage_Streams"] }
windows-future = "0.2.0"

[dev-dependencies]
Expand Down
22 changes: 22 additions & 0 deletions examples/adapters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// See the "macOS permissions note" in README.md before running this on macOS
// Big Sur or later.

use btleplug::api::{
Central, Manager as _,
};
use btleplug::platform::Manager;


#[tokio::main]
async fn main() -> anyhow::Result<()> {
pretty_env_logger::init();

let manager = Manager::new().await?;

for adapter in manager.adapters().await.unwrap() {
println!("Info: {:?}", adapter.adapter_info().await.unwrap());
println!("Mac: {:?}", adapter.adapter_mac().await.unwrap());
}

Ok(())
}
3 changes: 3 additions & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,9 @@ pub trait Central: Send + Sync + Clone {
/// be useful for debug logs.
async fn adapter_info(&self) -> Result<String>;

/// Get information about the Bluetooth adapter mac address.
async fn adapter_mac(&self) -> Result<BDAddr>;

/// Get information about the Bluetooth adapter state.
async fn adapter_state(&self) -> Result<CentralState>;
}
Expand Down
7 changes: 6 additions & 1 deletion src/bluez/adapter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::peripheral::{Peripheral, PeripheralId};
use crate::api::{Central, CentralEvent, CentralState, ScanFilter};
use crate::api::{BDAddr, Central, CentralEvent, CentralState, ScanFilter};
use crate::{Error, Result};
use async_trait::async_trait;
use bluez_async::{
Expand Down Expand Up @@ -113,6 +113,11 @@ impl Central for Adapter {
Ok(format!("{} ({})", adapter_info.id, adapter_info.modalias))
}

async fn adapter_mac(&self) -> Result<BDAddr> {
let adapter_info = self.session.get_adapter_info(&self.adapter).await?;
Ok(adapter_info.mac_address.into())
}

async fn adapter_state(&self) -> Result<CentralState> {
let mut powered = false;
if let Ok(info) = self.session.get_adapter_info(&self.adapter).await {
Expand Down
1 change: 1 addition & 0 deletions src/bluez/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod adapter;
pub mod manager;
pub mod peripheral;
pub mod utils;
8 changes: 1 addition & 7 deletions src/bluez/peripheral.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use async_trait::async_trait;
use bluez_async::{
BluetoothEvent, BluetoothSession, CharacteristicEvent, CharacteristicFlags, CharacteristicId,
CharacteristicInfo, DescriptorInfo, DeviceId, DeviceInfo, MacAddress, ServiceInfo,
CharacteristicInfo, DescriptorInfo, DeviceId, DeviceInfo, ServiceInfo,
WriteOptions,
};
use futures::future::{join_all, ready};
Expand Down Expand Up @@ -328,12 +328,6 @@ impl From<WriteType> for bluez_async::WriteType {
}
}

impl From<MacAddress> for BDAddr {
fn from(mac_address: MacAddress) -> Self {
<[u8; 6]>::into(mac_address.into())
}
}

impl From<DeviceId> for PeripheralId {
fn from(device_id: DeviceId) -> Self {
PeripheralId(device_id)
Expand Down
9 changes: 9 additions & 0 deletions src/bluez/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use bluez_async::MacAddress;

use crate::api::BDAddr;

impl From<MacAddress> for BDAddr {
fn from(mac_address: MacAddress) -> Self {
<[u8; 6]>::into(mac_address.into())
}
}
5 changes: 5 additions & 0 deletions src/corebluetooth/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::peripheral::{Peripheral, PeripheralId};
use crate::api::{Central, CentralEvent, CentralState, ScanFilter};
use crate::common::adapter_manager::AdapterManager;
use crate::{Error, Result};
use crate::api::BDAddr;
use async_trait::async_trait;
use futures::channel::mpsc::{self, Sender};
use futures::sink::SinkExt;
Expand Down Expand Up @@ -137,6 +138,10 @@ impl Central for Adapter {
Ok("CoreBluetooth".to_string())
}

async fn adapter_mac(&self) -> Result<BDAddr> {
todo!();
}

async fn adapter_state(&self) -> Result<CentralState> {
let fut = CoreBluetoothReplyFuture::default();
self.sender
Expand Down
4 changes: 4 additions & 0 deletions src/droidplug/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ impl Central for Adapter {
self.add(address.0)
}

async fn adapter_mac(&self) -> Result<BDAddr> {
todo!();
}

async fn adapter_state(&self) -> Result<CentralState> {
Ok(CentralState::Unknown)
}
Expand Down
17 changes: 13 additions & 4 deletions src/winrtble/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ use crate::{
};
use async_trait::async_trait;
use futures::stream::Stream;
use log::debug;
use std::convert::TryInto;
use std::fmt::{self, Debug, Formatter};
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use windows::{
Devices::Radios::{Radio, RadioState},
Devices::{Bluetooth::BluetoothAdapter, Radios::{Radio, RadioState}},
Foundation::TypedEventHandler,
};

Expand All @@ -34,6 +35,7 @@ pub struct Adapter {
watcher: Arc<Mutex<BLEWatcher>>,
manager: Arc<AdapterManager<Peripheral>>,
radio: Radio,
adapter: BluetoothAdapter,
}

// https://github.com/microsoft/windows-rs/blob/master/crates/libs/windows/src/Windows/Devices/Radios/mod.rs
Expand All @@ -47,7 +49,8 @@ fn get_central_state(radio: &Radio) -> CentralState {
}

impl Adapter {
pub(crate) fn new(radio: Radio) -> Result<Self> {
pub(crate) async fn new(adapter: BluetoothAdapter) -> Result<Self> {
let radio = adapter.GetRadioAsync()?.await?;
let watcher = Arc::new(Mutex::new(BLEWatcher::new()?));
let manager = Arc::new(AdapterManager::default());

Expand All @@ -66,6 +69,7 @@ impl Adapter {
watcher,
manager,
radio,
adapter,
})
}
}
Expand Down Expand Up @@ -129,8 +133,13 @@ impl Central for Adapter {
}

async fn adapter_info(&self) -> Result<String> {
// TODO: Get information about the adapter.
Ok("WinRT".to_string())
Ok(format!("DeviceId: {}\n", self.adapter.DeviceId()?.to_string()))
}

async fn adapter_mac(&self) -> Result<BDAddr> {
let address = self.adapter.BluetoothAddress()?;
let mac = BDAddr::try_from(address)?;
Ok(mac)
}

async fn adapter_state(&self) -> Result<CentralState> {
Expand Down
27 changes: 19 additions & 8 deletions src/winrtble/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
use super::adapter::Adapter;
use crate::{api, Result};
use async_trait::async_trait;
use std::future::IntoFuture;
use windows::Devices::Radios::{Radio, RadioKind};

use windows::{
Devices::Bluetooth::BluetoothAdapter,
Devices::Enumeration::DeviceInformation,
core::HSTRING
};
/// Implementation of [api::Manager](crate::api::Manager).
#[derive(Clone, Debug)]
pub struct Manager {}
Expand All @@ -32,11 +34,20 @@ impl api::Manager for Manager {
type Adapter = Adapter;

async fn adapters(&self) -> Result<Vec<Adapter>> {
let radios = Radio::GetRadiosAsync()?.into_future().await?;
radios
// Get the selector for Bluetooth adapters
let selector = BluetoothAdapter::GetDeviceSelector()?;

// Find all devices that match the selector
let devices = DeviceInformation::FindAllAsyncAqsFilter(&HSTRING::from(selector))?.await?;

let futures = devices
.into_iter()
.filter(|radio| radio.Kind() == Ok(RadioKind::Bluetooth))
.map(|radio| Adapter::new(radio))
.collect()
.map(|device| async move {
let device_id = device.Id()?;
let bt_adapter = BluetoothAdapter::FromIdAsync(&device_id)?.await?;
Adapter::new(bt_adapter).await
});
let adapters = futures::future::try_join_all(futures).await?;
Ok(adapters)
}
}