|
1 | 1 | import CoreBluetooth |
2 | 2 | import Foundation |
3 | 3 |
|
| 4 | +/// Various kinds of data that are advertised by peripherals and obtained by the ``CentralManager`` during scanning. |
4 | 5 | public struct AdvertisementData { |
5 | 6 | let dictionary: [String: Any] |
6 | 7 |
|
| 8 | + /// Initializes the advertisement data with the given dictionary, ideally obtained from `CoreBluetooth` itself. |
| 9 | + /// - Parameter dictionary: The advertisement data dictionary that backs all the properties of this type. |
7 | 10 | public init(_ dictionary: [String: Any] = [:]) { |
8 | 11 | self.dictionary = dictionary |
9 | 12 | } |
10 | 13 |
|
| 14 | + /// Initializes the advertisement data with the given dictionary, ideally obtained from `CoreBluetooth` itself. |
| 15 | + /// - Parameter dictionary: The advertisement data dictionary that backs all the properties of this type. |
11 | 16 | public init(_ dictionary: [Key: Any]) { |
12 | 17 | self.dictionary = Dictionary(uniqueKeysWithValues: dictionary.map({ (key, value) in |
13 | 18 | (key.rawValue, value) |
14 | 19 | })) |
15 | 20 | } |
16 | 21 |
|
| 22 | + /// The local name of a peripheral |
17 | 23 | public var localName: String? { |
18 | 24 | dictionary[Key.localName.rawValue] as? String |
19 | 25 | } |
20 | 26 |
|
| 27 | + /// The transmit power of a peripheral. |
21 | 28 | public var txPowerLevel: Double? { |
22 | 29 | (dictionary[Key.txPowerLevel.rawValue] as? NSNumber)?.doubleValue |
23 | 30 | } |
24 | 31 |
|
| 32 | + /// An array of advertised service UUIDs of a peripheral |
25 | 33 | public var serviceUUIDs: [CBUUID]? { |
26 | 34 | dictionary[Key.serviceUUIDs.rawValue] as? [CBUUID] |
27 | 35 | } |
28 | 36 |
|
| 37 | + /// A dictionary that contains service-specific advertisement data. |
29 | 38 | public var serviceData: [CBUUID: Data]? { |
30 | 39 | dictionary[Key.serviceData.rawValue] as? [CBUUID: Data] |
31 | 40 | } |
32 | 41 |
|
| 42 | + /// Manufacturer data of a peripheral |
33 | 43 | public var manufacturerData: Data? { |
34 | 44 | dictionary[Key.manufacturerData.rawValue] as? Data |
35 | 45 | } |
36 | 46 |
|
| 47 | + /// An array of UUIDs found in the overflow area of the advertisement data. |
37 | 48 | public var overflowServiceUUIDs: [CBUUID]? { |
38 | 49 | dictionary[Key.overflowServiceUUIDs.rawValue] as? [CBUUID] |
39 | 50 | } |
40 | 51 |
|
| 52 | + /// A Boolean value that indicates whether the advertising event type is connectable. |
41 | 53 | public var isConnectable: Bool? { |
42 | 54 | (dictionary[Key.isConnectable.rawValue] as? NSNumber)?.boolValue |
43 | 55 | } |
44 | 56 |
|
| 57 | + // An array of solicited service UUIDs. |
45 | 58 | public var solicitedServiceUUIDs: [CBUUID]? { |
46 | 59 | dictionary[Key.solicitedServiceUUIDs.rawValue] as? [CBUUID] |
47 | 60 | } |
48 | 61 |
|
| 62 | + /// Keys that may reference data in the ``AdvertisementData`` obtained by searching for peripherals. |
49 | 63 | public struct Key: Equatable, Hashable { |
50 | 64 | public let rawValue: String |
51 | 65 |
|
52 | 66 | private init(_ rawValue: String) { |
53 | 67 | self.rawValue = rawValue |
54 | 68 | } |
55 | 69 |
|
| 70 | + /// Key referencing the local name of a peripheral. Wrapper around `CBAdvertisementDataLocalNameKey` in `CoreBluetooth` |
56 | 71 | public static let localName: Key = .init(CBAdvertisementDataLocalNameKey) |
| 72 | + /// Key referencing the transmit power of a peripheral. Wrapper around `CBAdvertisementDataTxPowerLevelKey` in `CoreBluetooth` |
57 | 73 | public static let txPowerLevel: Key = .init(CBAdvertisementDataTxPowerLevelKey) |
| 74 | + /// Key referencing an array of advertised service UUIDs. Wrapper around `CBAdvertisementDataServiceUUIDsKey` in `CoreBluetooth` |
58 | 75 | public static let serviceUUIDs: Key = .init(CBAdvertisementDataServiceUUIDsKey) |
| 76 | + /// Key referencing a dictionary that contains service-specific advertisement data. Wrapper around `CBAdvertisementDataServiceDataKey` in `CoreBluetooth` |
59 | 77 | public static let serviceData: Key = .init(CBAdvertisementDataServiceDataKey) |
| 78 | + /// Key referencing the manufacturer data of a peripheral. Wrapper around `CBAdvertisementDataManufacturerDataKey` in `CoreBluetooth` |
60 | 79 | public static let manufacturerData: Key = .init(CBAdvertisementDataManufacturerDataKey) |
| 80 | + /// Key referencing an array of UUIDs found in the overflow area of the advertisement data. Wrapper around `CBAdvertisementDataOverflowServiceUUIDsKey` in `CoreBluetooth` |
61 | 81 | public static let overflowServiceUUIDs: Key = .init(CBAdvertisementDataOverflowServiceUUIDsKey) |
| 82 | + /// Key referencing a boolean value that indicates whether the advertising event type is connectable. Wrapper around `CBAdvertisementDataIsConnectable` in `CoreBluetooth` |
62 | 83 | public static let isConnectable: Key = .init(CBAdvertisementDataIsConnectable) |
| 84 | + /// Key referencing an array of solicited service UUIDs. Wrapper around `CBAdvertisementDataSolicitedServiceUUIDsKey` in `CoreBluetooth` |
63 | 85 | public static let solicitedServiceUUIDs: Key = .init(CBAdvertisementDataSolicitedServiceUUIDsKey) |
64 | 86 | } |
65 | 87 | } |
0 commit comments