@spencerls/react-native-nfc 1.0.9 → 1.0.11
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/API.md +231 -104
- package/README.md +155 -166
- package/dist/index.d.mts +53 -71
- package/dist/index.d.ts +53 -71
- package/dist/index.js +437 -300
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +433 -304
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/API.md
CHANGED
|
@@ -14,11 +14,10 @@ import {
|
|
|
14
14
|
} from "@spencerls/react-native-nfc";
|
|
15
15
|
|
|
16
16
|
import {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
NfcProvider
|
|
17
|
+
useNfcTech,
|
|
18
|
+
useNfcTechLoop,
|
|
19
|
+
useNfcTagEvent,
|
|
20
|
+
useNfcTagEventLoop,
|
|
22
21
|
} from "@spencerls/react-native-nfc";
|
|
23
22
|
```
|
|
24
23
|
|
|
@@ -30,63 +29,106 @@ import {
|
|
|
30
29
|
import { nfcService } from "@spencerls/react-native-nfc";
|
|
31
30
|
```
|
|
32
31
|
|
|
32
|
+
The `nfcService` is a singleton that manages NFC operations using a job-based architecture with strategies for different operation types.
|
|
33
|
+
|
|
33
34
|
### Methods
|
|
34
35
|
|
|
35
|
-
####
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
#### startTech(tech, withTechnology, afterTechnology?, options?)
|
|
37
|
+
|
|
38
|
+
Starts a one-shot technology session. Executes once and completes.
|
|
38
39
|
|
|
39
40
|
```ts
|
|
40
|
-
nfcService.
|
|
41
|
+
await nfcService.startTech(
|
|
42
|
+
tech: NfcTech[],
|
|
43
|
+
withTechnology: () => Promise<void>,
|
|
44
|
+
afterTechnology?: () => Promise<void>,
|
|
45
|
+
options?: RegisterTagEventOpts
|
|
46
|
+
): Promise<void>
|
|
41
47
|
```
|
|
42
48
|
|
|
43
49
|
**Example:**
|
|
44
50
|
```ts
|
|
45
|
-
import {
|
|
51
|
+
import { NfcTech } from "react-native-nfc-manager";
|
|
46
52
|
|
|
47
|
-
nfcService.
|
|
48
|
-
|
|
53
|
+
await nfcService.startTech(
|
|
54
|
+
[NfcTech.NfcV],
|
|
55
|
+
async () => {
|
|
56
|
+
const tag = await nfcTag.getTag();
|
|
57
|
+
console.log("Tag ID:", tag.id);
|
|
58
|
+
}
|
|
49
59
|
);
|
|
50
60
|
```
|
|
51
61
|
|
|
52
|
-
####
|
|
53
|
-
|
|
62
|
+
#### startTechLoop(tech, withTechnology, afterTechnology?, options?)
|
|
63
|
+
|
|
64
|
+
Starts a continuous technology session loop. Automatically restarts after each tag interaction.
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
await nfcService.startTechLoop(
|
|
68
|
+
tech: NfcTech[],
|
|
69
|
+
withTechnology: () => Promise<void>,
|
|
70
|
+
afterTechnology?: () => Promise<void>,
|
|
71
|
+
options?: RegisterTagEventOpts
|
|
72
|
+
): Promise<void>
|
|
73
|
+
```
|
|
54
74
|
|
|
75
|
+
**Example:**
|
|
55
76
|
```ts
|
|
56
|
-
nfcService.
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
)
|
|
77
|
+
await nfcService.startTechLoop(
|
|
78
|
+
[NfcTech.Ndef],
|
|
79
|
+
async () => {
|
|
80
|
+
const tag = await nfcTag.getTag();
|
|
81
|
+
console.log("Tag detected:", tag.id);
|
|
82
|
+
}
|
|
83
|
+
);
|
|
60
84
|
```
|
|
61
85
|
|
|
62
|
-
####
|
|
63
|
-
|
|
86
|
+
#### startTagEvent(onTag)
|
|
87
|
+
|
|
88
|
+
Starts a one-shot tag event handler. Executes once when a tag is detected.
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
await nfcService.startTagEvent(
|
|
92
|
+
onTag: (tag: TagEvent) => Promise<void>
|
|
93
|
+
): Promise<void>
|
|
94
|
+
```
|
|
64
95
|
|
|
96
|
+
**Example:**
|
|
65
97
|
```ts
|
|
66
|
-
await nfcService.
|
|
98
|
+
await nfcService.startTagEvent(async (tag) => {
|
|
99
|
+
console.log("Tag ID:", tag.id);
|
|
100
|
+
});
|
|
67
101
|
```
|
|
68
102
|
|
|
69
|
-
####
|
|
70
|
-
Opens an iOS/Android technology session. Stops reader mode before starting.
|
|
103
|
+
#### startTagEventLoop(onTag, options?)
|
|
71
104
|
|
|
105
|
+
Starts a continuous tag event loop. Automatically restarts after each tag detection.
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
await nfcService.startTagEventLoop(
|
|
109
|
+
onTag: (tag: TagEvent) => Promise<void>,
|
|
110
|
+
options?: RegisterTagEventOpts
|
|
111
|
+
): Promise<void>
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
**Example:**
|
|
72
115
|
```ts
|
|
73
|
-
await nfcService.
|
|
74
|
-
|
|
75
|
-
|
|
116
|
+
await nfcService.startTagEventLoop(
|
|
117
|
+
async (tag) => {
|
|
118
|
+
console.log("Tag detected:", tag.id);
|
|
119
|
+
},
|
|
120
|
+
{ readerModeFlags: ... }
|
|
76
121
|
);
|
|
77
122
|
```
|
|
78
123
|
|
|
79
|
-
####
|
|
80
|
-
|
|
124
|
+
#### stop()
|
|
125
|
+
|
|
126
|
+
Stops all NFC operations and cleans up active sessions.
|
|
81
127
|
|
|
82
128
|
```ts
|
|
83
|
-
|
|
129
|
+
await nfcService.stop(): Promise<void>
|
|
84
130
|
```
|
|
85
131
|
|
|
86
|
-
#### subscribe(listener)
|
|
87
|
-
Attach a state listener; returns an unsubscribe function.
|
|
88
|
-
|
|
89
|
-
---
|
|
90
132
|
|
|
91
133
|
# Namespace API: `nfc`
|
|
92
134
|
|
|
@@ -357,7 +399,7 @@ Builder.record(init: {
|
|
|
357
399
|
|
|
358
400
|
# Low-Level Tag Modules
|
|
359
401
|
|
|
360
|
-
These modules are used for advanced operations inside
|
|
402
|
+
These modules are used for advanced operations inside technology sessions.
|
|
361
403
|
|
|
362
404
|
## `nfcTag`
|
|
363
405
|
|
|
@@ -375,7 +417,7 @@ await nfcTag.getTag(): Promise<TagEvent>
|
|
|
375
417
|
|
|
376
418
|
**Example:**
|
|
377
419
|
```ts
|
|
378
|
-
await nfc.service.
|
|
420
|
+
await nfc.service.startTech([NfcTech.NfcV], async () => {
|
|
379
421
|
const tag = await nfcTag.getTag();
|
|
380
422
|
console.log("Tag ID:", tag.id);
|
|
381
423
|
});
|
|
@@ -389,11 +431,11 @@ await nfc.service.withTechnology(NfcTech.NfcV, async () => {
|
|
|
389
431
|
import { nfcVTag } from "@spencerls/react-native-nfc";
|
|
390
432
|
```
|
|
391
433
|
|
|
392
|
-
Low-level NFC-V operations. Use inside
|
|
434
|
+
Low-level NFC-V operations. Use inside technology sessions.
|
|
393
435
|
|
|
394
436
|
### Properties
|
|
395
437
|
|
|
396
|
-
- `tech`: NfcTech constant for NFC-V (use with
|
|
438
|
+
- `tech`: NfcTech constant for NFC-V (use with technology sessions)
|
|
397
439
|
- `utils`: ISO15693 utility functions
|
|
398
440
|
|
|
399
441
|
### Methods
|
|
@@ -450,9 +492,9 @@ await nfcVTag.transceive(bytes: number[]): Promise<number[]>
|
|
|
450
492
|
|
|
451
493
|
**Example:**
|
|
452
494
|
```ts
|
|
453
|
-
import { nfcTag, nfcVTag } from "@spencerls/react-native-nfc";
|
|
495
|
+
import { nfc, nfcTag, nfcVTag } from "@spencerls/react-native-nfc";
|
|
454
496
|
|
|
455
|
-
await nfc.service.
|
|
497
|
+
await nfc.service.startTech(nfcVTag.tech, async () => {
|
|
456
498
|
const tag = await nfcTag.getTag();
|
|
457
499
|
if (!tag?.id) throw new Error("No tag detected");
|
|
458
500
|
|
|
@@ -469,11 +511,11 @@ await nfc.service.withTechnology(nfcVTag.tech, async () => {
|
|
|
469
511
|
import { nfcNdefTag } from "@spencerls/react-native-nfc";
|
|
470
512
|
```
|
|
471
513
|
|
|
472
|
-
Low-level NDEF operations. Use inside
|
|
514
|
+
Low-level NDEF operations. Use inside technology sessions.
|
|
473
515
|
|
|
474
516
|
### Properties
|
|
475
517
|
|
|
476
|
-
- `tech`: NfcTech constant for NDEF (use with
|
|
518
|
+
- `tech`: NfcTech constant for NDEF (use with technology sessions)
|
|
477
519
|
|
|
478
520
|
### Methods
|
|
479
521
|
|
|
@@ -491,9 +533,9 @@ await nfcNdefTag.write(records: NdefRecord[]): Promise<void>
|
|
|
491
533
|
|
|
492
534
|
**Example:**
|
|
493
535
|
```ts
|
|
494
|
-
import { nfcNdefTag } from "@spencerls/react-native-nfc";
|
|
536
|
+
import { nfc, nfcNdefTag } from "@spencerls/react-native-nfc";
|
|
495
537
|
|
|
496
|
-
await nfc.service.
|
|
538
|
+
await nfc.service.startTech(nfcNdefTag.tech, async () => {
|
|
497
539
|
const message = await nfcNdefTag.readMessage();
|
|
498
540
|
console.log("Records:", message.ndefMessage);
|
|
499
541
|
});
|
|
@@ -511,96 +553,159 @@ import { ... } from "@spencerls/react-native-nfc";
|
|
|
511
553
|
|
|
512
554
|
---
|
|
513
555
|
|
|
514
|
-
##
|
|
515
|
-
|
|
516
|
-
Automatically starts reader mode on mount and stops on unmount.
|
|
556
|
+
## useNfcTech(tech, withTechnology, afterTechnology?, options?)
|
|
517
557
|
|
|
518
|
-
|
|
558
|
+
One-shot technology session. Executes once when triggered.
|
|
519
559
|
|
|
520
560
|
```ts
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
561
|
+
const { startTech } = useNfcTech(
|
|
562
|
+
tech: NfcTech[],
|
|
563
|
+
withTechnology: () => Promise<void>,
|
|
564
|
+
afterTechnology?: () => Promise<void>,
|
|
565
|
+
options?: RegisterTagEventOpts
|
|
526
566
|
);
|
|
527
567
|
```
|
|
528
568
|
|
|
529
569
|
**Example:**
|
|
530
570
|
```ts
|
|
531
|
-
import {
|
|
532
|
-
import {
|
|
533
|
-
|
|
534
|
-
// Call once at app startup
|
|
535
|
-
nfcService.enableReaderMode_ANDROID(NfcAdapter.FLAG_READER_NFC_V);
|
|
571
|
+
import { useNfcTech, nfcTag } from "@spencerls/react-native-nfc";
|
|
572
|
+
import { NfcTech } from "react-native-nfc-manager";
|
|
536
573
|
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
574
|
+
function ScanButton() {
|
|
575
|
+
const { startTech } = useNfcTech(
|
|
576
|
+
[NfcTech.NfcV],
|
|
577
|
+
async () => {
|
|
578
|
+
const tag = await nfcTag.getTag();
|
|
579
|
+
console.log("Tag ID:", tag.id);
|
|
580
|
+
}
|
|
581
|
+
);
|
|
582
|
+
|
|
583
|
+
return <Button title="Scan" onPress={startTech} />;
|
|
584
|
+
}
|
|
585
|
+
```
|
|
586
|
+
|
|
587
|
+
**With afterTechnology callback:**
|
|
588
|
+
```ts
|
|
589
|
+
const { startTech } = useNfcTech(
|
|
590
|
+
[NfcTech.NfcV],
|
|
591
|
+
async () => {
|
|
592
|
+
const tag = await nfcTag.getTag();
|
|
593
|
+
console.log("Tag ID:", tag.id);
|
|
594
|
+
},
|
|
595
|
+
async () => {
|
|
596
|
+
console.log("Scan complete");
|
|
597
|
+
}
|
|
598
|
+
);
|
|
541
599
|
```
|
|
542
600
|
|
|
543
601
|
---
|
|
544
602
|
|
|
545
|
-
##
|
|
603
|
+
## useNfcTechLoop(tech, withTechnology, afterTechnology?, options?)
|
|
546
604
|
|
|
547
|
-
|
|
605
|
+
Continuous technology session loop. Automatically restarts after each tag interaction.
|
|
548
606
|
|
|
549
|
-
```
|
|
550
|
-
const {
|
|
607
|
+
```ts
|
|
608
|
+
const { start, stop, isRunning } = useNfcTechLoop(
|
|
609
|
+
tech: NfcTech[],
|
|
610
|
+
withTechnology: () => Promise<void>,
|
|
611
|
+
afterTechnology?: () => Promise<void>,
|
|
612
|
+
options?: RegisterTagEventOpts
|
|
613
|
+
);
|
|
551
614
|
```
|
|
552
615
|
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
## useNfcReader()
|
|
556
|
-
|
|
557
|
-
Manual control over reader mode.
|
|
558
|
-
|
|
559
|
-
**Note:** Call `nfcService.enableReaderMode_ANDROID(flags)` before calling `start()` on Android.
|
|
560
|
-
|
|
616
|
+
**Example:**
|
|
561
617
|
```ts
|
|
562
|
-
|
|
618
|
+
import { useNfcTechLoop, nfcTag } from "@spencerls/react-native-nfc";
|
|
619
|
+
import { NfcTech } from "react-native-nfc-manager";
|
|
563
620
|
|
|
564
|
-
|
|
565
|
-
|
|
621
|
+
function ContinuousScanScreen() {
|
|
622
|
+
const { start, stop, isRunning } = useNfcTechLoop(
|
|
623
|
+
[NfcTech.Ndef],
|
|
624
|
+
async () => {
|
|
625
|
+
const tag = await nfcTag.getTag();
|
|
626
|
+
console.log("Tag detected:", tag.id);
|
|
627
|
+
}
|
|
628
|
+
);
|
|
629
|
+
|
|
630
|
+
return (
|
|
631
|
+
<View>
|
|
632
|
+
<Button title={isRunning ? "Stop" : "Start"} onPress={isRunning ? stop : start} />
|
|
633
|
+
<Text>{isRunning ? "Scanning..." : "Idle"}</Text>
|
|
634
|
+
</View>
|
|
635
|
+
);
|
|
636
|
+
}
|
|
637
|
+
```
|
|
638
|
+
|
|
639
|
+
**With afterTechnology callback:**
|
|
640
|
+
```ts
|
|
641
|
+
const { start, stop, isRunning } = useNfcTechLoop(
|
|
642
|
+
[NfcTech.Ndef],
|
|
643
|
+
async () => {
|
|
644
|
+
const tag = await nfcTag.getTag();
|
|
645
|
+
console.log("Tag detected:", tag.id);
|
|
646
|
+
},
|
|
647
|
+
async () => {
|
|
648
|
+
console.log("Waiting for next tag...");
|
|
649
|
+
}
|
|
650
|
+
);
|
|
566
651
|
```
|
|
567
652
|
|
|
568
653
|
---
|
|
569
654
|
|
|
570
|
-
##
|
|
655
|
+
## useNfcTagEvent(onTag)
|
|
571
656
|
|
|
572
|
-
|
|
657
|
+
One-shot tag event handler. Executes once when a tag is detected.
|
|
573
658
|
|
|
574
659
|
```ts
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
660
|
+
const { startTech } = useNfcTagEvent(
|
|
661
|
+
onTag: (tag: TagEvent) => Promise<void>
|
|
662
|
+
);
|
|
578
663
|
```
|
|
579
664
|
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
665
|
+
**Example:**
|
|
666
|
+
```ts
|
|
667
|
+
import { useNfcTagEvent } from "@spencerls/react-native-nfc";
|
|
583
668
|
|
|
584
|
-
|
|
669
|
+
function QuickScan() {
|
|
670
|
+
const { startTech } = useNfcTagEvent(async (tag) => {
|
|
671
|
+
console.log("Tag ID:", tag.id);
|
|
672
|
+
});
|
|
585
673
|
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
<App />
|
|
589
|
-
</NfcProvider>
|
|
674
|
+
return <Button title="Quick Scan" onPress={startTech} />;
|
|
675
|
+
}
|
|
590
676
|
```
|
|
591
677
|
|
|
592
678
|
---
|
|
593
679
|
|
|
594
|
-
|
|
680
|
+
## useNfcTagEventLoop(onTag, options?)
|
|
595
681
|
|
|
596
|
-
|
|
682
|
+
Continuous tag event loop. Automatically restarts after each tag detection.
|
|
597
683
|
|
|
598
|
-
|
|
684
|
+
```ts
|
|
685
|
+
const { start, stop, isRunning } = useNfcTagEventLoop(
|
|
686
|
+
onTag: (tag: TagEvent) => Promise<void>,
|
|
687
|
+
options?: RegisterTagEventOpts
|
|
688
|
+
);
|
|
689
|
+
```
|
|
599
690
|
|
|
691
|
+
**Example:**
|
|
600
692
|
```ts
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
693
|
+
import { useNfcTagEventLoop } from "@spencerls/react-native-nfc";
|
|
694
|
+
|
|
695
|
+
function MonitorScreen() {
|
|
696
|
+
const { start, stop, isRunning } = useNfcTagEventLoop(
|
|
697
|
+
async (tag) => {
|
|
698
|
+
console.log("Tag detected:", tag.id);
|
|
699
|
+
}
|
|
700
|
+
);
|
|
701
|
+
|
|
702
|
+
return (
|
|
703
|
+
<View>
|
|
704
|
+
<Button title={isRunning ? "Stop Monitoring" : "Start Monitoring"}
|
|
705
|
+
onPress={isRunning ? stop : start} />
|
|
706
|
+
</View>
|
|
707
|
+
);
|
|
708
|
+
}
|
|
604
709
|
```
|
|
605
710
|
|
|
606
711
|
---
|
|
@@ -621,11 +726,11 @@ await nfc.ndef.write(records);
|
|
|
621
726
|
|
|
622
727
|
### Low-Level (Advanced)
|
|
623
728
|
|
|
624
|
-
Use
|
|
729
|
+
Use `nfc.service.startTech()` with tag modules for complex custom operations.
|
|
625
730
|
|
|
626
731
|
```ts
|
|
627
732
|
// ✅ Advanced: custom multi-block read
|
|
628
|
-
await nfc.service.
|
|
733
|
+
await nfc.service.startTech(nfcVTag.tech, async () => {
|
|
629
734
|
const tag = await nfcTag.getTag();
|
|
630
735
|
if (!tag?.id) throw new Error("No NFC-V tag detected");
|
|
631
736
|
|
|
@@ -639,21 +744,43 @@ await nfc.service.withTechnology(nfcVTag.tech, async () => {
|
|
|
639
744
|
offset += block.length;
|
|
640
745
|
}
|
|
641
746
|
|
|
642
|
-
|
|
747
|
+
console.log("Data:", buffer);
|
|
643
748
|
});
|
|
644
749
|
```
|
|
645
750
|
|
|
751
|
+
## One-Shot vs Loop
|
|
752
|
+
|
|
753
|
+
### One-Shot Operations
|
|
754
|
+
|
|
755
|
+
Use for single tag interactions (scan once and done):
|
|
756
|
+
|
|
757
|
+
```ts
|
|
758
|
+
const { startTech } = useNfcTech(...);
|
|
759
|
+
// or
|
|
760
|
+
const { startTech } = useNfcTagEvent(...);
|
|
761
|
+
```
|
|
762
|
+
|
|
763
|
+
### Continuous Loops
|
|
764
|
+
|
|
765
|
+
Use for monitoring or multiple sequential tag interactions:
|
|
766
|
+
|
|
767
|
+
```ts
|
|
768
|
+
const { start, stop, isRunning } = useNfcTechLoop(...);
|
|
769
|
+
// or
|
|
770
|
+
const { start, stop, isRunning } = useNfcTagEventLoop(...);
|
|
771
|
+
```
|
|
772
|
+
|
|
646
773
|
---
|
|
647
774
|
|
|
648
775
|
# Internal Notes
|
|
649
776
|
|
|
650
|
-
-
|
|
651
|
-
-
|
|
652
|
-
-
|
|
653
|
-
-
|
|
654
|
-
- `NfcTech` enums must be used. Do not pass raw strings.
|
|
777
|
+
- All APIs are 100% cross-platform (iOS and Android).
|
|
778
|
+
- Technology sessions are automatically managed and cleaned up.
|
|
779
|
+
- Hooks automatically stop sessions on component unmount.
|
|
780
|
+
- The service uses a job-based architecture with retry mechanisms.
|
|
655
781
|
- High-level `nfc.*` operations automatically manage technology sessions.
|
|
656
|
-
-
|
|
782
|
+
- For custom operations, use `nfc.service.startTech()` with low-level tag modules.
|
|
783
|
+
- Loop operations automatically restart after each tag interaction.
|
|
657
784
|
|
|
658
785
|
---
|
|
659
786
|
|