@spencerls/react-native-nfc 1.0.10 → 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 CHANGED
@@ -14,10 +14,10 @@ import {
14
14
  } from "@spencerls/react-native-nfc";
15
15
 
16
16
  import {
17
- useNfc,
18
- useNfcState,
19
- useNfcReader,
20
- useNfcTechnology,
17
+ useNfcTech,
18
+ useNfcTechLoop,
19
+ useNfcTagEvent,
20
+ useNfcTagEventLoop,
21
21
  } from "@spencerls/react-native-nfc";
22
22
  ```
23
23
 
@@ -29,63 +29,106 @@ import {
29
29
  import { nfcService } from "@spencerls/react-native-nfc";
30
30
  ```
31
31
 
32
+ The `nfcService` is a singleton that manages NFC operations using a job-based architecture with strategies for different operation types.
33
+
32
34
  ### Methods
33
35
 
34
- #### enableReaderMode_ANDROID(flags)
35
- Configures Android reader mode flags. Must be called before `startReader()` or `withTechnology()`.
36
- No-op on iOS.
36
+ #### startTech(tech, withTechnology, afterTechnology?, options?)
37
+
38
+ Starts a one-shot technology session. Executes once and completes.
37
39
 
38
40
  ```ts
39
- nfcService.enableReaderMode_ANDROID(flags: number);
41
+ await nfcService.startTech(
42
+ tech: NfcTech[],
43
+ withTechnology: () => Promise<void>,
44
+ afterTechnology?: () => Promise<void>,
45
+ options?: RegisterTagEventOpts
46
+ ): Promise<void>
40
47
  ```
41
48
 
42
49
  **Example:**
43
50
  ```ts
44
- import { NfcAdapter } from "react-native-nfc-manager";
51
+ import { NfcTech } from "react-native-nfc-manager";
45
52
 
46
- nfcService.enableReaderMode_ANDROID(
47
- NfcAdapter.FLAG_READER_NFC_V | NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS
53
+ await nfcService.startTech(
54
+ [NfcTech.NfcV],
55
+ async () => {
56
+ const tag = await nfcTag.getTag();
57
+ console.log("Tag ID:", tag.id);
58
+ }
48
59
  );
49
60
  ```
50
61
 
51
- #### startReader(onTag, options?)
52
- Starts platform reader mode.
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
+ ```
53
74
 
75
+ **Example:**
54
76
  ```ts
55
- nfcService.startReader(
56
- onTag?: (tag: TagEvent) => void,
57
- options?: { cooldownMs?: number }
58
- )
77
+ await nfcService.startTechLoop(
78
+ [NfcTech.Ndef],
79
+ async () => {
80
+ const tag = await nfcTag.getTag();
81
+ console.log("Tag detected:", tag.id);
82
+ }
83
+ );
59
84
  ```
60
85
 
61
- #### stopReader()
62
- Stops reader mode.
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
+ ```
63
95
 
96
+ **Example:**
64
97
  ```ts
65
- await nfcService.stopReader();
98
+ await nfcService.startTagEvent(async (tag) => {
99
+ console.log("Tag ID:", tag.id);
100
+ });
66
101
  ```
67
102
 
68
- #### withTechnology(tech, handler)
69
- Opens an iOS/Android technology session. Stops reader mode before starting.
103
+ #### startTagEventLoop(onTag, options?)
70
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:**
71
115
  ```ts
72
- await nfcService.withTechnology(
73
- tech: NfcTech | NfcTech[],
74
- async () => { /* commands */ }
116
+ await nfcService.startTagEventLoop(
117
+ async (tag) => {
118
+ console.log("Tag detected:", tag.id);
119
+ },
120
+ { readerModeFlags: ... }
75
121
  );
76
122
  ```
77
123
 
78
- #### getState()
79
- Returns the current state snapshot.
124
+ #### stop()
125
+
126
+ Stops all NFC operations and cleans up active sessions.
80
127
 
81
128
  ```ts
82
- { mode, tag }
129
+ await nfcService.stop(): Promise<void>
83
130
  ```
84
131
 
85
- #### subscribe(listener)
86
- Attach a state listener; returns an unsubscribe function.
87
-
88
- ---
89
132
 
90
133
  # Namespace API: `nfc`
91
134
 
