@@ -23,58 +23,53 @@ public struct AndroidLowEnergyAdvertisementData: Equatable {
2323 self . data = data
2424 }
2525}
26- /*
26+
2727extension AndroidLowEnergyAdvertisementData : AdvertisementData {
2828
29- /// Decode GAP data types.
30- private func decode() -> [GAPData] {
31-
32- var decoder = GAPDataDecoder()
33- decoder.ignoreUnknownType = true
34- return (try? decoder.decode(data)) ?? []
35- }
29+ #if canImport(FoundationEssentials)
30+ public typealias Data = FoundationEssentials . Data
31+ #elseif canImport(Foundation)
32+ public typealias Data = Foundation . Data
33+ #endif
3634
3735 /// The local name of a peripheral.
3836 public var localName : String ? {
3937
40- let decoded = decode()
41-
42- guard let name = decoded.compactMap({ $0 as? GAPCompleteLocalName }).first?.name
43- ?? decoded.compactMap({ $0 as? GAPShortLocalName }).first? .name
44- else { return nil }
45-
46- return name
38+ if let decoded = try ? GAPDataDecoder . decode ( GAPCompleteLocalName . self , from : data ) {
39+ return decoded . name
40+ } else if let decoded = try ? GAPDataDecoder . decode ( GAPShortLocalName . self , from : data ) {
41+ return decoded. name
42+ } else {
43+ return nil
44+ }
4745 }
4846
4947 /// The Manufacturer data of a peripheral.
50- public var manufacturerData: ManufacturerSpecificData? {
51-
52- let decoded = decode()
48+ public var manufacturerData : GAPManufacturerSpecificData < Data > ? {
5349
54- guard let value = decoded.compactMap({ $0 as? GAPManufacturerSpecificData }).first
50+ guard let value = try ? GAPDataDecoder . decode ( GAPManufacturerSpecificData< Data > . self , from : data )
5551 else { return nil }
5652
57- return value
53+ return ManufacturerSpecificData (
54+ companyIdentifier: value. companyIdentifier,
55+ additionalData: value. additionalData
56+ )
5857 }
5958
6059 /// Service-specific advertisement data.
6160 public var serviceData : [ BluetoothUUID : Data ] ? {
6261
63- let decoded = decode( )
62+ var serviceData = [ BluetoothUUID : Data ] ( minimumCapacity : 3 )
6463
65- guard decoded.isEmpty == false
66- else { return nil }
67-
68- var serviceData = [BluetoothUUID: Data](minimumCapacity: decoded.count)
69-
70- decoded.compactMap { $0 as? GAPServiceData16BitUUID }
71- .forEach { serviceData[.bit16($0.uuid)] = $0.serviceData }
72-
73- decoded.compactMap { $0 as? GAPServiceData32BitUUID }
74- .forEach { serviceData[.bit32($0.uuid)] = $0.serviceData }
75-
76- decoded.compactMap { $0 as? GAPServiceData128BitUUID }
77- .forEach { serviceData[.bit128(UInt128(uuid: $0.uuid))] = $0.serviceData }
64+ if let value = try ? GAPDataDecoder . decode ( GAPServiceData16BitUUID< Data> . self , from: data) {
65+ serviceData [ . bit16( value. uuid) ] = value. serviceData
66+ }
67+ if let value = try ? GAPDataDecoder . decode ( GAPServiceData32BitUUID< Data> . self , from: data) {
68+ serviceData [ . bit32( value. uuid) ] = value. serviceData
69+ }
70+ if let value = try ? GAPDataDecoder . decode ( GAPServiceData128BitUUID< Data> . self , from: data) {
71+ serviceData [ . bit128( UInt128 ( uuid: value. uuid) ) ] = value. serviceData
72+ }
7873
7974 guard serviceData. isEmpty == false
8075 else { return nil }
@@ -85,37 +80,27 @@ extension AndroidLowEnergyAdvertisementData: AdvertisementData {
8580 /// An array of service UUIDs
8681 public var serviceUUIDs : [ BluetoothUUID ] ? {
8782
88- let decoded = decode()
89-
90- guard decoded.isEmpty == false
91- else { return nil }
92-
9383 var uuids = [ BluetoothUUID] ( )
94- uuids.reserveCapacity(decoded.count)
95-
96- uuids += decoded
97- .compactMap { $0 as? GAPCompleteListOf16BitServiceClassUUIDs }
98- .reduce([BluetoothUUID](), { $0 + $1.uuids.map { BluetoothUUID.bit16($0) } })
99-
100- uuids += decoded
101- .compactMap { $0 as? GAPIncompleteListOf16BitServiceClassUUIDs }
102- .reduce([BluetoothUUID](), { $0 + $1.uuids.map { BluetoothUUID.bit16($0) } })
103-
104- uuids += decoded
105- .compactMap { $0 as? GAPCompleteListOf32BitServiceClassUUIDs }
106- .reduce([BluetoothUUID](), { $0 + $1.uuids.map { BluetoothUUID.bit32($0) } })
107-
108- uuids += decoded
109- .compactMap { $0 as? GAPIncompleteListOf32BitServiceClassUUIDs }
110- .reduce([BluetoothUUID](), { $0 + $1.uuids.map { BluetoothUUID.bit32($0) } })
111-
112- uuids += decoded
113- .compactMap { $0 as? GAPCompleteListOf128BitServiceClassUUIDs }
114- .reduce([BluetoothUUID](), { $0 + $1.uuids.map { BluetoothUUID(uuid: $0) } })
115-
116- uuids += decoded
117- .compactMap { $0 as? GAPIncompleteListOf128BitServiceClassUUIDs }
118- .reduce([BluetoothUUID](), { $0 + $1.uuids.map { BluetoothUUID(uuid: $0) } })
84+ uuids. reserveCapacity ( 2 )
85+
86+ if let value = try ? GAPDataDecoder . decode ( GAPCompleteListOf16BitServiceClassUUIDs . self, from: data) {
87+ uuids += value. uuids. map { . bit16( $0) }
88+ }
89+ if let value = try ? GAPDataDecoder . decode ( GAPIncompleteListOf16BitServiceClassUUIDs . self, from: data) {
90+ uuids += value. uuids. map { . bit16( $0) }
91+ }
92+ if let value = try ? GAPDataDecoder . decode ( GAPIncompleteListOf32BitServiceClassUUIDs . self, from: data) {
93+ uuids += value. uuids. map { . bit32( $0) }
94+ }
95+ if let value = try ? GAPDataDecoder . decode ( GAPIncompleteListOf32BitServiceClassUUIDs . self, from: data) {
96+ uuids += value. uuids. map { . bit32( $0) }
97+ }
98+ if let value = try ? GAPDataDecoder . decode ( GAPCompleteListOf128BitServiceClassUUIDs . self, from: data) {
99+ uuids += value. uuids. map { . init( uuid: $0) }
100+ }
101+ if let value = try ? GAPDataDecoder . decode ( GAPIncompleteListOf128BitServiceClassUUIDs . self, from: data) {
102+ uuids += value. uuids. map { . init( uuid: $0) }
103+ }
119104
120105 guard uuids. isEmpty == false
121106 else { return nil }
@@ -127,38 +112,31 @@ extension AndroidLowEnergyAdvertisementData: AdvertisementData {
127112 /// Using the RSSI value and the Tx power level, it is possible to calculate path loss.
128113 public var txPowerLevel : Double ? {
129114
130- let decoded = decode()
131-
132- guard let gapData = decoded.compactMap({ $0 as? GAPTxPowerLevel }).first
115+ guard let value = try ? GAPDataDecoder . decode ( GAPTxPowerLevel . self, from: data)
133116 else { return nil }
134117
135- return Double(gapData .powerLevel)
118+ return Double ( value . powerLevel)
136119 }
137120
138121 /// An array of one or more `BluetoothUUID`, representing Service UUIDs.
139122 public var solicitedServiceUUIDs : [ BluetoothUUID ] ? {
140123
141- let decoded = decode()
142-
143- guard decoded.isEmpty == false
144- else { return nil }
145-
146124 var uuids = [ BluetoothUUID] ( )
147- uuids.reserveCapacity(decoded.count)
148-
149- decoded.compactMap { $0 as? GAPListOf16BitServiceSolicitationUUIDs }
150- .forEach { $0.uuids.forEach { uuids.append(.bit16($0)) } }
151-
152- decoded.compactMap { $0 as? GAPListOf32BitServiceSolicitationUUIDs }
153- .forEach { $0.uuids.forEach { uuids.append(.bit32($0)) } }
154-
155- decoded.compactMap { $0 as? GAPListOf128BitServiceSolicitationUUIDs }
156- .forEach { $0.uuids.forEach { uuids.append(.bit128(UInt128(uuid: $0))) } }
125+ uuids. reserveCapacity ( 2 )
126+
127+ if let value = try ? GAPDataDecoder . decode ( GAPListOf16BitServiceSolicitationUUIDs . self, from: data) {
128+ uuids += value. uuids. map { . bit16( $0) }
129+ }
130+ if let value = try ? GAPDataDecoder . decode ( GAPListOf32BitServiceSolicitationUUIDs . self, from: data) {
131+ uuids += value. uuids. map { . bit32( $0) }
132+ }
133+ if let value = try ? GAPDataDecoder . decode ( GAPListOf128BitServiceSolicitationUUIDs . self, from: data) {
134+ uuids += value. uuids. map { . init( uuid: $0) }
135+ }
157136
158137 guard uuids. isEmpty == false
159138 else { return nil }
160139
161140 return uuids
162141 }
163142}
164- */
0 commit comments