Skip to content

Commit

Permalink
fix: make local_name in corebluetooth more descriptive
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyoyuppe committed Jun 6, 2024
1 parent eb27a10 commit f5e6d76
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
21 changes: 17 additions & 4 deletions src/corebluetooth/central_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@
// This file may not be copied, modified, or distributed except
// according to those terms.

use super::utils::nsstring_to_string;
use super::utils::{core_bluetooth::cbuuid_to_uuid, nsuuid_to_uuid};
use futures::channel::mpsc::Sender;
use futures::sink::SinkExt;
use log::{error, trace};
use objc2::runtime::{AnyObject, ProtocolObject};
use objc2::{declare_class, msg_send_id, mutability, rc::Retained, ClassType, DeclaredClass};
use objc2_core_bluetooth::{
CBAdvertisementDataManufacturerDataKey, CBAdvertisementDataServiceDataKey,
CBAdvertisementDataServiceUUIDsKey, CBCentralManager, CBCentralManagerDelegate,
CBCharacteristic, CBDescriptor, CBPeripheral, CBPeripheralDelegate, CBService, CBUUID,
CBAdvertisementDataLocalNameKey, CBAdvertisementDataManufacturerDataKey,
CBAdvertisementDataServiceDataKey, CBAdvertisementDataServiceUUIDsKey, CBCentralManager,
CBCentralManagerDelegate, CBCharacteristic, CBDescriptor, CBPeripheral, CBPeripheralDelegate,
CBService, CBUUID,
};
use objc2_foundation::{
NSArray, NSData, NSDictionary, NSError, NSNumber, NSObject, NSObjectProtocol, NSString,
Expand All @@ -42,6 +44,7 @@ pub enum CentralDelegateEvent {
DidUpdateState,
DiscoveredPeripheral {
cbperipheral: Retained<CBPeripheral>,
local_name: Option<String>,
},
DiscoveredServices {
peripheral_uuid: Uuid,
Expand Down Expand Up @@ -126,9 +129,13 @@ impl Debug for CentralDelegateEvent {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
CentralDelegateEvent::DidUpdateState => f.debug_tuple("DidUpdateState").finish(),
CentralDelegateEvent::DiscoveredPeripheral { cbperipheral } => f
CentralDelegateEvent::DiscoveredPeripheral {
cbperipheral,
local_name,
} => f
.debug_struct("CentralDelegateEvent")
.field("cbperipheral", cbperipheral.deref())
.field("local_name", local_name)
.finish(),
CentralDelegateEvent::DiscoveredServices {
peripheral_uuid,
Expand Down Expand Up @@ -371,8 +378,14 @@ declare_class!(
peripheral_debug(peripheral)
);

let local_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,
});

let rssi_value = rssi.as_i16();
Expand Down
23 changes: 19 additions & 4 deletions src/corebluetooth/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,24 @@ impl CoreBluetoothInternal {
}
}

async fn on_discovered_peripheral(&mut self, peripheral: Retained<CBPeripheral>) {
async fn on_discovered_peripheral(
&mut self,
peripheral: Retained<CBPeripheral>,
local_name: Option<String>,
) {
let uuid = nsuuid_to_uuid(unsafe { &peripheral.identifier() });
let name = unsafe { peripheral.name() };
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,
};

if self.peripherals.contains_key(&uuid) {
if let Some(name) = name {
self.dispatch_event(CoreBluetoothEvent::DeviceUpdated {
Expand Down Expand Up @@ -1045,8 +1060,8 @@ impl CoreBluetoothInternal {
CentralDelegateEvent::DidUpdateState => {
self.dispatch_event(CoreBluetoothEvent::AdapterConnected).await
}
CentralDelegateEvent::DiscoveredPeripheral{cbperipheral} => {
self.on_discovered_peripheral(cbperipheral).await
CentralDelegateEvent::DiscoveredPeripheral{cbperipheral, local_name} => {
self.on_discovered_peripheral(cbperipheral, local_name).await
}
CentralDelegateEvent::DiscoveredServices{peripheral_uuid, services} => {
self.on_discovered_services(peripheral_uuid, services)
Expand Down
11 changes: 10 additions & 1 deletion src/corebluetooth/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@
// This file may not be copied, modified, or distributed except
// according to those terms.

use objc2_foundation::NSUUID;
use std::ffi::CStr;

use objc2_foundation::{NSString, NSUUID};
use uuid::Uuid;

pub mod core_bluetooth;

pub fn nsuuid_to_uuid(uuid: &NSUUID) -> Uuid {
uuid.UUIDString().to_string().parse().unwrap()
}

pub unsafe fn nsstring_to_string(nsstring: *const NSString) -> Option<String> {
nsstring
.as_ref()
.and_then(|ns| CStr::from_ptr(ns.UTF8String()).to_str().ok())
.map(String::from)
}

0 comments on commit f5e6d76

Please sign in to comment.