@iotize/device-com-ble.cordova 3.0.0 → 3.3.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.
Files changed (145) hide show
  1. package/README.md +174 -177
  2. package/bundles/iotize-device-com-ble.cordova.umd.js +1113 -129
  3. package/bundles/iotize-device-com-ble.cordova.umd.js.map +1 -1
  4. package/bundles/iotize-device-com-ble.cordova.umd.min.js +15 -1
  5. package/bundles/iotize-device-com-ble.cordova.umd.min.js.map +1 -1
  6. package/esm2015/iotize-device-com-ble.cordova.js +5 -1
  7. package/esm2015/iotize-device-com-ble.cordova.js.map +1 -0
  8. package/esm2015/iotize-device-com-ble.cordova.metadata.json +1 -0
  9. package/esm2015/iotize-device-com-ble.cordova.ngsummary.json +1 -1
  10. package/esm2015/lib/ble-com-protocol.js +68 -0
  11. package/esm2015/lib/ble-com-protocol.js.map +1 -0
  12. package/esm2015/lib/ble-com-protocol.metadata.json +1 -0
  13. package/esm2015/lib/ble-com-protocol.ngsummary.json +1 -0
  14. package/esm2015/lib/cordova-ble-error.js +36 -0
  15. package/esm2015/lib/cordova-ble-error.js.map +1 -0
  16. package/esm2015/lib/cordova-ble-error.metadata.json +1 -0
  17. package/esm2015/lib/cordova-ble-error.ngsummary.json +1 -0
  18. package/esm2015/lib/cordova-interface.js +2 -0
  19. package/esm2015/lib/cordova-interface.js.map +1 -0
  20. package/esm2015/lib/cordova-interface.metadata.json +1 -0
  21. package/esm2015/lib/cordova-interface.ngsummary.json +1 -0
  22. package/esm2015/lib/cordova-service-adapter.js +214 -0
  23. package/esm2015/lib/cordova-service-adapter.js.map +1 -0
  24. package/esm2015/lib/cordova-service-adapter.metadata.json +1 -0
  25. package/esm2015/lib/cordova-service-adapter.ngsummary.json +1 -0
  26. package/esm2015/lib/definitions.js +2 -0
  27. package/esm2015/lib/definitions.js.map +1 -0
  28. package/esm2015/lib/definitions.metadata.json +1 -0
  29. package/esm2015/lib/definitions.ngsummary.json +1 -0
  30. package/esm2015/lib/iotize-ble-cordova-plugin.js +170 -0
  31. package/esm2015/lib/iotize-ble-cordova-plugin.js.map +1 -0
  32. package/esm2015/lib/iotize-ble-cordova-plugin.metadata.json +1 -0
  33. package/esm2015/lib/iotize-ble-cordova-plugin.ngsummary.json +1 -0
  34. package/esm2015/lib/logger.js +3 -0
  35. package/esm2015/lib/logger.js.map +1 -0
  36. package/esm2015/lib/logger.metadata.json +1 -0
  37. package/esm2015/{www → lib}/logger.ngsummary.json +1 -1
  38. package/esm2015/lib/scanner.js +151 -0
  39. package/esm2015/lib/scanner.js.map +1 -0
  40. package/esm2015/lib/scanner.metadata.json +1 -0
  41. package/esm2015/lib/scanner.ngsummary.json +1 -0
  42. package/esm2015/lib/utility.js +8 -0
  43. package/esm2015/lib/utility.js.map +1 -0
  44. package/esm2015/lib/utility.metadata.json +1 -0
  45. package/esm2015/lib/utility.ngsummary.json +1 -0
  46. package/esm2015/public_api.js +5 -2
  47. package/esm2015/public_api.js.map +1 -0
  48. package/esm2015/public_api.metadata.json +1 -0
  49. package/esm2015/public_api.ngsummary.json +1 -1
  50. package/fesm2015/iotize-device-com-ble.cordova.js +543 -75
  51. package/fesm2015/iotize-device-com-ble.cordova.js.map +1 -1
  52. package/iotize-device-com-ble.cordova.d.ts +5 -0
  53. package/iotize-device-com-ble.cordova.metadata.json +1 -1
  54. package/lib/ble-com-protocol.d.ts +20 -0
  55. package/lib/cordova-ble-error.d.ts +29 -0
  56. package/lib/cordova-interface.d.ts +5 -0
  57. package/lib/cordova-service-adapter.d.ts +58 -0
  58. package/lib/definitions.d.ts +39 -0
  59. package/lib/iotize-ble-cordova-plugin.d.ts +27 -0
  60. package/lib/logger.d.ts +1 -0
  61. package/lib/scanner.d.ts +55 -0
  62. package/lib/utility.d.ts +2 -0
  63. package/package.json +10 -11
  64. package/plugin.xml +9 -11
  65. package/public_api.d.ts +5 -1
  66. package/src/android/.gradle/4.8.1/fileChanges/last-build.bin +0 -0
  67. package/src/android/.gradle/4.8.1/fileHashes/fileHashes.bin +0 -0
  68. package/src/android/.gradle/4.8.1/fileHashes/fileHashes.lock +0 -0
  69. package/src/android/.gradle/6.8.2/fileChanges/last-build.bin +0 -0
  70. package/src/android/.gradle/6.8.2/fileHashes/fileHashes.lock +0 -0
  71. package/src/android/.gradle/6.8.2/gc.properties +0 -0
  72. package/src/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
  73. package/src/android/.gradle/buildOutputCleanup/cache.properties +2 -0
  74. package/src/android/.gradle/checksums/checksums.lock +0 -0
  75. package/src/android/.gradle/configuration-cache/gc.properties +0 -0
  76. package/src/android/.gradle/vcs-1/gc.properties +0 -0
  77. package/src/android/.gradle/vcsWorkingDirs/gc.properties +0 -0
  78. package/src/android/.idea/android.iml +9 -0
  79. package/src/android/.idea/gradle.xml +16 -0
  80. package/src/android/.idea/modules.xml +8 -0
  81. package/src/android/.idea/vcs.xml +6 -0
  82. package/src/android/build.gradle +5 -4
  83. package/src/android/gradle/wrapper/gradle-wrapper.properties +1 -2
  84. package/src/android/src/ble/ArgsHelper.java +73 -0
  85. package/src/android/src/ble/BLECom.java +559 -0
  86. package/src/android/src/{BLEComError.java → ble/BLEComError.java} +22 -10
  87. package/src/android/src/{JSONBuilder.java → ble/JSONBuilder.java} +20 -5
  88. package/src/android/src/{PluginResponse.java → ble/PluginResponse.java} +51 -2
  89. package/src/android/src/ble/UUIDHelper.java +33 -0
  90. package/src/android/src/ble/models/CharacteristicDescription.java +39 -0
  91. package/src/android/src/ble/models/CharacteristicProperties.java +41 -0
  92. package/src/android/src/ble/models/DescriptorDescription.java +21 -0
  93. package/src/android/src/ble/models/RequestDeviceOptions.java +45 -0
  94. package/src/android/src/ble/models/ServiceDescription.java +51 -0
  95. package/src/ios/BLECom.swift +562 -124
  96. package/src/ios/BLEGenericPeripheral.swift +479 -0
  97. package/src/ios/BLEManager.swift +142 -47
  98. package/src/ios/{BLEPeripheral.swift → BLETapPeripheral.swift} +22 -85
  99. package/src/ios/CBPeripheralConverter.swift +86 -1
  100. package/src/ios/Queue.swift +71 -0
  101. package/src/windows/iotize-ble-com.js +159 -179
  102. package/www/plugin.js +2 -0
  103. package/www/plugin.js.LICENSE.txt +14 -0
  104. package/CHANGELOG.md +0 -42
  105. package/esm2015/www/ble-com-protocol.js +0 -75
  106. package/esm2015/www/ble-com-protocol.ngsummary.json +0 -1
  107. package/esm2015/www/cordova-interface.js +0 -1
  108. package/esm2015/www/cordova-interface.ngsummary.json +0 -1
  109. package/esm2015/www/definitions.js +0 -1
  110. package/esm2015/www/definitions.ngsummary.json +0 -1
  111. package/esm2015/www/index.js +0 -3
  112. package/esm2015/www/index.ngsummary.json +0 -1
  113. package/esm2015/www/logger.js +0 -3
  114. package/esm2015/www/scanner.js +0 -89
  115. package/esm2015/www/scanner.ngsummary.json +0 -1
  116. package/esm5/iotize-device-com-ble.cordova.js +0 -2
  117. package/esm5/public_api.js +0 -2
  118. package/esm5/www/ble-com-protocol.js +0 -121
  119. package/esm5/www/cordova-interface.js +0 -1
  120. package/esm5/www/definitions.js +0 -1
  121. package/esm5/www/index.js +0 -3
  122. package/esm5/www/logger.js +0 -3
  123. package/esm5/www/scanner.js +0 -111
  124. package/fesm5/iotize-device-com-ble.cordova.js +0 -233
  125. package/fesm5/iotize-device-com-ble.cordova.js.map +0 -1
  126. package/src/android/.project +0 -17
  127. package/src/android/.settings/org.eclipse.buildship.core.prefs +0 -13
  128. package/src/android/src/ArgsHelper.java +0 -30
  129. package/src/android/src/BLECom.java +0 -375
  130. package/src/public_api.ts +0 -1
  131. package/src/www/ble-com-protocol.spec.ts +0 -9
  132. package/src/www/ble-com-protocol.ts +0 -114
  133. package/src/www/cordova-interface.ts +0 -29
  134. package/src/www/definitions.ts +0 -11
  135. package/src/www/index.ts +0 -4
  136. package/src/www/logger.ts +0 -3
  137. package/src/www/scanner.spec.ts +0 -98
  138. package/src/www/scanner.ts +0 -132
  139. package/www/ble-com-protocol.d.ts +0 -16
  140. package/www/cordova-interface.d.ts +0 -11
  141. package/www/definitions.d.ts +0 -6
  142. package/www/index.d.ts +0 -4
  143. package/www/iotize-ble.js +0 -55
  144. package/www/logger.d.ts +0 -2
  145. package/www/scanner.d.ts +0 -18
