@spencerls/react-native-nfc 1.0.3 → 1.0.5

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 ADDED
@@ -0,0 +1,214 @@
1
+ # @spencerls/react-native-nfc – API Reference
2
+
3
+ This document describes the complete public API surface of the NFC package.
4
+
5
+ All exports come from:
6
+
7
+ ```ts
8
+ import { nfc, nfcService } from "@spencerls/react-native-nfc";
9
+ import {
10
+ useNfc,
11
+ useNfcState,
12
+ useNfcReader,
13
+ useNfcTechnology,
14
+ NfcProvider
15
+ } from "@spencerls/react-native-nfc";
16
+ ```
17
+
18
+ ---
19
+
20
+ # Core Service: `nfcService`
21
+
22
+ ```ts
23
+ import { nfcService } from "@spencerls/react-native-nfc";
24
+ ```
25
+
26
+ ### Methods
27
+
28
+ #### startReader(flags, onTag, options?)
29
+ Starts platform reader mode.
30
+
31
+ ```ts
32
+ nfcService.startReader(
33
+ flags: number,
34
+ onTag: (tag: TagEvent) => void,
35
+ options?: { cooldownMs?: number }
36
+ )
37
+ ```
38
+
39
+ #### stopReader()
40
+ Stops reader mode.
41
+
42
+ ```ts
43
+ await nfcService.stopReader();
44
+ ```
45
+
46
+ #### withTechnology(tech, handler)
47
+ Opens an iOS/Android technology session. Stops reader mode before starting.
48
+
49
+ ```ts
50
+ await nfcService.withTechnology(
51
+ tech: NfcTech | NfcTech[],
52
+ async () => { /* commands */ }
53
+ );
54
+ ```
55
+
56
+ #### getState()
57
+ Returns the current state snapshot.
58
+
59
+ ```ts
60
+ { mode, tag }
61
+ ```
62
+
63
+ #### subscribe(listener)
64
+ Attach a state listener; returns an unsubscribe function.
65
+
66
+ ---
67
+
68
+ # Namespace API: `nfc`
69
+
70
+ ```ts
71
+ import { nfc } from "@spencerls/react-native-nfc";
72
+ ```
73
+
74
+ `nfc` is a namespaced, easy-to-use interface:
75
+
76
+ ```
77
+ nfc.service (NfcService instance)
78
+ nfc.v.* NFC-V operations
79
+ nfc.a.* NFC-A operations
80
+ nfc.ndef.* NDEF operations
81
+ ```
82
+
83
+ ---
84
+
85
+ # NFC-V Namespace: `nfc.v`
86
+
87
+ High-level ISO15693 helpers and raw operations.
88
+
89
+ ```ts
90
+ nfc.v.getSystemInfoNfcV()
91
+ nfc.v.readSingleBlock(uid, blockNumber)
92
+ nfc.v.readMultipleBlocks(uid, start, count)
93
+ nfc.v.writeSingleBlock(uid, blockNumber, data)
94
+ nfc.v.getSecurityStatus(uid, blockNumber)
95
+ ```
96
+
97
+ ---
98
+
99
+ # NFC-A Namespace: `nfc.a`
100
+
101
+ ```ts
102
+ nfc.a.transceive(bytes)
103
+ nfc.a.getAtqa()
104
+ nfc.a.getSak()
105
+ ```
106
+
107
+ ---
108
+
109
+ # NDEF Namespace: `nfc.ndef`
110
+
111
+ ```ts
112
+ nfc.ndef.parse(bytes)
113
+ nfc.ndef.encode(records)
114
+ nfc.ndef.utils.*
115
+ ```
116
+
117
+ ---
118
+
119
+ # React Hooks
120
+
121
+ All hooks live under:
122
+
123
+ ```ts
124
+ import { ... } from "@spencerls/react-native-nfc";
125
+ ```
126
+
127
+ ---
128
+
129
+ ## useNfc(onTag, options)
130
+
131
+ Automatically starts reader mode on mount and stops on unmount.
132
+
133
+ ```ts
134
+ useNfc(
135
+ (tagId: string) => { ... },
136
+ {
137
+ flags?: number,
138
+ cooldownMs?: number
139
+ }
140
+ );
141
+ ```
142
+
143
+ ---
144
+
145
+ ## useNfcState()
146
+
147
+ Access current NFC state:
148
+
149
+ ```tsx
150
+ const { mode, tag } = useNfcState();
151
+ ```
152
+
153
+ ---
154
+
155
+ ## useNfcReader()
156
+
157
+ Manual control over reader mode.
158
+
159
+ ```ts
160
+ const { start, stop } = useNfcReader();
161
+ ```
162
+
163
+ ---
164
+
165
+ ## useNfcTechnology()
166
+
167
+ Runs an NFC technology session (NfcV, Ndef, etc).
168
+
169
+ ```ts
170
+ await runWithTech([NfcTech.NfcV], async () => {
171
+ const info = await nfc.v.getSystemInfoNfcV();
172
+ });
173
+ ```
174
+
175
+ ---
176
+
177
+ # NfcProvider
178
+
179
+ Optional provider that exposes service state to React tree.
180
+
181
+ ```tsx
182
+ <NfcProvider>
183
+ <App />
184
+ </NfcProvider>
185
+ ```
186
+
187
+ ---
188
+
189
+ # Types
190
+
191
+ All types are exported from `@spencerls/react-native-nfc/nfc`.
192
+
193
+ Notable types:
194
+
195
+ ```ts
196
+ NfcState
197
+ NfcMode
198
+ TagEvent
199
+ ```
200
+
201
+ ---
202
+
203
+ # Internal Notes
204
+
205
+ - iOS automatically restarts reader mode after each scan.
206
+ - Android waits for cooldown before accepting next scan.
207
+ - Technology sessions interrupt reader mode safely.
208
+ - `NfcTech` enums must be used. Do not pass raw strings.
209
+
210
+ ---
211
+
212
+ # License
213
+
214
+ MIT © Spencer Smith
package/README.md CHANGED
@@ -1,47 +1,174 @@
1
- # @spencer/nfc
1
+ # @spencerls/react-native-nfc
2
2
 
