@spencerls/react-native-nfc 1.0.4 → 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,63 +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
+ ---
87
+
88
+ ## Technology Sessions (NfcTech.*)
89
+
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();
37
100
 
38
- return () => nfc.service.stopReader();
39
- }, []);
40
-
41
- const start = async () => {
42
- await nfc.v
43
- .getSystemInfoNfcV()
44
- .then((i) => {
45
- console.log("info:", i);
46
- })
47
- .catch((e) => {
48
- console.error("error:", e);
49
- });
101
+ const readInfo = async () => {
102
+ await runWithTech([NfcTech.NfcV], async () => {
103
+ const info = await nfc.v.getSystemInfoNfcV();
104
+ console.log(info);
105
+ });
50
106
  };
51
107
 
108
+ return <Button title="Read NFC-V Info" onPress={readInfo} />;
109
+ }
110
+ ```
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() {
52
162
  return (
53
- <View style={{ padding: 100 }}>
54
- <Text style={{ color: "#999" }}>NFC Mode: {nfcState.mode}</Text>
55
- <Button title="Scan Tag" onPress={start} />
56
- </View>
163
+ <NfcProvider>
164
+ <RootApp />
165
+ </NfcProvider>
57
166
  );
58
167
  }
59
168
  ```
60
169
 
170
+ ---
171
+
61
172
  ## License
62
173
 
63
174
  MIT © Spencer Smith
package/dist/index.d.mts CHANGED
@@ -1,82 +1,6 @@
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 StrictTagEvent = TagEvent & {
5
- id: string;
6
- };
7
-
8
- type NfcMode = "idle" | "starting" | "active" | "stopping" | "technology";
9
-
10
- interface NfcState {
11
- mode: NfcMode;
12
- tag: TagEvent | null;
13
- }
14
-
15
- type NfcListener = (state: NfcState) => void;
16
- declare class NfcService {
17
- private state;
18
- private listeners;
19
- constructor();
20
- private setState;
21
- getState(): NfcState;
22
- subscribe(fn: NfcListener): () => void;
23
- startReader(readerModeFlags: number, onTag?: (tag: TagEvent) => void): Promise<void>;
24
- stopReader(): Promise<void>;
25
- withTechnology<T>(tech: NfcTech | NfcTech[], handler: () => Promise<T>): Promise<T>;
26
- }
27
- declare const nfcService: NfcService;
28
-
29
- /**
30
- * NFC root namespace providing access to:
31
- * - NfcService
32
- * - ISO15693 NFC-V ops
33
- * - NFC-A ops
34
- * - NDEF operations
35
- */
36
- declare const nfc: {
37
- readonly service: NfcService;
38
- /** ISO15693 protocol helpers and high-level operations */
39
- readonly v: {
40
- readonly utils: {
41
- readonly tech: react_native_nfc_manager.NfcTech.NfcV | react_native_nfc_manager.NfcTech[];
42
- readonly Flags: {
43
- readonly HIGH_DATA_RATE: 2;
44
- readonly ADDRESSED: 32;
45
- };
46
- readonly Commands: {
47
- readonly READ_SINGLE_BLOCK: 32;
48
- readonly WRITE_SINGLE_BLOCK: 33;
49
- readonly GET_SYSTEM_INFO: 43;
50
- };
51
- readonly flags: (...bits: number[]) => number;
52
- readonly reverseUid: (tagIdHex: string) => number[];
53
- readonly buildReadBlock: (uidReversed: number[], blockNumber: number) => number[];
54
- readonly buildWriteBlock: (uidReversed: number[], blockNumber: number, data: Uint8Array) => number[];
55
- readonly buildGetSystemInfo: (uidReversed: number[]) => number[];
56
- readonly parseReadResponse: (resp: number[]) => Uint8Array;
57
- readonly parseWriteResponse: (resp: number[]) => void;
58
- readonly parseSystemInfo: (resp: number[]) => any;
59
- readonly detectManufacturer: (uid: string) => string;
60
- };
61
- readonly withVTag: <T>(handler: (tag: StrictTagEvent) => Promise<T>) => Promise<T>;
62
- readonly writeBlockNfcV: (blockNumber: number, data: Uint8Array) => Promise<void>;
63
- readonly readBlockNfcV: (blockNumber: number) => Promise<Uint8Array<ArrayBufferLike>>;
64
- readonly getSystemInfoNfcV: () => Promise<any>;
65
- };
66
- /** NFC-A / Type 2 helpers and operations */
67
- readonly a: {
68
- readonly utils: {};
69
- readonly transceive: (data: number[]) => Promise<number[]>;
70
- };
71
- /** NDEF read/write utilities and operations */
72
- readonly ndef: {
73
- readonly utils: {};
74
- readonly writeNdef: (records: react_native_nfc_manager.NdefRecord[]) => Promise<void>;
75
- readonly writeTextNdef: (text: string) => Promise<void>;
76
- readonly writeUriNdef: (uri: string) => Promise<void>;
77
- };
78
- };
79
-
80
4
  declare const operations$2: {
81
5
  transceive(data: number[]): Promise<number[]>;
82
6
  };
@@ -87,26 +11,18 @@ declare namespace index$2 {
87
11
  export { operations$2 as operations, utils$2 as utils };
88
12
  }
89
13
 
90
- declare const operations$1: {
91
- writeNdef(records: NdefRecord[]): Promise<void>;
92
- writeTextNdef(text: string): Promise<void>;
93
- writeUriNdef(uri: string): Promise<void>;
14
+ type StrictTagEvent = TagEvent & {
15
+ id: string;
94
16
  };
95
17
 
96
- declare const utils$1: {};
97
-
98
- declare namespace index$1 {
99
- export { operations$1 as operations, utils$1 as utils };
100
- }
101
-
102
- declare const operations: {
18
+ declare const operations$1: {
103
19
  withVTag<T>(handler: (tag: StrictTagEvent) => Promise<T>): Promise<T>;
104
20
  writeBlockNfcV(blockNumber: number, data: Uint8Array): Promise<void>;
105
21
  readBlockNfcV(blockNumber: number): Promise<Uint8Array<ArrayBufferLike>>;
106
22
  getSystemInfoNfcV(): Promise<any>;
107
23
  };
108
24
 
109
- declare const utils: {
25
+ declare const utils$1: {
110
26
  readonly tech: NfcTech.NfcV | NfcTech[];
111
27
  readonly Flags: {
112
28
  readonly HIGH_DATA_RATE: 2;
@@ -162,10 +78,102 @@ declare const utils: {
162
78
  readonly detectManufacturer: (uid: string) => string;
163
79
  };
164
80
 
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
+
165
93
  declare const index_operations: typeof operations;
166
94
  declare const index_utils: typeof utils;
167
95
  declare namespace index {
168
96
  export { index_operations as operations, index_utils as utils };
169
97
  }
170
98
 
171
- export { type NfcMode, type NfcState, index$1 as ndef, nfc, index$2 as nfcA, nfcService, index as nfcV };
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>;
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
+ };
178
+
179
+ export { type NfcMode, type NfcState, nfc, index$2 as nfcA, index as nfcNdef, nfcService, index$1 as nfcV };