Skip to content

Commit cfac19e

Browse files
committed
Add DarwinCentral.name(for:)
1 parent 0903392 commit cfac19e

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Sources/DarwinGATT/DarwinCentral.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,25 @@ public final class DarwinCentral: CentralManager, ObservableObject {
318318
}
319319
}
320320

321+
/// Use this method to retrieve a human-readable name of the peripheral.
322+
///
323+
/// A peripheral may have two different name types: one that the device advertises and another that the device publishes in its database as its Bluetooth low energy Generic Access Profile (GAP) device name.
324+
/// If a peripheral has both types of names, this property returns its GAP device name.
325+
public func name(for peripheral: Peripheral) async throws -> String? {
326+
return try await withThrowingContinuation(for: peripheral) { [weak self] continuation in
327+
guard let self = self else { return }
328+
self.async {
329+
do {
330+
let name = try self._name(for: peripheral)
331+
continuation.resume(returning: name)
332+
}
333+
catch {
334+
continuation.resume(throwing: error)
335+
}
336+
}
337+
}
338+
}
339+
321340
// MARK - Private Methods
322341

323342
@inline(__always)
@@ -426,6 +445,15 @@ public final class DarwinCentral: CentralManager, ObservableObject {
426445
}
427446
return mtu
428447
}
448+
449+
private func _name(for peripheral: Peripheral) throws -> String? {
450+
// get peripheral
451+
guard let peripheralObject = self.cache.peripherals[peripheral] else {
452+
throw CentralError.unknownPeripheral
453+
}
454+
// return cached value
455+
return peripheralObject.name
456+
}
429457
}
430458

431459
// MARK: - Supporting Types

0 commit comments

Comments
 (0)