3
- An easy, clean, React-friendly NFC service built on `react-native-nfc-manager`.
3
+ A clean, React-friendly, cross-platform NFC layer built on top of
4
+ `react-native-nfc-manager`.
5
+
6
+ This package provides:
7
+
8
+ - A unified NFC service (`nfcService`)
9
+ - High-level protocol namespaces (`nfc.v`, `nfc.a`, `nfc.ndef`)
10
+ - Automatic iOS reader restarts
11
+ - Safe Android reader handling
12
+ - Optional React hooks and provider
13
+ - Technology sessions for NDEF/NfcV/NfcA and raw commands
14
+
15
+ The API is designed to be stable, predictable, and easy to use across iOS and Android.
16
+
17
+ ---
4
18
 
5
19
  ## Installation
6
20
 
7
21
  ```bash
8
- npm install @spencer/nfc
9
- # or
10
- yarn add @spencer/nfc
22
+ npm install @spencerls/react-native-nfc
23
+ npm install react-native-nfc-manager
11
24
  ```
12
25
 
13
- ## Requires:
26
+ Works with:
27
+
28
+ - React Native 0.74+
29
+ - Expo (Bare / Prebuild)
30
+ - iOS 13+ (Core NFC)
31
+ - Android API 21+
32
+
33
+ ---
34
+
35
+ ## Basic Usage (Reader Mode)
14
36
 