@@ -356,7 +399,7 @@ Builder.record(init: {
356
399
 
357
400
  # Low-Level Tag Modules
358
401
 
359
- These modules are used for advanced operations inside `withTechnology` sessions.
402
+ These modules are used for advanced operations inside technology sessions.
360
403
 
361
404
  ## `nfcTag`
362
405
 
@@ -374,7 +417,7 @@ await nfcTag.getTag(): Promise<TagEvent>
374
417
 
375
418
  **Example:**
376
419
  ```ts
377
- await nfc.service.withTechnology(NfcTech.NfcV, async () => {
420
+ await nfc.service.startTech([NfcTech.NfcV], async () => {
378
421
  const tag = await nfcTag.getTag();
379
422
  console.log("Tag ID:", tag.id);
380
423
  });
@@ -388,11 +431,11 @@ await nfc.service.withTechnology(NfcTech.NfcV, async () => {
388
431
  import { nfcVTag } from "@spencerls/react-native-nfc";
389
432
  ```
390
433
 
391
- Low-level NFC-V operations. Use inside `withTechnology` sessions.
434
+ Low-level NFC-V operations. Use inside technology sessions.
392
435
 
393
436
  ### Properties
394
437
 
395
- - `tech`: NfcTech constant for NFC-V (use with `withTechnology`)
438
+ - `tech`: NfcTech constant for NFC-V (use with technology sessions)
396
439
  - `utils`: ISO15693 utility functions
397
440
 
398
441
  ### Methods
@@ -449,9 +492,9 @@ await nfcVTag.transceive(bytes: number[]): Promise<number[]>
449
492
 
450
493
  **Example:**
451
494
  ```ts
452
- import { nfcTag, nfcVTag } from "@spencerls/react-native-nfc";
495
+ import { nfc, nfcTag, nfcVTag } from "@spencerls/react-native-nfc";
453
496
 
454
- await nfc.service.withTechnology(nfcVTag.tech, async () => {
497
+ await nfc.service.startTech(nfcVTag.tech, async () => {
455
498
  const tag = await nfcTag.getTag();
456
499
  if (!tag?.id) throw new Error("No tag detected");
457
500
 
@@ -468,11 +511,11 @@ await nfc.service.withTechnology(nfcVTag.tech, async () => {
468
511
  import { nfcNdefTag } from "@spencerls/react-native-nfc";
469
512
  ```
470
513
 
471
- Low-level NDEF operations. Use inside `withTechnology` sessions.
514
+ Low-level NDEF operations. Use inside technology sessions.
472
515
 
473
516
  ### Properties
474
517
 
475
- - `tech`: NfcTech constant for NDEF (use with `withTechnology`)
518
+ - `tech`: NfcTech constant for NDEF (use with technology sessions)
476
519
 
477
520
  ### Methods
478
521
 
@@ -490,9 +533,9 @@ await nfcNdefTag.write(records: NdefRecord[]): Promise<void>
490
533
 
491
534
  **Example:**
492
535
  ```ts
493
- import { nfcNdefTag } from "@spencerls/react-native-nfc";
536
+ import { nfc, nfcNdefTag } from "@spencerls/react-native-nfc";
494
537
 
495
- await nfc.service.withTechnology(nfcNdefTag.tech, async () => {
538
+ await nfc.service.startTech(nfcNdefTag.tech, async () => {
496
539
  const message = await nfcNdefTag.readMessage();
497
540
  console.log("Records:", message.ndefMessage);
498
541
  });
@@ -510,84 +553,159 @@ import { ... } from "@spencerls/react-native-nfc";
510
553
 
511
554
  ---
512
555
 
513
- ## useNfc(onTag, options)
514
-
515
- Automatically starts reader mode on mount and stops on unmount.
556
+ ## useNfcTech(tech, withTechnology, afterTechnology?, options?)
516
557
 
517
- **Note:** Call `nfcService.enableReaderMode_ANDROID(flags)` before using this hook on Android.
558
+ One-shot technology session. Executes once when triggered.
518
559
 
519
560
  ```ts
520
- useNfc(
521
- (tagId: string) => { ... },
522
- {
523
- cooldownMs?: number
524
- }
561
+ const { startTech } = useNfcTech(
562
+ tech: NfcTech[],
563
+ withTechnology: () => Promise<void>,
564
+ afterTechnology?: () => Promise<void>,
565
+ options?: RegisterTagEventOpts
525
566
  );
526
567
  ```
527
568
 
528
569
  **Example:**
529
570
  ```ts
530
- import { nfcService } from "@spencerls/react-native-nfc";
531
- import { NfcAdapter } from "react-native-nfc-manager";
532
-
533
- // Call once at app startup
534
- 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";
535
573
 
536
- // Then use the hook
537
- useNfc((tagId) => {
538
- console.log("Scanned:", tagId);
539
- }, { cooldownMs: 800 });
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
+ );
540
599
  ```
541
600
 
542
601
  ---
543
602
 
544
- ## useNfcState()
603
+ ## useNfcTechLoop(tech, withTechnology, afterTechnology?, options?)
604
+
605
+ Continuous technology session loop. Automatically restarts after each tag interaction.
545
606
 
546
- Access current NFC state:
607
+ ```ts
608
+ const { start, stop, isRunning } = useNfcTechLoop(
609
+ tech: NfcTech[],
610
+ withTechnology: () => Promise<void>,
611
+ afterTechnology?: () => Promise<void>,
612
+ options?: RegisterTagEventOpts
613
+ );
614
+ ```
615
+
616
+ **Example:**
617
+ ```ts
618
+ import { useNfcTechLoop, nfcTag } from "@spencerls/react-native-nfc";
619
+ import { NfcTech } from "react-native-nfc-manager";
547
620
 
548
- ```tsx
549
- const { mode, tag } = useNfcState();
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
+ );
550
651
  ```
551
652
 
552
653
  ---
553
654
 
554
- ## useNfcReader()
655
+ ## useNfcTagEvent(onTag)
555
656
 
556
- Manual control over reader mode.
657
+ One-shot tag event handler. Executes once when a tag is detected.
557
658
 
558
- **Note:** Call `nfcService.enableReaderMode_ANDROID(flags)` before calling `start()` on Android.
659
+ ```ts
660
+ const { startTech } = useNfcTagEvent(
661
+ onTag: (tag: TagEvent) => Promise<void>
662
+ );
663
+ ```
559
664
 
665
+ **Example:**
560
666
  ```ts
561
- const { start, stop } = useNfcReader();
667
+ import { useNfcTagEvent } from "@spencerls/react-native-nfc";
562
668
 
563
- // start signature:
564
- start(onTag?: (tag: TagEvent) => void, options?: { cooldownMs?: number })
669
+ function QuickScan() {
670
+ const { startTech } = useNfcTagEvent(async (tag) => {
671
+ console.log("Tag ID:", tag.id);
672
+ });
673
+
674
+ return <Button title="Quick Scan" onPress={startTech} />;
675
+ }
565
676
  ```
566
677
 
567
678
  ---
568
679
 
569
- ## useNfcTechnology()
680
+ ## useNfcTagEventLoop(onTag, options?)
570
681
 
571
- Runs an NFC technology session (NfcV, Ndef, etc).
682
+ Continuous tag event loop. Automatically restarts after each tag detection.
572
683
 
573
684
  ```ts
574
- await runWithTech([NfcTech.NfcV], async () => {
575
- const info = await nfc.v.getSystemInfoNfcV();
576
- });
685
+ const { start, stop, isRunning } = useNfcTagEventLoop(
686
+ onTag: (tag: TagEvent) => Promise<void>,
687
+ options?: RegisterTagEventOpts
688
+ );
577
689
  ```
578
690
 
579
- ---
580
-
581
- # Types
582
-
583
- All types are exported from `@spencerls/react-native-nfc/nfc`.
691
+ **Example:**
692
+ ```ts
693
+ import { useNfcTagEventLoop } from "@spencerls/react-native-nfc";
584
694
 
585
- Notable types:
695
+ function MonitorScreen() {
696
+ const { start, stop, isRunning } = useNfcTagEventLoop(
697
+ async (tag) => {
698
+ console.log("Tag detected:", tag.id);
699
+ }
700
+ );
586
701
 
587
- ```ts
588
- NfcState
589
- NfcMode
590
- TagEvent
702
+ return (
703
+ <View>
704
+ <Button title={isRunning ? "Stop Monitoring" : "Start Monitoring"}
705
+ onPress={isRunning ? stop : start} />
706
+ </View>
707
+ );
708
+ }
591
709
  ```
592
710
 
593
711
  ---
@@ -608,11 +726,11 @@ await nfc.ndef.write(records);
608
726
 
609
727
  ### Low-Level (Advanced)
610
728
 
611
- Use tag modules (`nfcTag`, `nfcVTag`, `nfcNdefTag`) inside `withTechnology` for complex operations.
729
+ Use `nfc.service.startTech()` with tag modules for complex custom operations.
612
730
 
613
731
  ```ts
614
732
  // ✅ Advanced: custom multi-block read
615
- await nfc.service.withTechnology(nfcVTag.tech, async () => {
733
+ await nfc.service.startTech(nfcVTag.tech, async () => {
616
734
  const tag = await nfcTag.getTag();
617
735
  if (!tag?.id) throw new Error("No NFC-V tag detected");
618
736
 
@@ -626,21 +744,43 @@ await nfc.service.withTechnology(nfcVTag.tech, async () => {
626
744
  offset += block.length;
627
745
  }
628
746
 
629
- return buffer;
747
+ console.log("Data:", buffer);
630
748
  });
631
749
  ```
632
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
+
633
773
  ---
634
774
 
635
775
  # Internal Notes
636
776
 
637
- - **Android reader mode flags** must be configured via `enableReaderMode_ANDROID()` before starting reader mode or using technology sessions.
638
- - iOS automatically restarts reader mode after each scan.
639
- - Android waits for cooldown before accepting next scan.
640
- - Technology sessions interrupt reader mode safely and auto-restart when done.
641
- - `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.
642
781
  - High-level `nfc.*` operations automatically manage technology sessions.
643
- - Low-level tag modules require manual `withTechnology` wrapping.
782
+ - For custom operations, use `nfc.service.startTech()` with low-level tag modules.
783
+ - Loop operations automatically restart after each tag interaction.
644
784
 
645
785
  ---
646
786