@@ -8,6 +8,23 @@
8
8
  import Foundation
9
9
  import CoreBluetooth
10
10
 
11
+ // Custom debug print functions
12
+
13
+ public func printBLE(_ object: Any...) {
14
+ #if true
15
+ for item in object {
16
+ Swift.print("BLE:", object)
17
+ }
18
+ #endif
19
+ }
20
+
21
+ public func printBLE(_ object: Any) {
22
+ #if true
23
+ Swift.print("BLE:", object)
24
+ #endif
25
+ }
26
+
27
+
11
28
  struct IoTizeBleError: Error {
12
29
 
13
30
  let code: Int
@@ -38,63 +55,143 @@ struct IoTizeBleError: Error {
38
55
 
39
56
  }
40
57
 
58
+ enum CordovaBLEErrorCode: CustomStringConvertible {
59
+ case InternalError
60
+ case IllegalArgument
61
+
62
+ var description: String {
63
+ switch self {
64
+ case .InternalError: return "CordovaBLEErrorInternalError"
65
+ case .IllegalArgument: return "CordovaBLEErrorIllegalArgument"
66
+ }
67
+ }
68
+ }
69
+
70
+ struct CordovaBLEError: Error {
71
+ var code: CordovaBLEErrorCode
72
+ var message: String
73
+
74
+ init(_ code: CordovaBLEErrorCode, _ message: String) {
75
+ self.code = code
76
+ self.message = message
77
+ }
78
+
79
+ func toJSON() -> [AnyHashable: Any] {
80
+ return ["code": code.description, "message": message]
81
+ }
82
+
83
+ static func InternalError(_ message: String) -> CordovaBLEError {return CordovaBLEError(.InternalError, message)}
84
+
85
+ static func IllegalArgument(_ message: String) -> CordovaBLEError {return CordovaBLEError(.IllegalArgument, message)}
86
+ }
87
+
88
+ struct BLEAction {
89
+ var id: String
90
+ var action: () -> Void
91
+
92
+ init(_ id: String, _ action: @escaping () -> Void) {
93
+ self.id = id
94
+ self.action = action
95
+ }
96
+ }
97
+
41
98
  //Main class handling the plugin functionalities.
42
99
  @objc(BLECom) class BLECom : CDVPlugin {
43
100
 
101
+ var bleActionQueue: Queue<BLEAction>!
102
+
103
+ var isHandlingQueue = false
104
+
44
105
  var bleController: BLEManager!
45
106
  var lastError: IoTizeBleError?
46
107
 
47
108
  //
48
109
  override func pluginInitialize(){
49
110
  bleController = BLEManager()
111
+ bleActionQueue = Queue<BLEAction>()
112
+ }
113
+
114
+ //helper to return an empty response
115
+ func sendSuccessWithoutResponse(command: CDVInvokedUrlCommand) {
116
+ let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK)
117
+
118
+ self.commandDelegate!.send(pluginResult, callbackId: command.callbackId)
119
+ if (bleActionQueue.first?.id == command.callbackId) {
120
+ removeFromQueue()
121
+ }
50
122
  }
51
123
 
52
124
  //helper to return a string
53
- func sendSuccess(command: CDVInvokedUrlCommand, result: String){
125
+ func sendSuccessWithSingleResponse(command: CDVInvokedUrlCommand, result: String){
54
126
  let pluginResult = CDVPluginResult(
55
127
  status: CDVCommandStatus_OK,
56
128
  messageAs: result
57
129
  )
58
130
  self.commandDelegate!.send( pluginResult, callbackId: command.callbackId)
131
+ if (bleActionQueue.first?.id == command.callbackId) {
132
+ removeFromQueue()
133
+ }
59
134
  }
60
135
 
61
136
  //helper to return a boolean
62
- private func sendSuccess(command: CDVInvokedUrlCommand, result: Bool) {
137
+ private func sendSuccessWithSingleResponse(command: CDVInvokedUrlCommand, result: Bool) {
63
138
  let pluginResult = CDVPluginResult(
64
139
  status: CDVCommandStatus_OK,
65
140
  messageAs: result
66
141
  )
67
142
  self.commandDelegate!.send(pluginResult, callbackId: command.callbackId)
143
+ if (bleActionQueue.first?.id == command.callbackId) {
144
+ removeFromQueue()
145
+ }
68
146
  }
69
147
 
70
148
  //helper to return a JSon object
71
- func sendSuccess(command: CDVInvokedUrlCommand, result: DiscoveredDeviceType){
149
+ func sendSuccessWithSingleResponse(command: CDVInvokedUrlCommand, result: DiscoveredDeviceType){
72
150
  let pluginResult = CDVPluginResult(
73
151
  status: CDVCommandStatus_OK,
74
152
  messageAs: result.ToJSON()
75
153
  )
76
154
  self.commandDelegate!.send( pluginResult, callbackId: command.callbackId)
155
+ if (bleActionQueue.first?.id == command.callbackId) {
156
+ removeFromQueue()
157
+ }
77
158
  }
78
159
 
79
160
  //helper to return a String with keeping the callback
80
- func sendSuccessWithResponse(command: CDVInvokedUrlCommand, result: String){
161
+ func sendSuccessWithMultipleResponse(command: CDVInvokedUrlCommand, result: String){
81
162
  let pluginResult = CDVPluginResult(
82
163
  status: CDVCommandStatus_OK,
83
164
  messageAs: result
84
165
  )
85
166
  pluginResult!.setKeepCallbackAs(true);
86
167
  self.commandDelegate!.send( pluginResult, callbackId: command.callbackId )
168
+ if (bleActionQueue.first?.id == command.callbackId) {
169
+ removeFromQueue()
170
+ }
87
171
  }
88
172
 
89
- //helper to return a JSon object with keeping the callback
90
- func sendSuccessWithResponse(command: CDVInvokedUrlCommand, result: DiscoveredDeviceType){
173
+ //helper to return a JSon DiscoveredDeviceType object with keeping the callback
174
+ func sendSuccessWithMultipleResponse(command: CDVInvokedUrlCommand, result: DiscoveredDeviceType){
91
175
  let pluginResult = CDVPluginResult(
92
176
  status: CDVCommandStatus_OK,
93
177
  messageAs: result.ToJSON()
94
178
  )
95
179
  pluginResult!.setKeepCallbackAs(true);
96
180
  self.commandDelegate!.send( pluginResult, callbackId: command.callbackId )
181
+ if (bleActionQueue.first?.id == command.callbackId) {
182
+ removeFromQueue()
183
+ }
97
184
  }
185
+ //helper to return a JSon Array object object
186
+ func sendSuccessWithSingleResponse(command: CDVInvokedUrlCommand, result: [[AnyHashable: Any]]) {
187
+ let pluginResult = CDVPluginResult(
188
+ status: CDVCommandStatus_OK,
189
+ messageAs: result
190
+ )
191
+ self.commandDelegate!.send( pluginResult, callbackId: command.callbackId )
192
+ if (bleActionQueue.first?.id == command.callbackId) {
193
+ removeFromQueue()
194
+ } }
98
195
 
99
196
  //helper to send back an error
100
197
  func sendError(command: CDVInvokedUrlCommand, result: String){
@@ -103,192 +200,533 @@ struct IoTizeBleError: Error {
103
200
  messageAs: result
104
201
  )
105
202
  self.commandDelegate!.send( pluginResult, callbackId: command.callbackId)
203
+ if (bleActionQueue.first?.id == command.callbackId) {
204
+ removeFromQueue()
205
+ } }
206
+
207
+ func sendError(command: CDVInvokedUrlCommand, result: CordovaBLEError){
208
+ let pluginResult = CDVPluginResult(
209
+ status: CDVCommandStatus_ERROR,
210
+ messageAs: result.toJSON()
211
+ )
212
+ self.commandDelegate!.send( pluginResult, callbackId: command.callbackId)
213
+ if (bleActionQueue.first?.id == command.callbackId) {
214
+ removeFromQueue()
215
+ }
106
216
  }
107
217
 
218
+
108
219
  //Is BLE available
109
220
  @objc(checkAvailable:)
110
221
  func checkAvailable(command: CDVInvokedUrlCommand) {
111
-
112
- DispatchQueue.main.async {
113
-
114
- //from ios5
115
- if ( floor(NSFoundationVersionNumber) <= floor(NSFoundationVersionNumber_iOS_5_1) ){
116
- self.sendError(command: command, result: IoTizeBleError.BleUnsupported().message)
117
- }
118
-
119
- //check State
120
- self.bleController.checkState(completion: {
121
- (error: IoTizeBleError?) -> () in
222
+ addToQueue(command: command, bleAction: {[self] in
223
+ DispatchQueue.main.async {
122
224
 
123
- DispatchQueue.main.async {
225
+ //from ios5
226
+ if ( floor(NSFoundationVersionNumber) <= floor(NSFoundationVersionNumber_iOS_5_1) ){
227
+ self.sendError(command: command, result: IoTizeBleError.BleUnsupported().message)
228
+ }
229
+
230
+ //check State
231
+ self.bleController.checkState(completion: {
232
+ (error: IoTizeBleError?) -> () in
124
233
 
125
- if (error != nil){
126
- self.lastError = error
127
- self.sendError(command: command, result: error!.message)
234
+ DispatchQueue.main.async {
235
+
236
+ if (error != nil){
237
+ self.lastError = error
238
+ self.sendError(command: command, result: error!.message)
239
+ }
240
+ else {
241
+ self.sendSuccessWithSingleResponse(command: command, result: true)
242
+ }
243
+
128
244
  }
129
- else {
130
- self.sendSuccess(command: command, result: true)
131
- }
132
-
133
- }
134
- })
135
- }
245
+ })
246
+ }
247
+ })
136
248
  }
137
249
 
138
250
 
139
251
  //Start scanning IoTize devices
140
252
  @objc(startScan:)
141
253
  func startScan(command: CDVInvokedUrlCommand) {
142
-
143
- if (!self.isReady()){
144
-
145
- DispatchQueue.main.async {
146
- Thread.sleep(forTimeInterval: 0.01)
147
- self.startScan(command: command)
254
+ addToQueue(command: command, bleAction: {[self] in
255
+ if (!self.isReady()){
256
+
257
+ DispatchQueue.main.async {
258
+ Thread.sleep(forTimeInterval: 0.01)
259
+ self.startScan(command: command)
260
+ }
261
+ return
148
262
  }
149
- return
150
- }
151
-
152
- self.bleController.beginScan(completion: {
153
- (result: Any, error: IoTizeBleError?) -> () in
154
263
 
155
- DispatchQueue.main.async {
264
+ self.bleController.beginScan(completion: {
265
+ (result: Any, error: IoTizeBleError?) -> () in
156
266
 
157
- if (error != nil){
158
- self.lastError = error
159
- self.sendError(command: command, result: error!.message)
160
- }
161
- else if let resultString = result as? String{
162
- self.sendSuccessWithResponse(command: command, result: resultString)
163
- } else if let resultDiscoveredDevice = result as? DiscoveredDeviceType {
164
- self.sendSuccessWithResponse(command: command, result: resultDiscoveredDevice)
165
- } else if let resultDevice = result as? CBPeripheral {
166
- self.sendSuccessWithResponse(command: command, result: CBPeripheralConverter.toDiscoveredDeviceType(device: resultDevice))
167
- } else {
267
+ DispatchQueue.main.async {
268
+
269
+ if (error != nil){
270
+ self.lastError = error
271
+ self.sendError(command: command, result: error!.message)
272
+ }
273
+ else if let resultString = result as? String{
274
+ self.sendSuccessWithMultipleResponse(command: command, result: resultString)
275
+ } else if let resultDiscoveredDevice = result as? DiscoveredDeviceType {
276
+ self.sendSuccessWithMultipleResponse(command: command, result: resultDiscoveredDevice)
277
+ } else if let resultDevice = result as? CBPeripheral {
278
+ self.sendSuccessWithMultipleResponse(command: command, result: CBPeripheralConverter.toDiscoveredDeviceType(device: resultDevice))
279
+ } else {
280
+ }
168
281
  }
169
- }
282
+ })
170
283
  })
171
284
  }
172
285
 
173
286
  //Stop scanning
174
287
  @objc(stopScan:)
175
288
  func stopScan(command: CDVInvokedUrlCommand) {
176
- bleController.stopScan();
177
- self.sendSuccess(command: command, result: "Ok")
289
+ addToQueue(command: command, bleAction: {[self] in
290
+ bleController.stopScan();
291
+ self.sendSuccessWithSingleResponse(command: command, result: "OK")
292
+ })
178
293
  }
179
294
 
180
295
 
181
296
  //Connect to a device using its UUID
297
+ @objc(connectAndDiscoverTapServices:)
298
+ func connectAndDiscoverTapServices(command: CDVInvokedUrlCommand) {
299
+ addToQueue(command: command, bleAction: {[self] in
300
+ //we need the UUID of the device
301
+ if (command.arguments.count == 0){
302
+ let error = CordovaBLEError.IllegalArgument("Missing arguments")
303
+ self.sendError(command: command, result: error)
304
+ return
305
+ }
306
+
307
+ let deviceUUID = command.arguments[0] as? String ?? ""
308
+
309
+ bleController.useBLETapPeripheral = true
310
+
311
+ bleController.connectWithUUID(device: deviceUUID, completion: {
312
+ (state: Any?, error: IoTizeBleError?) -> () in
313
+
314
+ DispatchQueue.main.async {
315
+
316
+ if (error != nil){
317
+ self.lastError = error
318
+ self.sendError(command: command, result: error!.message)
319
+ }
320
+ else {
321
+ //printBLE("##> Sending Connected Ok")
322
+ let _state = state as? String
323
+
324
+ self.sendSuccessWithMultipleResponse(command: command, result: _state ?? "") // keep callback
325
+ }
326
+ }
327
+ })
328
+ })
329
+ }
330
+
331
+
182
332
  @objc(connect:)
183
333
  func connect(command: CDVInvokedUrlCommand) {
184
-
185
- //we need the UUID of the device
186
- if (command.arguments.count == 0){
187
- self.sendError(command: command, result: "Connection parameter error")
188
- return
189
- }
190
-
191
- let deviceUUID = command.arguments[0] as? String ?? ""
192
-
193
- bleController.connectWithUUID(device: deviceUUID, completion: {
194
- (state: Any?, error: IoTizeBleError?) -> () in
334
+ addToQueue(command: command, bleAction: {[self] in
335
+ //we need the UUID of the device
336
+ if (command.arguments.count == 0){
337
+ self.sendError(command: command, result: CordovaBLEError.IllegalArgument("Missing arguments"))
338
+ return
339
+ }
195
340
 
196
- DispatchQueue.main.async {
341
+ let deviceUUID = command.arguments[0] as? String ?? ""
342
+
343
+ bleController.useBLETapPeripheral = false
344
+
345
+ bleController.connectWithUUID(device: deviceUUID, completion: {
346
+ (state: Any?, error: IoTizeBleError?) -> () in
197
347
 
198
- if (error != nil){
199
- self.lastError = error
200
- self.sendError(command: command, result: error!.message)
201
- }
202
- else {
203
- //print("##> Sending Connected Ok")
204
- let _state = state as? String
348
+ DispatchQueue.main.async {
349
+
350
+ if (error != nil){
351
+ self.lastError = error
352
+ self.sendError(command: command, result: error!.message)
353
+ }
354
+ else {
355
+ //printBLE("##> Sending Connected Ok")
356
+ let _state = state as? String
205
357
 
206
- self.sendSuccessWithResponse(command: command, result: _state ?? "") // keep callback
358
+ self.sendSuccessWithMultipleResponse(command: command, result: _state ?? "") // keep callback
359
+ }
207
360
  }
208
- }
209
- })
361
+ })
362
+ })
210
363
  }
211
364
 
212
365
  //Disconnect from a device using its name
213
366
  @objc(disConnect:)
214
367
  func disConnect(command: CDVInvokedUrlCommand) {
215
-
216
- //we need the name of the device
217
- if (command.arguments.count == 0){
218
- self.sendError(command: command, result: "disConnection parameter error")
219
- return
220
- }
221
-
222
- bleController.disconnect( completion: {
223
- (error: IoTizeBleError?) -> () in
224
-
225
- DispatchQueue.main.async {
368
+ addToQueue(command: command, bleAction: { [self] in
369
+ //we need the name of the device
370
+ if (command.arguments.count == 0){
371
+ self.sendError(command: command, result: CordovaBLEError.IllegalArgument("Missing arguments"))
372
+ return
373
+ }
374
+
375
+ bleController.disconnect( completion: {
376
+ (error: IoTizeBleError?) -> () in
226
377
 
227
- if (error != nil){
228
- self.lastError = error
229
- self.sendError(command: command, result: error!.message)
230
- }
231
- else {
232
- self.sendSuccess(command: command, result: "Ok")
378
+ DispatchQueue.main.async {
379
+
380
+ if (error != nil){
381
+ self.lastError = error
382
+ self.sendError(command: command, result: error!.message)
383
+ }
384
+ else {
385
+ self.sendSuccessWithSingleResponse(command: command, result: "OK")
386
+ }
233
387
  }
234
- }
388
+ })
235
389
  })
236
390
  }
237
391
 
238
392
  //Retrieve additional information
239
393
  @objc(getLastError:)
240
394
  func getLastError(command: CDVInvokedUrlCommand) {
241
- let msg: String = (lastError != nil) ? (lastError!.message) : ""
242
- self.sendSuccess(command: command, result: msg)
395
+ addToQueue(command: command, bleAction: {[self] in
396
+ let msg: String = (lastError != nil) ? (lastError!.message) : ""
397
+ self.sendSuccessWithSingleResponse(command: command, result: msg)
398
+ })
243
399
  }
244
400
 
245
401
  //Send Data to device
246
402
  @objc(sendRequest:)
247
403
  func sendRequest(command: CDVInvokedUrlCommand) {
248
- print("##> SendRequest")
249
- //we need data to send
250
- if (command.arguments.count == 1){
251
- self.sendError(command: command, result: "SendRequest parameter error")
252
- return
253
- }
254
-
255
- let data = command.arguments[1] as? String ?? ""
256
404
 
257
- bleController.sendRequest(data: data, completion: {
258
- (response: Any, error: IoTizeBleError?) -> () in
405
+ addToQueue(command: command, bleAction: {[self] in
406
+ printBLE("##> SendRequest")
407
+ //we need data to send
408
+ if (command.arguments.count == 1){
409
+ self.sendError(command: command, result: CordovaBLEError.IllegalArgument("Missing arguments"))
410
+ return
411
+ }
259
412
 
260
- DispatchQueue.main.async {
413
+ let data = command.arguments[1] as? String ?? ""
414
+
415
+ bleController.sendRequest(data: data, completion: {
416
+ (response: Any, error: IoTizeBleError?) -> () in
261
417
 
262
- if (error != nil){
263
- self.lastError = error
264
- self.sendError(command: command, result: error!.message)
265
- }
266
- else {
267
- if let responseString = response as? String {
268
- self.sendSuccess(command: command, result: responseString)
418
+ DispatchQueue.main.async {
419
+
420
+ if (error != nil){
421
+ self.lastError = error
422
+ self.sendError(command: command, result: error!.message)
269
423
  }
270
- else if let responseDiscoveredDevice = response as? DiscoveredDeviceType {
271
- self.sendSuccess(command: command, result: responseDiscoveredDevice)
272
- } else if let responseDevice = response as? CBPeripheral {
273
- self.sendSuccess(command: command, result: CBPeripheralConverter.toDiscoveredDeviceType(device: responseDevice))
424
+ else {
425
+ if let responseString = response as? String {
426
+ self.sendSuccessWithSingleResponse(command: command, result: responseString)
427
+ }
428
+ else if let responseDiscoveredDevice = response as? DiscoveredDeviceType {
429
+ self.sendSuccessWithSingleResponse(command: command, result: responseDiscoveredDevice)
430
+ } else if let responseDevice = response as? CBPeripheral {
431
+ self.sendSuccessWithSingleResponse(command: command, result: CBPeripheralConverter.toDiscoveredDeviceType(device: responseDevice))
432
+ }
274
433
  }
275
434
  }
276
- }
435
+ })
277
436
  })
278
437
  }
279
438
 
280
439
  @objc(isConnected:)
281
440
  func isConnected(command: CDVInvokedUrlCommand) {
282
-
283
- let error: Bool ;
284
-
285
- error = bleController.isConnected()
286
- self.sendSuccess(command: command, result: error)
441
+ addToQueue(command: command, bleAction: { [self] in
442
+ var status: Bool ;
443
+
444
+ if (command.arguments.count < 1) {
445
+ let error = CordovaBLEError.IllegalArgument("Missing arguments")
446
+ self.sendError(command: command, result: error)
447
+ }
448
+
449
+ let deviceUUID = command.arguments[0] as? String ?? ""
450
+ do {
451
+ status = try bleController.isConnected(deviceUUID)
452
+ self.sendSuccessWithSingleResponse(command: command, result: status)
453
+ }
454
+ catch {
455
+ if (error is CordovaBLEError) {
456
+ self.sendError(command: command, result: (error as! CordovaBLEError))
457
+ } else {
458
+ let cordovaError = CordovaBLEError.InternalError("Unkown error")
459
+ self.sendError(command: command, result: cordovaError)
460
+ }
461
+ }
462
+ })
287
463
 
288
464
  }
289
465
 
290
466
  func isReady() -> Bool {
291
467
  return bleController.isReady()
292
468
  }
469
+
470
+ @objc(discoverServices:)
471
+ func discoverServices(command: CDVInvokedUrlCommand) {
472
+ addToQueue(command: command, bleAction: {[self] in
473
+ printBLE("discoverServices called")
474
+ if (command.arguments.count == 0){
475
+ self.sendError(command: command, result: CordovaBLEError.IllegalArgument("Missing arguments"))
476
+ return
477
+ }
478
+
479
+ let deviceUUID = command.arguments[0] as? String ?? ""
480
+
481
+ bleController.discoverServices(forUUID: deviceUUID) { (anyPeripheral, error) in
482
+ if (error != nil) {
483
+ self.lastError = error
484
+ self.sendError(command: command, result: error?.message ?? "discoverServicesError")
485
+ } else {
486
+ if let peripheral = anyPeripheral as? CBPeripheral {
487
+ let result = CBPeripheralConverter.toServiceDescriptionsJSON(device: peripheral)
488
+ self.sendSuccessWithSingleResponse(command: command, result: result)
489
+ }
490
+ }
491
+ }
492
+ })
493
+ }
494
+
495
+ @objc(characteristicWriteWithoutResponse:)
496
+ func characteristicWriteWithoutResponse(command: CDVInvokedUrlCommand) {
497
+
498
+ addToQueue(command: command, bleAction: {[self] in
499
+ printBLE("characteristicWriteWithoutResponse called")
500
+ if (command.arguments.count < 4){
501
+ self.sendError(command: command, result: CordovaBLEError.IllegalArgument("Missing arguments"))
502
+ return
503
+ }
504
+ let deviceID = command.arguments[0] as? String ?? ""
505
+
506
+ guard let serviceUUID = CBUUIDHelper.fromString(command.arguments[1] as? String ?? "") else {
507
+ let cordovaError = CordovaBLEError.IllegalArgument("\"\(command.arguments[1] as? String ?? "")\" is not a valid BLE UUID")
508
+ self.sendError(command: command, result: cordovaError)
509
+ return
510
+ }
511
+ guard let characteristicUUID = CBUUIDHelper.fromString(command.arguments[2] as? String ?? "") else {
512
+ let cordovaError = CordovaBLEError.IllegalArgument("\"\(command.arguments[2] as? String ?? "")\" is not a valid BLE UUID")
513
+ self.sendError(command: command, result: cordovaError)
514
+ return
515
+ }
516
+
517
+ let dataToWrite = Data(Request.stringToHexArray(command.arguments[3] as? String ?? ""))
518
+
519
+ bleController.characteristicWriteWithoutResponse(deviceID: deviceID, serviceUUID: serviceUUID, characteristicUUID: characteristicUUID, data: dataToWrite, completion: { (error) in
520
+ if (error != nil) {
521
+ self.sendError(command: command, result: error!.message)
522
+ return
523
+ }
524
+ self.sendSuccessWithSingleResponse(command: command, result: "OK")
525
+ })
526
+ })
527
+ }
528
+
529
+ @objc(characteristicWrite:)
530
+ func characteristicWrite(command: CDVInvokedUrlCommand) {
531
+ addToQueue(command: command, bleAction: {[self] in
532
+ printBLE("characteristicWrite called")
533
+ if (command.arguments.count < 4){
534
+ self.sendError(command: command, result: CordovaBLEError.IllegalArgument("Missing arguments"))
535
+ return
536
+ }
537
+ let deviceID = command.arguments[0] as? String ?? ""
538
+
539
+ guard let serviceUUID = CBUUIDHelper.fromString(command.arguments[1] as? String ?? "") else {
540
+ let cordovaError = CordovaBLEError.IllegalArgument("\"\(command.arguments[1] as? String ?? "")\" is not a valid BLE UUID")
541
+ self.sendError(command: command, result: cordovaError)
542
+ return
543
+ }
544
+
545
+ guard let characteristicUUID = CBUUIDHelper.fromString(command.arguments[2] as? String ?? "") else {
546
+ let cordovaError = CordovaBLEError.IllegalArgument("\"\(command.arguments[2] as? String ?? "")\" is not a valid BLE UUID")
547
+
548
+ self.sendError(command: command, result: cordovaError)
549
+ return
550
+ }
551
+
552
+ let dataToWrite = Data(Request.stringToHexArray(command.arguments[3] as? String ?? ""))
553
+
554
+ bleController.characteristicWrite(deviceID: deviceID, serviceUUID: serviceUUID, characteristicUUID: characteristicUUID, data: dataToWrite, completion: { (error) in
555
+ if (error != nil) {
556
+ self.sendError(command: command, result: error!.message)
557
+ return
558
+ }
559
+ self.sendSuccessWithSingleResponse(command: command, result: "OK")
560
+ })
561
+ })
562
+ }
563
+
564
+ @objc(characteristicRead:)
565
+ func characteristicRead(command: CDVInvokedUrlCommand) {
566
+
567
+ addToQueue(command: command, bleAction: {[self] in
568
+ printBLE("characteristicRead called")
569
+ if (command.arguments.count < 3){
570
+ self.sendError(command: command, result: CordovaBLEError.IllegalArgument("Missing arguments"))
571
+ return
572
+ }
573
+ let deviceID = command.arguments[0] as? String ?? ""
574
+ guard let serviceUUID = CBUUIDHelper.fromString(command.arguments[1] as? String ?? "") else {
575
+ let cordovaError = CordovaBLEError.IllegalArgument("\"\(command.arguments[1] as? String ?? "")\" is not a valid BLE UUID")
576
+ self.sendError(command: command, result: cordovaError)
577
+ return
578
+ }
579
+ guard let characteristicUUID = CBUUIDHelper.fromString(command.arguments[2] as? String ?? "") else {
580
+ let cordovaError = CordovaBLEError.IllegalArgument("\"\(command.arguments[2] as? String ?? "")\" is not a valid BLE UUID")
581
+ self.sendError(command: command, result: cordovaError)
582
+ return
583
+ }
584
+
585
+ bleController.characteristicRead(deviceID: deviceID, serviceUUID: serviceUUID, characteristicUUID: characteristicUUID, completion: { (readData, error) in
586
+ if (error != nil) {
587
+ self.sendError(command: command, result: error!.message)
588
+ return
589
+ }
590
+ let data = readData as! String
591
+ self.sendSuccessWithSingleResponse(command: command, result: data)
592
+ })
593
+ })
594
+
595
+
596
+ }
597
+
598
+ @objc(characteristicStartNotification:)
599
+ func characteristicStartNotification(command: CDVInvokedUrlCommand) {
600
+
601
+ addToQueue(command: command, bleAction: {[self] () in
602
+ printBLE("characteristicStartNotification called")
603
+ if (command.arguments.count < 3){
604
+ self.sendError(command: command, result: "characteristicStartNotification parameter error")
605
+ return
606
+ }
607
+ let deviceID = command.arguments[0] as? String ?? ""
608
+
609
+ guard let serviceUUID = CBUUIDHelper.fromString(command.arguments[1] as? String ?? "") else {
610
+ let cordovaError = CordovaBLEError.IllegalArgument("\"\(command.arguments[1] as? String ?? "")\" is not a valid BLE UUID")
611
+ self.sendError(command: command, result: cordovaError)
612
+ return
613
+ }
614
+ guard let characteristicUUID = CBUUIDHelper.fromString(command.arguments[2] as? String ?? "") else {
615
+ let cordovaError = CordovaBLEError.IllegalArgument("\"\(command.arguments[2] as? String ?? "")\" is not a valid BLE UUID")
616
+ self.sendError(command: command, result: cordovaError)
617
+ return
618
+ }
293
619
 
620
+ bleController.characteristicStartNotification(deviceID: deviceID, serviceUUID: serviceUUID, characteristicUUID: characteristicUUID, completion: { error in
621
+ if (error != nil) {
622
+ printBLE("characteristicStartNotification response error")
623
+ self.sendError(command: command, result: error!.message)
624
+ return
625
+ }
626
+ printBLE("characteristicStartNotification response OK")
627
+ self.sendSuccessWithSingleResponse(command: command, result: "OK")
628
+ })
629
+ })
630
+
631
+ }
632
+
633
+ @objc(characteristicStopNotification:)
634
+ func characteristicStopNotification(command: CDVInvokedUrlCommand) {
635
+
636
+ addToQueue(command: command, bleAction: { [self] () in
637
+ printBLE("characteristicStopNotification called")
638
+ if (command.arguments.count < 3){
639
+ self.sendError(command: command, result: "characteristicStopNotification parameter error")
640
+ return
641
+ }
642
+ let deviceID = command.arguments[0] as? String ?? ""
643
+
644
+ guard let serviceUUID = CBUUIDHelper.fromString(command.arguments[1] as? String ?? "") else {
645
+ let cordovaError = CordovaBLEError.IllegalArgument("\"\(command.arguments[1] as? String ?? "")\" is not a valid BLE UUID")
646
+ self.sendError(command: command, result: cordovaError)
647
+ return
648
+ }
649
+ guard let characteristicUUID = CBUUIDHelper.fromString(command.arguments[2] as? String ?? "") else {
650
+ let cordovaError = CordovaBLEError.IllegalArgument("\"\(command.arguments[2] as? String ?? "")\" is not a valid BLE UUID")
651
+ self.sendError(command: command, result: cordovaError)
652
+ return
653
+ }
654
+
655
+ bleController.characteristicStopNotification(deviceID: deviceID, serviceUUID: serviceUUID, characteristicUUID: characteristicUUID, completion: { error in
656
+ if (error != nil) {
657
+ printBLE("characteristicStopNotification response error")
658
+ self.sendError(command: command, result: error!.message)
659
+ return
660
+ }
661
+ printBLE("characteristicStopNotification response OK")
662
+ self.sendSuccessWithSingleResponse(command: command, result: "OK")
663
+ })
664
+ })
665
+
666
+ }
667
+
668
+ @objc(characteristicChanged:)
669
+ func characteristicChanged(command: CDVInvokedUrlCommand) {
670
+
671
+ addToQueue(command: command, bleAction: { [self] () in
672
+
673
+ printBLE("characteristicChanged called")
674
+ if (command.arguments.count < 3){
675
+ self.sendError(command: command, result: "SendRequest parameter error")
676
+ return
677
+ }
678
+ let deviceID = command.arguments[0] as? String ?? ""
679
+
680
+ guard let serviceUUID = CBUUIDHelper.fromString(command.arguments[1] as? String ?? "") else {
681
+ let cordovaError = CordovaBLEError.IllegalArgument("\"\(command.arguments[1] as? String ?? "")\" is not a valid BLE UUID")
682
+ self.sendError(command: command, result: cordovaError)
683
+ return
684
+ }
685
+ guard let characteristicUUID = CBUUIDHelper.fromString(command.arguments[2] as? String ?? "") else {
686
+ let cordovaError = CordovaBLEError.IllegalArgument("\"\(command.arguments[2] as? String ?? "")\" is not a valid BLE UUID")
687
+ self.sendError(command: command, result: cordovaError)
688
+ return
689
+ }
690
+
691
+ bleController.characteristicChanged(deviceID: deviceID, serviceUUID: serviceUUID, characteristicUUID: characteristicUUID, completion: { (result, error) in
692
+ if (error != nil) {
693
+ self.sendError(command: command, result: error!.message)
694
+ return
695
+ }
696
+ self.sendSuccessWithMultipleResponse(command: command, result: result as! String)
697
+ })
698
+ if (bleActionQueue.first?.id == command.callbackId) {
699
+ removeFromQueue()
700
+ }
701
+ })
702
+ }
703
+
704
+ func addToQueue(command: CDVInvokedUrlCommand, bleAction: @escaping () -> Void) {
705
+ bleActionQueue.enqueue(BLEAction(command.callbackId, bleAction))
706
+ if (!isHandlingQueue) {
707
+ isHandlingQueue = true
708
+ DispatchQueue.main.async {
709
+ bleAction()
710
+ }
711
+ }
712
+ }
713
+
714
+ func removeFromQueue() {
715
+ bleActionQueue.dequeue()
716
+ if (isHandlingQueue) {
717
+ if (bleActionQueue.first != nil) {
718
+ DispatchQueue.main.async {
719
+ self.bleActionQueue.first!.action()
720
+ }
721
+ } else {
722
+ isHandlingQueue = false
723
+ }
724
+ }
725
+ }
726
+
727
+
728
+ @objc(close:)
729
+ func close(command: CDVInvokedUrlCommand) {
730
+ disConnect(command)
731
+ }
294
732
  }