15
- `react-native-nfc-manager`
37
+ ```tsx
38
+ import { useNfc, useNfcState } from "@spencerls/react-native-nfc";
39
+ import { NfcAdapter } from "react-native-nfc-manager";
40
+
41
+ export default function ScannerScreen() {
42
+ const { mode } = useNfcState();
43
+
44
+ useNfc((tagId) => {
45
+ console.log("Scanned:", tagId);
46
+ }, {
47
+ flags:
48
+ NfcAdapter.FLAG_READER_NFC_V |
49
+ NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS,
50
+ cooldownMs: 800
51
+ });
52
+
53
+ return (
54
+ <View>
55
+ <Text>NFC Mode: {mode}</Text>
56
+ </View>
57
+ );
58
+ }
59
+ ```
16
60
 
17
- React Native 0.74+ or Expo (Bare / Prebuild)
61
+ ---
18
62
 
19
- ## Usage
63
+ ## Manual Reader Control
20
64
 
21
65
  ```tsx
22
- import { nfc } from "@spencer/nfc";
23
- import { Ndef, NfcAdapter } from "react-native-nfc-manager";
24
-
25
- export default function Example() {
26
- const { nfcState } = useNfc();
27
-
28
- useEffect(() => {
29
- nfc.service.startReader(
30
- NfcAdapter.FLAG_READER_NFC_V | NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS,
31
- async (tag) => {
32
- console.log("Tag:", tag);
33
- await nfc.service.stopReader();
34
- await nfc.service.writeNdef([Ndef.textRecord("Hello NFC!")]);
35
- }
66
+ import { useNfcReader } from "@spencerls/react-native-nfc";
67
+ import { NfcAdapter } from "react-native-nfc-manager";
68
+
69
+ export default function Screen() {
70
+ const { start, stop } = useNfcReader();
71
+
72
+ const begin = () => {
73
+ start(
74
+ NfcAdapter.FLAG_READER_NFC_V,
75
+ (tag) => {
76
+ console.log("Tag:", tag.id);
77
+ },
78
+ 1200
36
79
  );
80
+ };
81
+
82
+ return <Button title="Start" onPress={begin} />;
83
+ }
84
+ ```
85
+
86
+ ---
37
87
 
38
- return () => nfc.service.stopReader();
39
- }, []);
88
+ ## Technology Sessions (NfcTech.*)
40
89
 
41
- return <Text>NFC state: {nfcState}</Text>;
90
+ Always use `NfcTech` enums.
91
+ Do not pass raw strings.
92
+
93
+ ```tsx
94
+ import { nfc } from "@spencerls/react-native-nfc";
95
+ import { useNfcTechnology } from "@spencerls/react-native-nfc";
96
+ import { NfcTech } from "react-native-nfc-manager";
97
+
98
+ export function ReadSystemInfo() {
99
+ const { runWithTech } = useNfcTechnology();
100
+
101
+ const readInfo = async () => {
102
+ await runWithTech([NfcTech.NfcV], async () => {
103
+ const info = await nfc.v.getSystemInfoNfcV();
104
+ console.log(info);
105
+ });
106
+ };
107
+
108
+ return <Button title="Read NFC-V Info" onPress={readInfo} />;
42
109
  }
43
110
  ```
44
111
 
112
+ ---
113
+
114
+ ## NDEF Read/Write
115
+
116
+ ```tsx
117
+ import { useNfcTechnology } from "@spencerls/react-native-nfc";
118
+ import { NfcTech, Ndef, NfcManager } from "react-native-nfc-manager";
119
+
120
+ export default function WriteScreen() {
121
+ const { runWithTech } = useNfcTechnology();
122
+
123
+ const writeHello = async () => {
124
+ await runWithTech([NfcTech.Ndef], async () => {
125
+ const bytes = Ndef.encodeMessage([
126
+ Ndef.textRecord("Hello NFC!")
127
+ ]);
128
+ await NfcManager.ndefHandler.writeNdefMessage(bytes);
129
+ });
130
+ };
131
+
132
+ return <Button title="Write NDEF" onPress={writeHello} />;
133
+ }
134
+ ```
135
+
136
+ ---
137
+
138
+ ## Namespace API
139
+
140
+ ```ts
141
+ import { nfc } from "@spencerls/react-native-nfc";
142
+
143
+ await nfc.v.getSystemInfoNfcV();
144
+ await nfc.v.readSingleBlock(uid, 0);
145
+
146
+ await nfc.a.transceive(rawBytes);
147
+
148
+ await nfc.ndef.parse(ndefBytes);
149
+
150
+ nfc.service.startReader(...);
151
+ nfc.service.withTechnology(...);
152
+ ```
153
+
154
+ ---
155
+
156
+ ## Global Provider (Optional)
157
+
158
+ ```tsx
159
+ import { NfcProvider } from "@spencerls/react-native-nfc";
160
+
161
+ export default function App() {
162
+ return (
163
+ <NfcProvider>
164
+ <RootApp />
165
+ </NfcProvider>
166
+ );
167
+ }
168
+ ```
169
+
170
+ ---
171
+
45
172
  ## License
