react-native-nfc-manager 3.14.0 → 3.14.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.
@@ -1,5 +1,6 @@
1
1
  package community.revteltech.nfc;
2
2
 
3
+ import android.annotation.SuppressLint;
3
4
  import android.app.Activity;
4
5
  import android.content.BroadcastReceiver;
5
6
  import android.content.Context;
@@ -7,6 +8,8 @@ import android.content.Intent;
7
8
  import android.content.IntentFilter;
8
9
  import android.content.pm.PackageManager;
9
10
  import android.os.Build;
11
+
12
+ import androidx.annotation.NonNull;
10
13
  import androidx.annotation.Nullable;
11
14
  import android.util.Log;
12
15
  import android.provider.Settings;
@@ -40,10 +43,9 @@ import java.util.*;
40
43
 
41
44
  class NfcManager extends ReactContextBaseJavaModule implements ActivityEventListener, LifecycleEventListener {
42
45
  private static final String LOG_TAG = "ReactNativeNfcManager";
43
- private final List<IntentFilter> intentFilters = new ArrayList<IntentFilter>();
44
- private final ArrayList<String[]> techLists = new ArrayList<String[]>();
45
- private Context context;
46
- private ReactApplicationContext reactContext;
46
+ private final List<IntentFilter> intentFilters = new ArrayList<>();
47
+ private final ArrayList<String[]> techLists = new ArrayList<>();
48
+ private final Context context;
47
49
  private Boolean isForegroundEnabled = false;
48
50
  private Boolean isResumed = false;
49
51
  private WriteNdefRequest writeNdefRequest = null;
@@ -64,7 +66,7 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
64
66
  private static final String ERR_GET_ACTIVITY_FAIL = "fail to get current activity";
65
67
  private static final String ERR_NO_NFC_SUPPORT = "no nfc support";
66
68
 
67
- class WriteNdefRequest {
69
+ static class WriteNdefRequest {
68
70
  NdefMessage message;
69
71
  Callback callback;
70
72
  boolean format;
@@ -81,12 +83,12 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
81
83
  public NfcManager(ReactApplicationContext reactContext) {
82
84
  super(reactContext);
83
85
  context = reactContext;
84
- this.reactContext = reactContext;
85
86
  reactContext.addActivityEventListener(this);
86
87
  reactContext.addLifecycleEventListener(this);
87
88
  Log.d(LOG_TAG, "NfcManager created");
88
89
  }
89
90
 
91
+ @NonNull
90
92
  @Override
91
93
  public String getName() {
92
94
  return "NfcManager";
@@ -115,17 +117,14 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
115
117
  if (techRequest != null) {
116
118
  techRequest.close();
117
119
  try {
118
- techRequest.getPendingCallback().invoke(ERR_CANCEL);
120
+ techRequest.invokePendingCallbackWithError(ERR_CANCEL);
119
121
  } catch (RuntimeException ex) {
120
122
  // the pending callback might already been invoked when there is an ongoing
121
123
  // connected tag, bypass this case explicitly
122
124
  }
123
125
  techRequest = null;
124
- callback.invoke();
125
- } else {
126
- // explicitly allow this
127
- callback.invoke();
128
126
  }
127
+ callback.invoke();
129
128
  }
130
129
  }
131
130
 
@@ -151,11 +150,8 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
151
150
  if (techRequest != null) {
152
151
  techRequest.close();
153
152
  techRequest = null;
154
- callback.invoke();
155
- } else {
156
- // explicitly allow this
157
- callback.invoke();
158
153
  }
154
+ callback.invoke();
159
155
  }
160
156
  }
161
157
 
@@ -171,6 +167,7 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
171
167
  Ndef ndef = Ndef.get(tag);
172
168
  parsed = ndef2React(ndef, new NdefMessage[]{ndef.getCachedNdefMessage()});
173
169
  } catch (Exception ex) {
170
+ Log.d(LOG_TAG, ex.toString());
174
171
  }
175
172
  }
176
173
  callback.invoke(null, parsed);
@@ -306,17 +303,17 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
306
303
  return;
307
304
  } else if (sector >= mifareTag.getSectorCount()) {
308
305
  // Check if in range
309
- String msg = String.format("mifareClassicAuthenticate fail: invalid sector %d (max %d)", sector, mifareTag.getSectorCount());
306
+ @SuppressLint("DefaultLocale") String msg = String.format("mifareClassicAuthenticate fail: invalid sector %d (max %d)", sector, mifareTag.getSectorCount());
310
307
  callback.invoke(msg);
311
308
  return;
312
309
  } else if (key.size() != 6) {
313
310
  // Invalid key length
314
- String msg = String.format("mifareClassicAuthenticate fail: invalid key (needs length 6 but has %d characters)", key.size());
311
+ @SuppressLint("DefaultLocale") String msg = String.format("mifareClassicAuthenticate fail: invalid key (needs length 6 but has %d characters)", key.size());
315
312
  callback.invoke(msg);
316
313
  return;
317
314
  }
318
315
 
319
- boolean result = false;
316
+ boolean result;
320
317
  if (type == 'A') {
321
318
  result = mifareTag.authenticateSectorWithKeyA(sector, rnArrayToBytes(key));
322
319
  } else {
@@ -332,7 +329,7 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
332
329
  } catch (TagLostException ex) {
333
330
  callback.invoke("mifareClassicAuthenticate fail: TAG_LOST");
334
331
  } catch (Exception ex) {
335
- callback.invoke("mifareClassicAuthenticate fail: " + ex.toString());
332
+ callback.invoke("mifareClassicAuthenticate fail: " + ex);
336
333
  }
