Skip to content

Commit

Permalink
Add DarwinCentral.name(for:)
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Mar 26, 2023
1 parent 0903392 commit cfac19e
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Sources/DarwinGATT/DarwinCentral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,25 @@ public final class DarwinCentral: CentralManager, ObservableObject {
}
}

/// Use this method to retrieve a human-readable name of the peripheral.
///
/// 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.
/// If a peripheral has both types of names, this property returns its GAP device name.
public func name(for peripheral: Peripheral) async throws -> String? {
return try await withThrowingContinuation(for: peripheral) { [weak self] continuation in
guard let self = self else { return }
self.async {
do {
let name = try self._name(for: peripheral)
continuation.resume(returning: name)
}
catch {
continuation.resume(throwing: error)
}
}
}
}

// MARK - Private Methods

@inline(__always)
Expand Down Expand Up @@ -426,6 +445,15 @@ public final class DarwinCentral: CentralManager, ObservableObject {
}
return mtu
}

private func _name(for peripheral: Peripheral) throws -> String? {
// get peripheral
guard let peripheralObject = self.cache.peripherals[peripheral] else {
throw CentralError.unknownPeripheral
}
// return cached value
return peripheralObject.name
}
}

// MARK: - Supporting Types
Expand Down

0 comments on commit cfac19e

Please sign in to comment.