46
173
 
47
174
  MIT © Spencer Smith
package/dist/index.d.mts CHANGED
@@ -1,114 +1,28 @@
1
1
  import * as react_native_nfc_manager from 'react-native-nfc-manager';
2
2
  import { TagEvent, NfcTech, NdefRecord } from 'react-native-nfc-manager';
3
3
 
4
- type NfcMode = "idle" | "starting" | "active" | "stopping" | "technology";
5
-
6
- interface NfcState {
7
- mode: NfcMode;
8
- tag: TagEvent | null;
9
- }
10
-
11
- type NfcListener = (state: NfcState) => void;
12
- declare class NfcService {
13
- private state;
14
- private listeners;
15
- constructor();
16
- private setState;
17
- getState(): NfcState;
18
- subscribe(fn: NfcListener): () => void;
19
- startReader(readerModeFlags: number, onTag?: (tag: TagEvent) => void): Promise<void>;
20
- stopReader(): Promise<void>;
21
- withTechnology<T>(tech: NfcTech | NfcTech[], handler: () => Promise<T>): Promise<T>;
22
- }
23
- declare const nfcService: NfcService;
24
-
25
- /**
26
- * NFC root namespace providing access to:
27
- * - NfcService
28
- * - ISO15693 NFC-V ops
29
- * - NFC-A ops
30
- * - NDEF operations
31
- */
32
- declare const nfc: {
33
- readonly service: NfcService;
34
- /** ISO15693 protocol helpers and high-level operations */
35
- readonly v: {
36
- readonly utils: {
37
- readonly tech: react_native_nfc_manager.NfcTech.NfcV | react_native_nfc_manager.NfcTech[];
38
- readonly Flags: {
39
- readonly HIGH_DATA_RATE: 2;
40
- readonly ADDRESSED: 32;
41
- };
42
- readonly Commands: {
43
- readonly READ_SINGLE_BLOCK: 32;
44
- readonly WRITE_SINGLE_BLOCK: 33;
45
- readonly GET_SYSTEM_INFO: 43;
46
- };
47
- readonly flags: (...bits: number[]) => number;
48
- readonly reverseUid: (tagIdHex: string) => number[];
49
- readonly buildReadBlock: (uidReversed: number[], blockNumber: number) => number[];
50
- readonly buildWriteBlock: (uidReversed: number[], blockNumber: number, data: Uint8Array) => number[];
51
- readonly buildGetSystemInfo: (uidReversed: number[]) => number[];
52
- readonly parseReadResponse: (resp: number[]) => Uint8Array;
53
- readonly parseWriteResponse: (resp: number[]) => void;
54
- readonly parseSystemInfo: (resp: number[]) => any;
55
- readonly detectManufacturer: (uid: string) => string;
56
- };
57
- readonly withVTag: <T>(handler: (tag: react_native_nfc_manager.TagEvent & {
58
- id: string;
59
- }) => Promise<T>) => Promise<T>;
60
- readonly writeBlockNfcV: (blockNumber: number, data: Uint8Array) => Promise<void>;
61
- readonly readBlockNfcV: (blockNumber: number) => Promise<Uint8Array<ArrayBufferLike>>;
62
- readonly getSystemInfoNfcV: () => Promise<any>;
63
- };
64
- /** NFC-A / Type 2 helpers and operations */
65
- readonly a: {
66
- readonly utils: {};
67
- readonly transceive: (data: number[]) => Promise<number[]>;
68
- };
69
- /** NDEF read/write utilities and operations */
70
- readonly ndef: {
71
- readonly utils: {};
72
- readonly writeNdef: (records: react_native_nfc_manager.NdefRecord[]) => Promise<void>;
73
- readonly writeTextNdef: (text: string) => Promise<void>;
74
- readonly writeUriNdef: (uri: string) => Promise<void>;
75
- };
76
- };
77
-
78
4
  declare const operations$2: {
79
5
  transceive(data: number[]): Promise<number[]>;
80
6
  };