337
334
  } else {
338
335
  callback.invoke(ERR_NO_TECH_REQ);
@@ -365,14 +362,14 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
365
362
  return;
366
363
  } else if (sectorIndex >= mifareTag.getSectorCount()) {
367
364
  // Check if in range
368
- String msg = String.format("mifareClassicGetBlockCountInSector fail: invalid sector %d (max %d)", sectorIndex, mifareTag.getSectorCount());
365
+ @SuppressLint("DefaultLocale") String msg = String.format("mifareClassicGetBlockCountInSector fail: invalid sector %d (max %d)", sectorIndex, mifareTag.getSectorCount());
369
366
  callback.invoke(msg);
370
367
  return;
371
368
  }
372
369
 
373
370
  callback.invoke(null, mifareTag.getBlockCountInSector(sectorIndex));
374
371
  } catch (Exception ex) {
375
- callback.invoke("mifareClassicGetBlockCountInSector fail: " + ex.toString());
372
+ callback.invoke("mifareClassicGetBlockCountInSector fail: " + ex);
376
373
  }
377
374
  } else {
378
375
  callback.invoke(ERR_NO_TECH_REQ);
@@ -394,7 +391,7 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
394
391
 
395
392
  callback.invoke(null, mifareTag.getSectorCount());
396
393
  } catch (Exception ex) {
397
- callback.invoke("mifareClassicGetSectorCount fail: " + ex.toString());
394
+ callback.invoke("mifareClassicGetSectorCount fail: " + ex);
398
395
  }
399
396
  } else {
400
397
  callback.invoke(ERR_NO_TECH_REQ);
@@ -414,14 +411,14 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
414
411
  return;
415
412
  } else if (sectorIndex >= mifareTag.getSectorCount()) {
416
413
  // Check if in range
417
- String msg = String.format("mifareClassicSectorToBlock fail: invalid sector %d (max %d)", sectorIndex, mifareTag.getSectorCount());
414
+ @SuppressLint("DefaultLocale") String msg = String.format("mifareClassicSectorToBlock fail: invalid sector %d (max %d)", sectorIndex, mifareTag.getSectorCount());
418
415
  callback.invoke(msg);
419
416
  return;
420
417
  }
421
418
 
422
419
  callback.invoke(null, mifareTag.sectorToBlock(sectorIndex));
423
420
  } catch (Exception ex) {
424
- callback.invoke("mifareClassicSectorToBlock fail: " + ex.toString());
421
+ callback.invoke("mifareClassicSectorToBlock fail: " + ex);
425
422
  }
426
423
  } else {
427
424
  callback.invoke(ERR_NO_TECH_REQ);
@@ -441,20 +438,19 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
441
438
  return;
442
439
  } else if (blockIndex >= mifareTag.getBlockCount()) {
443
440
  // Check if in range
444
- String msg = String.format("mifareClassicReadBlock fail: invalid block %d (max %d)", blockIndex, mifareTag.getBlockCount());
441
+ @SuppressLint("DefaultLocale") String msg = String.format("mifareClassicReadBlock fail: invalid block %d (max %d)", blockIndex, mifareTag.getBlockCount());
445
442
  callback.invoke(msg);
446
443
  return;
447
444
  }
448
445
 
449
- byte[] buffer = new byte[MifareClassic.BLOCK_SIZE];
450
- buffer = mifareTag.readBlock(blockIndex);
446
+ byte[] buffer = mifareTag.readBlock(blockIndex);
451
447
 
452
448
  WritableArray result = bytesToRnArray(buffer);
453
449
  callback.invoke(null, result);
454
450
  } catch (TagLostException ex) {
455
451
  callback.invoke("mifareClassicReadBlock fail: TAG_LOST");
456
452
  } catch (Exception ex) {
457
- callback.invoke("mifareClassicReadBlock fail: " + ex.toString());
453
+ callback.invoke("mifareClassicReadBlock fail: " + ex);
458
454
  }
459
455
  } else {
460
456
  callback.invoke(ERR_NO_TECH_REQ);
@@ -474,14 +470,14 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
474
470
  return;
475
471
  } else if (sectorIndex >= mifareTag.getSectorCount()) {
476
472
  // Check if in range
477
- String msg = String.format("mifareClassicReadSector fail: invalid sector %d (max %d)", sectorIndex, mifareTag.getSectorCount());
473
+ @SuppressLint("DefaultLocale") String msg = String.format("mifareClassicReadSector fail: invalid sector %d (max %d)", sectorIndex, mifareTag.getSectorCount());
478
474
  callback.invoke(msg);
479
475
  return;
480
476
  }
481
477
 
482
478
  WritableArray result = Arguments.createArray();
483
479
  int blocks = mifareTag.getBlockCountInSector(sectorIndex);
484
- byte[] buffer = new byte[MifareClassic.BLOCK_SIZE];
480
+ byte[] buffer;
485
481
  for (int i = 0; i < blocks; i++) {
486
482
  buffer = mifareTag.readBlock(mifareTag.sectorToBlock(sectorIndex)+i);
487
483
  appendBytesToRnArray(result, buffer);
@@ -491,7 +487,7 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
491
487
  } catch (TagLostException ex) {
492
488
  callback.invoke("mifareClassicReadSector fail: TAG_LOST");
493
489
  } catch (Exception ex) {
494
- callback.invoke("mifareClassicReadSector fail: " + ex.toString());
490
+ callback.invoke("mifareClassicReadSector fail: " + ex);
495
491
  }
