@iotize/device-com-nfc.cordova 3.6.0 → 3.7.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.
@@ -17,6 +17,20 @@ import CoreNFC
17
17
  var lastError: Error?
18
18
  var channelCommand: CDVInvokedUrlCommand?
19
19
  var isListeningNDEF = false
20
+
21
+ private var _isTapDiscoveryEnabled: Bool!
22
+
23
+ override func pluginInitialize(){
24
+ //Need to be initialized here, otherwise it is not set to true
25
+ self._isTapDiscoveryEnabled = true
26
+ }
27
+
28
+ func sendSuccess(command: CDVInvokedUrlCommand) {
29
+ let pluginResult = CDVPluginResult(
30
+ status: CDVCommandStatus_OK
31
+ )
32
+ commandDelegate!.send(pluginResult, callbackId: command.callbackId)
33
+ }
20
34
 
21
35
  // helper to return a string
22
36
  func sendSuccess(command: CDVInvokedUrlCommand, result: String) {
@@ -55,30 +69,47 @@ import CoreNFC
55
69
  commandDelegate!.send(pluginResult, callbackId: command.callbackId)
56
70
  }
57
71
 
72
+ //IoTize Tap specific connect. Opens reading session and connect to tap on discover
58
73
  @objc(connect:)
59
74
  func connect(command: CDVInvokedUrlCommand) {
60
- print("CONNECT NFC")
75
+ printNFC("connect")
61
76
  guard #available(iOS 13.0, *) else {
62
77
  sendError(command: command, result: "connect is only available on iOS 13+")
63
78
  return
64
79
  }
80
+
81
+ var alertMessage = "Bring your phone close to the Tap."
65
82
 