81
7
 
82
- declare const NfcAUtils: {};
8
+ declare const utils$2: {};
83
9
 
84
- declare const index$2_NfcAUtils: typeof NfcAUtils;
85
10
  declare namespace index$2 {
86
- export { index$2_NfcAUtils as NfcAUtils, operations$2 as operations };
11
+ export { operations$2 as operations, utils$2 as utils };
87
12
  }
88
13
 
89
- declare const operations$1: {
90
- writeNdef(records: NdefRecord[]): Promise<void>;
91
- writeTextNdef(text: string): Promise<void>;
92
- writeUriNdef(uri: string): Promise<void>;
14
+ type StrictTagEvent = TagEvent & {
15
+ id: string;
93
16
  };
94
17
 
95
- declare const NdefUtils: {};
96
-
97
- declare const index$1_NdefUtils: typeof NdefUtils;
98
- declare namespace index$1 {
99
- export { index$1_NdefUtils as NdefUtils, operations$1 as operations };
100
- }
101
-
102
- declare const operations: {
103
- withVTag<T>(handler: (tag: TagEvent & {
104
- id: string;
105
- }) => Promise<T>): Promise<T>;
18
+ declare const operations$1: {
19
+ withVTag<T>(handler: (tag: StrictTagEvent) => Promise<T>): Promise<T>;
106
20
  writeBlockNfcV(blockNumber: number, data: Uint8Array): Promise<void>;
107
21
  readBlockNfcV(blockNumber: number): Promise<Uint8Array<ArrayBufferLike>>;
108
22
  getSystemInfoNfcV(): Promise<any>;
109
23
  };
110
24
 
111
- declare const NfcVUtils: {
25
+ declare const utils$1: {
112
26
  readonly tech: NfcTech.NfcV | NfcTech[];
113
27
  readonly Flags: {
114
28
  readonly HIGH_DATA_RATE: 2;
@@ -164,10 +78,102 @@ declare const NfcVUtils: {
164
78
  readonly detectManufacturer: (uid: string) => string;
165
79
  };
166
80
 
167
- declare const index_NfcVUtils: typeof NfcVUtils;
81
+ declare namespace index$1 {
82
+ export { operations$1 as operations, utils$1 as utils };
83
+ }
84
+
85
+ declare const operations: {
86
+ writeNdef(records: NdefRecord[]): Promise<void>;
87
+ writeTextNdef(text: string): Promise<void>;
88
+ writeUriNdef(uri: string): Promise<void>;
89
+ };
90
+
91
+ declare const utils: {};
92
+
168
93
  declare const index_operations: typeof operations;
94
+ declare const index_utils: typeof utils;
169
95
  declare namespace index {
170
- export { index_NfcVUtils as NfcVUtils, index_operations as operations };
96
+ export { index_operations as operations, index_utils as utils };
97
+ }
98
+
99
+ type NfcMode = "idle" | "starting" | "active" | "stopping" | "technology";
100
+
101
+ interface NfcState {
102
+ mode: NfcMode;
103
+ tag: TagEvent | null;
104
+ }
105
+
106
+ type NfcListener = (state: NfcState) => void;
107
+ declare class NfcService {
108
+ private state;
109
+ private listeners;
110
+ private isProcessingTag;
111
+ private currentOnTag?;
112
+ private currentCooldownMs;
113
+ private lastUsedReaderFlags;
114
+ constructor();
115
+ private setState;
116
+ getState(): NfcState;
117
+ subscribe(fn: NfcListener): () => void;
118
+ startReader(readerModeFlags: number, onTag?: (tag: TagEvent) => Promise<void> | void, options?: {
119
+ cooldownMs?: number;
120
+ }): Promise<void>;
121
+ stopReader(): Promise<void>;
122
+ private _resetReaderState;
123
+ private _restartIosReader;
124
+ withTechnology<T>(tech: NfcTech | NfcTech[], handler: () => Promise<T>): Promise<T>;
171
125
  }
126
+ declare const nfcService: NfcService;
127
+
128
+ /**
129
+ * NFC root namespace providing access to:
130
+ * - NfcService
131
+ * - ISO15693 NFC-V ops
132
+ * - NFC-A ops
133
+ * - NDEF operations
134
+ */
135
+ declare const nfc: {
136
+ readonly service: NfcService;
137
+ /** ISO15693 protocol helpers and high-level operations */
138
+ readonly v: {
139
+ readonly utils: {
140
+ readonly tech: react_native_nfc_manager.NfcTech.NfcV | react_native_nfc_manager.NfcTech[];
141
+ readonly Flags: {
142
+ readonly HIGH_DATA_RATE: 2;
143
+ readonly ADDRESSED: 32;
144
+ };
145
+ readonly Commands: {
146
+ readonly READ_SINGLE_BLOCK: 32;
147
+ readonly WRITE_SINGLE_BLOCK: 33;
148
+ readonly GET_SYSTEM_INFO: 43;
149
+ };
150
+ readonly flags: (...bits: number[]) => number;
151
+ readonly reverseUid: (tagIdHex: string) => number[];
152
+ readonly buildReadBlock: (uidReversed: number[], blockNumber: number) => number[];
153
+ readonly buildWriteBlock: (uidReversed: number[], blockNumber: number, data: Uint8Array) => number[];
154
+ readonly buildGetSystemInfo: (uidReversed: number[]) => number[];
155
+ readonly parseReadResponse: (resp: number[]) => Uint8Array;
156
+ readonly parseWriteResponse: (resp: number[]) => void;
157
+ readonly parseSystemInfo: (resp: number[]) => any;
158
+ readonly detectManufacturer: (uid: string) => string;
159
+ };
160
+ readonly withVTag: <T>(handler: (tag: StrictTagEvent) => Promise<T>) => Promise<T>;
161
+ readonly writeBlockNfcV: (blockNumber: number, data: Uint8Array) => Promise<void>;
162
+ readonly readBlockNfcV: (blockNumber: number) => Promise<Uint8Array<ArrayBufferLike>>;
163
+ readonly getSystemInfoNfcV: () => Promise<any>;
164
+ };
165
+ /** NFC-A / Type 2 helpers and operations */
166
+ readonly a: {
167
+ readonly utils: {};
168
+ readonly transceive: (data: number[]) => Promise<number[]>;
169
+ };
170
+ /** NDEF read/write utilities and operations */
171
+ readonly ndef: {
172
+ readonly utils: {};
173
+ readonly writeNdef: (records: react_native_nfc_manager.NdefRecord[]) => Promise<void>;
174
+ readonly writeTextNdef: (text: string) => Promise<void>;
175
+ readonly writeUriNdef: (uri: string) => Promise<void>;
176
+ };
177
+ };
172
178
 
173
- export { type NfcMode, type NfcState, index$1 as ndef, nfc, index$2 as nfcA, nfcService, index as nfcV };
179
+ export { type NfcMode, type NfcState, nfc, index$2 as nfcA, index as nfcNdef, nfcService, index$1 as nfcV };