496
492
  } else {
497
493
  callback.invoke(ERR_NO_TECH_REQ);
@@ -511,12 +507,12 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
511
507
  return;
512
508
  } else if (blockIndex >= mifareTag.getBlockCount()) {
513
509
  // Check if in range
514
- String msg = String.format("mifareClassicWriteBlock fail: invalid block %d (max %d)", blockIndex, mifareTag.getBlockCount());
510
+ @SuppressLint("DefaultLocale") String msg = String.format("mifareClassicWriteBlock fail: invalid block %d (max %d)", blockIndex, mifareTag.getBlockCount());
515
511
  callback.invoke(msg);
516
512
  return;
517
513
  } else if (block.size() != MifareClassic.BLOCK_SIZE) {
518
514
  // Wrong block count
519
- String msg = String.format("mifareClassicWriteBlock fail: invalid block size %d (should be %d)", block.size(), MifareClassic.BLOCK_SIZE);
515
+ @SuppressLint("DefaultLocale") String msg = String.format("mifareClassicWriteBlock fail: invalid block size %d (should be %d)", block.size(), MifareClassic.BLOCK_SIZE);
520
516
  callback.invoke(msg);
521
517
  return;
522
518
  }
@@ -528,7 +524,7 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
528
524
  } catch (TagLostException ex) {
529
525
  callback.invoke("mifareClassicWriteBlock fail: TAG_LOST");
530
526
  } catch (Exception ex) {
531
- callback.invoke("mifareClassicWriteBlock fail: " + ex.toString());
527
+ callback.invoke("mifareClassicWriteBlock fail: " + ex);
532
528
  }
533
529
  } else {
534
530
  callback.invoke(ERR_NO_TECH_REQ);
@@ -548,7 +544,7 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
548
544
  return;
549
545
  } else if (blockIndex >= mifareTag.getBlockCount()) {
550
546
  // Check if in range
551
- String msg = String.format("mifareClassicIncrementBlock fail: invalid block %d (max %d)", blockIndex, mifareTag.getBlockCount());
547
+ @SuppressLint("DefaultLocale") String msg = String.format("mifareClassicIncrementBlock fail: invalid block %d (max %d)", blockIndex, mifareTag.getBlockCount());
552
548
  callback.invoke(msg);
553
549
  return;
554
550
  }
@@ -559,7 +555,7 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
559
555
  } catch (TagLostException ex) {
560
556
  callback.invoke("mifareClassicIncrementBlock fail: TAG_LOST");
561
557
  } catch (Exception ex) {
562
- callback.invoke("mifareClassicIncrementBlock fail: " + ex.toString());
558
+ callback.invoke("mifareClassicIncrementBlock fail: " + ex);
563
559
  }
564
560
  } else {
565
561
  callback.invoke(ERR_NO_TECH_REQ);
@@ -579,7 +575,7 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
579
575
  return;
580
576
  } else if (blockIndex >= mifareTag.getBlockCount()) {
581
577
  // Check if in range
582
- String msg = String.format("mifareClassicDecrementBlock fail: invalid block %d (max %d)", blockIndex, mifareTag.getBlockCount());
578
+ @SuppressLint("DefaultLocale") String msg = String.format("mifareClassicDecrementBlock fail: invalid block %d (max %d)", blockIndex, mifareTag.getBlockCount());
583
579
  callback.invoke(msg);
584
580
  return;
585
581
  }
@@ -590,7 +586,7 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
590
586
  } catch (TagLostException ex) {
591
587
  callback.invoke("mifareClassicDecrementBlock fail: TAG_LOST");
592
588
  } catch (Exception ex) {
593
- callback.invoke("mifareClassicDecrementBlock fail: " + ex.toString());
589
+ callback.invoke("mifareClassicDecrementBlock fail: " + ex);
594
590
  }
595
591
  } else {
596
592
  callback.invoke(ERR_NO_TECH_REQ);
@@ -610,7 +606,7 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
610
606
  return;
611
607
  } else if (blockIndex >= mifareTag.getBlockCount()) {
612
608
  // Check if in range
613
- String msg = String.format("mifareClassicTransferBlock fail: invalid block %d (max %d)", blockIndex, mifareTag.getBlockCount());
609
+ @SuppressLint("DefaultLocale") String msg = String.format("mifareClassicTransferBlock fail: invalid block %d (max %d)", blockIndex, mifareTag.getBlockCount());
614
610
  callback.invoke(msg);
615
611
  return;
616
612
  }
@@ -621,7 +617,7 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
621
617
  } catch (TagLostException ex) {
622
618
  callback.invoke("mifareClassicTransferBlock fail: TAG_LOST");
623
619
  } catch (Exception ex) {
624
- callback.invoke("mifareClassicTransferBlock fail: " + ex.toString());
620
+ callback.invoke("mifareClassicTransferBlock fail: " + ex);
625
621
  }
626
622
  } else {
627
623
  callback.invoke(ERR_NO_TECH_REQ);
@@ -638,11 +634,10 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
638
634
  byte[] resultBytes = techHandle.readPages(pageOffset);
639
635
  WritableArray resultRnArray = bytesToRnArray(resultBytes);
640
636
  callback.invoke(null, resultRnArray);
641
- return;
642
637
  } catch (TagLostException ex) {
643
638
  callback.invoke("mifareUltralight fail: TAG_LOST");
644
639
  } catch (Exception ex) {
645
- callback.invoke("mifareUltralight fail: " + ex.toString());
640
+ callback.invoke("mifareUltralight fail: " + ex);
646
641
  }
647
642
  } else {
648
643
  callback.invoke(ERR_NO_TECH_REQ);
@@ -659,11 +654,10 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
659
654
  MifareUltralight techHandle = (MifareUltralight)techRequest.getTechHandle();
660
655
  techHandle.writePage(pageOffset, bytes);
661
656
  callback.invoke();
662
- return;
663
657
  } catch (TagLostException ex) {
664
658
  callback.invoke("mifareUltralight fail: TAG_LOST");
665
659
  } catch (Exception ex) {
666
- callback.invoke("mifareUltralight fail: " + ex.toString());
660
+ callback.invoke("mifareUltralight fail: " + ex);
667
661
  }
