Skip to content

Commit 336263d

Browse files
committed
fix: make local_name in corebluetooth more descriptive
1 parent fb4bac7 commit 336263d

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

src/corebluetooth/central_delegate.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub enum CentralDelegateEvent {
5151
DidUpdateState,
5252
DiscoveredPeripheral {
5353
cbperipheral: StrongPtr,
54+
local_name: Option<String>,
5455
},
5556
DiscoveredServices {
5657
peripheral_uuid: Uuid,
@@ -136,9 +137,13 @@ impl Debug for CentralDelegateEvent {
136137
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
137138
match self {
138139
CentralDelegateEvent::DidUpdateState => f.debug_tuple("DidUpdateState").finish(),
139-
CentralDelegateEvent::DiscoveredPeripheral { cbperipheral } => f
140+
CentralDelegateEvent::DiscoveredPeripheral {
141+
cbperipheral,
142+
local_name,
143+
} => f
140144
.debug_struct("CentralDelegateEvent")
141145
.field("cbperipheral", cbperipheral.deref())
146+
.field("local_name", local_name)
142147
.finish(),
143148
CentralDelegateEvent::DiscoveredServices {
144149
peripheral_uuid,
@@ -520,10 +525,24 @@ pub mod CentralDelegate {
520525
);
521526

522527
let held_peripheral = unsafe { StrongPtr::retain(peripheral) };
528+
529+
let local_name = unsafe {
530+
let name_key = ns::dictionary_objectforkey(
531+
adv_data,
532+
cb::ADVERTISEMENT_DATA_MANUFACTURER_LOCAL_NAME_KEY,
533+
);
534+
if name_key != nil {
535+
nsstring_to_string(name_key)
536+
} else {
537+
None
538+
}
539+
};
540+
523541
send_delegate_event(
524542
delegate,
525543
CentralDelegateEvent::DiscoveredPeripheral {
526544
cbperipheral: held_peripheral,
545+
local_name,
527546
},
528547
);
529548

src/corebluetooth/framework.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ pub mod cb {
135135
pub static CBAdvertisementDataManufacturerDataKey: id;
136136
pub static CBAdvertisementDataServiceDataKey: id;
137137
pub static CBAdvertisementDataServiceUUIDsKey: id;
138+
pub static CBAdvertisementDataLocalNameKey: id;
138139

139140
pub static CBCentralManagerScanOptionAllowDuplicatesKey: id;
140141
}
@@ -381,4 +382,5 @@ pub mod cb {
381382
pub use self::link::CBAdvertisementDataManufacturerDataKey as ADVERTISEMENT_DATA_MANUFACTURER_DATA_KEY;
382383
pub use self::link::CBAdvertisementDataServiceDataKey as ADVERTISEMENT_DATA_SERVICE_DATA_KEY;
383384
pub use self::link::CBAdvertisementDataServiceUUIDsKey as ADVERTISEMENT_DATA_SERVICE_UUIDS_KEY;
385+
pub use self::link::CBAdvertisementDataLocalNameKey as ADVERTISEMENT_DATA_MANUFACTURER_LOCAL_NAME_KEY;
384386
}

src/corebluetooth/internal.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use log::{error, trace, warn};
3838
use objc::{rc::StrongPtr, runtime::YES};
3939
use std::{
4040
collections::{BTreeSet, HashMap, VecDeque},
41-
fmt::{self, Debug, Formatter},
41+
fmt::{self, format, Debug, Formatter},
4242
ops::Deref,
4343
thread,
4444
};
@@ -512,9 +512,24 @@ impl CoreBluetoothInternal {
512512
}
513513
}
514514

515-
async fn on_discovered_peripheral(&mut self, peripheral: StrongPtr) {
515+
async fn on_discovered_peripheral(
516+
&mut self,
517+
peripheral: StrongPtr,
518+
local_name: Option<String>,
519+
) {
516520
let uuid = nsuuid_to_uuid(cb::peer_identifier(*peripheral));
517-
let name = nsstring_to_string(cb::peripheral_name(*peripheral));
521+
let peripheral_name = nsstring_to_string(cb::peripheral_name(*peripheral));
522+
523+
let name = match (peripheral_name, local_name) {
524+
(Some(p_name), Some(l_name)) if p_name != l_name => {
525+
Some(format!("{p_name} [{l_name}]"))
526+
}
527+
(Some(p_name), Some(_)) => Some(p_name),
528+
(Some(p_name), None) => Some(p_name),
529+
(None, Some(l_name)) => Some(l_name),
530+
(None, None) => None,
531+
};
532+
518533
if self.peripherals.contains_key(&uuid) {
519534
if let Some(name) = name {
520535
self.dispatch_event(CoreBluetoothEvent::DeviceUpdated { uuid, name })
@@ -1012,8 +1027,8 @@ impl CoreBluetoothInternal {
10121027
CentralDelegateEvent::DidUpdateState => {
10131028
self.dispatch_event(CoreBluetoothEvent::AdapterConnected).await
10141029
}
1015-
CentralDelegateEvent::DiscoveredPeripheral{cbperipheral} => {
1016-
self.on_discovered_peripheral(cbperipheral).await
1030+
CentralDelegateEvent::DiscoveredPeripheral{cbperipheral, local_name} => {
1031+
self.on_discovered_peripheral(cbperipheral, local_name).await
10171032
}
10181033
CentralDelegateEvent::DiscoveredServices{peripheral_uuid, services} => {
10191034
self.on_discovered_services(peripheral_uuid, services)

0 commit comments

Comments
 (0)