66
- if let controller = self.nfcController as? ST25DVReader {
67
- if let tag = controller.tag {
68
- if (tag.isAvailable) {
69
- //already connected, refire info
70
- sendSuccess(command: command, result: "CONNECTED")
71
- return
72
- }
73
- }
83
+ if let alertMessageFromCommand = command.argument(at:0) as? String {
84
+ alertMessage = alertMessageFromCommand
74
85
  }
86
+
87
+
75
88
  DispatchQueue.main.async {
76
- print("Begin session \(String(describing: self.nfcController))")
89
+ printNFC("Begin session \(String(describing: self.nfcController))")
77
90
  if self.nfcController == nil {
78
- self.nfcController = ST25DVReader()
91
+ self.nfcController = NFCTagReader(plugin: self)
92
+ }
93
+
94
+ if let isSessionReady = (self.nfcController as! NFCTagReader).comSession?.isReady {
95
+ if isSessionReady {
96
+ // We reuse the current session
97
+ (self.nfcController as! NFCTagReader).connect(tech: NfcTech.NfcV, connectionCompletion: {
98
+ (error: Error?) -> Void in
99
+
100
+ DispatchQueue.main.async {
101
+ if error != nil {
102
+ self.sendError(command: command, result: error!.localizedDescription)
103
+ } else {
104
+ self.sendSuccess(command: command, result: "")
105
+ }
106
+ }
107
+ })
108
+ return
109
+ }
79
110
  }
80
111
 
81
- (self.nfcController as! ST25DVReader).initSession(pollingOption: [.iso15693], alertMessage: "Bring your phone close to the Tap.", completed: {
112
+ (self.nfcController as! NFCTagReader).initSession(pollingOption: [.iso15693], alertMessage: alertMessage, completed: {
82
113
  (error: Error?) -> Void in
83
114
 
84
115
  DispatchQueue.main.async {
@@ -94,6 +125,7 @@ import CoreNFC
94
125
 
95
126
  @objc(close:)
96
127
  func close(command: CDVInvokedUrlCommand) {
128
+ printNFC("close")
97
129
  guard #available(iOS 13.0, *) else {
98
130
  sendError(command: command, result: "close is only available on iOS 13+")
99
131
  return
@@ -104,19 +136,21 @@ import CoreNFC
104
136
  return
105
137
  }
106
138
 
107
- (self.nfcController as! NFCTagReader).invalidateSession(message: "Sesssion Ended!")
139
+ (self.nfcController as! NFCTagReader).invalidateSession(message: "Session Ended!")
108
140
  self.nfcController = nil
109
141
  }
110
142
  }
111
143
 
112
144
  @objc(transceiveTap:)
113
145
  func transceiveTap(command: CDVInvokedUrlCommand) {
146
+ printNFC("transceiveTap")
147
+
114
148
  guard #available(iOS 13.0, *) else {
115
149
  sendError(command: command, result: "transceive is only available on iOS 13+")
116
150
  return
117
151
  }
118
152
  DispatchQueue.main.async {
119
- print("sending ...")
153
+ printNFC("sending ...")
120
154
  if self.nfcController == nil {
121
155
  self.sendError(command: command, result: "no session available")
122
156
  return
@@ -134,9 +168,9 @@ import CoreNFC
134
168
  }
135
169
  let request = data.map { String(format: "%02x", $0) }
136
170
  .joined()
137
- print("send request - \(request)")
171
+ printNFC("send request - \(request)")
138
172
 
139
- (self.nfcController as! ST25DVReader).send(request: request, completed: {
173
+ (self.nfcController as! NFCTagReader).sendTap(request: request, completed: {
140
174
  (response: Data?, error: Error?) -> Void in
141
175
 
142
176
  DispatchQueue.main.async {
@@ -144,7 +178,7 @@ import CoreNFC
144
178
  self.lastError = error
145
179
  self.sendError(command: command, result: error!.localizedDescription)
146
180
  } else {
147
- print("responded \(response!.hexEncodedString())")
181
+ printNFC("responded \(response!.hexEncodedString())")
148
182
  self.sendSuccess(command: command, result: response!.hexEncodedString())
149
183
  }
150
184
  }
@@ -154,28 +188,41 @@ import CoreNFC
154
188
 
155
189
  @objc(registerTag:)
156
190
  func registerTag(command: CDVInvokedUrlCommand) {
157
- print("Registered NDEF Listener")
158
- //isListeningNDEF = true // Flag for the AppDelegate
159
- sendSuccess(command: command, result: "Tag Listener is on")
191
+ printNFC("registerTag")
192
+ registerChannel()
193
+ //No need to register, NFC tag discovery handled in sessions
194
+ sendSuccess(command: command)
195
+ }
196
+
197
+ @objc(registerTapDevice:)
198
+ func registerTapDevice(command: CDVInvokedUrlCommand) {
199
+ printNFC("registerTapDevice")
200
+ registerChannel()
201
+ //No need to register, NFC tag discovery handled in sessions
202
+ sendSuccess(command: command)
160
203
  }
161
204
 
162
205
  @objc(registerNdef:)
163
206
  func registerNdef(command: CDVInvokedUrlCommand) {
164
- print("Registered NDEF Listener")
165
- isListeningNDEF = true // Flag for the AppDelegate
166
- sendSuccess(command: command, result: "NDEF Listener is on")
207
+ printNFC("registerNdef")
208
+ registerChannel()
209
+ sendSuccess(command: command)
167
210
  }
168
211
 
169
212
  @objc(registerMimeType:)
170
213
  func registerMimeType(command: CDVInvokedUrlCommand) {
171
- print("Registered Mi Listener")
214
+ printNFC("registerMimeType")
215
+ registerChannel()
216
+ //No need to register, NFC tag discovery handled in sessions
172
217
  sendSuccess(command: command, result: "NDEF Listener is on")
173
218
  }
174
219
 
175
220
  @objc(beginNDEFSession:)
176
221
  func beginNDEFSession(command: CDVInvokedUrlCommand) {
222
+ printNFC("beginNDEFSession")
223
+
177
224
  DispatchQueue.main.async {
178
- print("Begin NDEF reading session")
225
+ printNFC("Begin NDEF reading session")
179
226
 
180
227
  if self.ndefController == nil {
181
228
  var message: String?
@@ -185,7 +232,7 @@ import CoreNFC
185
232
  self.ndefController = NFCNDEFDelegate(completed: {
186
233
  (response: [AnyHashable: Any]?, error: Error?) -> Void in
187
234
  DispatchQueue.main.async {
188
- print("handle NDEF")
235
+ printNFC("handle NDEF")
189
236
  if error != nil {
190
237
  self.lastError = error
191
238
  self.sendError(command: command, result: error!.localizedDescription)
@@ -202,6 +249,8 @@ import CoreNFC
202
249
 
203
250
  @objc(invalidateNDEFSession:)
204
251
  func invalidateNDEFSession(command: CDVInvokedUrlCommand) {
252
+ printNFC("invalidateNDEFSession")
253
+
205
254
  guard #available(iOS 11.0, *) else {
206
255
  sendError(command: command, result: "close is only available on iOS 13+")
207
256
  return
@@ -220,8 +269,9 @@ import CoreNFC
220
269
 
221
270
  @objc(channel:)
222
271
  func channel(command: CDVInvokedUrlCommand) {
272
+ printNFC("channel")
223
273
  DispatchQueue.main.async {
224
- print("Creating NDEF Channel")
274
+ printNFC("Creating NDEF Channel")
225
275
  self.channelCommand = command
226
276
  self.sendThroughChannel(message: "Did create NDEF Channel")
227
277
  }
@@ -229,11 +279,11 @@ import CoreNFC
229
279
 
230
280
  func sendThroughChannel(message: String) {
231
281
  guard let command: CDVInvokedUrlCommand = self.channelCommand else {
232
- print("Channel is not set")
282
+ printNFC("Channel is not set")
233
283
  return
234
284
  }
235
285
  guard let response = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: message) else {
236
- print("sendThroughChannel Did not create CDVPluginResult")
286
+ printNFC("sendThroughChannel Did not create CDVPluginResult")
237
287
  return
238
288
  }
239
289
 
@@ -243,11 +293,11 @@ import CoreNFC
243
293
 
244
294
  func sendThroughChannel(jsonDictionary: [AnyHashable: Any]) {
245
295
  guard let command: CDVInvokedUrlCommand = self.channelCommand else {
246
- print("Channel is not set")
296
+ printNFC("Channel is not set")
247
297
  return
248
298
  }
249
299
  guard let response = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: jsonDictionary) else {
250
- print("sendThroughChannel Did not create CDVPluginResult")
300
+ printNFC("sendThroughChannel Did not create CDVPluginResult")
251
301
  return
252
302
  }
253
303
 
@@ -259,6 +309,8 @@ import CoreNFC
259
309
 
260
310
  @objc(enabled:)
261
311
  func enabled(command: CDVInvokedUrlCommand) {
312
+ printNFC("enabled")
313
+
262
314
  guard #available(iOS 11.0, *) else {
263
315
  sendError(command: command, result: "enabled is only available on iOS 11+")
264
316
  return
@@ -270,13 +322,15 @@ import CoreNFC
270
322
  //full transparent mode
271
323
  @objc(transceive:)
272
324
  func transceive(command: CDVInvokedUrlCommand) {
325
+ printNFC("transceive")
326
+
273
327
  guard #available(iOS 14.0, *) else {
274
328
  sendError(command: command, result: "Tranparent transceive is only available in iOS 14+")
275
329
  return
276
330
  }
277
331
 
278
332
  DispatchQueue.main.async {
279
- print("sending ...")
333
+ printNFC("sending ...")
280
334
  if self.nfcController == nil {
281
335
  self.sendError(command: command, result: "no session available")
282
336
  return
@@ -293,7 +347,7 @@ import CoreNFC
293
347
  return
294
348
  }
295
349
 
296
- (self.nfcController as! ST25DVReader).transceiveRaw(request: data, completed: {
350
+ (self.nfcController as! NFCTagReader).transceiveRaw(request: data, completed: {
297
351
  (response: Data?, error: Error?) -> Void in
298
352
 
299
353
  DispatchQueue.main.async {
@@ -301,7 +355,7 @@ import CoreNFC
301
355
  self.lastError = error
302
356
  self.sendError(command: command, result: error!.localizedDescription)
303
357
  } else {
304
- print("responded \(response!.hexEncodedString())")
358
+ printNFC("responded \(response!.hexEncodedString())")
305
359
  self.sendSuccess(command: command, result: response!.hexEncodedString())
306
360
  }
307
361
  }
@@ -311,69 +365,89 @@ import CoreNFC
311
365
 
312
366
  @objc(beginSessionFromTech:)
313
367
  func beginSessionFromTech(command: CDVInvokedUrlCommand) {
314
-
368
+ printNFC("beginSessionFromTech")
369
+
315
370
  // self.nfcController = nil // Clear previous session, if any
316
371
 
317
372
  guard #available(iOS 13.0, *) else {
318
- sendError(command: command, result: "NFC Tag Session is only available in iOS 13+")
373
+ sendError(command: command, result: "connectRaw is only available on iOS 13+")
319
374
  return
320
375
  }
321
- // we need data to send
322
- if command.arguments.count <= 0 {
323
- self.sendError(command: command, result: "beginSessionFromTech parameter error: missing tech")
324
- return
376
+ var tech = ""
377
+
378
+ if let techFromCommand = command.argument(at: 0) as? String {
379
+ tech = techFromCommand
325
380
  }
326
-
327
- guard let tech = command.argument(at: 0) as? String else {
328
- self.sendError(command: command, result: "beginSessionFromTech parameter error: improper tech")
381
+
382
+ var pollingOption: NFCTagReaderSession.PollingOption = []
383
+
384
+ if let techAsNfcTech = NfcTech(rawValue: tech) {
385
+ switch (techAsNfcTech) {
386
+ case .NfcV:
387
+ pollingOption.insert(.iso15693)
388
+ break
389
+ case .IsoDep:
390
+ fallthrough
391
+ case .NfcA:
392
+ fallthrough
393
+ case .NfcB:
394
+ pollingOption.insert(.iso14443)
395
+ break
396
+ case .AnyTag:
397
+ pollingOption.insert(.iso14443)
398
+ pollingOption.insert(.iso15693)
399
+ break
400
+ default:
401
+ self.sendError(command: command, result: "Tech \(tech) not available")
402
+ break
403
+ }
404
+ } else {
405
+ self.sendError(command: command, result: "Invalid parameter \(tech)")
329
406
  return
330
407
  }
408
+
331
409
  var alertMessage = "Begin NFC Session"
332
410
 
333
411
  if let alertMessageFromCommand = command.argument(at:1) as? String {
334
412
  alertMessage = alertMessageFromCommand
335
413
  }
336
414
 
337
- switch tech {
338
- case "nfcV":
339
- DispatchQueue.main.async {
340
- print("Begin session \(String(describing: self.nfcController))")
341
- if self.nfcController == nil {
342
- self.nfcController = ST25DVReader()
343
- }
415
+ DispatchQueue.main.async {
416
+ printNFC("Begin session NFC \(String(describing: pollingOption))")
417
+ if self.nfcController == nil {
418
+ self.nfcController = NFCTagReader(plugin: self)
419
+ }
344
420
 
345
- (self.nfcController as! ST25DVReader).initSession(pollingOption: [.iso15693], alertMessage: alertMessage, completed: {
346
- (error: Error?) -> Void in
421
+ (self.nfcController as! NFCTagReader).initSession(pollingOption: pollingOption, alertMessage: alertMessage, initSessionCompletion: {
422
+ (error: Error?) -> Void in
347
423
 
348
- DispatchQueue.main.async {
349
- if error != nil {
350
- print("onBeginSessionFromTech error \(error!.localizedDescription)")
351
- self.sendError(command: command, result: error!.localizedDescription)
352
- } else {
353
- print("onBeginSessionFromTech sucess")
354
- self.sendSuccess(command: command, result: "nfcV session started")
355
- }
424
+ DispatchQueue.main.async {
425
+ if error != nil {
426
+ printNFC("onBeginSessionFromTech error \(error!.localizedDescription)")
427
+ self.sendError(command: command, result: error!.localizedDescription)
428
+ } else {
429
+ printNFC("onBeginSessionFromTech sucess")
430
+ self.sendSuccess(command: command, result: "nfcV session started")
356
431
  }
357
- }, onDiscover: {(discovered: [AnyHashable: Any]?, error: Error?) -> Void in
358
- DispatchQueue.main.async {
359
- if error != nil {
360
- print("onDiscover error \(error!.localizedDescription)")
361
- self.sendError(command: command, result: error!.localizedDescription)
362
- } else if (discovered != nil){
363
- print("onDiscover success")
364
- self.sendThroughChannel(jsonDictionary: discovered!)
365
- }
432
+ }
433
+ }, onDiscover: {(discovered: [AnyHashable: Any]?, error: Error?) -> Void in
434
+ DispatchQueue.main.async {
435
+ if error != nil {
436
+ printNFC("onDiscover error \(error!.localizedDescription)")
437
+ self.sendError(command: command, result: error!.localizedDescription)
438
+ } else if (discovered != nil){
439
+ printNFC("onDiscover success")
440
+ self.sendThroughChannel(jsonDictionary: discovered!)
366
441
  }
367
- })
368
- }
369
- return
370
- default:
371
- self.sendError(command: command, result: "Unsupported tech \(tech)")
442
+ }
443
+ })
372
444
  }
373
445
  }
374
446
 
375
447
  @objc(endSession:)
376
448
  func endSession(command: CDVInvokedUrlCommand) {
449
+ printNFC("endSession")
450
+
377
451
  guard #available(iOS 13.0, *) else {
378
452
  sendError(command: command, result: "endSession is only available on iOS 13+")
379
453
  return
@@ -390,9 +464,64 @@ import CoreNFC
390
464
  }
391
465
  }
392
466
 
393
- @objc(registerTapDevice:)
394
- func registerTapDevice(command: CDVInvokedUrlCommand) {
395
- let result = CDVPluginResult(status: CDVCommandStatus.ok)
396
- commandDelegate.send(result, callbackId: command.callbackId)
467
+ @objc(connectRaw:)
468
+ func connectRaw(command: CDVInvokedUrlCommand) {
469
+ printNFC("connectRaw")
470
+
471
+ guard #available(iOS 13.0, *) else {
472
+ sendError(command: command, result: "connectRaw is only available on iOS 13+")
473
+ return
474
+ }
475
+
476
+ guard let tech = command.argument(at: 0) as? String else {
477
+ self.sendError(command: command, result: "beginSessionFromTech parameter error: improper tech")
478
+ return
479
+ }
480
+
481
+ guard let techAsNfcTech = NfcTech(rawValue: tech) else {
482
+ self.sendError(command: command, result: "Tech \(tech) not available")
483
+ return
484
+ }
485
+
486
+ // DispatchQueue.main.async {
487
+ printNFC("sending ...")
488
+ if self.nfcController == nil {
489
+ self.sendError(command: command, result: "no session available")
490
+ return
491
+ }
492
+
493
+ (self.nfcController as! NFCTagReader).connect(tech: techAsNfcTech, connectionCompletion: {
494
+ (error: Error?) -> Void in
495
+
496
+ DispatchQueue.main.async {
497
+ if error != nil {
498
+ self.sendError(command: command, result: error!.localizedDescription)
499
+ } else {
500
+ self.sendSuccess(command: command, result: "")
501
+ }
502
+ }
503
+ })
504
+ // }
505
+
506
+
507
+ }
508
+
509
+ @objc(setTapDeviceDiscoveryEnabled:)
510
+ func setTapDeviceDiscoveryEnabled(command: CDVInvokedUrlCommand) {
511
+ printNFC("setTapDeviceDiscoveryEnabled")
512
+ if let enabled = command.argument(at: 0) as? Bool {
513
+ self._isTapDiscoveryEnabled = enabled
514
+ } else {
515
+ self._isTapDiscoveryEnabled = false
516
+ }
397
517
  }
518
+
519
+ func isTapDiscoveryEnabled() -> Bool {
520
+ return self._isTapDiscoveryEnabled
521
+ }
522
+
523
+ func registerChannel() {
524
+ isListeningNDEF = true // Flag for the AppDelegate
525
+ }
526
+
398
527
  }
@@ -5,6 +5,7 @@ import { NfcError } from './errors';
5
5
  export declare class NFCComProtocol extends QueueComProtocol {
6
6
  constructor(options?: ComProtocolOptions);
7
7
  static iOSProtocol(): NFCComProtocol;
8
+ private _sendQueue;
8
9
  /**
9
10
  * We force tag connection with nfc as we need to refresh tag
10
11
  * @param options