react-native-ble-manager 12.2.2 → 12.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/ios/Helper.swift CHANGED
@@ -19,164 +19,196 @@ class Helper {
19
19
  return "unknown"
20
20
  }
21
21
  }
22
-
22
+
23
23
  static func dataToArrayBuffer(_ data: Data) -> [String: Any] {
24
24
  return [
25
25
  "CDVType": "ArrayBuffer",
26
26
  "data": data.base64EncodedString(options: []),
27
- "bytes": data.map { $0 }
27
+ "bytes": data.map { $0 },
28
28
  ]
29
29
  }
30
-
31
- static func reformatAdvertisementData(_ advertisementData: [String:Any]) -> [String:Any] {
30
+
31
+ static func reformatAdvertisementData(_ advertisementData: [String: Any])
32
+ -> [String: Any]
33
+ {
32
34
  var adv = advertisementData
33
35
  // Rename 'local name' key
34
36
  if let localName = adv[CBAdvertisementDataLocalNameKey] {
35
37
  adv.removeValue(forKey: CBAdvertisementDataLocalNameKey)
36
38
  adv["localName"] = localName
37
39
  }
38
-
40
+
39
41
  // Rename 'isConnectable' key
40
42
  if let isConnectable = adv[CBAdvertisementDataIsConnectable] {
41
43
  adv.removeValue(forKey: CBAdvertisementDataIsConnectable)
42
44
  adv["isConnectable"] = isConnectable
43
45
  }
44
-
46
+
45
47
  // Rename 'power level' key
46
48
  if let powerLevel = adv[CBAdvertisementDataTxPowerLevelKey] {
47
49
  adv.removeValue(forKey: CBAdvertisementDataTxPowerLevelKey)
48
50
  adv["txPowerLevel"] = powerLevel
49
51
  }
50
-
52
+
51
53
  // Service Data is a dictionary of CBUUID and NSData
52
54
  // Convert to String keys with Array Buffer values
53
- if let serviceData = adv[CBAdvertisementDataServiceDataKey] as? NSMutableDictionary {
55
+ if let serviceData = adv[CBAdvertisementDataServiceDataKey]
56
+ as? NSMutableDictionary
57
+ {
54
58
  for (key, value) in serviceData {
55
- if let uuidKey = key as? CBUUID, let dataValue = value as? Data {
59
+ if let uuidKey = key as? CBUUID, let dataValue = value as? Data
60
+ {
56
61
  serviceData.removeObject(forKey: uuidKey)
57
- serviceData[uuidKey.uuidString.lowercased()] = Helper.dataToArrayBuffer(dataValue)
62
+ serviceData[uuidKey.uuidString.lowercased()] =
63
+ Helper.dataToArrayBuffer(dataValue)
58
64
  }
59
65
  }
60
-
66
+
61
67
  adv.removeValue(forKey: CBAdvertisementDataServiceDataKey)
62
68
  adv["serviceData"] = serviceData
63
69
  }
64
-
70
+
65
71
  // Create a new list of Service UUIDs as Strings instead of CBUUIDs
66
- if let serviceUUIDs = adv[CBAdvertisementDataServiceUUIDsKey] as? NSMutableArray {
67
- var serviceUUIDStrings:Array<String> = []
72
+ if let serviceUUIDs = adv[CBAdvertisementDataServiceUUIDsKey]
73
+ as? NSMutableArray
74
+ {
75
+ var serviceUUIDStrings: [String] = []
68
76
  for value in serviceUUIDs {
69
77
  if let uuid = value as? CBUUID {
70
78
  serviceUUIDStrings.append(uuid.uuidString.lowercased())
71
79
  }
72
80
  }
73
-
81
+
74
82
  adv.removeValue(forKey: CBAdvertisementDataServiceUUIDsKey)
75
83
  adv["serviceUUIDs"] = serviceUUIDStrings
76
84
  }
77
85
  // Solicited Services UUIDs is an array of CBUUIDs, convert into Strings
78
- if let solicitiedServiceUUIDs = adv[CBAdvertisementDataSolicitedServiceUUIDsKey] as? NSMutableArray {
79
- var solicitiedServiceUUIDStrings:Array<String> = []
86
+ if let solicitiedServiceUUIDs = adv[
87
+ CBAdvertisementDataSolicitedServiceUUIDsKey
88
+ ] as? NSMutableArray {
89
+ var solicitiedServiceUUIDStrings: [String] = []
80
90
  for value in solicitiedServiceUUIDs {
81
91
  if let uuid = value as? CBUUID {
82
- solicitiedServiceUUIDStrings.append(uuid.uuidString.lowercased())
92
+ solicitiedServiceUUIDStrings.append(
93
+ uuid.uuidString.lowercased()
94
+ )
83
95
  }
84
96
  }
85
-
97
+
86
98
  adv.removeValue(forKey: CBAdvertisementDataSolicitedServiceUUIDsKey)
87
99
  adv["solicitedServiceUUIDs"] = solicitiedServiceUUIDStrings
88
100
  }
89
-
101
+
90
102
  // Overflow Services UUIDs is an array of CBUUIDs, convert into Strings
91
- if let overflowServiceUUIDs = adv[CBAdvertisementDataOverflowServiceUUIDsKey] as? NSMutableArray {
92
- var overflowServiceUUIDStrings:Array<String> = []
103
+ if let overflowServiceUUIDs = adv[
104
+ CBAdvertisementDataOverflowServiceUUIDsKey
105
+ ] as? NSMutableArray {
106
+ var overflowServiceUUIDStrings: [String] = []
93
107
  for value in overflowServiceUUIDs {
94
108
  if let uuid = value as? CBUUID {
95
- overflowServiceUUIDStrings.append(uuid.uuidString.lowercased())
109
+ overflowServiceUUIDStrings.append(
110
+ uuid.uuidString.lowercased()
111
+ )
96
112
  }
97
113
  }
98
-
114
+
99
115
  adv.removeValue(forKey: CBAdvertisementDataOverflowServiceUUIDsKey)
100
116
  adv["overflowServiceUUIDs"] = overflowServiceUUIDStrings
101
117
  }
102
-
118
+
103
119
  // Convert the manufacturer data
104
120
  if let mfgData = adv[CBAdvertisementDataManufacturerDataKey] as? Data {
105
121
  if mfgData.count > 1 {
106
122
  let manufactureID = UInt16(mfgData[0]) + UInt16(mfgData[1]) << 8
107
123
  var manInfo: [String: Any] = [:]
108
- manInfo[String(format: "%04x", manufactureID)] = Helper.dataToArrayBuffer(mfgData.subdata(in: 2..<mfgData.endIndex))
124
+ manInfo[String(format: "%04x", manufactureID)] =
125
+ Helper.dataToArrayBuffer(
126
+ mfgData.subdata(in: 2..<mfgData.endIndex)
127
+ )
109
128
  adv["manufacturerData"] = manInfo
110
129
  }
111
130
  adv.removeValue(forKey: CBAdvertisementDataManufacturerDataKey)
112
131
  adv["manufacturerRawData"] = Helper.dataToArrayBuffer(mfgData)
113
-
132
+
114
133
  }
115
-
134
+
116
135
  return adv
117
136
  }
118
-
119
- static func decodeCharacteristicProperties(_ p: CBCharacteristicProperties) -> Dictionary<String, Any> {
137
+
138
+ static func decodeCharacteristicProperties(_ p: CBCharacteristicProperties)
139
+ -> [String: Any]
140
+ {
120
141
  var props: [String: Any] = [:]
121
-
142
+
122
143
  // NOTE: props strings need to be consistent across iOS and Android
123
144
  if p.contains(.broadcast) {
124
145
  props["Broadcast"] = "Broadcast"
125
146
  }
126
-
147
+
127
148
  if p.contains(.read) {
128
149
  props["Read"] = "Read"
129
150
  }
130
-
151
+
131
152
  if p.contains(.writeWithoutResponse) {
132
153
  props["WriteWithoutResponse"] = "WriteWithoutResponse"
133
154
  }
134
-
155
+
135
156
  if p.contains(.write) {
136
157
  props["Write"] = "Write"
137
158
  }
138
-
159
+
139
160
  if p.contains(.notify) {
140
161
  props["Notify"] = "Notify"
141
162
  }
142
-
163
+
143
164
  if p.contains(.indicate) {
144
165
  props["Indicate"] = "Indicate"
145
166
  }
146
-
167
+
147
168
  if p.contains(.authenticatedSignedWrites) {
148
169
  props["AuthenticateSignedWrites"] = "AuthenticateSignedWrites"
149
170
  }
150
-
171
+
151
172
  if p.contains(.extendedProperties) {
152
173
  props["ExtendedProperties"] = "ExtendedProperties"
153
174
  }
154
-
175
+
155
176
  if p.contains(.notifyEncryptionRequired) {
156
177
  props["NotifyEncryptionRequired"] = "NotifyEncryptionRequired"
157
178
  }
158
-
179
+
159
180
  if p.contains(.indicateEncryptionRequired) {
160
181
  props["IndicateEncryptionRequired"] = "IndicateEncryptionRequired"
161
182
  }
162
-
183
+
163
184
  return props
164
185
  }
165
-
166
- static func key(forPeripheral peripheral: CBPeripheral,
167
- andCharacteristic characteristic: CBCharacteristic) -> String {
168
- return "\(String(describing: peripheral.uuidAsString()))|\(characteristic.uuid)"
186
+
187
+ static func key(
188
+ forPeripheral peripheral: CBPeripheral,
189
+ andCharacteristic characteristic: CBCharacteristic
190
+ ) -> String {
191
+ return
192
+ "\(String(describing: peripheral.uuidAsString()))|\(characteristic.uuid)"
169
193
  }
170
-
171
- static func key(forPeripheral peripheral: CBPeripheral,
172
- andCharacteristic characteristic: CBCharacteristic,
173
- andDescriptor descriptor: CBDescriptor) -> String {
174
- return "\(String(describing: peripheral.uuidAsString()))|\(characteristic.uuid)|\(descriptor.uuid)"
194
+
195
+ static func key(
196
+ forPeripheral peripheral: CBPeripheral,
197
+ andCharacteristic characteristic: CBCharacteristic,
198
+ andDescriptor descriptor: CBDescriptor
199
+ ) -> String {
200
+ return
201
+ "\(String(describing: peripheral.uuidAsString()))|\(characteristic.uuid)|\(descriptor.uuid)"
175
202
  }
176
-
177
- static func findDescriptor(fromUUID UUID: CBUUID, characteristic: CBCharacteristic) -> CBDescriptor? {
203
+
204
+ static func findDescriptor(
205
+ fromUUID UUID: CBUUID,
206
+ characteristic: CBCharacteristic
207
+ ) -> CBDescriptor? {
178
208
  if SwiftBleManager.verboseLogging {
179
- NSLog("Looking for descriptor \(UUID) on characteristic \(characteristic.uuid)")
209
+ NSLog(
210
+ "Looking for descriptor \(UUID) on characteristic \(characteristic.uuid)"
211
+ )
180
212
  }
181
213
  for descriptor in characteristic.descriptors ?? [] {
182
214
  if descriptor.uuid.isEqual(UUID) {
@@ -186,38 +218,48 @@ class Helper {
186
218
  return descriptor
187
219
  }
188
220
  }
189
- return nil // Descriptor not found on this characteristic
221
+ return nil // Descriptor not found on this characteristic
190
222
  }
191
-
223
+
192
224
  // Find a service in a peripheral
193
- static func findService(fromUUID UUID: CBUUID, peripheral p: CBPeripheral) -> CBService? {
225
+ static func findService(fromUUID UUID: CBUUID, peripheral p: CBPeripheral)
226
+ -> CBService?
227
+ {
194
228
  for service in p.services ?? [] {
195
229
  if service.uuid.isEqual(UUID) {
196
230
  return service
197
231
  }
198
232
  }
199
-
200
- return nil // Service not found on this peripheral
233
+
234
+ return nil // Service not found on this peripheral
201
235
  }
202
-
236
+
203
237
  // Find a characteristic in service with a specific property
204
- static func findCharacteristic(fromUUID UUID: CBUUID, service: CBService, prop: CBCharacteristicProperties) -> CBCharacteristic? {
238
+ static func findCharacteristic(
239
+ fromUUID UUID: CBUUID,
240
+ service: CBService,
241
+ prop: CBCharacteristicProperties
242
+ ) -> CBCharacteristic? {
205
243
  if SwiftBleManager.verboseLogging {
206
244
  NSLog("Looking for \(UUID) with properties \(prop.rawValue)")
207
245
  }
208
246
  for characteristic in service.characteristics ?? [] {
209
- if (characteristic.properties.contains(prop) && characteristic.uuid.isEqual(UUID)) {
247
+ if characteristic.properties.contains(prop)
248
+ && characteristic.uuid.isEqual(UUID)
249
+ {
210
250
  if SwiftBleManager.verboseLogging {
211
251
  NSLog("Found \(UUID)")
212
252
  }
213
253
  return characteristic
214
254
  }
215
255
  }
216
- return nil // Characteristic with prop not found on this service
256
+ return nil // Characteristic with prop not found on this service
217
257
  }
218
-
258
+
219
259
  // Find a characteristic in service by UUID
220
- static func findCharacteristic(fromUUID UUID: CBUUID, service: CBService) -> CBCharacteristic? {
260
+ static func findCharacteristic(fromUUID UUID: CBUUID, service: CBService)
261
+ -> CBCharacteristic?
262
+ {
221
263
  if SwiftBleManager.verboseLogging {
222
264
  NSLog("Looking for \(UUID)")
223
265
  }
@@ -229,105 +271,117 @@ class Helper {
229
271
  return characteristic
230
272
  }
231
273
  }
232
- return nil // Characteristic not found on this service
274
+ return nil // Characteristic not found on this service
233
275
  }
234
-
235
-
276
+
236
277
  }
237
278
 
238
- class Peripheral:Hashable {
279
+ class Peripheral: Hashable {
239
280
  var instance: CBPeripheral
240
281
  var rssi: NSNumber?
241
- var advertisementData: [String:Any]?
242
-
243
- init(peripheral: CBPeripheral, rssi: NSNumber? = nil, advertisementData: [String:Any]? = nil) {
282
+ var advertisementData: [String: Any]?
283
+
284
+ init(
285
+ peripheral: CBPeripheral,
286
+ rssi: NSNumber? = nil,
287
+ advertisementData: [String: Any]? = nil
288
+ ) {
244
289
  self.instance = peripheral
245
290
  self.rssi = rssi
246
291
  self.advertisementData = advertisementData
247
292
  }
248
-
293
+
249
294
  func setRSSI(_ newRSSI: NSNumber?) {
250
295
  self.rssi = newRSSI
251
296
  }
252
-
253
- func setAdvertisementData(_ advertisementData: [String:Any]) {
297
+
298
+ func setAdvertisementData(_ advertisementData: [String: Any]) {
254
299
  self.advertisementData = advertisementData
255
300
  }
256
-
257
- func advertisingInfo() -> Dictionary<String, Any> {
301
+
302
+ func advertisingInfo() -> [String: Any] {
258
303
  var peripheralInfo: [String: Any] = [:]
259
-
304
+
260
305
  peripheralInfo["name"] = instance.name
261
306
  peripheralInfo["id"] = instance.uuidAsString()
262
307
  peripheralInfo["rssi"] = self.rssi
263
308
  if let adv = self.advertisementData {
264
- peripheralInfo["advertising"] = Helper.reformatAdvertisementData(adv)
309
+ peripheralInfo["advertising"] = Helper.reformatAdvertisementData(
310
+ adv
311
+ )
265
312
  }
266
-
313
+
267
314
  return peripheralInfo
268
315
  }
269
-
270
- func servicesInfo() -> Dictionary<String, Any> {
316
+
317
+ func servicesInfo() -> [String: Any] {
271
318
  var servicesInfo: [String: Any] = advertisingInfo()
272
-
319
+
273
320
  var serviceList = [[String: Any]]()
274
321
  var characteristicList = [[String: Any]]()
275
-
322
+
276
323
  for service in instance.services ?? [] {
277
324
  var serviceDictionary = [String: Any]()
278
325
  serviceDictionary["uuid"] = service.uuid.uuidString.lowercased()
279
326
  serviceList.append(serviceDictionary)
280
-
327
+
281
328
  for characteristic in service.characteristics ?? [] {
282
329
  var characteristicDictionary = [String: Any]()
283
- characteristicDictionary["service"] = service.uuid.uuidString.lowercased()
284
- characteristicDictionary["characteristic"] = characteristic.uuid.uuidString.lowercased()
285
-
330
+ characteristicDictionary["service"] = service.uuid.uuidString
331
+ .lowercased()
332
+ characteristicDictionary["characteristic"] = characteristic.uuid
333
+ .uuidString.lowercased()
334
+
286
335
  if let value = characteristic.value, value.count > 0 {
287
- characteristicDictionary["value"] = Helper.dataToArrayBuffer(value)
336
+ characteristicDictionary["value"] =
337
+ Helper.dataToArrayBuffer(value)
288
338
  }
289
-
290
- characteristicDictionary["properties"] = Helper.decodeCharacteristicProperties(characteristic.properties)
291
-
292
- characteristicDictionary["isNotifying"] = characteristic.isNotifying
293
-
339
+
340
+ characteristicDictionary["properties"] =
341
+ Helper.decodeCharacteristicProperties(
342
+ characteristic.properties
343
+ )
344
+
345
+ characteristicDictionary["isNotifying"] =
346
+ characteristic.isNotifying
347
+
294
348
  var descriptorList = [[String: Any]]()
295
349
  for descriptor in characteristic.descriptors ?? [] {
296
350
  var descriptorDictionary = [String: Any]()
297
- descriptorDictionary["uuid"] = descriptor.uuid.uuidString.lowercased()
298
-
351
+ descriptorDictionary["uuid"] = descriptor.uuid.uuidString
352
+ .lowercased()
353
+
299
354
  if let value = descriptor.value {
300
355
  descriptorDictionary["value"] = value
301
356
  }
302
-
357
+
303
358
  descriptorList.append(descriptorDictionary)
304
359
  }
305
-
360
+
306
361
  if descriptorList.count > 0 {
307
362
  characteristicDictionary["descriptors"] = descriptorList
308
363
  }
309
-
364
+
310
365
  characteristicList.append(characteristicDictionary)
311
366
  }
312
367
  }
313
-
368
+
314
369
  servicesInfo["services"] = serviceList
315
370
  servicesInfo["characteristics"] = characteristicList
316
-
371
+
317
372
  return servicesInfo
318
373
  }
319
-
320
-
374
+
321
375
  static func == (lhs: Peripheral, rhs: Peripheral) -> Bool {
322
376
  return lhs.instance.uuidAsString() == rhs.instance.uuidAsString()
323
377
  }
324
-
378
+
325
379
  func hash(into hasher: inout Hasher) {
326
380
  hasher.combine(instance.uuidAsString())
327
381
  }
328
382
  }
329
383
 
330
- class BLECommandContext:NSObject {
384
+ class BLECommandContext: NSObject {
331
385
  var peripheral: Peripheral?
332
386
  var service: CBService?
333
387
  var characteristic: CBCharacteristic?
@@ -339,24 +393,26 @@ extension Data {
339
393
  if self.isEmpty {
340
394
  return ""
341
395
  }
342
- return map { "0x" + String(format: "%02hhx", $0) }.joined(separator: " ")
396
+ return map { "0x" + String(format: "%02hhx", $0) }.joined(
397
+ separator: " "
398
+ )
343
399
  }
344
-
400
+
345
401
  func toArray() -> [NSNumber] {
346
402
  /* Returns an array of NSNumber representing the hexadecimal values of the bytes in the data. Empty array if data is empty. */
347
-
403
+
348
404
  let dataBuffer = [UInt8](self)
349
-
405
+
350
406
  if dataBuffer.isEmpty {
351
407
  return []
352
408
  }
353
-
409
+
354
410
  return dataBuffer.map { NSNumber(value: $0) }
355
411
  }
356
412
  }
357
413
 
358
414
  extension CBPeripheral {
359
-
415
+
360
416
  func uuidAsString() -> String {
361
417
  return self.identifier.uuidString.lowercased()
362
418
  }
@@ -3,46 +3,45 @@ import Foundation
3
3
  class NotifyBufferContainer {
4
4
  public var items: Data
5
5
  public let capacity: Int
6
-
6
+
7
7
  public var count: Int {
8
8
  items.count
9
9
  }
10
-
10
+
11
11
  public var remaining: Int {
12
12
  capacity - items.count
13
13
  }
14
-
14
+
15
15
  public var isBufferFull: Bool {
16
16
  items.count >= capacity
17
17
  }
18
-
18
+
19
19
  init(size: Int) {
20
20
  self.capacity = size
21
21
  self.items = Data(capacity: size)
22
22
  }
23
-
23
+
24
24
  public func resetBuffer() {
25
25
  items.removeAll(keepingCapacity: true)
26
26
  }
27
-
27
+
28
28
  public func put(_ value: Data) -> Data {
29
29
  let remainingCapacity = self.remaining
30
30
  guard remainingCapacity > 0 else { return value }
31
-
31
+
32
32
  let restLength = value.count - remainingCapacity
33
-
33
+
34
34
  let toInsert: Data
35
35
  let rest: Data
36
36
  if restLength > 0 {
37
37
  toInsert = value.prefix(remainingCapacity)
38
38
  rest = value.suffix(restLength)
39
- }
40
- else {
39
+ } else {
41
40
  toInsert = value
42
41
  rest = Data()
43
42
  }
44
43
  items.append(toInsert)
45
-
44
+
46
45
  return rest
47
46
  }
48
- }
47
+ }