@iotize/device-com-nfc.cordova 3.9.0 → 3.9.2
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/bundles/iotize-device-com-nfc.cordova.umd.js +12 -33
- package/bundles/iotize-device-com-nfc.cordova.umd.js.map +1 -1
- package/bundles/iotize-device-com-nfc.cordova.umd.min.js +1 -1
- package/bundles/iotize-device-com-nfc.cordova.umd.min.js.map +1 -1
- package/esm2015/www/cordova-interface.js.map +1 -1
- package/esm2015/www/index.js.map +1 -1
- package/esm2015/www/ndef/parse-ndef-message.js.map +1 -1
- package/esm2015/www/nfc-com-protocol.js.map +1 -1
- package/fesm2015/iotize-device-com-nfc.cordova.js.map +1 -1
- package/package.json +2 -2
- package/plugin.xml +98 -98
- package/src/android/.gradle/4.10.1/fileHashes/fileHashes.bin +0 -0
- package/src/android/.gradle/4.10.1/fileHashes/fileHashes.lock +0 -0
- package/src/android/.gradle/vcs-1/gc.properties +0 -0
- package/src/android/src/com/chariotsolutions/nfc/plugin/NfcPlugin.java +1162 -1151
- package/src/ios/AppDelegate+NFC.swift +51 -51
- package/src/ios/NFCHelpers.swift +144 -144
- package/src/ios/NFCNDEFDelegate.swift +78 -78
- package/src/ios/NFCTagReader.swift +536 -506
- package/src/ios/NFCTapPlugin.swift +57 -1
- package/www/phonegap-nfc.js +911 -911
|
@@ -1,506 +1,536 @@
|
|
|
1
|
-
import CoreNFC
|
|
2
|
-
|
|
3
|
-
@available(iOS 13.0, *)
|
|
4
|
-
class NFCTagReader : NSObject, NFCTagReaderSessionDelegate {
|
|
5
|
-
|
|
6
|
-
private var plugin: NfcPlugin
|
|
7
|
-
|
|
8
|
-
typealias Completion = (Error?) -> ()
|
|
9
|
-
typealias CompletionWithJSONResponse = ([AnyHashable: Any]?, Error?) -> ()
|
|
10
|
-
|
|
11
|
-
internal var comSession: NFCTagReaderSession?
|
|
12
|
-
internal var tag: NFCTag?
|
|
13
|
-
|
|
14
|
-
static var MB_CTRL_DYN : UInt8 = 0x0D
|
|
15
|
-
|
|
16
|
-
internal var connectionCompleted : Completion?
|
|
17
|
-
internal var initSessionCompletion: Completion?
|
|
18
|
-
internal var onDiscoverCompletion : CompletionWithJSONResponse?
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
static var DELAY : UInt32 = 1000; // timeout resolution in millionths of second
|
|
22
|
-
static var NB_MAX_RETRY : Int = 50;
|
|
23
|
-
|
|
24
|
-
init(plugin: NfcPlugin) {
|
|
25
|
-
self.plugin = plugin
|
|
26
|
-
super.init()
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
func initSession( pollingOption: NFCTagReaderSession.PollingOption, alertMessage: String, completed: Completion? ) {
|
|
30
|
-
return self.initSession(pollingOption: pollingOption, alertMessage: alertMessage, completed: completed, onDiscover: nil)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
func initSession( pollingOption: NFCTagReaderSession.PollingOption, alertMessage: String, onDiscover: CompletionWithJSONResponse?) {
|
|
34
|
-
return self.initSession(pollingOption: pollingOption, alertMessage: alertMessage, completed: nil, onDiscover: onDiscover)
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
func initSession( pollingOption: NFCTagReaderSession.PollingOption, alertMessage: String, completed: Completion?, onDiscover: CompletionWithJSONResponse? ) {
|
|
38
|
-
connectionCompleted = completed
|
|
39
|
-
onDiscoverCompletion = onDiscover
|
|
40
|
-
|
|
41
|
-
if NFCNDEFReaderSession.readingAvailable {
|
|
42
|
-
comSession = NFCTagReaderSession(pollingOption: pollingOption, delegate: self, queue: nil)
|
|
43
|
-
comSession?.alertMessage = alertMessage
|
|
44
|
-
comSession?.begin()
|
|
45
|
-
} else {
|
|
46
|
-
completed?(NFCReaderError.readerTransceiveErrorSessionInvalidated as? Error)
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
func initSession( pollingOption: NFCTagReaderSession.PollingOption, alertMessage: String, initSessionCompletion: Completion?, onDiscover: CompletionWithJSONResponse? ) {
|
|
51
|
-
self.initSessionCompletion = initSessionCompletion
|
|
52
|
-
return self.initSession(pollingOption: pollingOption, alertMessage: alertMessage, completed: nil, onDiscover: onDiscover)
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
func invalidateSession( message :String) {
|
|
56
|
-
comSession?.alertMessage = message
|
|
57
|
-
comSession?.invalidate()
|
|
58
|
-
tag = nil
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
func tagReaderSessionDidBecomeActive(_ session: NFCTagReaderSession) {
|
|
62
|
-
// If necessary, you may perform additional operations on session start.
|
|
63
|
-
// At this point RF polling is enabled.
|
|
64
|
-
if let actualInitSessionCompletion = initSessionCompletion {
|
|
65
|
-
actualInitSessionCompletion(nil)
|
|
66
|
-
initSessionCompletion = nil //do not keep session completion
|
|
67
|
-
}
|
|
68
|
-
printNFC( "tagReaderSessionDidBecomeActive" )
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
1
|
+
import CoreNFC
|
|
2
|
+
|
|
3
|
+
@available(iOS 13.0, *)
|
|
4
|
+
class NFCTagReader : NSObject, NFCTagReaderSessionDelegate {
|
|
5
|
+
|
|
6
|
+
private var plugin: NfcPlugin
|
|
7
|
+
|
|
8
|
+
typealias Completion = (Error?) -> ()
|
|
9
|
+
typealias CompletionWithJSONResponse = ([AnyHashable: Any]?, Error?) -> ()
|
|
10
|
+
|
|
11
|
+
internal var comSession: NFCTagReaderSession?
|
|
12
|
+
internal var tag: NFCTag?
|
|
13
|
+
|
|
14
|
+
static var MB_CTRL_DYN : UInt8 = 0x0D
|
|
15
|
+
|
|
16
|
+
internal var connectionCompleted : Completion?
|
|
17
|
+
internal var initSessionCompletion: Completion?
|
|
18
|
+
internal var onDiscoverCompletion : CompletionWithJSONResponse?
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
static var DELAY : UInt32 = 1000; // timeout resolution in millionths of second
|
|
22
|
+
static var NB_MAX_RETRY : Int = 50;
|
|
23
|
+
|
|
24
|
+
init(plugin: NfcPlugin) {
|
|
25
|
+
self.plugin = plugin
|
|
26
|
+
super.init()
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
func initSession( pollingOption: NFCTagReaderSession.PollingOption, alertMessage: String, completed: Completion? ) {
|
|
30
|
+
return self.initSession(pollingOption: pollingOption, alertMessage: alertMessage, completed: completed, onDiscover: nil)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
func initSession( pollingOption: NFCTagReaderSession.PollingOption, alertMessage: String, onDiscover: CompletionWithJSONResponse?) {
|
|
34
|
+
return self.initSession(pollingOption: pollingOption, alertMessage: alertMessage, completed: nil, onDiscover: onDiscover)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
func initSession( pollingOption: NFCTagReaderSession.PollingOption, alertMessage: String, completed: Completion?, onDiscover: CompletionWithJSONResponse? ) {
|
|
38
|
+
connectionCompleted = completed
|
|
39
|
+
onDiscoverCompletion = onDiscover
|
|
40
|
+
|
|
41
|
+
if NFCNDEFReaderSession.readingAvailable {
|
|
42
|
+
comSession = NFCTagReaderSession(pollingOption: pollingOption, delegate: self, queue: nil)
|
|
43
|
+
comSession?.alertMessage = alertMessage
|
|
44
|
+
comSession?.begin()
|
|
45
|
+
} else {
|
|
46
|
+
completed?(NFCReaderError.readerTransceiveErrorSessionInvalidated as? Error)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
func initSession( pollingOption: NFCTagReaderSession.PollingOption, alertMessage: String, initSessionCompletion: Completion?, onDiscover: CompletionWithJSONResponse? ) {
|
|
51
|
+
self.initSessionCompletion = initSessionCompletion
|
|
52
|
+
return self.initSession(pollingOption: pollingOption, alertMessage: alertMessage, completed: nil, onDiscover: onDiscover)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
func invalidateSession( message :String) {
|
|
56
|
+
comSession?.alertMessage = message
|
|
57
|
+
comSession?.invalidate()
|
|
58
|
+
tag = nil
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
func tagReaderSessionDidBecomeActive(_ session: NFCTagReaderSession) {
|
|
62
|
+
// If necessary, you may perform additional operations on session start.
|
|
63
|
+
// At this point RF polling is enabled.
|
|
64
|
+
if let actualInitSessionCompletion = initSessionCompletion {
|
|
65
|
+
actualInitSessionCompletion(nil)
|
|
66
|
+
initSessionCompletion = nil //do not keep session completion
|
|
67
|
+
}
|
|
68
|
+
printNFC( "tagReaderSessionDidBecomeActive" )
|
|
69
|
+
if (self.comSession == nil) {
|
|
70
|
+
self.comSession = session
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
func tagReaderSession(_ session: NFCTagReaderSession, didInvalidateWithError error: Error) {
|
|
75
|
+
// If necessary, you may handle the error. Note session is no longer valid.
|
|
76
|
+
// You must create a new session to restart RF polling.
|
|
77
|
+
printNFC( "tagReaderSession:didInvalidateWithError - \(error)" )
|
|
78
|
+
if let actualInitSessionCompletion = initSessionCompletion {
|
|
79
|
+
actualInitSessionCompletion(error)
|
|
80
|
+
initSessionCompletion = nil //do not keep session completion
|
|
81
|
+
}
|
|
82
|
+
connectionCompleted?(error)
|
|
83
|
+
clear()
|
|
84
|
+
self.comSession = nil
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
func tagReaderSession(_ session: NFCTagReaderSession, didDetect tags: [NFCTag]) {
|
|
88
|
+
printNFC( "tagReaderSession:didDetectTag" )
|
|
89
|
+
guard let session = self.comSession else {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if tags.count > 1 {
|
|
93
|
+
// Restart polling in 500 milliseconds.
|
|
94
|
+
let retryInterval = DispatchTimeInterval.milliseconds(500)
|
|
95
|
+
session.alertMessage = "More than 1 Tap is detected. Please remove all tags and try again."
|
|
96
|
+
DispatchQueue.global().asyncAfter(deadline: .now() + retryInterval, execute: {
|
|
97
|
+
session.restartPolling()
|
|
98
|
+
})
|
|
99
|
+
return
|
|
100
|
+
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
guard let tag = tags.first else {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
self.tag = tag
|
|
108
|
+
|
|
109
|
+
if self.onDiscoverCompletion != nil {
|
|
110
|
+
|
|
111
|
+
Task {
|
|
112
|
+
self.onDiscoverCompletion?(await self.createJSON(tag: tag), nil)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if let connectionCompletion = self.connectionCompleted {
|
|
118
|
+
connect(tech: nil, connectionCompletion: connectionCompletion)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
//full transparent mode
|
|
124
|
+
|
|
125
|
+
@available(iOS 14.0, *)
|
|
126
|
+
func transceiveRaw(request: Data, completed: @escaping (Data?, Error?) -> (), nbTry: Int = NB_MAX_RETRY) {
|
|
127
|
+
guard (request.count >= 2) else {
|
|
128
|
+
completed(nil, NFCReaderError(NFCReaderError.readerErrorInvalidParameterLength))
|
|
129
|
+
return
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
switch self.tag {
|
|
133
|
+
case .iso15693(let iso15693Tag):
|
|
134
|
+
|
|
135
|
+
let flags = Int(request[0])
|
|
136
|
+
let commandCode = Int(request[1])
|
|
137
|
+
let dataToSend = request.dropFirst(2)
|
|
138
|
+
|
|
139
|
+
printNFC("TRANSCEIVE RAW SEND \(request.hexEncodedString())")
|
|
140
|
+
printNFC("TRANSCEIVE RAW SEND ON ISO15TAG TRY \(nbTry), SESSION READY \(String(describing: comSession?.isReady))")
|
|
141
|
+
|
|
142
|
+
iso15693Tag.sendRequest(requestFlags: flags, commandCode: commandCode, data: dataToSend, resultHandler: {(result: Result<(NFCISO15693ResponseFlag, Data?), any Error>) in
|
|
143
|
+
|
|
144
|
+
printNFC("SEND_REQUEST_CALLBACK")
|
|
145
|
+
switch result {
|
|
146
|
+
case .success((let flag, let data)):
|
|
147
|
+
let firstByteBuffer = withUnsafeBytes(of: flag.rawValue) { Data($0)}
|
|
148
|
+
var resultData = Data(firstByteBuffer)
|
|
149
|
+
if let nonNilData = data {
|
|
150
|
+
resultData.append(nonNilData)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
//Last delay to let ST25DV "breathe"
|
|
154
|
+
//usleep(1000*NFCTagReader.DELAY)
|
|
155
|
+
|
|
156
|
+
printNFC("TRANSCEIVE RAW RETURN \(resultData.hexEncodedString())")
|
|
157
|
+
completed(resultData, nil)
|
|
158
|
+
return
|
|
159
|
+
case .failure(let error):
|
|
160
|
+
printNFC("TRANSCEIVE RAW ERROR \(error.localizedDescription), TRY \(nbTry)")
|
|
161
|
+
if (nbTry >= 0) {
|
|
162
|
+
usleep(NFCTagReader.DELAY)
|
|
163
|
+
return self.transceiveRaw(request: request, completed: completed, nbTry: nbTry - 1)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
completed(nil, error)
|
|
167
|
+
return
|
|
168
|
+
}
|
|
169
|
+
})
|
|
170
|
+
break
|
|
171
|
+
case .iso7816(let iso7816Tag):
|
|
172
|
+
printNFC("TRANSCEIVE RAW SEND ON iso7816Tag TRY \(nbTry), SESSION READY \(String(describing: comSession?.isReady))")
|
|
173
|
+
|
|
174
|
+
guard let requestAPDU = NFCISO7816APDU(data: request) else {
|
|
175
|
+
completed(nil, NFCReaderError(NFCReaderError.readerErrorInvalidParameterLength) )
|
|
176
|
+
return
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
iso7816Tag.sendCommand(apdu: requestAPDU, resultHandler: {(result: Result<NFCISO7816ResponseAPDU, any Error>) in
|
|
180
|
+
|
|
181
|
+
printNFC("SEND_REQUEST_CALLBACK")
|
|
182
|
+
switch result {
|
|
183
|
+
case .success(let response):
|
|
184
|
+
var resultData = Data()
|
|
185
|
+
|
|
186
|
+
if let nonNilData = response.payload {
|
|
187
|
+
resultData.append(nonNilData)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
resultData.append(response.statusWord1)
|
|
191
|
+
resultData.append(response.statusWord2)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
printNFC("TRANSCEIVE RAW RETURN \(resultData.hexEncodedString())")
|
|
195
|
+
completed(resultData, nil)
|
|
196
|
+
return
|
|
197
|
+
case .failure(let error):
|
|
198
|
+
printNFC("TRANSCEIVE RAW ERROR \(error.localizedDescription), TRY \(nbTry)")
|
|
199
|
+
if (error is NFCReaderError) {
|
|
200
|
+
if (!iso7816Tag.isAvailable) {
|
|
201
|
+
completed(nil, error)
|
|
202
|
+
return
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if (nbTry >= 0) {
|
|
206
|
+
usleep(NFCTagReader.DELAY)
|
|
207
|
+
return self.transceiveRaw(request: request, completed: completed, nbTry: nbTry - 1)
|
|
208
|
+
}
|
|
209
|
+
completed(nil, error)
|
|
210
|
+
return
|
|
211
|
+
}
|
|
212
|
+
})
|
|
213
|
+
default:
|
|
214
|
+
completed(nil, NFCReaderError(NFCReaderError.readerTransceiveErrorTagConnectionLost))
|
|
215
|
+
return
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
func connect(tech: NfcTech?, connectionCompletion: Completion?) {
|
|
222
|
+
|
|
223
|
+
// Check if a tag has been detected
|
|
224
|
+
guard let tag = self.tag else {
|
|
225
|
+
connectionCompletion?(NFCReaderError( NFCReaderError.readerTransceiveErrorTagNotConnected ))
|
|
226
|
+
return
|
|
227
|
+
}
|
|
228
|
+
// Connect to tag
|
|
229
|
+
if let actualTech = tech {
|
|
230
|
+
switch actualTech {
|
|
231
|
+
case NfcTech.NfcV:
|
|
232
|
+
guard case .iso15693(_) = self.tag else {
|
|
233
|
+
connectionCompletion?(NFCReaderError( NFCReaderError.readerErrorInvalidParameter ))
|
|
234
|
+
return
|
|
235
|
+
}
|
|
236
|
+
break
|
|
237
|
+
case NfcTech.IsoDep:
|
|
238
|
+
fallthrough
|
|
239
|
+
case NfcTech.NfcA:
|
|
240
|
+
fallthrough
|
|
241
|
+
case NfcTech.NfcB:
|
|
242
|
+
guard case .iso7816(_) = self.tag else {
|
|
243
|
+
connectionCompletion?(NFCReaderError( NFCReaderError.readerErrorInvalidParameter ))
|
|
244
|
+
return
|
|
245
|
+
}
|
|
246
|
+
break
|
|
247
|
+
default:
|
|
248
|
+
connectionCompletion?(NFCReaderError( NFCReaderError.readerErrorInvalidParameter ))
|
|
249
|
+
return
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
self.connectionCompleted = connectionCompletion
|
|
253
|
+
|
|
254
|
+
self.comSession?.connect(to: tag) { [weak self] (error: Error?) in
|
|
255
|
+
guard let strongSelf = self else {
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if error != nil {
|
|
260
|
+
let error = NFCReaderError( NFCReaderError.readerTransceiveErrorTagNotConnected )
|
|
261
|
+
strongSelf.invalidateSession( message: error.localizedDescription )
|
|
262
|
+
strongSelf.connectionCompleted?(error)
|
|
263
|
+
strongSelf.connectionCompleted = nil
|
|
264
|
+
return
|
|
265
|
+
}
|
|
266
|
+
printNFC( "connected to tag" )
|
|
267
|
+
strongSelf.tag = tag
|
|
268
|
+
strongSelf.connectionCompleted?(nil)
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
func clear() {
|
|
273
|
+
self.tag = nil
|
|
274
|
+
self.connectionCompleted = nil
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
@available(iOS 13.0, *)
|
|
279
|
+
extension NFCTagReader {
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
func transceiveTap(request: Data, completed: @escaping (Data?, Error?)->()){
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
checkMBEnabled( completed: { ( error: Error?) in
|
|
287
|
+
if nil != error {
|
|
288
|
+
self.invalidateSession( message: error?.localizedDescription ?? "Error" )
|
|
289
|
+
completed( nil, error )
|
|
290
|
+
return
|
|
291
|
+
}
|
|
292
|
+
printNFC( "Com enabled" )
|
|
293
|
+
self.sendRequest( request: request,
|
|
294
|
+
nbTry: NFCTagReader.NB_MAX_RETRY,
|
|
295
|
+
completed: { ( response: Data?, error: Error?) in
|
|
296
|
+
if nil != error {
|
|
297
|
+
self.invalidateSession( message: error?.localizedDescription ?? "Error" )
|
|
298
|
+
completed( nil, error )
|
|
299
|
+
return
|
|
300
|
+
}
|
|
301
|
+
completed(response, nil)
|
|
302
|
+
})
|
|
303
|
+
})
|
|
304
|
+
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
func sendRequest(request: Data, nbTry: Int, completed: @escaping (Data?, Error?)->() ) {
|
|
308
|
+
guard case let .iso15693(iso15693tag) = self.tag else {
|
|
309
|
+
let error = NFCReaderError( NFCReaderError.readerTransceiveErrorTagNotConnected )
|
|
310
|
+
invalidateSession( message: error.localizedDescription )
|
|
311
|
+
completed(nil, error )
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
if (nbTry <= 0){
|
|
316
|
+
let error = NFCReaderError( NFCReaderError.readerTransceiveErrorRetryExceeded )
|
|
317
|
+
invalidateSession( message: error.localizedDescription )
|
|
318
|
+
completed(nil, error )
|
|
319
|
+
return
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
var parameters = Data( bytes:[request.count - 1], count: 1 )
|
|
323
|
+
parameters.append(request)
|
|
324
|
+
printNFC( "Send - \(parameters.hexEncodedString())" )
|
|
325
|
+
|
|
326
|
+
iso15693tag.customCommand(requestFlags: [.highDataRate],
|
|
327
|
+
customCommandCode: 0xAA,
|
|
328
|
+
customRequestParameters: parameters,
|
|
329
|
+
completionHandler: { (response: Data?, error: Error?) in
|
|
330
|
+
if nil != error {
|
|
331
|
+
usleep(NFCTagReader.DELAY)
|
|
332
|
+
self.sendRequest( request: request, nbTry: nbTry - 1, completed: completed )
|
|
333
|
+
return
|
|
334
|
+
}
|
|
335
|
+
usleep(NFCTagReader.DELAY * 10) // free ST25DV for SPI
|
|
336
|
+
self.readResponse( nbTry: nbTry , completed: completed)
|
|
337
|
+
})
|
|
338
|
+
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
func readResponse( nbTry: Int, completed: @escaping (Data?, Error?)->() ) {
|
|
342
|
+
|
|
343
|
+
guard case let .iso15693(iso15693tag) = self.tag else {
|
|
344
|
+
let error = NFCReaderError( NFCReaderError.readerTransceiveErrorTagNotConnected )
|
|
345
|
+
invalidateSession( message: error.localizedDescription )
|
|
346
|
+
completed( nil, error )
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
//We have tried enough timeout and return
|
|
351
|
+
if (nbTry <= 0){
|
|
352
|
+
printNFC( "Read Abandonned" )
|
|
353
|
+
let error = NFCReaderError( NFCReaderError.readerTransceiveErrorRetryExceeded )
|
|
354
|
+
invalidateSession( message: error.localizedDescription )
|
|
355
|
+
completed( nil, error )
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
printNFC( "Read \(nbTry)" )
|
|
360
|
+
|
|
361
|
+
//check Mailbox
|
|
362
|
+
iso15693tag.customCommand(requestFlags: [.highDataRate],
|
|
363
|
+
customCommandCode: 0xAD,
|
|
364
|
+
customRequestParameters: Data(bytes: [UInt8(0x0D)], count: 1),
|
|
365
|
+
completionHandler: { (response: Data, error: Error?) in
|
|
366
|
+
if nil != error {
|
|
367
|
+
usleep(NFCTagReader.DELAY)
|
|
368
|
+
self.readResponse( nbTry: nbTry - 1, completed: completed )
|
|
369
|
+
return
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
printNFC( "Read response" )
|
|
373
|
+
|
|
374
|
+
if ( (response.count >= 1) && ( (response[0]&0x1) != 0 ) && ( (response[0]&0x2) != 0 )){
|
|
375
|
+
|
|
376
|
+
printNFC( "Read Value - \(Data(response).hexEncodedString())" )
|
|
377
|
+
iso15693tag.customCommand(requestFlags: [.highDataRate],
|
|
378
|
+
customCommandCode: 0xAC,
|
|
379
|
+
customRequestParameters: Data(bytes: [UInt8(0), UInt8(0)], count: 2),
|
|
380
|
+
completionHandler: { (response: Data, error: Error?) in
|
|
381
|
+
if nil != error {
|
|
382
|
+
self.invalidateSession( message: error?.localizedDescription ?? "Error" )
|
|
383
|
+
completed( nil, error )
|
|
384
|
+
return
|
|
385
|
+
}
|
|
386
|
+
printNFC( "got Value - \(Data(response).hexEncodedString())" )
|
|
387
|
+
completed(response,nil)
|
|
388
|
+
return
|
|
389
|
+
})
|
|
390
|
+
|
|
391
|
+
}
|
|
392
|
+
else {
|
|
393
|
+
usleep(NFCTagReader.DELAY)
|
|
394
|
+
self.readResponse( nbTry: nbTry - 1, completed: completed )
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
})
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
func checkMBEnabled(completed: @escaping (Error?)->()) {
|
|
403
|
+
guard case let .iso15693(iso15693tag) = self.tag else {
|
|
404
|
+
let error = NFCReaderError( NFCReaderError.readerTransceiveErrorTagNotConnected )
|
|
405
|
+
invalidateSession( message: error.localizedDescription )
|
|
406
|
+
completed( error )
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
//Read Config
|
|
411
|
+
iso15693tag.customCommand(requestFlags: [.highDataRate],
|
|
412
|
+
customCommandCode: 0xAD,
|
|
413
|
+
customRequestParameters: Data(bytes: [UInt8(0x0D)], count: 1),
|
|
414
|
+
completionHandler: { (response: Data, error: Error?) in
|
|
415
|
+
if nil != error {
|
|
416
|
+
self.invalidateSession( message: error?.localizedDescription ?? "Error" )
|
|
417
|
+
completed(error)
|
|
418
|
+
return
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
if ( response.count == 0) {
|
|
422
|
+
let error = NFCReaderError( NFCReaderError.readerTransceiveErrorTagResponseError )
|
|
423
|
+
self.invalidateSession( message: error.localizedDescription )
|
|
424
|
+
completed( error )
|
|
425
|
+
return
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
let current = response[0];
|
|
430
|
+
|
|
431
|
+
//We should reset mailbox
|
|
432
|
+
if ( (current != 0x41) && (current != 0x81) ) {
|
|
433
|
+
|
|
434
|
+
//disable
|
|
435
|
+
iso15693tag.customCommand(requestFlags: [.highDataRate],
|
|
436
|
+
customCommandCode: 0xAE,
|
|
437
|
+
customRequestParameters: Data(bytes: [UInt8(0x0D), UInt8(0x00)], count: 2),
|
|
438
|
+
completionHandler: { (response: Data, error: Error?) in
|
|
439
|
+
if nil != error {
|
|
440
|
+
self.invalidateSession( message: error?.localizedDescription ?? "Error" )
|
|
441
|
+
completed( error )
|
|
442
|
+
return
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
//enable
|
|
446
|
+
iso15693tag.customCommand(requestFlags: [.highDataRate],
|
|
447
|
+
customCommandCode: 0xAE,
|
|
448
|
+
customRequestParameters: Data(bytes: [UInt8(0x0D), UInt8(0x01)], count: 2),
|
|
449
|
+
completionHandler: { (response: Data, error: Error?) in
|
|
450
|
+
if nil != error {
|
|
451
|
+
self.invalidateSession( message: error?.localizedDescription ?? "Error" )
|
|
452
|
+
completed( error )
|
|
453
|
+
return
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
completed(nil)
|
|
457
|
+
return
|
|
458
|
+
|
|
459
|
+
})
|
|
460
|
+
})
|
|
461
|
+
}
|
|
462
|
+
//We are ok to go
|
|
463
|
+
else
|
|
464
|
+
{
|
|
465
|
+
completed(nil)
|
|
466
|
+
return
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
})
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
func sendTap( request: String, completed: @escaping (Data?,Error?)->() ) {
|
|
473
|
+
|
|
474
|
+
guard NFCNDEFReaderSession.readingAvailable else {
|
|
475
|
+
let error = NFCReaderError( NFCReaderError.readerErrorUnsupportedFeature )
|
|
476
|
+
invalidateSession( message: error.localizedDescription )
|
|
477
|
+
completed( nil, error )
|
|
478
|
+
return
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
guard comSession != nil && comSession!.isReady else {
|
|
482
|
+
let error = NFCReaderError( NFCReaderError.readerTransceiveErrorTagNotConnected )
|
|
483
|
+
invalidateSession( message: error.localizedDescription )
|
|
484
|
+
completed( nil, error )
|
|
485
|
+
return
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
let requestData : Data = request.dataFromHexString()
|
|
489
|
+
printNFC( "Transceive - \(requestData.hexEncodedString())" )
|
|
490
|
+
transceiveTap(request: requestData,
|
|
491
|
+
completed: { ( response: Data?, error: Error?) in
|
|
492
|
+
if nil != error {
|
|
493
|
+
self.invalidateSession( message: error?.localizedDescription ?? "Error" )
|
|
494
|
+
completed( nil, error )
|
|
495
|
+
return
|
|
496
|
+
}
|
|
497
|
+
else {
|
|
498
|
+
completed( response, nil)
|
|
499
|
+
return
|
|
500
|
+
}
|
|
501
|
+
})
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
func createJSON(tag: NFCTag) async -> [AnyHashable: Any] {
|
|
505
|
+
try? await self.comSession?.connect(to: tag)
|
|
506
|
+
return await tag.toJSON(isTapDiscoveryEnabled: self.plugin.isTapDiscoveryEnabled())
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
func writeNDEF(message: NFCNDEFMessage, completed: @escaping (Error?)->()) {
|
|
510
|
+
guard let ndefTag = self.tag else {
|
|
511
|
+
completed(NFCReaderError(NFCReaderError.readerTransceiveErrorTagNotConnected))
|
|
512
|
+
return
|
|
513
|
+
}
|
|
514
|
+
var tag: NFCNDEFTag? = nil
|
|
515
|
+
switch(ndefTag) {
|
|
516
|
+
case .iso15693(let iso15693Tag):
|
|
517
|
+
tag = iso15693Tag
|
|
518
|
+
default:
|
|
519
|
+
completed(NFCReaderError(NFCReaderError.readerTransceiveErrorTagNotConnected))
|
|
520
|
+
return
|
|
521
|
+
|
|
522
|
+
}
|
|
523
|
+
if (tag != nil) {
|
|
524
|
+
tag?.writeNDEF(message, completionHandler: {
|
|
525
|
+
(error:Error?) in
|
|
526
|
+
if (error != nil) {
|
|
527
|
+
completed(error)
|
|
528
|
+
} else {
|
|
529
|
+
completed(nil)
|
|
530
|
+
}
|
|
531
|
+
})
|
|
532
|
+
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|