From dbcc3b9cc05b8b2562f176ef4e34561a302d0e10 Mon Sep 17 00:00:00 2001 From: ryan kurte Date: Thu, 27 Jan 2022 13:29:32 +1300 Subject: [PATCH 1/2] Add ConnectDevice API for adapter1 https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/adapter-api.txt\#n170 --- bluez-async/src/lib.rs | 27 +++++++++++++++++++++++++++ bluez-generated/src/adapter1.rs | 6 ++++++ 2 files changed, 33 insertions(+) diff --git a/bluez-async/src/lib.rs b/bluez-async/src/lib.rs index eb48d8f3..d33952a2 100644 --- a/bluez-async/src/lib.rs +++ b/bluez-async/src/lib.rs @@ -196,6 +196,26 @@ impl From<&DiscoveryFilter> for PropMap { } } +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct ConnectProperties { + pub address: MacAddress, + pub address_type: Option, +} + +impl From<&ConnectProperties> for PropMap { + fn from(properties: &ConnectProperties) -> Self { + let mut map: PropMap = HashMap::new(); + + map.insert("Address".to_string(), Variant(Box::new(properties.address.to_string()))); + + if let Some(address_type) = properties.address_type { + map.insert("AddressType".to_string(), Variant(Box::new(address_type.to_string()))); + } + + map + } +} + /// The type of write operation to use. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum WriteType { @@ -390,6 +410,13 @@ impl BluetoothSession { Ok(()) } + /// Connect to a device with the provided properties on the specified adaptor + pub async fn connect_device_on_adapter(&self, adapter_id: &AdapterId, properties: &ConnectProperties) -> Result<(), BluetoothError> { + let adapter = self.adapter(adapter_id); + adapter.connect_device(properties.into()).await?; + Ok(()) + } + /// Get a list of all Bluetooth adapters on the system. pub async fn get_adapters(&self) -> Result, BluetoothError> { let bluez_root = Proxy::new( diff --git a/bluez-generated/src/adapter1.rs b/bluez-generated/src/adapter1.rs index 1003fbf8..d13aaee6 100644 --- a/bluez-generated/src/adapter1.rs +++ b/bluez-generated/src/adapter1.rs @@ -9,6 +9,7 @@ pub trait OrgBluezAdapter1 { fn stop_discovery(&self) -> nonblock::MethodReply<()>; fn remove_device(&self, device: dbus::Path) -> nonblock::MethodReply<()>; fn get_discovery_filters(&self) -> nonblock::MethodReply>; + fn connect_device(&self, properties: arg::PropMap) -> nonblock::MethodReply>; fn address(&self) -> nonblock::MethodReply; fn address_type(&self) -> nonblock::MethodReply; fn name(&self) -> nonblock::MethodReply; @@ -134,6 +135,11 @@ impl<'a, T: nonblock::NonblockReply, C: ::std::ops::Deref> OrgBluezA .and_then(|r: (Vec,)| Ok(r.0)) } + fn connect_device(&self, properties: arg::PropMap) -> nonblock::MethodReply> { + self.method_call("org.bluez.Adapter1", "ConnectDevice", (properties,)) + .and_then(|r: (Vec,)| Ok(r.0)) + } + fn address(&self) -> nonblock::MethodReply { ::get( &self, From 831f2ba199dad807dea953de85ca509d2bc244c0 Mon Sep 17 00:00:00 2001 From: Andrew Walbran Date: Mon, 9 Sep 2024 17:23:16 +0100 Subject: [PATCH 2/2] Provide parameters directly rather than via ConnectProperties. Also moved and renamed method, added a note to the Rustdoc that it is experimental, added await_service_discovery like the other connect methods, and changed the return type. --- bluez-async/src/lib.rs | 54 +++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/bluez-async/src/lib.rs b/bluez-async/src/lib.rs index d33952a2..89a5761c 100644 --- a/bluez-async/src/lib.rs +++ b/bluez-async/src/lib.rs @@ -196,26 +196,6 @@ impl From<&DiscoveryFilter> for PropMap { } } -#[derive(Clone, Debug, Eq, PartialEq)] -pub struct ConnectProperties { - pub address: MacAddress, - pub address_type: Option, -} - -impl From<&ConnectProperties> for PropMap { - fn from(properties: &ConnectProperties) -> Self { - let mut map: PropMap = HashMap::new(); - - map.insert("Address".to_string(), Variant(Box::new(properties.address.to_string()))); - - if let Some(address_type) = properties.address_type { - map.insert("AddressType".to_string(), Variant(Box::new(address_type.to_string()))); - } - - map - } -} - /// The type of write operation to use. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum WriteType { @@ -410,13 +390,6 @@ impl BluetoothSession { Ok(()) } - /// Connect to a device with the provided properties on the specified adaptor - pub async fn connect_device_on_adapter(&self, adapter_id: &AdapterId, properties: &ConnectProperties) -> Result<(), BluetoothError> { - let adapter = self.adapter(adapter_id); - adapter.connect_device(properties.into()).await?; - Ok(()) - } - /// Get a list of all Bluetooth adapters on the system. pub async fn get_adapters(&self) -> Result, BluetoothError> { let bluez_root = Proxy::new( @@ -750,6 +723,33 @@ impl BluetoothSession { self.await_service_discovery(id).await } + /// Connect to a device with the given address on the specified adapter. + /// + /// The BlueZ documentation says that this method is experimental. + pub async fn connect_device( + &self, + adapter_id: &AdapterId, + address: MacAddress, + address_type: Option, + ) -> Result { + let mut properties: PropMap = HashMap::new(); + properties.insert( + "Address".to_string(), + Variant(Box::new(address.to_string())), + ); + if let Some(address_type) = address_type { + properties.insert( + "AddressType".to_string(), + Variant(Box::new(address_type.to_string())), + ); + } + + let adapter = self.adapter(adapter_id); + let id = adapter.connect_device(properties).await?; + self.await_service_discovery(id).await?; + Ok(id) + } + /// Disconnect from the given Bluetooth device. pub async fn disconnect(&self, id: &DeviceId) -> Result<(), BluetoothError> { Ok(self