diff --git a/src/api/mod.rs b/src/api/mod.rs index bb3acf68..3c5dc068 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -179,8 +179,10 @@ pub struct PeripheralProperties { pub address: BDAddr, /// The type of address (either random or public) pub address_type: Option, - /// The local name. This is generally a human-readable string that identifies the type of device. + /// The GAP local name. This is generally a human-readable string that identifies the type of device. pub local_name: Option, + /// The advertisement name. May be different than local_name. + pub advertisement_name: Option, /// The transmission power level for the device pub tx_power_level: Option, /// The most recent Received Signal Strength Indicator for the device diff --git a/src/bluez/peripheral.rs b/src/bluez/peripheral.rs index d2803bd7..17513c1d 100644 --- a/src/bluez/peripheral.rs +++ b/src/bluez/peripheral.rs @@ -144,6 +144,7 @@ impl api::Peripheral for Peripheral { address: device_info.mac_address.into(), address_type: Some(device_info.address_type.into()), local_name: device_info.name, + advertisement_name: None, tx_power_level: device_info.tx_power, rssi: device_info.rssi, manufacturer_data: device_info.manufacturer_data, diff --git a/src/corebluetooth/adapter.rs b/src/corebluetooth/adapter.rs index b8626bd1..7f8fa5b6 100644 --- a/src/corebluetooth/adapter.rs +++ b/src/corebluetooth/adapter.rs @@ -57,22 +57,28 @@ impl Adapter { match msg { CoreBluetoothEvent::DeviceDiscovered { uuid, - name, + local_name, + advertisement_name, event_receiver, } => { manager_clone.add_peripheral(Peripheral::new( uuid, - name, + local_name, + advertisement_name, Arc::downgrade(&manager_clone), event_receiver, adapter_sender_clone.clone(), )); manager_clone.emit(CentralEvent::DeviceDiscovered(uuid.into())); } - CoreBluetoothEvent::DeviceUpdated { uuid, name } => { + CoreBluetoothEvent::DeviceUpdated { + uuid, + local_name, + advertisement_name, + } => { let id = uuid.into(); if let Some(entry) = manager_clone.peripheral_mut(&id) { - entry.value().update_name(&name); + entry.value().update_name(local_name, advertisement_name); manager_clone.emit(CentralEvent::DeviceUpdated(id)); } } diff --git a/src/corebluetooth/central_delegate.rs b/src/corebluetooth/central_delegate.rs index d7651e10..152f5d32 100644 --- a/src/corebluetooth/central_delegate.rs +++ b/src/corebluetooth/central_delegate.rs @@ -46,7 +46,7 @@ pub enum CentralDelegateEvent { }, DiscoveredPeripheral { cbperipheral: Retained, - local_name: Option, + advertisement_name: Option, }, DiscoveredServices { peripheral_uuid: Uuid, @@ -136,11 +136,11 @@ impl Debug for CentralDelegateEvent { .finish(), CentralDelegateEvent::DiscoveredPeripheral { cbperipheral, - local_name, + advertisement_name, } => f .debug_struct("CentralDelegateEvent") .field("cbperipheral", cbperipheral.deref()) - .field("local_name", local_name) + .field("advertisement_name", advertisement_name) .finish(), CentralDelegateEvent::DiscoveredServices { peripheral_uuid, @@ -385,14 +385,14 @@ declare_class!( peripheral_debug(peripheral) ); - let local_name = adv_data + let advertisement_name = adv_data .get(unsafe { CBAdvertisementDataLocalNameKey }) .map(|name| (name as *const AnyObject as *const NSString)) .and_then(|name| unsafe { nsstring_to_string(name) }); self.send_event(CentralDelegateEvent::DiscoveredPeripheral { cbperipheral: peripheral.retain(), - local_name, + advertisement_name, }); let rssi_value = rssi.as_i16(); diff --git a/src/corebluetooth/internal.rs b/src/corebluetooth/internal.rs index 93597bc7..1ace3512 100644 --- a/src/corebluetooth/internal.rs +++ b/src/corebluetooth/internal.rs @@ -461,12 +461,14 @@ pub enum CoreBluetoothEvent { }, DeviceDiscovered { uuid: Uuid, - name: Option, + local_name: Option, + advertisement_name: Option, event_receiver: Receiver, }, DeviceUpdated { uuid: Uuid, - name: String, + local_name: Option, + advertisement_name: Option, }, DeviceDisconnected { uuid: Uuid, @@ -569,26 +571,20 @@ impl CoreBluetoothInternal { async fn on_discovered_peripheral( &mut self, peripheral: Retained, - local_name: Option, + advertisement_name: Option, ) { let uuid = nsuuid_to_uuid(unsafe { &peripheral.identifier() }); let peripheral_name = unsafe { peripheral.name() }; - - let name = match (peripheral_name.map(|n| n.to_string()), local_name) { - (Some(p_name), Some(l_name)) if p_name != l_name => { - Some(format!("{p_name} [{l_name}]")) - } - (Some(p_name), Some(_)) => Some(p_name), - (Some(p_name), None) => Some(p_name), - (None, Some(l_name)) => Some(l_name), - (None, None) => None, - }; + let local_name = peripheral_name + .map(|n| n.to_string()) + .or(advertisement_name.clone()); if self.peripherals.contains_key(&uuid) { - if let Some(name) = name { + if local_name.is_some() { self.dispatch_event(CoreBluetoothEvent::DeviceUpdated { uuid, - name: name.to_string(), + local_name, + advertisement_name, }) .await; } @@ -599,7 +595,8 @@ impl CoreBluetoothInternal { .insert(uuid, PeripheralInternal::new(peripheral, event_sender)); self.dispatch_event(CoreBluetoothEvent::DeviceDiscovered { uuid, - name: name.map(|name| name.to_string()), + local_name, + advertisement_name, event_receiver, }) .await; @@ -1095,8 +1092,8 @@ impl CoreBluetoothInternal { CentralDelegateEvent::DidUpdateState{state} => { self.dispatch_event(CoreBluetoothEvent::DidUpdateState{state}).await } - CentralDelegateEvent::DiscoveredPeripheral{cbperipheral, local_name} => { - self.on_discovered_peripheral(cbperipheral, local_name).await + CentralDelegateEvent::DiscoveredPeripheral{cbperipheral, advertisement_name} => { + self.on_discovered_peripheral(cbperipheral, advertisement_name).await } CentralDelegateEvent::DiscoveredServices{peripheral_uuid, services} => { self.on_discovered_services(peripheral_uuid, services) diff --git a/src/corebluetooth/peripheral.rs b/src/corebluetooth/peripheral.rs index 5ef24288..60955f50 100644 --- a/src/corebluetooth/peripheral.rs +++ b/src/corebluetooth/peripheral.rs @@ -84,6 +84,7 @@ impl Peripheral { pub(crate) fn new( uuid: Uuid, local_name: Option, + advertisement_name: Option, manager: Weak>, event_receiver: Receiver, message_sender: Sender, @@ -94,6 +95,7 @@ impl Peripheral { address: BDAddr::default(), address_type: None, local_name, + advertisement_name, tx_power_level: None, rssi: None, manufacturer_data: HashMap::new(), @@ -171,8 +173,15 @@ impl Peripheral { Self { shared: shared } } - pub(super) fn update_name(&self, name: &str) { - self.shared.properties.lock().unwrap().local_name = Some(name.to_string()); + pub(super) fn update_name( + &self, + local_name: Option, + advertisement_name: Option, + ) { + if let Ok(mut props) = self.shared.properties.lock() { + props.local_name = local_name; + props.advertisement_name = advertisement_name; + } } } diff --git a/src/droidplug/jni/objects.rs b/src/droidplug/jni/objects.rs index 8e4a688d..3f34f38e 100644 --- a/src/droidplug/jni/objects.rs +++ b/src/droidplug/jni/objects.rs @@ -733,6 +733,7 @@ impl<'a: 'b, 'b> TryFrom> for (BDAddr, Option