Skip to content
Merged
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
8 changes: 4 additions & 4 deletions src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Adapter {

/// A stream of [`AdapterEvent`] which allows the application to identify when the adapter is enabled or disabled.
#[inline]
pub async fn events(&self) -> Result<impl Stream<Item = Result<AdapterEvent>> + Unpin + '_> {
pub async fn events(&self) -> Result<impl Stream<Item = Result<AdapterEvent>> + Send + Unpin + '_> {
self.0.events().await
}

Expand Down Expand Up @@ -87,7 +87,7 @@ impl Adapter {
pub async fn scan<'a>(
&'a self,
services: &'a [Uuid],
) -> Result<impl Stream<Item = AdvertisingDevice> + Unpin + 'a> {
) -> Result<impl Stream<Item = AdvertisingDevice> + Send + Unpin + 'a> {
self.0.scan(services).await
}

Expand All @@ -101,7 +101,7 @@ impl Adapter {
pub async fn discover_devices<'a>(
&'a self,
services: &'a [Uuid],
) -> Result<impl Stream<Item = Result<Device>> + Unpin + 'a> {
) -> Result<impl Stream<Item = Result<Device>> + Send + Unpin + 'a> {
self.0.discover_devices(services).await
}

Expand Down Expand Up @@ -170,7 +170,7 @@ impl Adapter {
pub async fn device_connection_events<'a>(
&'a self,
device: &'a Device,
) -> Result<impl Stream<Item = ConnectionEvent> + Unpin + 'a> {
) -> Result<impl Stream<Item = ConnectionEvent> + Send + Unpin + 'a> {
self.0.device_connection_events(device).await
}
}
2 changes: 1 addition & 1 deletion src/characteristic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl Characteristic {
///
/// Returns a stream of values for the characteristic sent from the device.
#[inline]
pub async fn notify(&self) -> Result<impl Stream<Item = Result<Vec<u8>>> + Unpin + '_> {
pub async fn notify(&self) -> Result<impl Stream<Item = Result<Vec<u8>>> + Send + Unpin + '_> {
self.0.notify().await
}

Expand Down
2 changes: 2 additions & 0 deletions src/corebluetooth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ pub mod error;
pub mod l2cap_channel;
pub mod service;

mod ad;
pub(crate) mod delegates;
pub(crate) mod dispatch;

/// A platform-specific device identifier.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down
90 changes: 90 additions & 0 deletions src/corebluetooth/ad.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use std::collections::HashMap;

use objc2_core_bluetooth::{
CBAdvertisementDataIsConnectable, CBAdvertisementDataLocalNameKey,
CBAdvertisementDataManufacturerDataKey, CBAdvertisementDataOverflowServiceUUIDsKey,
CBAdvertisementDataServiceDataKey, CBAdvertisementDataServiceUUIDsKey,
CBAdvertisementDataTxPowerLevelKey, CBUUID,
};
use objc2_foundation::{NSArray, NSData, NSDictionary, NSNumber, NSString};
use uuid::Uuid;

use crate::{AdvertisementData, BluetoothUuidExt, ManufacturerData};

impl AdvertisementData {
pub(crate) fn from_nsdictionary(adv_data: &NSDictionary<NSString>) -> Self {
let is_connectable = adv_data
.objectForKey(unsafe { CBAdvertisementDataIsConnectable })
.is_some_and(|val| {
val.downcast_ref::<NSNumber>()
.map(|b| b.as_bool())
.unwrap_or(false)
});

let local_name = adv_data
.objectForKey(unsafe { CBAdvertisementDataLocalNameKey })
.and_then(|val| val.downcast_ref::<NSString>().map(|s| s.to_string()));

let manufacturer_data = adv_data
.objectForKey(unsafe { CBAdvertisementDataManufacturerDataKey })
.and_then(|val| val.downcast_ref::<NSData>().map(|v| v.to_vec()))
.and_then(|val| {
(val.len() >= 2).then(|| ManufacturerData {
company_id: u16::from_le_bytes(val[0..2].try_into().unwrap()),
data: val[2..].to_vec(),
})
});

let tx_power_level: Option<i16> = adv_data
.objectForKey(unsafe { CBAdvertisementDataTxPowerLevelKey })
.and_then(|val| val.downcast_ref::<NSNumber>().map(|val| val.shortValue()));

let service_data = if let Some(val) =
adv_data.objectForKey(unsafe { CBAdvertisementDataServiceDataKey })
{
unsafe {
if let Some(val) = val.downcast_ref::<NSDictionary>() {
let mut res = HashMap::with_capacity(val.count());
for k in val.allKeys() {
if let Some(key) = k.downcast_ref::<CBUUID>() {
if let Some(val) = val
.objectForKey_unchecked(&k)
.and_then(|val| val.downcast_ref::<NSData>())
{
res.insert(
Uuid::from_bluetooth_bytes(key.data().as_bytes_unchecked()),
val.to_vec(),
);
}
}
}
res
} else {
HashMap::new()
}
}
} else {
HashMap::new()
};

let services = adv_data
.objectForKey(unsafe { CBAdvertisementDataServiceUUIDsKey })
.into_iter()
.chain(adv_data.objectForKey(unsafe { CBAdvertisementDataOverflowServiceUUIDsKey }))
.flat_map(|x| x.downcast::<NSArray>())
.flatten()
.flat_map(|obj| obj.downcast::<CBUUID>())
.map(|uuid| unsafe { uuid.data() })
.map(|data| unsafe { Uuid::from_bluetooth_bytes(data.as_bytes_unchecked()) })
.collect();

AdvertisementData {
local_name,
manufacturer_data,
services,
service_data,
tx_power_level,
is_connectable,
}
}
}
Loading