668
662
  } else {
669
663
  callback.invoke(ERR_NO_TECH_REQ);
@@ -698,31 +692,37 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
698
692
  TagTechnology baseTechHandle = techRequest.getTechHandle();
699
693
  // TagTechnology is the base class for each tech (ex, NfcA, NfcB, IsoDep ...)
700
694
  // but it doesn't provide transceive in its interface, so we need to explicitly cast it
701
- if (tech.equals("NfcA")) {
702
- NfcA techHandle = (NfcA) baseTechHandle;
703
- techHandle.setTimeout(timeout);
704
- callback.invoke();
705
- return;
706
- } else if (tech.equals("NfcF")) {
707
- NfcF techHandle = (NfcF) baseTechHandle;
708
- techHandle.setTimeout(timeout);
709
- callback.invoke();
710
- return;
711
- } else if (tech.equals("IsoDep")) {
712
- IsoDep techHandle = (IsoDep) baseTechHandle;
713
- techHandle.setTimeout(timeout);
714
- callback.invoke();
715
- return;
716
- } else if (tech.equals("MifareClassic")) {
717
- MifareClassic techHandle = (MifareClassic) baseTechHandle;
718
- techHandle.setTimeout(timeout);
719
- callback.invoke();
720
- return;
721
- } else if (tech.equals("MifareUltralight")) {
722
- MifareUltralight techHandle = (MifareUltralight) baseTechHandle;
723
- techHandle.setTimeout(timeout);
724
- callback.invoke();
725
- return;
695
+ switch (tech) {
696
+ case "NfcA": {
697
+ NfcA techHandle = (NfcA) baseTechHandle;
698
+ techHandle.setTimeout(timeout);
699
+ callback.invoke();
700
+ return;
701
+ }
702
+ case "NfcF": {
703
+ NfcF techHandle = (NfcF) baseTechHandle;
704
+ techHandle.setTimeout(timeout);
705
+ callback.invoke();
706
+ return;
707
+ }
708
+ case "IsoDep": {
709
+ IsoDep techHandle = (IsoDep) baseTechHandle;
710
+ techHandle.setTimeout(timeout);
711
+ callback.invoke();
712
+ return;
713
+ }
714
+ case "MifareClassic": {
715
+ MifareClassic techHandle = (MifareClassic) baseTechHandle;
716
+ techHandle.setTimeout(timeout);
717
+ callback.invoke();
718
+ return;
719
+ }
720
+ case "MifareUltralight": {
721
+ MifareUltralight techHandle = (MifareUltralight) baseTechHandle;
722
+ techHandle.setTimeout(timeout);
723
+ callback.invoke();
724
+ return;
725
+ }
726
726
  }
727
727
  Log.d(LOG_TAG, "setTimeout not supported");
728
728
  callback.invoke(ERR_API_NOT_SUPPORT);
@@ -743,7 +743,6 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
743
743
  techRequest = new TagTechnologyRequest(techs.toArrayList(), callback);
744
744
  techRequest.connect(this.tag);
745
745
  callback.invoke(null, null);
746
- return;
747
746
  } catch (Exception ex) {
748
747
  callback.invoke(ex.toString());
749
748
  }
@@ -756,7 +755,6 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
756
755
  try {
757
756
  techRequest.close();
758
757
  callback.invoke(null, null);
759
- return;
760
758
  } catch (Exception ex) {
761
759
  callback.invoke(ex.toString());
762
760
  }
@@ -774,53 +772,61 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
774
772
  TagTechnology baseTechHandle = techRequest.getTechHandle();
775
773
  // TagTechnology is the base class for each tech (ex, NfcA, NfcB, IsoDep ...)
776
774
  // but it doesn't provide transceive in its interface, so we need to explicitly cast it
777
- if (tech.equals("NfcA")) {
778
- NfcA techHandle = (NfcA)baseTechHandle;
779
- byte[] resultBytes = techHandle.transceive(bytes);
780
- WritableArray resultRnArray = bytesToRnArray(resultBytes);
781
- callback.invoke(null, resultRnArray);
782
- return;
783
- } else if (tech.equals("NfcB")) {
784
- NfcB techHandle = (NfcB)baseTechHandle;
785
- byte[] resultBytes = techHandle.transceive(bytes);
786
- WritableArray resultRnArray = bytesToRnArray(resultBytes);
787
- callback.invoke(null, resultRnArray);
788
- return;
789
- } else if (tech.equals("NfcF")) {
790
- NfcF techHandle = (NfcF)baseTechHandle;
791
- byte[] resultBytes = techHandle.transceive(bytes);
792
- WritableArray resultRnArray = bytesToRnArray(resultBytes);
793
- callback.invoke(null, resultRnArray);
794
- return;
795
- } else if (tech.equals("NfcV")) {
796
- NfcV techHandle = (NfcV)baseTechHandle;
797
- byte[] resultBytes = techHandle.transceive(bytes);
798
- WritableArray resultRnArray = bytesToRnArray(resultBytes);
799
- callback.invoke(null, resultRnArray);
800
- return;
801
- } else if (tech.equals("IsoDep")) {
802
- IsoDep techHandle = (IsoDep)baseTechHandle;
803
- byte[] resultBytes = techHandle.transceive(bytes);
804
- WritableArray resultRnArray = bytesToRnArray(resultBytes);
805
- callback.invoke(null, resultRnArray);
806
- return;
807
- } else if (tech.equals("MifareClassic")) {
808
- MifareClassic techHandle = (MifareClassic) baseTechHandle;
809
- byte[] resultBytes = techHandle.transceive(bytes);
810
- WritableArray resultRnArray = bytesToRnArray(resultBytes);
811
- callback.invoke(null, resultRnArray);
812
- return;
813
- } else if (tech.equals("MifareUltralight")) {
814
- MifareUltralight techHandle = (MifareUltralight)baseTechHandle;
815
- byte[] resultBytes = techHandle.transceive(bytes);
816
- WritableArray resultRnArray = bytesToRnArray(resultBytes);
817
- callback.invoke(null, resultRnArray);
818
- return;
775
+ switch (tech) {
776
+ case "NfcA": {
777
+ NfcA techHandle = (NfcA) baseTechHandle;
778
+ byte[] resultBytes = techHandle.transceive(bytes);
779
+ WritableArray resultRnArray = bytesToRnArray(resultBytes);
780
+ callback.invoke(null, resultRnArray);
781
+ return;
782
+ }
783
+ case "NfcB": {
784
+ NfcB techHandle = (NfcB) baseTechHandle;
785
+ byte[] resultBytes = techHandle.transceive(bytes);
786
+ WritableArray resultRnArray = bytesToRnArray(resultBytes);
787
+ callback.invoke(null, resultRnArray);
788
+ return;
789
+ }
790
+ case "NfcF": {
791
+ NfcF techHandle = (NfcF) baseTechHandle;
792
+ byte[] resultBytes = techHandle.transceive(bytes);
793
+ WritableArray resultRnArray = bytesToRnArray(resultBytes);
794
+ callback.invoke(null, resultRnArray);
795
+ return;
796
+ }
797
+ case "NfcV": {
798
+ NfcV techHandle = (NfcV) baseTechHandle;
799
+ byte[] resultBytes = techHandle.transceive(bytes);
800
+ WritableArray resultRnArray = bytesToRnArray(resultBytes);
801
+ callback.invoke(null, resultRnArray);
802
+ return;
803
+ }
804
+ case "IsoDep": {
805
+ IsoDep techHandle = (IsoDep) baseTechHandle;
806
+ byte[] resultBytes = techHandle.transceive(bytes);
807
+ WritableArray resultRnArray = bytesToRnArray(resultBytes);
808
+ callback.invoke(null, resultRnArray);
809
+ return;
810
+ }
811
+ case "MifareClassic": {
812
+ MifareClassic techHandle = (MifareClassic) baseTechHandle;
813
+ byte[] resultBytes = techHandle.transceive(bytes);
814
+ WritableArray resultRnArray = bytesToRnArray(resultBytes);
815
+ callback.invoke(null, resultRnArray);
816
+ return;
817
+ }
818
+ case "MifareUltralight": {
819
+ MifareUltralight techHandle = (MifareUltralight) baseTechHandle;
820
+ byte[] resultBytes = techHandle.transceive(bytes);
821
+ WritableArray resultRnArray = bytesToRnArray(resultBytes);
822
+ callback.invoke(null, resultRnArray);
823
+ return;
824
+ }
819
825
  }
820
826
  Log.d(LOG_TAG, "transceive not supported");
821
827
  callback.invoke(ERR_API_NOT_SUPPORT);
822
828
  } catch (Exception ex) {
823
- Log.d(LOG_TAG, "transceive fail: " + ex.toString());
829
+ Log.d(LOG_TAG, "transceive fail: " + ex);
824
830
  callback.invoke(ERR_TRANSCEIVE_FAIL);
825
831
  }
826
832
  } else {
@@ -839,36 +845,43 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
839
845
  TagTechnology baseTechHandle = techRequest.getTechHandle();
840
846
  // TagTechnology is the base class for each tech (ex, NfcA, NfcB, IsoDep ...)
841
847
  // but it doesn't provide transceive in its interface, so we need to explicitly cast it
842
- if (tech.equals("NfcA")) {
843
- NfcA techHandle = (NfcA)baseTechHandle;
844
- int max = techHandle.getMaxTransceiveLength();
845
- callback.invoke(null, max);
846
- return;
847
- } else if (tech.equals("NfcB")) {
848
- NfcB techHandle = (NfcB)baseTechHandle;
849
- int max = techHandle.getMaxTransceiveLength();
850
- callback.invoke(null, max);
851
- return;
852
- } else if (tech.equals("NfcF")) {
853
- NfcF techHandle = (NfcF)baseTechHandle;
854
- int max = techHandle.getMaxTransceiveLength();
855
- callback.invoke(null, max);
856
- return;
857
- } else if (tech.equals("NfcV")) {
858
- NfcV techHandle = (NfcV)baseTechHandle;
859
- int max = techHandle.getMaxTransceiveLength();
860
- callback.invoke(null, max);
861
- return;
862
- } else if (tech.equals("IsoDep")) {
863
- IsoDep techHandle = (IsoDep)baseTechHandle;
864
- int max = techHandle.getMaxTransceiveLength();
865
- callback.invoke(null, max);
866
- return;
867
- } else if (tech.equals("MifareUltralight")) {
868
- MifareUltralight techHandle = (MifareUltralight)baseTechHandle;
869
- int max = techHandle.getMaxTransceiveLength();
870
- callback.invoke(null, max);
871
- return;
848
+ switch (tech) {
849
+ case "NfcA": {
850
+ NfcA techHandle = (NfcA) baseTechHandle;
851
+ int max = techHandle.getMaxTransceiveLength();
852
+ callback.invoke(null, max);
853
+ return;
854
+ }
855
+ case "NfcB": {
856
+ NfcB techHandle = (NfcB) baseTechHandle;
857
+ int max = techHandle.getMaxTransceiveLength();
858
+ callback.invoke(null, max);
859
+ return;
860
+ }
861
+ case "NfcF": {
862
+ NfcF techHandle = (NfcF) baseTechHandle;
863
+ int max = techHandle.getMaxTransceiveLength();
864
+ callback.invoke(null, max);
865
+ return;
866
+ }
867
+ case "NfcV": {
868
+ NfcV techHandle = (NfcV) baseTechHandle;
869
+ int max = techHandle.getMaxTransceiveLength();
870
+ callback.invoke(null, max);
871
+ return;
872
+ }
873
+ case "IsoDep": {
874
+ IsoDep techHandle = (IsoDep) baseTechHandle;
875
+ int max = techHandle.getMaxTransceiveLength();
876
+ callback.invoke(null, max);
877
+ return;
878
+ }
879
+ case "MifareUltralight": {
880
+ MifareUltralight techHandle = (MifareUltralight) baseTechHandle;
881
+ int max = techHandle.getMaxTransceiveLength();
882
+ callback.invoke(null, max);
883
+ return;
884
+ }
872
885
  }
873
886
  Log.d(LOG_TAG, "getMaxTransceiveLength not supported");
874
887
  callback.invoke(ERR_API_NOT_SUPPORT);
@@ -1158,10 +1171,6 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
1158
1171
  if (nfcAdapter != null && currentActivity != null && !currentActivity.isFinishing()) {
1159
1172
  try {
1160
1173
  if (isReaderModeEnabled) {
1161
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
1162
- throw new RuntimeException("minSdkVersion must be Honeycomb (19) or later.");
1163
- }
1164
-
1165
1174
  if (enable) {
1166
1175
  Log.i(LOG_TAG, "enableReaderMode: " + readerModeFlags);
1167
1176
  Bundle readerModeExtras = new Bundle();
@@ -1172,7 +1181,7 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
1172
1181
  synchronized (this) {
1173
1182
  manager.tag = tag;
1174
1183
  Log.d(LOG_TAG, "readerMode onTagDiscovered");
1175
- WritableMap nfcTag = null;
1184
+ WritableMap nfcTag;
1176
1185
  // if the tag contains NDEF, we want to report the content
1177
1186
  if (Arrays.asList(tag.getTechList()).contains(Ndef.class.getName())) {
1178
1187
  Ndef ndef = Ndef.get(tag);
@@ -1186,10 +1195,10 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
1186
1195
  if (techRequest!= null && !techRequest.isConnected()) {
1187
1196
  boolean result = techRequest.connect(tag);
1188
1197
  if (result) {
1189
- techRequest.getPendingCallback().invoke(null, techRequest.getTechType());
1198
+ techRequest.invokePendingCallback(techRequest.getTechType());
1190
1199
  } else {
1191
1200
  // this indicates that we get a NFC tag, but none of the user required tech is matched
1192
- techRequest.getPendingCallback().invoke(null, null);
1201
+ techRequest.invokePendingCallback(null);
1193
1202
  }
1194
1203
  }
1195
1204
  }
@@ -1215,18 +1224,19 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
1215
1224
 
1216
1225
  private PendingIntent getPendingIntent() {
1217
1226
  Activity activity = getCurrentActivity();
1227
+ assert activity != null;
1218
1228
  Intent intent = new Intent(activity, activity.getClass());
1219
1229
  intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
1220
1230
 
1221
1231
  int flag = 0;
1222
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
1232
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
1223
1233
  flag = PendingIntent.FLAG_MUTABLE;
1224
1234
  }
1225
1235
  return PendingIntent.getActivity(activity, 0, intent, flag);
1226
1236
  }
1227
1237
 
1228
1238
  private IntentFilter[] getIntentFilters() {
1229
- return intentFilters.toArray(new IntentFilter[intentFilters.size()]);
1239
+ return intentFilters.toArray(new IntentFilter[0]);
1230
1240
  }
1231
1241
 
1232
1242
  private String[][] getTechLists() {
@@ -1240,16 +1250,6 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
1240
1250
  .emit(eventName, params);
1241
1251
  }
1242
1252
 
1243
- private void sendEventWithJson(String eventName,
1244
- JSONObject json) {
1245
- try {
1246
- WritableMap map = JsonConvert.jsonToReact(json);
1247
- sendEvent(eventName, map);
1248
- } catch (JSONException ex) {
1249
- Log.d(LOG_TAG, "fireNdefEvent fail: " + ex);
1250
- }
1251
- }
1252
-
1253
1253
  private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1254
1254
  @Override
1255
1255
  public void onReceive(Context context, Intent intent) {
@@ -1297,10 +1297,6 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
1297
1297
  WritableMap nfcTag = parseNfcIntent(intent);
1298
1298
  if (nfcTag != null) {
1299
1299
  if (isForegroundEnabled) {
1300
- Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
1301
- Intent intentNew=new Intent("TagFound");
1302
- intentNew.putExtra("tag",tagFromIntent);
1303
- context.sendBroadcast(intentNew);
1304
1300
  sendEvent("NfcManagerDiscoverTag", nfcTag);
1305
1301
  } else {
1306
1302
  sendEvent("NfcManagerDiscoverBackgroundTag", nfcTag);
@@ -1335,10 +1331,10 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
1335
1331
  if (!techRequest.isConnected()) {
1336
1332
  boolean result = techRequest.connect(tag);
1337
1333
  if (result) {
1338
- techRequest.getPendingCallback().invoke(null, techRequest.getTechType());
1334
+ techRequest.invokePendingCallback(techRequest.getTechType());
1339
1335
  } else {
1340
1336
  // this indicates that we get a NFC tag, but none of the user required tech is matched
1341
- techRequest.getPendingCallback().invoke(null, null);
1337
+ techRequest.invokePendingCallback(null);
1342
1338
  }
1343
1339
  }
1344
1340
 
@@ -1347,20 +1343,25 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
1347
1343
  }
1348
1344
  }
1349
1345
 
1350
- if (action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {
1351
- Ndef ndef = Ndef.get(tag);
1352
- Parcelable[] messages = intent.getParcelableArrayExtra((NfcAdapter.EXTRA_NDEF_MESSAGES));
1353
- parsed = ndef2React(ndef, messages);
1354
- } else if (action.equals(NfcAdapter.ACTION_TECH_DISCOVERED)) {
1355
- // if the tag contains NDEF, we want to report the content
1356
- if (Arrays.asList(tag.getTechList()).contains(Ndef.class.getName())) {
1357
- Ndef ndef = Ndef.get(tag);
1358
- parsed = ndef2React(ndef, new NdefMessage[] { ndef.getCachedNdefMessage() });
1359
- } else {
1346
+ Ndef ndef;
1347
+ switch (action) {
1348
+ case NfcAdapter.ACTION_NDEF_DISCOVERED:
1349
+ ndef = Ndef.get(tag);
1350
+ Parcelable[] messages = intent.getParcelableArrayExtra((NfcAdapter.EXTRA_NDEF_MESSAGES));
1351
+ parsed = ndef2React(ndef, messages);
1352
+ break;
1353
+ case NfcAdapter.ACTION_TECH_DISCOVERED:
1354
+ // if the tag contains NDEF, we want to report the content
1355
+ if (Arrays.asList(tag.getTechList()).contains(Ndef.class.getName())) {
1356
+ ndef = Ndef.get(tag);
1357
+ parsed = ndef2React(ndef, new NdefMessage[]{ndef.getCachedNdefMessage()});
1358
+ } else {
1359
+ parsed = tag2React(tag);
1360
+ }
1361
+ break;
1362
+ case NfcAdapter.ACTION_TAG_DISCOVERED:
1360
1363
  parsed = tag2React(tag);
1361
- }
1362
- } else if (action.equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
1363
- parsed = tag2React(tag);
1364
+ break;
1364
1365
  }
1365
1366
 
1366
1367
  return parsed;
@@ -1471,8 +1472,8 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
1471
1472
  }
1472
1473
 
1473
1474
  private static WritableArray appendBytesToRnArray(WritableArray value, byte[] bytes) {
1474
- for (int i = 0; i < bytes.length; i++) {
1475
- value.pushInt((bytes[i] & 0xFF));
1475
+ for (byte aByte : bytes) {
1476
+ value.pushInt((aByte & 0xFF));
1476
1477
  }
1477
1478
  return value;
1478
1479
  }
@@ -1,8 +1,6 @@
1
1
  package community.revteltech.nfc;
2
- import android.nfc.NdefRecord;
3
- import android.nfc.NfcAdapter;
2
+
4
3
  import android.nfc.Tag;
5
- import android.nfc.TagLostException;
6
4
  import android.nfc.tech.TagTechnology;
7
5
  import android.nfc.tech.Ndef;
8
6
  import android.nfc.tech.NfcA;
@@ -34,8 +32,18 @@ class TagTechnologyRequest {
34
32
  return mTechType;
35
33
  }
36
34
 
37
- Callback getPendingCallback() {
38
- return mJsCallback;
35
+ void invokePendingCallbackWithError(String err) {
36
+ if (mJsCallback != null) {
37
+ mJsCallback.invoke(err);
38
+ mJsCallback = null;
39
+ }
40
+ }
41
+
42
+ void invokePendingCallback(String connectedTech) {
43
+ if (mJsCallback != null) {
44
+ mJsCallback.invoke(null, connectedTech);
45
+ mJsCallback = null;
46
+ }
39
47
  }
40
48
 
41
49
  TagTechnology getTechHandle() {
@@ -61,24 +69,34 @@ class TagTechnologyRequest {
61
69
  for (int i = 0; i < mTechTypes.size(); i++) {
62
70
  String techType = (String)mTechTypes.get(i);
63
71
 
64
- if (techType.equals("Ndef")) {
65
- mTech = Ndef.get(tag);
66
- } else if (techType.equals("NfcA")) {
67
- mTech = NfcA.get(tag);
68
- } else if (techType.equals("NfcB")) {
69
- mTech = NfcB.get(tag);
70
- } else if (techType.equals("NfcF")) {
71
- mTech = NfcF.get(tag);
72
- } else if (techType.equals("NfcV")) {
73
- mTech = NfcV.get(tag);
74
- } else if (techType.equals("IsoDep")) {
75
- mTech = IsoDep.get(tag);
76
- } else if (techType.equals("MifareClassic")) {
77
- mTech = MifareClassic.get(tag);
78
- } else if (techType.equals("MifareUltralight")) {
79
- mTech = MifareUltralight.get(tag);
80
- } else if (techType.equals("NdefFormatable")) {
81
- mTech = NdefFormatable.get(tag);
72
+ switch (techType) {
73
+ case "Ndef":
74
+ mTech = Ndef.get(tag);
75
+ break;
76
+ case "NfcA":
77
+ mTech = NfcA.get(tag);
78
+ break;
79
+ case "NfcB":
80
+ mTech = NfcB.get(tag);
81
+ break;
82
+ case "NfcF":
83
+ mTech = NfcF.get(tag);
84
+ break;
85
+ case "NfcV":
86
+ mTech = NfcV.get(tag);
87
+ break;
88
+ case "IsoDep":
89
+ mTech = IsoDep.get(tag);
90
+ break;
91
+ case "MifareClassic":
92
+ mTech = MifareClassic.get(tag);
93
+ break;
94
+ case "MifareUltralight":
95
+ mTech = MifareUltralight.get(tag);
96
+ break;
97
+ case "NdefFormatable":
98
+ mTech = NdefFormatable.get(tag);
99
+ break;
82
100
  }
83
101
 
84
102
  if (mTech == null) {
@@ -44,7 +44,7 @@ public class Util {
44
44
  json.put("canMakeReadOnly", JSONObject.NULL);
45
45
  }
46
46
  } catch (JSONException e) {
47
- Log.e(TAG, "Failed to convert ndef into json: " + ndef.toString(), e);
47
+ Log.e(TAG, "Failed to convert ndef into json: " + ndef, e);
48
48
  }
49
49
  }
50
50
  return json;
@@ -58,7 +58,7 @@ public class Util {
58
58
  json.put("id", bytesToHex(tag.getId()));
59
59
  json.put("techTypes", new JSONArray(Arrays.asList(tag.getTechList())));
60
60
  } catch (JSONException e) {
61
- Log.e(TAG, "Failed to convert tag into json: " + tag.toString(), e);
61
+ Log.e(TAG, "Failed to convert tag into json: " + tag, e);
62
62
  }
63
63
  }
64
64
  return json;
@@ -66,34 +66,26 @@ public class Util {
66
66
 
67
67
  static String translateType(String type) {
68
68
  String translation;
69
- if (type.equals(Ndef.NFC_FORUM_TYPE_1)) {
70
- translation = "NFC Forum Type 1";
71
- } else if (type.equals(Ndef.NFC_FORUM_TYPE_2)) {
72
- translation = "NFC Forum Type 2";
73
- } else if (type.equals(Ndef.NFC_FORUM_TYPE_3)) {
74
- translation = "NFC Forum Type 3";
75
- } else if (type.equals(Ndef.NFC_FORUM_TYPE_4)) {
76
- translation = "NFC Forum Type 4";
77
- } else {
78
- translation = type;
69
+ switch (type) {
70
+ case Ndef.NFC_FORUM_TYPE_1:
71
+ translation = "NFC Forum Type 1";
72
+ break;
73
+ case Ndef.NFC_FORUM_TYPE_2:
74
+ translation = "NFC Forum Type 2";
75
+ break;
76
+ case Ndef.NFC_FORUM_TYPE_3:
77
+ translation = "NFC Forum Type 3";
78
+ break;
79
+ case Ndef.NFC_FORUM_TYPE_4:
80
+ translation = "NFC Forum Type 4";
81
+ break;
82
+ default:
83
+ translation = type;
84
+ break;
79
85
  }
80
86
  return translation;
81
87
  }
82
88
 
83
- static NdefRecord[] jsonToNdefRecords(String ndefMessageAsJSON) throws JSONException {
84
- JSONArray jsonRecords = new JSONArray(ndefMessageAsJSON);
85
- NdefRecord[] records = new NdefRecord[jsonRecords.length()];
86
- for (int i = 0; i < jsonRecords.length(); i++) {
87
- JSONObject record = jsonRecords.getJSONObject(i);
88
- byte tnf = (byte) record.getInt("tnf");
89
- byte[] type = jsonToByteArray(record.getJSONArray("type"));
90
- byte[] id = jsonToByteArray(record.getJSONArray("id"));
91
- byte[] payload = jsonToByteArray(record.getJSONArray("payload"));
92
- records[i] = new NdefRecord(tnf, type, id, payload);
93
- }
94
- return records;
95
- }
96
-
97
89
  static JSONArray byteArrayToJSON(byte[] bytes) {
98
90
  JSONArray json = new JSONArray();
99
91
  for (byte aByte : bytes) {
@@ -116,20 +108,12 @@ public class Util {
116
108
  return new String(hexChars);
117
109
  }
118
110
 
119
- static byte[] jsonToByteArray(JSONArray json) throws JSONException {
120
- byte[] b = new byte[json.length()];
121
- for (int i = 0; i < json.length(); i++) {
122
- b[i] = (byte) json.getInt(i);
123
- }
124
- return b;
125
- }
126
-
127
111
  static JSONArray messageToJSON(NdefMessage message) {
128
112
  if (message == null) {
129
113
  return null;
130
114
  }
131
115
 
132
- List<JSONObject> list = new ArrayList<JSONObject>();
116
+ List<JSONObject> list = new ArrayList<>();
133
117
 
134
118
  for (NdefRecord ndefRecord : message.getRecords()) {
135
119
  list.add(recordToJSON(ndefRecord));
@@ -147,7 +131,7 @@ public class Util {
147
131
  json.put("payload", byteArrayToJSON(record.getPayload()));
148
132
  } catch (JSONException e) {
149
133
  //Not sure why this would happen, documentation is unclear.
150
- Log.e(TAG, "Failed to convert ndef record into json: " + record.toString(), e);
134
+ Log.e(TAG, "Failed to convert ndef record into json: " + record, e);
151
135
  }
152
136
  return json;
153
137
  }
package/app.plugin.js CHANGED
@@ -39,13 +39,17 @@ function addValuesToArray(obj, key, values) {
39
39
  return obj;
40
40
  }
41
41
 
42
- function withIosNfcEntitlement(c) {
42
+ function withIosNfcEntitlement(c, {includeNdefEntitlement}) {
43
43
  return withEntitlementsPlist(c, (config) => {
44
44
  // Add the required formats
45
+ let entitlements = ['NDEF', 'TAG']
46
+ if (includeNdefEntitlement === false) {
47
+ entitlements = ['TAG']
48
+ }
45
49
  config.modResults = addValuesToArray(
46
50
  config.modResults,
47
51
  'com.apple.developer.nfc.readersession.formats',
48
- ['NDEF', 'TAG'],
52
+ entitlements,
49
53
  );
50
54
 
51
55
  return config;
@@ -81,8 +85,8 @@ function withIosNfcSystemCodes(c, {systemCodes}) {
81
85
  }
82
86
 
83
87
  function withNfc(config, props = {}) {
84
- const {nfcPermission, selectIdentifiers, systemCodes} = props;
85
- config = withIosNfcEntitlement(config);
88
+ const {nfcPermission, selectIdentifiers, systemCodes, includeNdefEntitlement} = props;
89
+ config = withIosNfcEntitlement(config, {includeNdefEntitlement});
86
90
  config = withIosNfcSelectIdentifiers(config, {selectIdentifiers});
87
91
  config = withIosNfcSystemCodes(config, {systemCodes});
88
92
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-nfc-manager",
3
- "version": "3.14.0",
3
+ "version": "3.14.2",
4
4
  "description": "A NFC module for react native.",
5
5
  "main": "src/index.js",
6
6
  "repository": {