@iotize/device-com-nfc.cordova 3.3.0 → 3.5.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/bundles/iotize-device-com-nfc.cordova.umd.js +199 -12
- 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/iotize-device-com-nfc.cordova.ngsummary.json +1 -1
- package/esm2015/public_api.ngsummary.json +1 -1
- package/esm2015/www/cordova-interface.js.map +1 -1
- package/esm2015/www/index.js +3 -1
- package/esm2015/www/index.js.map +1 -1
- package/esm2015/www/index.metadata.json +1 -1
- package/esm2015/www/index.ngsummary.json +1 -1
- package/esm2015/www/ndef/definitions.js +17 -0
- package/esm2015/www/ndef/definitions.js.map +1 -0
- package/esm2015/www/ndef/definitions.metadata.json +1 -0
- package/esm2015/www/ndef/definitions.ngsummary.json +1 -0
- package/esm2015/www/ndef/parse-ndef-message.js +165 -0
- package/esm2015/www/ndef/parse-ndef-message.js.map +1 -0
- package/esm2015/www/ndef/parse-ndef-message.metadata.json +1 -0
- package/esm2015/www/ndef/parse-ndef-message.ngsummary.json +1 -0
- package/esm2015/www/nfc-com-protocol.js +1 -1
- package/esm2015/www/nfc-com-protocol.js.map +1 -1
- package/esm2015/www/tap-ndef/definitions.js.map +1 -1
- package/esm2015/www/tap-ndef/parse-ndef-message.js +3 -8
- package/esm2015/www/tap-ndef/parse-ndef-message.js.map +1 -1
- package/esm2015/www/utility.js +11 -0
- package/esm2015/www/utility.js.map +1 -0
- package/esm2015/www/utility.metadata.json +1 -0
- package/esm2015/www/utility.ngsummary.json +1 -0
- package/fesm2015/iotize-device-com-nfc.cordova.js +195 -10
- package/fesm2015/iotize-device-com-nfc.cordova.js.map +1 -1
- package/iotize-device-com-nfc.cordova-3.5.0.tgz +0 -0
- package/iotize-device-com-nfc.cordova.metadata.json +1 -1
- package/package.json +1 -1
- package/plugin.xml +99 -97
- package/src/android/src/com/chariotsolutions/nfc/plugin/NfcPlugin.java +1308 -1183
- package/src/ios/AppDelegate+NFC.swift +51 -51
- package/src/ios/ISO15Reader.swift +115 -130
- package/src/ios/NFCHelpers.swift +94 -0
- package/src/ios/NFCNDEFDelegate.swift +51 -78
- package/src/ios/NFCPlugin-Bridging-Header.h +1 -1
- package/src/ios/NFCTagReader.swift +103 -0
- package/src/ios/NFCTapPlugin.swift +152 -5
- package/www/cordova-interface.d.ts +14 -2
- package/www/index.d.ts +3 -1
- package/www/ndef/definitions.d.ts +15 -0
- package/www/ndef/parse-ndef-message.d.ts +69 -0
- package/www/phonegap-nfc.js +928 -885
- package/www/utility.d.ts +2 -0
|
@@ -1,1183 +1,1308 @@
|
|
|
1
|
-
package com.chariotsolutions.nfc.plugin;
|
|
2
|
-
|
|
3
|
-
import android.app.Activity;
|
|
4
|
-
import android.app.PendingIntent;
|
|
5
|
-
import android.content.Context;
|
|
6
|
-
import android.content.Intent;
|
|
7
|
-
import android.content.IntentFilter;
|
|
8
|
-
import android.content.IntentFilter.MalformedMimeTypeException;
|
|
9
|
-
import android.net.Uri;
|
|
10
|
-
import android.nfc.FormatException;
|
|
11
|
-
import android.nfc.NdefMessage;
|
|
12
|
-
import android.nfc.NdefRecord;
|
|
13
|
-
import android.nfc.NfcAdapter;
|
|
14
|
-
import android.nfc.NfcEvent;
|
|
15
|
-
import android.nfc.Tag;
|
|
16
|
-
import android.nfc.TagLostException;
|
|
17
|
-
import android.nfc.tech.Ndef;
|
|
18
|
-
import android.nfc.tech.NdefFormatable;
|
|
19
|
-
import android.
|
|
20
|
-
import android.os.
|
|
21
|
-
import android.
|
|
22
|
-
import android.
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
import com.iotize.android.communication.client.impl.
|
|
26
|
-
import com.iotize.android.communication.client.impl.
|
|
27
|
-
import com.iotize.android.communication.protocol.
|
|
28
|
-
import com.iotize.android.communication.protocol.nfc.
|
|
29
|
-
import com.iotize.android.communication.protocol.nfc.
|
|
30
|
-
import com.iotize.android.
|
|
31
|
-
import com.iotize.android.
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
import org.apache.cordova.
|
|
35
|
-
import org.apache.cordova.
|
|
36
|
-
import org.apache.cordova.
|
|
37
|
-
import org.
|
|
38
|
-
import org.json.
|
|
39
|
-
import org.json.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
import java.
|
|
43
|
-
import java.lang.reflect.
|
|
44
|
-
import java.
|
|
45
|
-
import java.
|
|
46
|
-
import java.util.
|
|
47
|
-
import java.util.
|
|
48
|
-
|
|
49
|
-
import
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
private static final String
|
|
58
|
-
private static final String
|
|
59
|
-
private static final String
|
|
60
|
-
private static final String
|
|
61
|
-
private static final String
|
|
62
|
-
private static final String
|
|
63
|
-
private static final String
|
|
64
|
-
private static final String
|
|
65
|
-
private static final String
|
|
66
|
-
private static final String
|
|
67
|
-
private static final String
|
|
68
|
-
private static final String
|
|
69
|
-
private static final String
|
|
70
|
-
private static final String
|
|
71
|
-
private static final String
|
|
72
|
-
|
|
73
|
-
private static final String
|
|
74
|
-
|
|
75
|
-
private static final String
|
|
76
|
-
private static final String
|
|
77
|
-
|
|
78
|
-
private static final String
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
private static final String
|
|
85
|
-
|
|
86
|
-
private static final String
|
|
87
|
-
private static final String
|
|
88
|
-
|
|
89
|
-
private static final String
|
|
90
|
-
private static final String
|
|
91
|
-
private static final String
|
|
92
|
-
private static final String
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
private
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
private final
|
|
107
|
-
private final
|
|
108
|
-
|
|
109
|
-
private
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
private
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
private
|
|
117
|
-
|
|
118
|
-
private
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
private
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
if
|
|
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
|
-
} else if (action.equalsIgnoreCase(
|
|
171
|
-
|
|
172
|
-
} else if (action.equalsIgnoreCase(
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
} else if (action.equalsIgnoreCase(
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
} else if (action.
|
|
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
|
-
callbackContext
|
|
212
|
-
|
|
213
|
-
} else if (action.equalsIgnoreCase(
|
|
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
|
-
} else {
|
|
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
|
-
private void
|
|
366
|
-
|
|
367
|
-
restartNfc();
|
|
368
|
-
callbackContext.success();
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
private void
|
|
372
|
-
|
|
373
|
-
restartNfc();
|
|
374
|
-
callbackContext.success();
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
private void
|
|
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
|
-
private void
|
|
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
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
if (
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
}
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
private void
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
private void
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
} catch (IllegalStateException e) {
|
|
694
|
-
// issue
|
|
695
|
-
Log.w(TAG, "Illegal State Exception
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
private void
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
}
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
}
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
return
|
|
1006
|
-
}
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
if (
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
}
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
if (nfcProtocol
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
}
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1
|
+
package com.chariotsolutions.nfc.plugin;
|
|
2
|
+
|
|
3
|
+
import android.app.Activity;
|
|
4
|
+
import android.app.PendingIntent;
|
|
5
|
+
import android.content.Context;
|
|
6
|
+
import android.content.Intent;
|
|
7
|
+
import android.content.IntentFilter;
|
|
8
|
+
import android.content.IntentFilter.MalformedMimeTypeException;
|
|
9
|
+
import android.net.Uri;
|
|
10
|
+
import android.nfc.FormatException;
|
|
11
|
+
import android.nfc.NdefMessage;
|
|
12
|
+
import android.nfc.NdefRecord;
|
|
13
|
+
import android.nfc.NfcAdapter;
|
|
14
|
+
import android.nfc.NfcEvent;
|
|
15
|
+
import android.nfc.Tag;
|
|
16
|
+
import android.nfc.TagLostException;
|
|
17
|
+
import android.nfc.tech.Ndef;
|
|
18
|
+
import android.nfc.tech.NdefFormatable;
|
|
19
|
+
import android.nfc.tech.TagTechnology;
|
|
20
|
+
import android.os.Bundle;
|
|
21
|
+
import android.os.Parcelable;
|
|
22
|
+
import android.util.Log;
|
|
23
|
+
import android.widget.Toast;
|
|
24
|
+
|
|
25
|
+
import com.iotize.android.communication.client.impl.EncryptionAlgo;
|
|
26
|
+
import com.iotize.android.communication.client.impl.TapClient;
|
|
27
|
+
import com.iotize.android.communication.client.impl.protocol.ProtocolFactory;
|
|
28
|
+
import com.iotize.android.communication.protocol.nfc.NFCIntentParser;
|
|
29
|
+
import com.iotize.android.communication.protocol.nfc.NFCProtocol;
|
|
30
|
+
import com.iotize.android.communication.protocol.nfc.NFCProtocolFactory;
|
|
31
|
+
import com.iotize.android.core.util.Helper;
|
|
32
|
+
import com.iotize.android.device.device.impl.IoTizeDevice;
|
|
33
|
+
|
|
34
|
+
import org.apache.cordova.CallbackContext;
|
|
35
|
+
import org.apache.cordova.CordovaArgs;
|
|
36
|
+
import org.apache.cordova.CordovaPlugin;
|
|
37
|
+
import org.apache.cordova.PluginResult;
|
|
38
|
+
import org.json.JSONArray;
|
|
39
|
+
import org.json.JSONException;
|
|
40
|
+
import org.json.JSONObject;
|
|
41
|
+
|
|
42
|
+
import java.io.IOException;
|
|
43
|
+
import java.lang.reflect.Field;
|
|
44
|
+
import java.lang.reflect.InvocationTargetException;
|
|
45
|
+
import java.lang.reflect.Method;
|
|
46
|
+
import java.util.ArrayList;
|
|
47
|
+
import java.util.Arrays;
|
|
48
|
+
import java.util.Iterator;
|
|
49
|
+
import java.util.List;
|
|
50
|
+
|
|
51
|
+
import io.reactivex.annotations.NonNull;
|
|
52
|
+
import io.reactivex.annotations.Nullable;
|
|
53
|
+
|
|
54
|
+
// using wildcard imports so we can support Cordova 3.x
|
|
55
|
+
|
|
56
|
+
public class NfcPlugin extends CordovaPlugin implements NfcAdapter.OnNdefPushCompleteCallback {
|
|
57
|
+
private static final String REGISTER_MIME_TYPE = "registerMimeType";
|
|
58
|
+
private static final String REMOVE_MIME_TYPE = "removeMimeType";
|
|
59
|
+
private static final String REGISTER_NDEF = "registerNdef";
|
|
60
|
+
private static final String REMOVE_NDEF = "removeNdef";
|
|
61
|
+
private static final String REGISTER_NDEF_FORMATABLE = "registerNdefFormatable";
|
|
62
|
+
private static final String REGISTER_DEFAULT_TAG = "registerTag";
|
|
63
|
+
private static final String REMOVE_DEFAULT_TAG = "removeTag";
|
|
64
|
+
private static final String WRITE_TAG = "writeTag";
|
|
65
|
+
private static final String MAKE_READ_ONLY = "makeReadOnly";
|
|
66
|
+
private static final String ERASE_TAG = "eraseTag";
|
|
67
|
+
private static final String SHARE_TAG = "shareTag";
|
|
68
|
+
private static final String UNSHARE_TAG = "unshareTag";
|
|
69
|
+
private static final String HANDOVER = "handover"; // Android Beam
|
|
70
|
+
private static final String STOP_HANDOVER = "stopHandover";
|
|
71
|
+
private static final String ENABLED = "enabled";
|
|
72
|
+
private static final String INIT = "init";
|
|
73
|
+
private static final String SHOW_SETTINGS = "showSettings";
|
|
74
|
+
|
|
75
|
+
private static final String NDEF = "ndef";
|
|
76
|
+
private static final String NDEF_MIME = "ndef-mime";
|
|
77
|
+
private static final String NDEF_FORMATABLE = "ndef-formatable";
|
|
78
|
+
private static final String TAG_DEFAULT = "tag";
|
|
79
|
+
|
|
80
|
+
private static final String READER_MODE = "readerMode";
|
|
81
|
+
private static final String DISABLE_READER_MODE = "disableReaderMode";
|
|
82
|
+
|
|
83
|
+
// TagTechnology IsoDep, NfcA, NfcB, NfcV, NfcF, MifareClassic, MifareUltralight
|
|
84
|
+
private static final String CONNECT = "connect";
|
|
85
|
+
private static final String CLOSE = "close";
|
|
86
|
+
private static final String TRANSCEIVE_TAP = "transceiveTap";
|
|
87
|
+
private static final String TRANSCEIVE = "transceive";
|
|
88
|
+
|
|
89
|
+
private static final String NFC_TAP_DEVICE = "nfc-tap-device";
|
|
90
|
+
private static final String PREF_ENABLE_TAP_DEVICE_DISCOVERY = "EnableNFCTapDeviceDiscovery";
|
|
91
|
+
private static final String PREF_TAP_DEVICE_MIME_TYPE = "NFCTapDeviceMimeType";
|
|
92
|
+
private static final String PREF_ENABLE_NFC_PAIRING = "EnableNFCPairing";
|
|
93
|
+
private static final String PREF_ENABLE_ENCRYPTION_WITH_NFC = "EnableEncryptionWithNFC";
|
|
94
|
+
private static final String PREF_NFC_PAIRING_DONE_TOAST_MESSAGE = "NFCParingDoneToastMessage";
|
|
95
|
+
private static final String REGISTER_NFC_TAP_DEVICE = "registerTapDevice";
|
|
96
|
+
private static final String SET_TAP_DEVICE_DISCOVERY_ENABLED = "setTapDeviceDiscoveryEnabled";
|
|
97
|
+
|
|
98
|
+
@Nullable
|
|
99
|
+
private TagTechnology tagTechnology = null;
|
|
100
|
+
|
|
101
|
+
@Nullable
|
|
102
|
+
private Class<?> tagTechnologyClass;
|
|
103
|
+
|
|
104
|
+
private static final String CHANNEL = "channel";
|
|
105
|
+
|
|
106
|
+
private static final String STATUS_NFC_OK = "NFC_OK";
|
|
107
|
+
private static final String STATUS_NO_NFC = "NO_NFC";
|
|
108
|
+
private static final String STATUS_NFC_DISABLED = "NFC_DISABLED";
|
|
109
|
+
private static final String STATUS_NDEF_PUSH_DISABLED = "NDEF_PUSH_DISABLED";
|
|
110
|
+
|
|
111
|
+
private static final String TAG = "NfcPlugin";
|
|
112
|
+
private final List<IntentFilter> intentFilters = new ArrayList<>();
|
|
113
|
+
private final ArrayList<String[]> techLists = new ArrayList<>();
|
|
114
|
+
|
|
115
|
+
private NdefMessage p2pMessage = null;
|
|
116
|
+
private PendingIntent pendingIntent = null;
|
|
117
|
+
|
|
118
|
+
private Intent savedIntent = null;
|
|
119
|
+
|
|
120
|
+
private CallbackContext readerModeCallback;
|
|
121
|
+
@Nullable
|
|
122
|
+
private CallbackContext channelCallback;
|
|
123
|
+
private CallbackContext shareTagCallback;
|
|
124
|
+
private CallbackContext handoverCallback;
|
|
125
|
+
|
|
126
|
+
@Nullable
|
|
127
|
+
private NFCProtocol nfcProtocol;
|
|
128
|
+
@Nullable
|
|
129
|
+
private IoTizeDevice mLastTapDiscovered;
|
|
130
|
+
@Nullable
|
|
131
|
+
private Intent mLastTapDiscoveredIntent;
|
|
132
|
+
|
|
133
|
+
private boolean _isTapDeviceDiscoveryEnabled = true;
|
|
134
|
+
|
|
135
|
+
@Override
|
|
136
|
+
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
|
|
137
|
+
|
|
138
|
+
Log.d(TAG, "execute " + action);
|
|
139
|
+
|
|
140
|
+
// showSettings can be called if NFC is disabled
|
|
141
|
+
// might want to skip this if NO_NFC
|
|
142
|
+
if (action.equalsIgnoreCase(SHOW_SETTINGS)) {
|
|
143
|
+
showSettings(callbackContext);
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// the channel is set up when the plugin starts
|
|
148
|
+
if (action.equalsIgnoreCase(CHANNEL)) {
|
|
149
|
+
channelCallback = callbackContext;
|
|
150
|
+
return true; // short circuit
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// allow reader mode to be disabled even if nfc is disabled
|
|
154
|
+
if (action.equalsIgnoreCase(DISABLE_READER_MODE)) {
|
|
155
|
+
disableReaderMode(callbackContext);
|
|
156
|
+
return true; // short circuit
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (!getNfcStatus().equals(STATUS_NFC_OK)) {
|
|
160
|
+
callbackContext.error(getNfcStatus());
|
|
161
|
+
return true; // short circuit
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
createPendingIntent();
|
|
165
|
+
|
|
166
|
+
if (action.equalsIgnoreCase(READER_MODE)) {
|
|
167
|
+
int flags = data.getInt(0);
|
|
168
|
+
readerMode(flags, callbackContext);
|
|
169
|
+
|
|
170
|
+
} else if (action.equalsIgnoreCase(REGISTER_MIME_TYPE)) {
|
|
171
|
+
registerMimeType(data, callbackContext);
|
|
172
|
+
} else if (action.equalsIgnoreCase(REGISTER_NFC_TAP_DEVICE)) {
|
|
173
|
+
// JSONObject jsonStringOptions = data.getJSONObject(0);
|
|
174
|
+
registerTapDevice(callbackContext);
|
|
175
|
+
} else if (action.equalsIgnoreCase(REMOVE_MIME_TYPE)) {
|
|
176
|
+
removeMimeType(data, callbackContext);
|
|
177
|
+
|
|
178
|
+
} else if (action.equalsIgnoreCase(REGISTER_NDEF)) {
|
|
179
|
+
registerNdef(callbackContext);
|
|
180
|
+
} else if (action.equalsIgnoreCase(REMOVE_NDEF)) {
|
|
181
|
+
removeNdef(callbackContext);
|
|
182
|
+
|
|
183
|
+
} else if (action.equalsIgnoreCase(REGISTER_NDEF_FORMATABLE)) {
|
|
184
|
+
registerNdefFormatable(callbackContext);
|
|
185
|
+
|
|
186
|
+
} else if (action.equals(REGISTER_DEFAULT_TAG)) {
|
|
187
|
+
registerDefaultTag(callbackContext);
|
|
188
|
+
|
|
189
|
+
} else if (action.equals(REMOVE_DEFAULT_TAG)) {
|
|
190
|
+
removeDefaultTag(callbackContext);
|
|
191
|
+
|
|
192
|
+
} else if (action.equalsIgnoreCase(WRITE_TAG)) {
|
|
193
|
+
writeTag(data, callbackContext);
|
|
194
|
+
|
|
195
|
+
} else if (action.equalsIgnoreCase(MAKE_READ_ONLY)) {
|
|
196
|
+
makeReadOnly(callbackContext);
|
|
197
|
+
|
|
198
|
+
} else if (action.equalsIgnoreCase(ERASE_TAG)) {
|
|
199
|
+
eraseTag(callbackContext);
|
|
200
|
+
|
|
201
|
+
} else if (action.equalsIgnoreCase(SHARE_TAG)) {
|
|
202
|
+
shareTag(data, callbackContext);
|
|
203
|
+
|
|
204
|
+
} else if (action.equalsIgnoreCase(UNSHARE_TAG)) {
|
|
205
|
+
unshareTag(callbackContext);
|
|
206
|
+
|
|
207
|
+
} else if (action.equalsIgnoreCase(HANDOVER)) {
|
|
208
|
+
handover(data, callbackContext);
|
|
209
|
+
|
|
210
|
+
} else if (action.equalsIgnoreCase(STOP_HANDOVER)) {
|
|
211
|
+
stopHandover(callbackContext);
|
|
212
|
+
|
|
213
|
+
} else if (action.equalsIgnoreCase(INIT)) {
|
|
214
|
+
init(callbackContext);
|
|
215
|
+
|
|
216
|
+
} else if (action.equalsIgnoreCase(ENABLED)) {
|
|
217
|
+
// status is checked before every call
|
|
218
|
+
// if code made it here, NFC is enabled
|
|
219
|
+
callbackContext.success(STATUS_NFC_OK);
|
|
220
|
+
|
|
221
|
+
} else if (action.equalsIgnoreCase(CONNECT)) {
|
|
222
|
+
String tech = data.getString(0);
|
|
223
|
+
int timeout = data.optInt(1, -1);
|
|
224
|
+
connect(tech, timeout, callbackContext);
|
|
225
|
+
|
|
226
|
+
} else if (action.equalsIgnoreCase(TRANSCEIVE)) {
|
|
227
|
+
CordovaArgs args = new CordovaArgs(data); // execute is using the old signature with JSON data
|
|
228
|
+
|
|
229
|
+
byte[] command = args.getArrayBuffer(0);
|
|
230
|
+
transceiveRaw(command, callbackContext);
|
|
231
|
+
|
|
232
|
+
} else if (action.equalsIgnoreCase(TRANSCEIVE_TAP)) {
|
|
233
|
+
CordovaArgs args = new CordovaArgs(data); // execute is using the old signature with JSON data
|
|
234
|
+
|
|
235
|
+
byte[] command = args.getArrayBuffer(0);
|
|
236
|
+
transceiveTap(command, callbackContext);
|
|
237
|
+
|
|
238
|
+
} else if (action.equalsIgnoreCase(CLOSE)) {
|
|
239
|
+
close(callbackContext);
|
|
240
|
+
|
|
241
|
+
} else if (action.equalsIgnoreCase(SET_TAP_DEVICE_DISCOVERY_ENABLED)) {
|
|
242
|
+
CordovaArgs args = new CordovaArgs(data);
|
|
243
|
+
this._isTapDeviceDiscoveryEnabled = args.getBoolean(0);
|
|
244
|
+
} else {
|
|
245
|
+
// invalid action
|
|
246
|
+
callbackContext.error("Invalid NFC action \"" + action +"\"");
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return true;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
private String getNfcStatus() {
|
|
254
|
+
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
|
|
255
|
+
if (nfcAdapter == null) {
|
|
256
|
+
return STATUS_NO_NFC;
|
|
257
|
+
} else if (!nfcAdapter.isEnabled()) {
|
|
258
|
+
return STATUS_NFC_DISABLED;
|
|
259
|
+
} else {
|
|
260
|
+
return STATUS_NFC_OK;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
private void readerMode(int flags, CallbackContext callbackContext) {
|
|
265
|
+
Bundle extras = new Bundle(); // not used
|
|
266
|
+
readerModeCallback = callbackContext;
|
|
267
|
+
getActivity().runOnUiThread(() -> {
|
|
268
|
+
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
|
|
269
|
+
nfcAdapter.enableReaderMode(getActivity(), callback, flags, extras);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
private void disableReaderMode(CallbackContext callbackContext) {
|
|
275
|
+
getActivity().runOnUiThread(() -> {
|
|
276
|
+
readerModeCallback = null;
|
|
277
|
+
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
|
|
278
|
+
if (nfcAdapter != null) {
|
|
279
|
+
nfcAdapter.disableReaderMode(getActivity());
|
|
280
|
+
}
|
|
281
|
+
callbackContext.success();
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
private NfcAdapter.ReaderCallback callback = new NfcAdapter.ReaderCallback() {
|
|
286
|
+
@Override
|
|
287
|
+
public void onTagDiscovered(Tag tag) {
|
|
288
|
+
|
|
289
|
+
JSONObject json;
|
|
290
|
+
|
|
291
|
+
// If the tag supports Ndef, try and return an Ndef message
|
|
292
|
+
List<String> techList = Arrays.asList(tag.getTechList());
|
|
293
|
+
if (techList.contains(Ndef.class.getName())) {
|
|
294
|
+
Ndef ndef = Ndef.get(tag);
|
|
295
|
+
json = Util.ndefToJSON(ndef);
|
|
296
|
+
} else {
|
|
297
|
+
json = Util.tagToJSON(tag);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
PluginResult result = new PluginResult(PluginResult.Status.OK, json);
|
|
301
|
+
result.setKeepCallback(true);
|
|
302
|
+
readerModeCallback.sendPluginResult(result);
|
|
303
|
+
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
@NonNull
|
|
308
|
+
private NFCIntentParser getIntentParser(Intent intent) {
|
|
309
|
+
NFCIntentParser parser = new NFCIntentParser(intent);
|
|
310
|
+
Tag tag = parser.getTag();
|
|
311
|
+
if (tag == null) {
|
|
312
|
+
Log.wtf(TAG, "Intent has a nfc tag null. Intent = " + intent);
|
|
313
|
+
throw new IllegalArgumentException("NFC tag is null. Retry nfc tap ?");
|
|
314
|
+
}
|
|
315
|
+
return parser;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
private IoTizeDevice createTapFromIntent(Intent intent) throws Exception {
|
|
319
|
+
Context context = getActivity();
|
|
320
|
+
NFCIntentParser parser = this.getIntentParser(intent);
|
|
321
|
+
ProtocolFactory nfcProtocolFactory = new NFCProtocolFactory(parser.getTag());
|
|
322
|
+
IoTizeDevice tap = IoTizeDevice.fromProtocol(nfcProtocolFactory.create(context));
|
|
323
|
+
tap.connect();
|
|
324
|
+
if (preferences.getBoolean(PREF_ENABLE_NFC_PAIRING, true)) {
|
|
325
|
+
byte[] response = tap.nfcPairing();
|
|
326
|
+
}
|
|
327
|
+
if (preferences.getBoolean(PREF_ENABLE_ENCRYPTION_WITH_NFC, false)) {
|
|
328
|
+
tap.encryption(true, true);
|
|
329
|
+
}
|
|
330
|
+
return tap;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* @param intent
|
|
335
|
+
* @return true if nfc intent has been handld
|
|
336
|
+
*/
|
|
337
|
+
private boolean onTapDeviceDiscoveredIntent(Intent intent) {
|
|
338
|
+
try {
|
|
339
|
+
Log.d(TAG, "creating tap device...");
|
|
340
|
+
IoTizeDevice tap = this.createTapFromIntent(intent);
|
|
341
|
+
mLastTapDiscovered = tap;
|
|
342
|
+
mLastTapDiscoveredIntent = intent;
|
|
343
|
+
String nfcPairingDoneUserFeedback = preferences.getString(PREF_NFC_PAIRING_DONE_TOAST_MESSAGE, "NFC pairing done!");
|
|
344
|
+
if (nfcPairingDoneUserFeedback.length() > 0) {
|
|
345
|
+
Activity activity = cordova.getActivity();
|
|
346
|
+
if (activity != null) {
|
|
347
|
+
activity.runOnUiThread(() -> {
|
|
348
|
+
try {
|
|
349
|
+
Toast.makeText(activity, nfcPairingDoneUserFeedback, Toast.LENGTH_LONG).show();
|
|
350
|
+
} catch (Throwable err) {
|
|
351
|
+
Log.w(TAG, err.getMessage(), err);
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
fireTapDeviceEvent(tap, intent);
|
|
357
|
+
return true;
|
|
358
|
+
} catch (Exception e) {
|
|
359
|
+
Log.w(TAG, e.getMessage(), e);
|
|
360
|
+
return false;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
private void registerDefaultTag(CallbackContext callbackContext) {
|
|
366
|
+
addTagFilter();
|
|
367
|
+
restartNfc();
|
|
368
|
+
callbackContext.success();
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
private void removeDefaultTag(CallbackContext callbackContext) {
|
|
372
|
+
removeTagFilter();
|
|
373
|
+
restartNfc();
|
|
374
|
+
callbackContext.success();
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
private void registerNdefFormatable(CallbackContext callbackContext) {
|
|
378
|
+
addTechList(new String[]{NdefFormatable.class.getName()});
|
|
379
|
+
restartNfc();
|
|
380
|
+
callbackContext.success();
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
private void registerNdef(CallbackContext callbackContext) {
|
|
384
|
+
addTechList(new String[]{Ndef.class.getName()});
|
|
385
|
+
restartNfc();
|
|
386
|
+
callbackContext.success();
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
private void removeNdef(CallbackContext callbackContext) {
|
|
390
|
+
removeTechList(new String[]{Ndef.class.getName()});
|
|
391
|
+
restartNfc();
|
|
392
|
+
callbackContext.success();
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
private void unshareTag(CallbackContext callbackContext) {
|
|
396
|
+
p2pMessage = null;
|
|
397
|
+
stopNdefPush();
|
|
398
|
+
shareTagCallback = null;
|
|
399
|
+
callbackContext.success();
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
private void init(CallbackContext callbackContext) {
|
|
403
|
+
Log.d(TAG, "Enabling plugin " + getIntent());
|
|
404
|
+
|
|
405
|
+
startNfc();
|
|
406
|
+
if (!recycledIntent()) {
|
|
407
|
+
parseMessage();
|
|
408
|
+
}
|
|
409
|
+
callbackContext.success();
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
private void removeMimeType(JSONArray data, CallbackContext callbackContext) throws JSONException {
|
|
413
|
+
String mimeType = data.getString(0);
|
|
414
|
+
removeIntentFilter(mimeType);
|
|
415
|
+
restartNfc();
|
|
416
|
+
callbackContext.success();
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
private void registerMimeType(JSONArray data, CallbackContext callbackContext) throws JSONException {
|
|
420
|
+
String mimeType = "";
|
|
421
|
+
try {
|
|
422
|
+
mimeType = data.getString(0);
|
|
423
|
+
intentFilters.add(createIntentFilter(mimeType));
|
|
424
|
+
restartNfc();
|
|
425
|
+
callbackContext.success();
|
|
426
|
+
} catch (MalformedMimeTypeException e) {
|
|
427
|
+
callbackContext.error("Invalid MIME Type " + mimeType);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
private void registerTapDevice(CallbackContext callbackContext) throws JSONException {
|
|
432
|
+
Log.d(TAG, "registerTapDevice");
|
|
433
|
+
if (mLastTapDiscovered != null) {
|
|
434
|
+
Log.d(TAG, "a tap was detected before function call registerTapDevice");
|
|
435
|
+
fireTapDeviceEvent(mLastTapDiscovered, mLastTapDiscoveredIntent);
|
|
436
|
+
}
|
|
437
|
+
callbackContext.success();
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
private void initializeTapDeviceListener() {
|
|
441
|
+
if (this.isTapDeviceDiscoveryEnabled()) {
|
|
442
|
+
addTechList(new String[]{Ndef.class.getName()});
|
|
443
|
+
String mimeType = getTapDeviceMimeType();
|
|
444
|
+
try {
|
|
445
|
+
if (mimeType != null) {
|
|
446
|
+
intentFilters.add(createIntentFilter(mimeType));
|
|
447
|
+
}
|
|
448
|
+
} catch (MalformedMimeTypeException e) {
|
|
449
|
+
Log.e(TAG, "MalformedMimeTypeException " + e.getMessage(), e);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
private String getTapDeviceMimeType() {
|
|
457
|
+
return preferences.getString(PREF_TAP_DEVICE_MIME_TYPE, null);
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
private boolean isTapDeviceDiscoveryEnabled() {
|
|
461
|
+
return this._isTapDeviceDiscoveryEnabled && preferences.getBoolean(PREF_ENABLE_TAP_DEVICE_DISCOVERY, false);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// Cheating and writing an empty record. We may actually be able to erase some tag types.
|
|
465
|
+
private void eraseTag(CallbackContext callbackContext) {
|
|
466
|
+
Tag tag = savedIntent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
|
|
467
|
+
NdefRecord[] records = {
|
|
468
|
+
new NdefRecord(NdefRecord.TNF_EMPTY, new byte[0], new byte[0], new byte[0])
|
|
469
|
+
};
|
|
470
|
+
writeNdefMessage(new NdefMessage(records), tag, callbackContext);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
private void writeTag(JSONArray data, CallbackContext callbackContext) throws JSONException {
|
|
474
|
+
if (getIntent() == null) { // TODO remove this and handle LostTag
|
|
475
|
+
callbackContext.error("Failed to write tag, received null intent");
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
Tag tag = savedIntent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
|
|
479
|
+
NdefRecord[] records = Util.jsonToNdefRecords(data.getString(0));
|
|
480
|
+
writeNdefMessage(new NdefMessage(records), tag, callbackContext);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
private void writeNdefMessage(final NdefMessage message, final Tag tag,
|
|
484
|
+
final CallbackContext callbackContext) {
|
|
485
|
+
cordova.getThreadPool().execute(() -> {
|
|
486
|
+
try {
|
|
487
|
+
Ndef ndef = Ndef.get(tag);
|
|
488
|
+
if (ndef != null) {
|
|
489
|
+
ndef.connect();
|
|
490
|
+
|
|
491
|
+
if (ndef.isWritable()) {
|
|
492
|
+
int size = message.toByteArray().length;
|
|
493
|
+
if (ndef.getMaxSize() < size) {
|
|
494
|
+
callbackContext.error("Tag capacity is " + ndef.getMaxSize() +
|
|
495
|
+
" bytes, message is " + size + " bytes.");
|
|
496
|
+
} else {
|
|
497
|
+
ndef.writeNdefMessage(message);
|
|
498
|
+
callbackContext.success();
|
|
499
|
+
}
|
|
500
|
+
} else {
|
|
501
|
+
callbackContext.error("Tag is read only");
|
|
502
|
+
}
|
|
503
|
+
ndef.close();
|
|
504
|
+
} else {
|
|
505
|
+
NdefFormatable formatable = NdefFormatable.get(tag);
|
|
506
|
+
if (formatable != null) {
|
|
507
|
+
formatable.connect();
|
|
508
|
+
formatable.format(message);
|
|
509
|
+
callbackContext.success();
|
|
510
|
+
formatable.close();
|
|
511
|
+
} else {
|
|
512
|
+
callbackContext.error("Tag doesn't support NDEF");
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
} catch (FormatException e) {
|
|
516
|
+
callbackContext.error(e.getMessage());
|
|
517
|
+
} catch (TagLostException e) {
|
|
518
|
+
callbackContext.error(e.getMessage());
|
|
519
|
+
} catch (IOException e) {
|
|
520
|
+
callbackContext.error(e.getMessage());
|
|
521
|
+
}
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
private void makeReadOnly(final CallbackContext callbackContext) {
|
|
526
|
+
|
|
527
|
+
if (getIntent() == null) { // Lost Tag
|
|
528
|
+
callbackContext.error("Failed to make tag read only, received null intent");
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
final Tag tag = savedIntent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
|
|
533
|
+
if (tag == null) {
|
|
534
|
+
callbackContext.error("Failed to make tag read only, tag is null");
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
cordova.getThreadPool().execute(() -> {
|
|
539
|
+
boolean success = false;
|
|
540
|
+
String message = "Could not make tag read only";
|
|
541
|
+
|
|
542
|
+
Ndef ndef = Ndef.get(tag);
|
|
543
|
+
|
|
544
|
+
try {
|
|
545
|
+
if (ndef != null) {
|
|
546
|
+
|
|
547
|
+
ndef.connect();
|
|
548
|
+
|
|
549
|
+
if (!ndef.isWritable()) {
|
|
550
|
+
message = "Tag is not writable";
|
|
551
|
+
} else if (ndef.canMakeReadOnly()) {
|
|
552
|
+
success = ndef.makeReadOnly();
|
|
553
|
+
} else {
|
|
554
|
+
message = "Tag can not be made read only";
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
} else {
|
|
558
|
+
message = "Tag is not NDEF";
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
} catch (IOException e) {
|
|
562
|
+
Log.e(TAG, "Failed to make tag read only", e);
|
|
563
|
+
if (e.getMessage() != null) {
|
|
564
|
+
message = e.getMessage();
|
|
565
|
+
} else {
|
|
566
|
+
message = e.toString();
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
if (success) {
|
|
571
|
+
callbackContext.success();
|
|
572
|
+
} else {
|
|
573
|
+
callbackContext.error(message);
|
|
574
|
+
}
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
private void shareTag(JSONArray data, CallbackContext callbackContext) throws JSONException {
|
|
579
|
+
NdefRecord[] records = Util.jsonToNdefRecords(data.getString(0));
|
|
580
|
+
this.p2pMessage = new NdefMessage(records);
|
|
581
|
+
|
|
582
|
+
startNdefPush(callbackContext);
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
// setBeamPushUris
|
|
586
|
+
// Every Uri you provide must have either scheme 'file' or scheme 'content'.
|
|
587
|
+
// Note that this takes priority over setNdefPush
|
|
588
|
+
//
|
|
589
|
+
// See http://developer.android.com/reference/android/nfc/NfcAdapter.html#setBeamPushUris(android.net.Uri[],%20android.app.Activity)
|
|
590
|
+
private void handover(JSONArray data, CallbackContext callbackContext) throws JSONException {
|
|
591
|
+
|
|
592
|
+
Uri[] uri = new Uri[data.length()];
|
|
593
|
+
|
|
594
|
+
for (int i = 0; i < data.length(); i++) {
|
|
595
|
+
uri[i] = Uri.parse(data.getString(i));
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
startNdefBeam(callbackContext, uri);
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
private void stopHandover(CallbackContext callbackContext) {
|
|
602
|
+
stopNdefBeam();
|
|
603
|
+
handoverCallback = null;
|
|
604
|
+
callbackContext.success();
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
private void showSettings(CallbackContext callbackContext) {
|
|
608
|
+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
|
|
609
|
+
Intent intent = new Intent(android.provider.Settings.ACTION_NFC_SETTINGS);
|
|
610
|
+
getActivity().startActivity(intent);
|
|
611
|
+
} else {
|
|
612
|
+
Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
|
|
613
|
+
getActivity().startActivity(intent);
|
|
614
|
+
}
|
|
615
|
+
callbackContext.success();
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
private void createPendingIntent() {
|
|
619
|
+
if (pendingIntent == null) {
|
|
620
|
+
Activity activity = getActivity();
|
|
621
|
+
Intent intent = new Intent(activity, activity.getClass());
|
|
622
|
+
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
623
|
+
int pendingIntentFlags = 0;
|
|
624
|
+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
|
|
625
|
+
pendingIntentFlags = PendingIntent.FLAG_MUTABLE;
|
|
626
|
+
}
|
|
627
|
+
pendingIntent = PendingIntent.getActivity(activity, 0, intent, pendingIntentFlags);
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
private void addTechList(String[] list) {
|
|
632
|
+
this.addTechFilter();
|
|
633
|
+
this.addToTechList(list);
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
private void removeTechList(String[] list) {
|
|
637
|
+
this.removeTechFilter();
|
|
638
|
+
this.removeFromTechList(list);
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
private void addTechFilter() {
|
|
642
|
+
intentFilters.add(new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED));
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
private void removeTechFilter() {
|
|
646
|
+
Iterator<IntentFilter> iterator = intentFilters.iterator();
|
|
647
|
+
while (iterator.hasNext()) {
|
|
648
|
+
IntentFilter intentFilter = iterator.next();
|
|
649
|
+
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intentFilter.getAction(0))) {
|
|
650
|
+
iterator.remove();
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
private void addTagFilter() {
|
|
656
|
+
intentFilters.add(new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED));
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
private void removeTagFilter() {
|
|
660
|
+
Iterator<IntentFilter> iterator = intentFilters.iterator();
|
|
661
|
+
while (iterator.hasNext()) {
|
|
662
|
+
IntentFilter intentFilter = iterator.next();
|
|
663
|
+
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intentFilter.getAction(0))) {
|
|
664
|
+
iterator.remove();
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
private void restartNfc() {
|
|
670
|
+
stopNfc();
|
|
671
|
+
startNfc();
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
private void startNfc() {
|
|
675
|
+
createPendingIntent(); // onResume can call startNfc before execute
|
|
676
|
+
|
|
677
|
+
getActivity().runOnUiThread(() -> {
|
|
678
|
+
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
|
|
679
|
+
|
|
680
|
+
if (nfcAdapter != null && !getActivity().isFinishing()) {
|
|
681
|
+
try {
|
|
682
|
+
IntentFilter[] intentFilters = getIntentFilters();
|
|
683
|
+
String[][] techLists = getTechLists();
|
|
684
|
+
// don't start NFC unless some intent filters or tech lists have been added,
|
|
685
|
+
// because empty lists act as wildcards and receives ALL scan events
|
|
686
|
+
if (intentFilters.length > 0 || techLists.length > 0) {
|
|
687
|
+
nfcAdapter.enableForegroundDispatch(getActivity(), getPendingIntent(), intentFilters, techLists);
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
if (p2pMessage != null) {
|
|
691
|
+
nfcAdapter.setNdefPushMessage(p2pMessage, getActivity());
|
|
692
|
+
}
|
|
693
|
+
} catch (IllegalStateException e) {
|
|
694
|
+
// issue 110 - user exits app with home button while nfc is initializing
|
|
695
|
+
Log.w(TAG, "Illegal State Exception starting NFC. Assuming application is terminating.");
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
}
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
private void stopNfc() {
|
|
703
|
+
Log.d(TAG, "stopNfc");
|
|
704
|
+
getActivity().runOnUiThread(() -> {
|
|
705
|
+
|
|
706
|
+
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
|
|
707
|
+
|
|
708
|
+
if (nfcAdapter != null) {
|
|
709
|
+
try {
|
|
710
|
+
nfcAdapter.disableForegroundDispatch(getActivity());
|
|
711
|
+
} catch (IllegalStateException e) {
|
|
712
|
+
// issue 125 - user exits app with back button while nfc
|
|
713
|
+
Log.w(TAG, "Illegal State Exception stopping NFC. Assuming application is terminating.");
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
private void startNdefBeam(final CallbackContext callbackContext, final Uri[] uris) {
|
|
720
|
+
getActivity().runOnUiThread(() -> {
|
|
721
|
+
|
|
722
|
+
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
|
|
723
|
+
|
|
724
|
+
if (nfcAdapter == null) {
|
|
725
|
+
callbackContext.error(STATUS_NO_NFC);
|
|
726
|
+
} else if (!nfcAdapter.isNdefPushEnabled()) {
|
|
727
|
+
callbackContext.error(STATUS_NDEF_PUSH_DISABLED);
|
|
728
|
+
} else {
|
|
729
|
+
nfcAdapter.setOnNdefPushCompleteCallback(NfcPlugin.this, getActivity());
|
|
730
|
+
try {
|
|
731
|
+
nfcAdapter.setBeamPushUris(uris, getActivity());
|
|
732
|
+
|
|
733
|
+
PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
|
|
734
|
+
result.setKeepCallback(true);
|
|
735
|
+
handoverCallback = callbackContext;
|
|
736
|
+
callbackContext.sendPluginResult(result);
|
|
737
|
+
|
|
738
|
+
} catch (IllegalArgumentException e) {
|
|
739
|
+
callbackContext.error(e.getMessage());
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
});
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
private void startNdefPush(final CallbackContext callbackContext) {
|
|
746
|
+
getActivity().runOnUiThread(() -> {
|
|
747
|
+
|
|
748
|
+
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
|
|
749
|
+
|
|
750
|
+
if (nfcAdapter == null) {
|
|
751
|
+
callbackContext.error(STATUS_NO_NFC);
|
|
752
|
+
} else if (!nfcAdapter.isNdefPushEnabled()) {
|
|
753
|
+
callbackContext.error(STATUS_NDEF_PUSH_DISABLED);
|
|
754
|
+
} else {
|
|
755
|
+
nfcAdapter.setNdefPushMessage(p2pMessage, getActivity());
|
|
756
|
+
nfcAdapter.setOnNdefPushCompleteCallback(NfcPlugin.this, getActivity());
|
|
757
|
+
|
|
758
|
+
PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
|
|
759
|
+
result.setKeepCallback(true);
|
|
760
|
+
shareTagCallback = callbackContext;
|
|
761
|
+
callbackContext.sendPluginResult(result);
|
|
762
|
+
}
|
|
763
|
+
});
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
private void stopNdefPush() {
|
|
767
|
+
getActivity().runOnUiThread(() -> {
|
|
768
|
+
|
|
769
|
+
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
|
|
770
|
+
|
|
771
|
+
if (nfcAdapter != null) {
|
|
772
|
+
nfcAdapter.setNdefPushMessage(null, getActivity());
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
});
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
private void stopNdefBeam() {
|
|
779
|
+
getActivity().runOnUiThread(() -> {
|
|
780
|
+
|
|
781
|
+
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
|
|
782
|
+
|
|
783
|
+
if (nfcAdapter != null) {
|
|
784
|
+
nfcAdapter.setBeamPushUris(null, getActivity());
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
});
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
private void addToTechList(String[] techs) {
|
|
791
|
+
techLists.add(techs);
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
private void removeFromTechList(String[] techs) {
|
|
795
|
+
Iterator<String[]> iterator = techLists.iterator();
|
|
796
|
+
while (iterator.hasNext()) {
|
|
797
|
+
String[] list = iterator.next();
|
|
798
|
+
if (Arrays.equals(list, techs)) {
|
|
799
|
+
iterator.remove();
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
private void removeIntentFilter(String mimeType) {
|
|
805
|
+
Iterator<IntentFilter> iterator = intentFilters.iterator();
|
|
806
|
+
while (iterator.hasNext()) {
|
|
807
|
+
IntentFilter intentFilter = iterator.next();
|
|
808
|
+
String mt = intentFilter.getDataType(0);
|
|
809
|
+
if (mimeType.equals(mt)) {
|
|
810
|
+
iterator.remove();
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
private IntentFilter createIntentFilter(String mimeType) throws MalformedMimeTypeException {
|
|
816
|
+
IntentFilter intentFilter = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
|
|
817
|
+
intentFilter.addDataType(mimeType);
|
|
818
|
+
return intentFilter;
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
private PendingIntent getPendingIntent() {
|
|
822
|
+
return pendingIntent;
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
private IntentFilter[] getIntentFilters() {
|
|
826
|
+
return intentFilters.toArray(new IntentFilter[intentFilters.size()]);
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
private String[][] getTechLists() {
|
|
830
|
+
//noinspection ToArrayCallWithZeroLengthArrayArgument
|
|
831
|
+
return techLists.toArray(new String[0][0]);
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
private void parseMessage() {
|
|
835
|
+
cordova.getThreadPool().execute(() -> {
|
|
836
|
+
try {
|
|
837
|
+
Log.d(TAG, "parseMessage " + getIntent());
|
|
838
|
+
Intent intent = getIntent();
|
|
839
|
+
String action = intent.getAction();
|
|
840
|
+
Log.d(TAG, "action " + action);
|
|
841
|
+
if (action == null) {
|
|
842
|
+
return;
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
final Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
|
|
846
|
+
if (tag != null) {
|
|
847
|
+
Parcelable[] messages = intent.getParcelableArrayExtra((NfcAdapter.EXTRA_NDEF_MESSAGES));
|
|
848
|
+
|
|
849
|
+
|
|
850
|
+
if (isTapDeviceDiscoveryEnabled() && isIoTizeTag(tag)) {
|
|
851
|
+
onTapDeviceDiscoveredIntent(intent);
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
if (action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {
|
|
855
|
+
Ndef ndef = Ndef.get(tag);
|
|
856
|
+
fireNdefEvent(NDEF_MIME, ndef, messages);
|
|
857
|
+
Log.d(TAG, "Saving Intent for connect" + intent);
|
|
858
|
+
savedIntent = intent;
|
|
859
|
+
fireTagEvent(tag, messages);
|
|
860
|
+
|
|
861
|
+
} else if (action.equals(NfcAdapter.ACTION_TECH_DISCOVERED)) {
|
|
862
|
+
this._fireTagEventsFromTechList(tag, messages);
|
|
863
|
+
fireTagEvent(tag, messages);
|
|
864
|
+
} else if (action.equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
|
|
865
|
+
fireTagEvent(tag, messages);
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
catch (RuntimeException err) {
|
|
870
|
+
Log.w(TAG, "Unhandled error with parseMessage()", err);
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
setIntent(new Intent());
|
|
874
|
+
});
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
private void _fireTagEventsFromTechList(@NonNull Tag tag, @NonNull Parcelable[] messages) {
|
|
878
|
+
for (String tagTech : tag.getTechList()) {
|
|
879
|
+
Log.d(TAG, tagTech);
|
|
880
|
+
if (tagTech.equals(NdefFormatable.class.getName())) {
|
|
881
|
+
fireNdefFormatableEvent(tag);
|
|
882
|
+
} else if (tagTech.equals(Ndef.class.getName())) { //
|
|
883
|
+
this._fireNdefEvent(tag, messages);
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
private void _fireNdefEvent(@NonNull Tag tag, @NonNull Parcelable[] messages) {
|
|
889
|
+
Ndef ndef = Ndef.get(tag);
|
|
890
|
+
fireNdefEvent(NDEF, ndef, messages);
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
private boolean isIoTizeTag(@Nullable Tag tag) {
|
|
894
|
+
if (tag == null) {
|
|
895
|
+
return false;
|
|
896
|
+
}
|
|
897
|
+
boolean hasNfcV = Arrays.stream(tag.getTechList()).anyMatch(s -> s.endsWith("NfcV"));
|
|
898
|
+
if (!hasNfcV) {
|
|
899
|
+
return false;
|
|
900
|
+
}
|
|
901
|
+
Ndef ndef = Ndef.get(tag);
|
|
902
|
+
if (ndef == null) {
|
|
903
|
+
return false;
|
|
904
|
+
}
|
|
905
|
+
NdefMessage ndefMessages = ndef.getCachedNdefMessage();
|
|
906
|
+
if (ndefMessages == null) {
|
|
907
|
+
return false;
|
|
908
|
+
}
|
|
909
|
+
NdefRecord[] records = ndefMessages.getRecords();
|
|
910
|
+
|
|
911
|
+
return records.length >= 4; // TODO improve condition
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
private void sendEvent(String type, JSONObject tag, JSONObject tap) {
|
|
915
|
+
try {
|
|
916
|
+
JSONObject event = new JSONObject();
|
|
917
|
+
event.put("type", type); // TAG_DEFAULT, NDEF, NDEF_MIME, NDEF_FORMATABLE
|
|
918
|
+
event.put("tag", tag); // JSON representing the NFC tag and NDEF messages
|
|
919
|
+
event.put("tap", tap);
|
|
920
|
+
sendEvent(event);
|
|
921
|
+
} catch (JSONException e) {
|
|
922
|
+
Log.e(TAG, "Error sending NFC event through the channel", e);
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
private void sendEvent(String type, JSONObject tag) {
|
|
928
|
+
try {
|
|
929
|
+
JSONObject event = new JSONObject();
|
|
930
|
+
event.put("type", type); // TAG_DEFAULT, NDEF, NDEF_MIME, NDEF_FORMATABLE
|
|
931
|
+
event.put("tag", tag); // JSON representing the NFC tag and NDEF messages
|
|
932
|
+
sendEvent(event);
|
|
933
|
+
} catch (JSONException e) {
|
|
934
|
+
Log.e(TAG, "Error sending NFC event through the channel", e);
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
// Send the event data through a channel so the JavaScript side can fire the event
|
|
939
|
+
private void sendEvent(JSONObject event) {
|
|
940
|
+
PluginResult result = new PluginResult(PluginResult.Status.OK, event);
|
|
941
|
+
result.setKeepCallback(true);
|
|
942
|
+
if (channelCallback != null) {
|
|
943
|
+
channelCallback.sendPluginResult(result);
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
private void fireNdefEvent(String type, Ndef ndef, Parcelable[] messages) {
|
|
948
|
+
try {
|
|
949
|
+
JSONObject json = buildNdefJSON(ndef, messages);
|
|
950
|
+
sendEvent(type, json);
|
|
951
|
+
} catch (Throwable e) {
|
|
952
|
+
Log.w(TAG, "Failed to fire NDef event", e);
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
private void fireTapDeviceEvent(IoTizeDevice tap, Intent intent) {
|
|
957
|
+
try {
|
|
958
|
+
Log.d(TAG, "fireTapDeviceEvent " + tap);
|
|
959
|
+
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
|
|
960
|
+
Ndef ndef = Ndef.get(tag);
|
|
961
|
+
Parcelable[] messages = intent.getParcelableArrayExtra((NfcAdapter.EXTRA_NDEF_MESSAGES));
|
|
962
|
+
|
|
963
|
+
sendEvent(NFC_TAP_DEVICE, buildNdefJSON(ndef, messages), buildTapJSON(tap));
|
|
964
|
+
} catch (JSONException e) {
|
|
965
|
+
Log.e(TAG, e.getMessage(), e);
|
|
966
|
+
} catch (Throwable e) {
|
|
967
|
+
Log.w(TAG, "Failed to fire Tap Device event", e);
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
private JSONObject buildTapJSON(IoTizeDevice tap) throws JSONException {
|
|
972
|
+
JSONObject tapInfo = new JSONObject();
|
|
973
|
+
tapInfo.put("nfcPairingDone", true); // TODO
|
|
974
|
+
JSONObject encryptionJSON = new JSONObject();
|
|
975
|
+
boolean encryptionEnabled = false;
|
|
976
|
+
if (tap.isEncryptionEnabled()) {
|
|
977
|
+
EncryptionAlgo encryptionAlgo = tap.getClient().getEncryptionAlgo();
|
|
978
|
+
if (encryptionAlgo != null) {
|
|
979
|
+
encryptionEnabled = true;
|
|
980
|
+
encryptionJSON.put("enabled", true);
|
|
981
|
+
JSONObject keysOptions = new JSONObject();
|
|
982
|
+
keysOptions.put("sessionKey", Util.byteArrayToJSON(encryptionAlgo.getKey()));
|
|
983
|
+
int frameCounter = 0;
|
|
984
|
+
try {
|
|
985
|
+
// TODO change with frameCounter
|
|
986
|
+
TapClient client = tap.getClient();
|
|
987
|
+
Field field = client.getClass().getDeclaredField("frameCounter");
|
|
988
|
+
field.setAccessible(true);
|
|
989
|
+
frameCounter = (int) field.get(client);
|
|
990
|
+
} catch (NoSuchFieldException e) {
|
|
991
|
+
Log.w(TAG, e.getMessage(), e);
|
|
992
|
+
} catch (IllegalAccessException e) {
|
|
993
|
+
Log.w(TAG, e.getMessage(), e);
|
|
994
|
+
}
|
|
995
|
+
keysOptions.put("sessionKeyHex", Helper.ByteArrayToHexString(encryptionAlgo.getKey()));
|
|
996
|
+
// keysOptions.put("ivEncode", Util.byteArrayToJSON(());
|
|
997
|
+
// keysOptions.put("ivDecode", Util.byteArrayToJSON(());
|
|
998
|
+
|
|
999
|
+
encryptionJSON.put("keys", keysOptions);
|
|
1000
|
+
encryptionJSON.put("frameCounter", frameCounter);
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
encryptionJSON.put("enabled", encryptionEnabled);
|
|
1004
|
+
tapInfo.put("encryption", encryptionJSON);
|
|
1005
|
+
return tapInfo;
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
private void fireNdefFormatableEvent(Tag tag) {
|
|
1009
|
+
sendEvent(NDEF_FORMATABLE, Util.tagToJSON(tag));
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
private void fireTagEvent(Tag tag, Parcelable[] messages) {
|
|
1013
|
+
if (Arrays.asList(tag.getTechList()).contains(Ndef.class.getName())) {
|
|
1014
|
+
sendEvent(TAG_DEFAULT, buildNdefJSON(Ndef.get(tag), messages));
|
|
1015
|
+
}
|
|
1016
|
+
else {
|
|
1017
|
+
sendEvent(TAG_DEFAULT, Util.tagToJSON(tag));
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
/**
|
|
1022
|
+
* May throw a java.lang.SecurityException error if Tag is out of date (tested on Android 13)
|
|
1023
|
+
*/
|
|
1024
|
+
private JSONObject buildNdefJSON(Ndef ndef, Parcelable[] messages) throws SecurityException {
|
|
1025
|
+
|
|
1026
|
+
JSONObject json = Util.ndefToJSON(ndef);
|
|
1027
|
+
|
|
1028
|
+
// ndef is null for peer-to-peer
|
|
1029
|
+
// ndef and messages are null for ndef format-able
|
|
1030
|
+
if (ndef == null && messages != null) {
|
|
1031
|
+
|
|
1032
|
+
try {
|
|
1033
|
+
|
|
1034
|
+
if (messages.length > 0) {
|
|
1035
|
+
NdefMessage message = (NdefMessage) messages[0];
|
|
1036
|
+
json.put("ndefMessage", Util.messageToJSON(message));
|
|
1037
|
+
// guessing type, would prefer a more definitive way to determine type
|
|
1038
|
+
json.put("type", "NDEF Push Protocol");
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
if (messages.length > 1) {
|
|
1042
|
+
Log.wtf(TAG, "Expected one ndefMessage but found " + messages.length);
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
} catch (JSONException e) {
|
|
1046
|
+
// shouldn't happen
|
|
1047
|
+
Log.e(Util.TAG, "Failed to convert ndefMessage into json", e);
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
return json;
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
private boolean recycledIntent() { // TODO this is a kludge, find real solution
|
|
1054
|
+
|
|
1055
|
+
int flags = getIntent().getFlags();
|
|
1056
|
+
if ((flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) {
|
|
1057
|
+
Log.i(TAG, "Launched from history, killing recycled intent");
|
|
1058
|
+
setIntent(new Intent());
|
|
1059
|
+
return true;
|
|
1060
|
+
}
|
|
1061
|
+
return false;
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
@Override
|
|
1065
|
+
public void onStart() {
|
|
1066
|
+
super.onStart();
|
|
1067
|
+
this.initializeTapDeviceListener();
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
@Override
|
|
1071
|
+
public void onPause(boolean multitasking) {
|
|
1072
|
+
Log.d(TAG, "onPause " + getIntent());
|
|
1073
|
+
super.onPause(multitasking);
|
|
1074
|
+
if (multitasking) {
|
|
1075
|
+
// nfc can't run in background
|
|
1076
|
+
stopNfc();
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
@Override
|
|
1081
|
+
public void onResume(boolean multitasking) {
|
|
1082
|
+
Log.d(TAG, "onResume " + getIntent());
|
|
1083
|
+
super.onResume(multitasking);
|
|
1084
|
+
startNfc();
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
@Override
|
|
1088
|
+
public void onNewIntent(Intent intent) {
|
|
1089
|
+
Log.d(TAG, "onNewIntent " + intent);
|
|
1090
|
+
super.onNewIntent(intent);
|
|
1091
|
+
setIntent(intent);
|
|
1092
|
+
savedIntent = intent;
|
|
1093
|
+
parseMessage();
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
private Activity getActivity() {
|
|
1097
|
+
return this.cordova.getActivity();
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
private Intent getIntent() {
|
|
1101
|
+
return getActivity().getIntent();
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
private void setIntent(Intent intent) {
|
|
1105
|
+
getActivity().setIntent(intent);
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
@Override
|
|
1109
|
+
public void onNdefPushComplete(NfcEvent event) {
|
|
1110
|
+
|
|
1111
|
+
// handover (beam) take precedence over share tag (ndef push)
|
|
1112
|
+
if (handoverCallback != null) {
|
|
1113
|
+
PluginResult result = new PluginResult(PluginResult.Status.OK, "Beamed Message to Peer");
|
|
1114
|
+
result.setKeepCallback(true);
|
|
1115
|
+
handoverCallback.sendPluginResult(result);
|
|
1116
|
+
} else if (shareTagCallback != null) {
|
|
1117
|
+
PluginResult result = new PluginResult(PluginResult.Status.OK, "Shared Message with Peer");
|
|
1118
|
+
result.setKeepCallback(true);
|
|
1119
|
+
shareTagCallback.sendPluginResult(result);
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
/**
|
|
1125
|
+
* Enable I/O operations to the tag from this TagTechnology object.
|
|
1126
|
+
* *
|
|
1127
|
+
*
|
|
1128
|
+
* @param tech TagTechnology class name e.g. 'android.nfc.tech.IsoDep' or 'android.nfc.tech.NfcV'
|
|
1129
|
+
* @param timeout tag timeout
|
|
1130
|
+
* @param callbackContext Cordova callback context
|
|
1131
|
+
*/
|
|
1132
|
+
private void connect(final String tech, final int timeout,
|
|
1133
|
+
final CallbackContext callbackContext) {
|
|
1134
|
+
this.cordova.getThreadPool().execute(() -> {
|
|
1135
|
+
try {
|
|
1136
|
+
this._initIntentTag(tech);
|
|
1137
|
+
|
|
1138
|
+
if (nfcProtocol == null) {
|
|
1139
|
+
callbackContext.error("Tag does not support " + tech);
|
|
1140
|
+
return;
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
nfcProtocol.connect();
|
|
1144
|
+
setTimeout(timeout);
|
|
1145
|
+
Log.d(TAG, "NFC Connection successful");
|
|
1146
|
+
callbackContext.success();
|
|
1147
|
+
} catch (IOException ex) {
|
|
1148
|
+
Log.e(TAG, "Tag connection failed", ex);
|
|
1149
|
+
callbackContext.error("Tag connection failed");
|
|
1150
|
+
} catch (Throwable e) {
|
|
1151
|
+
Log.e(TAG, e.getMessage(), e);
|
|
1152
|
+
callbackContext.error(e.getMessage());
|
|
1153
|
+
}
|
|
1154
|
+
});
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
private void _initIntentTag() throws Exception {
|
|
1158
|
+
this._initIntentTag("android.nfc.tech.NfcV"); // TODO
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
private void _initIntentTag(final String tech) throws Exception {
|
|
1162
|
+
Intent intent = getIntent();
|
|
1163
|
+
Tag tag = null;
|
|
1164
|
+
if (intent != null) {
|
|
1165
|
+
tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
|
|
1166
|
+
}
|
|
1167
|
+
if (tag == null && savedIntent != null) {
|
|
1168
|
+
tag = savedIntent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
if (tag == null) {
|
|
1172
|
+
Log.e(TAG, "No Tag");
|
|
1173
|
+
throw new Exception("No Tag");
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
// get technologies supported by this tag
|
|
1177
|
+
List<String> techList = Arrays.asList(tag.getTechList());
|
|
1178
|
+
if (!techList.contains(tech)) {
|
|
1179
|
+
throw new Exception("Tech " + tech + " not available");
|
|
1180
|
+
}
|
|
1181
|
+
// use reflection to call the static function Tech.get(tag)
|
|
1182
|
+
tagTechnologyClass = Class.forName(tech);
|
|
1183
|
+
nfcProtocol = NFCProtocol.create(tag);
|
|
1184
|
+
Method method = tagTechnologyClass.getMethod("get", Tag.class);
|
|
1185
|
+
tagTechnology = (TagTechnology) method.invoke(null, tag);
|
|
1186
|
+
if (tagTechnology == null) {
|
|
1187
|
+
Log.e(TAG, "No Tag Technology");
|
|
1188
|
+
throw new Exception("No Tag");
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
private void setTimeout(int timeout) {
|
|
1193
|
+
if (timeout < 0) {
|
|
1194
|
+
return;
|
|
1195
|
+
}
|
|
1196
|
+
if (nfcProtocol != null) {
|
|
1197
|
+
nfcProtocol.getConfiguration().connectionTimeoutMillis = timeout;
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
|
|
1201
|
+
/**
|
|
1202
|
+
* Disable I/O operations to the tag from this TagTechnology object, and release resources.
|
|
1203
|
+
*
|
|
1204
|
+
* @param callbackContext Cordova callback context
|
|
1205
|
+
*/
|
|
1206
|
+
private void close(CallbackContext callbackContext) {
|
|
1207
|
+
cordova.getThreadPool().execute(() -> {
|
|
1208
|
+
try {
|
|
1209
|
+
if (nfcProtocol != null && nfcProtocol.isConnected()) {
|
|
1210
|
+
nfcProtocol.disconnect();
|
|
1211
|
+
}
|
|
1212
|
+
callbackContext.success();
|
|
1213
|
+
|
|
1214
|
+
} catch (Exception ex) {
|
|
1215
|
+
Log.e(TAG, "Error closing nfc connection", ex);
|
|
1216
|
+
callbackContext.error("Error closing nfc connection " + ex.getLocalizedMessage());
|
|
1217
|
+
}
|
|
1218
|
+
finally {
|
|
1219
|
+
nfcProtocol = null;
|
|
1220
|
+
tagTechnology = null;
|
|
1221
|
+
tagTechnologyClass = null;
|
|
1222
|
+
}
|
|
1223
|
+
});
|
|
1224
|
+
}
|
|
1225
|
+
|
|
1226
|
+
/**
|
|
1227
|
+
* Send raw commands to the tag and receive the response.
|
|
1228
|
+
*
|
|
1229
|
+
* @param data byte[] command to be passed to the tag
|
|
1230
|
+
* @param callbackContext Cordova callback context
|
|
1231
|
+
*/
|
|
1232
|
+
private void transceiveRaw(final byte[] data, final CallbackContext callbackContext) {
|
|
1233
|
+
cordova.getThreadPool().execute(() -> {
|
|
1234
|
+
try {
|
|
1235
|
+
this._connectIntentTagIfNeeded();
|
|
1236
|
+
Method transceiveMethod = tagTechnologyClass.getMethod("transceive", byte[].class);
|
|
1237
|
+
try {
|
|
1238
|
+
@SuppressWarnings("PrimitiveArrayArgumentToVarargsMethod")
|
|
1239
|
+
byte[] response = (byte[]) transceiveMethod.invoke(tagTechnology, data);
|
|
1240
|
+
callbackContext.success(Helper.ByteArrayToHexString(response));
|
|
1241
|
+
}
|
|
1242
|
+
catch (InvocationTargetException e) {
|
|
1243
|
+
Throwable targetException = e.getTargetException();
|
|
1244
|
+
String errorMessage = targetException.getMessage();
|
|
1245
|
+
if (errorMessage != null && (errorMessage.endsWith("is out of date") ||
|
|
1246
|
+
errorMessage.contains("Call connect() first"))) {
|
|
1247
|
+
this._connectIntentTag();
|
|
1248
|
+
// Retry
|
|
1249
|
+
byte[] response = (byte[]) transceiveMethod.invoke(tagTechnology, data);
|
|
1250
|
+
callbackContext.success(Helper.ByteArrayToHexString(response));
|
|
1251
|
+
}
|
|
1252
|
+
else {
|
|
1253
|
+
throw e;
|
|
1254
|
+
}
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
1257
|
+
catch (InvocationTargetException e) {
|
|
1258
|
+
String msg = e.getTargetException().getMessage();
|
|
1259
|
+
Log.e(TAG, msg, e);
|
|
1260
|
+
callbackContext.error(msg);
|
|
1261
|
+
}
|
|
1262
|
+
catch (Throwable e) {
|
|
1263
|
+
Log.e(TAG, e.getMessage(), e);
|
|
1264
|
+
callbackContext.error(e.getMessage());
|
|
1265
|
+
}
|
|
1266
|
+
});
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
private void _connectIntentTag() throws Exception {
|
|
1270
|
+
this._initIntentTag();
|
|
1271
|
+
tagTechnology.connect();
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
private void _connectIntentTagIfNeeded() throws Exception {
|
|
1275
|
+
if (tagTechnology == null) {
|
|
1276
|
+
this._initIntentTag();
|
|
1277
|
+
tagTechnology.connect();
|
|
1278
|
+
}
|
|
1279
|
+
}
|
|
1280
|
+
|
|
1281
|
+
/**
|
|
1282
|
+
* Send raw commands to the tag and receive the response.
|
|
1283
|
+
*
|
|
1284
|
+
* @param data byte[] command to be passed to the tag
|
|
1285
|
+
* @param callbackContext Cordova callback context
|
|
1286
|
+
*/
|
|
1287
|
+
private void transceiveTap(final byte[] data, final CallbackContext callbackContext) {
|
|
1288
|
+
cordova.getThreadPool().execute(() -> {
|
|
1289
|
+
try {
|
|
1290
|
+
if (nfcProtocol == null) {
|
|
1291
|
+
Log.e(TAG, "No Tech");
|
|
1292
|
+
callbackContext.error("No Tech");
|
|
1293
|
+
return;
|
|
1294
|
+
}
|
|
1295
|
+
if (!nfcProtocol.isConnected()) {
|
|
1296
|
+
Log.e(TAG, "Not connected");
|
|
1297
|
+
callbackContext.error("Not connected");
|
|
1298
|
+
return;
|
|
1299
|
+
}
|
|
1300
|
+
byte[] response = nfcProtocol.send(data);
|
|
1301
|
+
callbackContext.success(Helper.ByteArrayToHexString(response));
|
|
1302
|
+
} catch (Exception e) {
|
|
1303
|
+
Log.e(TAG, e.getMessage(), e);
|
|
1304
|
+
callbackContext.error(e.getMessage());
|
|
1305
|
+
}
|
|
1306
|
+
});
|
|
1307
|
+
}
|
|
1308
|
+
}
|