Skip to content

Commit 3a7d50c

Browse files
szymonlesiszqdot
authored andcommitted
fix: corebluetooth peripheral name
1 parent c0e60a5 commit 3a7d50c

4 files changed

Lines changed: 41 additions & 30 deletions

File tree

src/corebluetooth/adapter.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,28 @@ impl Adapter {
5757
match msg {
5858
CoreBluetoothEvent::DeviceDiscovered {
5959
uuid,
60-
name,
60+
local_name,
61+
advertisement_name,
6162
event_receiver,
6263
} => {
6364
manager_clone.add_peripheral(Peripheral::new(
6465
uuid,
65-
name,
66+
local_name,
67+
advertisement_name,
6668
Arc::downgrade(&manager_clone),
6769
event_receiver,
6870
adapter_sender_clone.clone(),
6971
));
7072
manager_clone.emit(CentralEvent::DeviceDiscovered(uuid.into()));
7173
}
72-
CoreBluetoothEvent::DeviceUpdated { uuid, name } => {
74+
CoreBluetoothEvent::DeviceUpdated {
75+
uuid,
76+
local_name,
77+
advertisement_name,
78+
} => {
7379
let id = uuid.into();
7480
if let Some(entry) = manager_clone.peripheral_mut(&id) {
75-
entry.value().update_name(&name);
81+
entry.value().update_name(local_name, advertisement_name);
7682
manager_clone.emit(CentralEvent::DeviceUpdated(id));
7783
}
7884
}

src/corebluetooth/central_delegate.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub enum CentralDelegateEvent {
4646
},
4747
DiscoveredPeripheral {
4848
cbperipheral: Retained<CBPeripheral>,
49-
local_name: Option<String>,
49+
advertisement_name: Option<String>,
5050
},
5151
DiscoveredServices {
5252
peripheral_uuid: Uuid,
@@ -136,11 +136,11 @@ impl Debug for CentralDelegateEvent {
136136
.finish(),
137137
CentralDelegateEvent::DiscoveredPeripheral {
138138
cbperipheral,
139-
local_name,
139+
advertisement_name,
140140
} => f
141141
.debug_struct("CentralDelegateEvent")
142142
.field("cbperipheral", cbperipheral.deref())
143-
.field("local_name", local_name)
143+
.field("advertisement_name", advertisement_name)
144144
.finish(),
145145
CentralDelegateEvent::DiscoveredServices {
146146
peripheral_uuid,
@@ -385,14 +385,14 @@ declare_class!(
385385
peripheral_debug(peripheral)
386386
);
387387

388-
let local_name = adv_data
388+
let advertisement_name = adv_data
389389
.get(unsafe { CBAdvertisementDataLocalNameKey })
390390
.map(|name| (name as *const AnyObject as *const NSString))
391391
.and_then(|name| unsafe { nsstring_to_string(name) });
392392

393393
self.send_event(CentralDelegateEvent::DiscoveredPeripheral {
394394
cbperipheral: peripheral.retain(),
395-
local_name,
395+
advertisement_name,
396396
});
397397

398398
let rssi_value = rssi.as_i16();

src/corebluetooth/internal.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -461,12 +461,14 @@ pub enum CoreBluetoothEvent {
461461
},
462462
DeviceDiscovered {
463463
uuid: Uuid,
464-
name: Option<String>,
464+
local_name: Option<String>,
465+
advertisement_name: Option<String>,
465466
event_receiver: Receiver<PeripheralEventInternal>,
466467
},
467468
DeviceUpdated {
468469
uuid: Uuid,
469-
name: String,
470+
local_name: Option<String>,
471+
advertisement_name: Option<String>,
470472
},
471473
DeviceDisconnected {
472474
uuid: Uuid,
@@ -569,26 +571,20 @@ impl CoreBluetoothInternal {
569571
async fn on_discovered_peripheral(
570572
&mut self,
571573
peripheral: Retained<CBPeripheral>,
572-
local_name: Option<String>,
574+
advertisement_name: Option<String>,
573575
) {
574576
let uuid = nsuuid_to_uuid(unsafe { &peripheral.identifier() });
575577
let peripheral_name = unsafe { peripheral.name() };
576-
577-
let name = match (peripheral_name.map(|n| n.to_string()), local_name) {
578-
(Some(p_name), Some(l_name)) if p_name != l_name => {
579-
Some(format!("{p_name} [{l_name}]"))
580-
}
581-
(Some(p_name), Some(_)) => Some(p_name),
582-
(Some(p_name), None) => Some(p_name),
583-
(None, Some(l_name)) => Some(l_name),
584-
(None, None) => None,
585-
};
578+
let local_name = peripheral_name
579+
.map(|n| n.to_string())
580+
.or(advertisement_name.clone());
586581

587582
if self.peripherals.contains_key(&uuid) {
588-
if let Some(name) = name {
583+
if local_name.is_some() {
589584
self.dispatch_event(CoreBluetoothEvent::DeviceUpdated {
590585
uuid,
591-
name: name.to_string(),
586+
local_name,
587+
advertisement_name,
592588
})
593589
.await;
594590
}
@@ -599,7 +595,8 @@ impl CoreBluetoothInternal {
599595
.insert(uuid, PeripheralInternal::new(peripheral, event_sender));
600596
self.dispatch_event(CoreBluetoothEvent::DeviceDiscovered {
601597
uuid,
602-
name: name.map(|name| name.to_string()),
598+
local_name,
599+
advertisement_name,
603600
event_receiver,
604601
})
605602
.await;
@@ -1096,8 +1093,8 @@ impl CoreBluetoothInternal {
10961093
CentralDelegateEvent::DidUpdateState{state} => {
10971094
self.dispatch_event(CoreBluetoothEvent::DidUpdateState{state}).await
10981095
}
1099-
CentralDelegateEvent::DiscoveredPeripheral{cbperipheral, local_name} => {
1100-
self.on_discovered_peripheral(cbperipheral, local_name).await
1096+
CentralDelegateEvent::DiscoveredPeripheral{cbperipheral, advertisement_name} => {
1097+
self.on_discovered_peripheral(cbperipheral, advertisement_name).await
11011098
}
11021099
CentralDelegateEvent::DiscoveredServices{peripheral_uuid, services} => {
11031100
self.on_discovered_services(peripheral_uuid, services)

src/corebluetooth/peripheral.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ impl Peripheral {
8484
pub(crate) fn new(
8585
uuid: Uuid,
8686
local_name: Option<String>,
87+
advertisement_name: Option<String>,
8788
manager: Weak<AdapterManager<Self>>,
8889
event_receiver: Receiver<PeripheralEventInternal>,
8990
message_sender: Sender<CoreBluetoothMessage>,
@@ -94,7 +95,7 @@ impl Peripheral {
9495
address: BDAddr::default(),
9596
address_type: None,
9697
local_name,
97-
advertisement_name: None,
98+
advertisement_name,
9899
tx_power_level: None,
99100
rssi: None,
100101
manufacturer_data: HashMap::new(),
@@ -176,8 +177,15 @@ impl Peripheral {
176177
Self { shared: shared }
177178
}
178179

179-
pub(super) fn update_name(&self, name: &str) {
180-
self.shared.properties.lock().unwrap().local_name = Some(name.to_string());
180+
pub(super) fn update_name(
181+
&self,
182+
local_name: Option<String>,
183+
advertisement_name: Option<String>,
184+
) {
185+
if let Ok(mut props) = self.shared.properties.lock() {
186+
props.local_name = local_name;
187+
props.advertisement_name = advertisement_name;
188+
}
181189
}
182190
}
183191

0 commit comments

Comments
 (0)