@spencerls/react-native-nfc 1.0.7 → 1.0.9

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
@@ -5,7 +5,14 @@ This document describes the complete public API surface of the NFC package.
5
5
  All exports come from:
6
6
 
7
7
  ```ts
8
- import { nfc, nfcService } from "@spencerls/react-native-nfc";
8
+ import {
9
+ nfc,
10
+ nfcService,
11
+ nfcTag,
12
+ nfcVTag,
13
+ nfcNdefTag
14
+ } from "@spencerls/react-native-nfc";
15
+
9
16
  import {
10
17
  useNfc,
11
18
  useNfcState,
@@ -25,13 +32,29 @@ import { nfcService } from "@spencerls/react-native-nfc";
25
32
 
26
33
  ### Methods
27
34
 
28
- #### startReader(flags, onTag, options?)
35
+ #### enableReaderMode_ANDROID(flags)
36
+ Configures Android reader mode flags. Must be called before `startReader()` or `withTechnology()`.
37
+ No-op on iOS.
38
+
39
+ ```ts
40
+ nfcService.enableReaderMode_ANDROID(flags: number);
41
+ ```
42
+
43
+ **Example:**
44
+ ```ts
45
+ import { NfcAdapter } from "react-native-nfc-manager";
46
+
47
+ nfcService.enableReaderMode_ANDROID(
48
+ NfcAdapter.FLAG_READER_NFC_V | NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS
49
+ );
50
+ ```
51
+
52
+ #### startReader(onTag, options?)
29
53
  Starts platform reader mode.
30
54
 
31
55
  ```ts
32
56
  nfcService.startReader(
33
- flags: number,
34
- onTag: (tag: TagEvent) => void,
57
+ onTag?: (tag: TagEvent) => void,
35
58
  options?: { cooldownMs?: number }
36
59
  )
37
60
  ```
@@ -71,47 +94,409 @@ Attach a state listener; returns an unsubscribe function.
71
94
  import { nfc } from "@spencerls/react-native-nfc";
72
95
  ```
73
96
 
74
- `nfc` is a namespaced, easy-to-use interface:
97
+ `nfc` is a namespaced, high-level interface that automatically manages technology sessions:
75
98
 
76
99
  ```
77
100
  nfc.service (NfcService instance)
78
- nfc.v.* NFC-V operations
79
- nfc.a.* NFC-A operations
101
+ nfc.tag.* Basic tag operations
102
+ nfc.v.* NFC-V (ISO15693) operations
80
103
  nfc.ndef.* NDEF operations
81
104
  ```
82
105
 
83
106
  ---
84
107
 
85
- # NFC-V Namespace: `nfc.v`
108
+ # Tag Namespace: `nfc.tag`
109
+
110
+ Get basic tag information.
111
+
112
+ ### getTag(tech)
86
113
 
87
- High-level ISO15693 helpers and raw operations.
114
+ Requests technology session and returns tag information.
88
115
 
89
116
  ```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)
117
+ await nfc.tag.getTag(tech: NfcTech | NfcTech[]): Promise<TagEvent>
118
+ ```
119
+
120
+ **Example:**
121
+ ```ts
122
+ import { NfcTech } from "react-native-nfc-manager";
123
+
124
+ const tag = await nfc.tag.getTag([NfcTech.NfcA, NfcTech.NfcV, NfcTech.Ndef]);
125
+ console.log("Tag ID:", tag.id);
126
+ console.log("Tag Type:", tag.type);
95
127
  ```
96
128
 
97
129
  ---
98
130
 
99
- # NFC-A Namespace: `nfc.a`
131
+ # NFC-V Namespace: `nfc.v`
132
+
133
+ High-level ISO15693 (NFC-V) operations. Automatically manages technology sessions.
134
+
135
+ ### readBlock(blockNumber)
136
+
137
+ ```ts
138
+ await nfc.v.readBlock(blockNumber: number): Promise<Uint8Array>
139
+ ```
140
+
141
+ ### readBlocks(startBlock, endBlock)
142
+
143
+ ```ts
144
+ await nfc.v.readBlocks(
145
+ startBlock: number,
146
+ endBlock: number
147
+ ): Promise<Uint8Array>
148
+ ```
149
+
150
+ ### writeBlock(blockNumber, data)
151
+
152
+ ```ts
153
+ await nfc.v.writeBlock(
154
+ blockNumber: number,
155
+ data: Uint8Array
156
+ ): Promise<void>
157
+ ```
158
+
159
+ ### writeBlocks(blockNumber, data)
160
+
161
+ ```ts
162
+ await nfc.v.writeBlocks(
163
+ blockNumber: number,
164
+ data: Uint8Array[]
165
+ ): Promise<void>
166
+ ```
167
+
168
+ ### getSystemInfo()
100
169
 
101
170
  ```ts
102
- nfc.a.transceive(bytes)
103
- nfc.a.getAtqa()
104
- nfc.a.getSak()
171
+ await nfc.v.getSystemInfo(): Promise<SystemInfo>
172
+ ```
173
+
174
+ **Example:**
175
+ ```ts
176
+ const info = await nfc.v.getSystemInfo();
177
+ console.log("Blocks:", info.numberOfBlocks);
178
+ console.log("Block size:", info.blockSize);
105
179
  ```
106
180
 
107
181
  ---
108
182
 
109
183
  # NDEF Namespace: `nfc.ndef`
110
184
 
185
+ High-level NDEF operations and builder.
186
+
187
+ ### write(records)
188
+
189
+ Writes NDEF records to a tag.
190
+
191
+ ```ts
192
+ await nfc.ndef.write(records: NdefRecord[]): Promise<void>
193
+ ```
194
+
195
+ ### writeText(text, lang?, encoding?, id?)
196
+
197
+ ```ts
198
+ await nfc.ndef.writeText(
199
+ text: string,
200
+ lang?: ISOLangCode,
201
+ encoding?: "utf8" | "utf16",
202
+ id?: string
203
+ ): Promise<void>
204
+ ```
205
+
206
+ ### writeUri(uri, id?)
207
+
208
+ ```ts
209
+ await nfc.ndef.writeUri(uri: string, id?: string): Promise<void>
210
+ ```
211
+
212
+ ### writeJson(data, id?)
213
+
214
+ ```ts
215
+ await nfc.ndef.writeJson(data: unknown, id?: string): Promise<void>
216
+ ```
217
+
218
+ ### writeMime(mimeType, payload, id?)
219
+
220
+ ```ts
221
+ await nfc.ndef.writeMime(
222
+ mimeType: string,
223
+ payload: string | Uint8Array | number[],
224
+ id?: string
225
+ ): Promise<void>
226
+ ```
227
+
228
+ ### writeExternal(domain, type, payload, id?)
229
+
230
+ ```ts
231
+ await nfc.ndef.writeExternal(
232
+ domain: string,
233
+ type: string,
234
+ payload: string | Uint8Array | number[],
235
+ id?: string
236
+ ): Promise<void>
237
+ ```
238
+
239
+ ### readMessage()
240
+
241
+ ```ts
242
+ await nfc.ndef.readMessage(): Promise<NdefMessageResult>
243
+ ```
244
+
245
+ ### readFull()
246
+
247
+ Reads NDEF message and tag information.
248
+
249
+ ```ts
250
+ await nfc.ndef.readFull(): Promise<{
251
+ message: NdefMessageResult;
252
+ tag: TagEvent;
253
+ }>
254
+ ```
255
+
256
+ ### getStatus()
257
+
258
+ ```ts
259
+ await nfc.ndef.getStatus(): Promise<{
260
+ status: NdefStatus;
261
+ capacity: number;
262
+ }>
263
+ ```
264
+
265
+ ### makeReadOnly()
266
+
267
+ ```ts
268
+ await nfc.ndef.makeReadOnly(): Promise<void>
269
+ ```
270
+
271
+ ---
272
+
273
+ # NDEF Builder: `nfc.ndef.Builder`
274
+
275
+ Create NDEF records using a builder pattern.
276
+
277
+ ### Builder.records(builder)
278
+
279
+ ```ts
280
+ nfc.ndef.Builder.records((B) => NdefRecord[]): NdefRecord[]
281
+ ```
282
+
283
+ **Example:**
284
+ ```ts
285
+ const records = nfc.ndef.Builder.records((B) => [
286
+ B.textRecord("Hello, world!"),
287
+ B.uriRecord("https://example.com"),
288
+ B.jsonRecord(JSON.stringify({ key: "value" })),
289
+ ]);
290
+
291
+ await nfc.ndef.write(records);
292
+ ```
293
+
294
+ ### Builder Methods
295
+
296
+ #### textRecord(text, lang?, encoding?, id?)
297
+
298
+ ```ts
299
+ Builder.textRecord(
300
+ text: string,
301
+ lang: ISOLangCode = "en",
302
+ encoding: "utf8" | "utf16" = "utf8",
303
+ id?: string
304
+ ): NdefRecord
305
+ ```
306
+
307
+ #### uriRecord(uri, id?)
308
+
309
+ ```ts
310
+ Builder.uriRecord(uri: string, id?: string): NdefRecord
311
+ ```
312
+
313
+ #### jsonRecord(json, id?)
314
+
315
+ ```ts
316
+ Builder.jsonRecord(
317
+ json: string | Uint8Array | number[],
318
+ id?: string
319
+ ): NdefRecord
320
+ ```
321
+
322
+ #### mimeRecord(mimeType, payload, id?)
323
+
324
+ ```ts
325
+ Builder.mimeRecord(
326
+ mimeType: string,
327
+ payload: string | Uint8Array | number[],
328
+ id?: string
329
+ ): NdefRecord
330
+ ```
331
+
332
+ #### externalRecord(domain, type, payload, id?)
333
+
334
+ ```ts
335
+ Builder.externalRecord(
336
+ domain: string,
337
+ type: string,
338
+ payload: string | Uint8Array | number[],
339
+ id?: string
340
+ ): NdefRecord
341
+ ```
342
+
343
+ #### record(init)
344
+
345
+ Low-level record builder.
346
+
347
+ ```ts
348
+ Builder.record(init: {
349
+ tnf: number;
350
+ type: string | number[];
351
+ id?: string | number[];
352
+ payload?: string | number[];
353
+ }): NdefRecord
354
+ ```
355
+
356
+ ---
357
+
358
+ # Low-Level Tag Modules
359
+
360
+ These modules are used for advanced operations inside `withTechnology` sessions.
361
+
362
+ ## `nfcTag`
363
+
364
+ ```ts
365
+ import { nfcTag } from "@spencerls/react-native-nfc";
366
+ ```
367
+
368
+ ### getTag()
369
+
370
+ Gets the current tag within a technology session.
371
+
372
+ ```ts
373
+ await nfcTag.getTag(): Promise<TagEvent>
374
+ ```
375
+
376
+ **Example:**
377
+ ```ts
378
+ await nfc.service.withTechnology(NfcTech.NfcV, async () => {
379
+ const tag = await nfcTag.getTag();
380
+ console.log("Tag ID:", tag.id);
381
+ });
382
+ ```
383
+
384
+ ---
385
+
386
+ ## `nfcVTag`
387
+
388
+ ```ts
389
+ import { nfcVTag } from "@spencerls/react-native-nfc";
390
+ ```
391
+
392
+ Low-level NFC-V operations. Use inside `withTechnology` sessions.
393
+
394
+ ### Properties
395
+
396
+ - `tech`: NfcTech constant for NFC-V (use with `withTechnology`)
397
+ - `utils`: ISO15693 utility functions
398
+
399
+ ### Methods
400
+
401
+ #### readBlock(tagId, blockNumber)
402
+
403
+ ```ts
404
+ await nfcVTag.readBlock(tagId: string, blockNumber: number): Promise<Uint8Array>
405
+ ```
406
+
407
+ #### readBlocks(tagId, startBlock, endBlock)
408
+
409
+ ```ts
410
+ await nfcVTag.readBlocks(
411
+ tagId: string,
412
+ startBlock: number,
413
+ endBlock: number
414
+ ): Promise<Uint8Array>
415
+ ```
416
+
417
+ #### writeBlock(tagId, blockNumber, data)
418
+
419
+ ```ts
420
+ await nfcVTag.writeBlock(
421
+ tagId: string,
422
+ blockNumber: number,
423
+ data: Uint8Array
424
+ ): Promise<void>
425
+ ```
426
+
427
+ #### writeBlocks(tagId, blockNumber, data)
428
+
429
+ ```ts
430
+ await nfcVTag.writeBlocks(
431
+ tagId: string,
432
+ blockNumber: number,
433
+ data: Uint8Array[]
434
+ ): Promise<void>
435
+ ```
436
+
437
+ #### getSystemInfo()
438
+
439
+ ```ts
440
+ await nfcVTag.getSystemInfo(): Promise<SystemInfo>
441
+ ```
442
+
443
+ #### transceive(bytes)
444
+
445
+ Send raw ISO15693 command.
446
+
447
+ ```ts
448
+ await nfcVTag.transceive(bytes: number[]): Promise<number[]>
449
+ ```
450
+
451
+ **Example:**
452
+ ```ts
453
+ import { nfcTag, nfcVTag } from "@spencerls/react-native-nfc";
454
+
455
+ await nfc.service.withTechnology(nfcVTag.tech, async () => {
456
+ const tag = await nfcTag.getTag();
457
+ if (!tag?.id) throw new Error("No tag detected");
458
+
459
+ const block = await nfcVTag.readBlock(tag.id, 0);
460
+ console.log("Block 0:", block);
461
+ });
462
+ ```
463
+
464
+ ---
465
+
466
+ ## `nfcNdefTag`
467
+
468
+ ```ts
469
+ import { nfcNdefTag } from "@spencerls/react-native-nfc";
470
+ ```
471
+
472
+ Low-level NDEF operations. Use inside `withTechnology` sessions.
473
+
474
+ ### Properties
475
+
476
+ - `tech`: NfcTech constant for NDEF (use with `withTechnology`)
477
+
478
+ ### Methods
479
+
480
+ #### readMessage()
481
+
482
+ ```ts
483
+ await nfcNdefTag.readMessage(): Promise<NdefMessageResult>
484
+ ```
485
+
486
+ #### write(records)
487
+
488
+ ```ts
489
+ await nfcNdefTag.write(records: NdefRecord[]): Promise<void>
490
+ ```
491
+
492
+ **Example:**
111
493
  ```ts
112
- nfc.ndef.parse(bytes)
113
- nfc.ndef.encode(records)
114
- nfc.ndef.utils.*
494
+ import { nfcNdefTag } from "@spencerls/react-native-nfc";
495
+
496
+ await nfc.service.withTechnology(nfcNdefTag.tech, async () => {
497
+ const message = await nfcNdefTag.readMessage();
498
+ console.log("Records:", message.ndefMessage);
499
+ });
115
500
  ```
116
501
 
117
502
  ---
@@ -130,16 +515,31 @@ import { ... } from "@spencerls/react-native-nfc";
130
515
 
131
516
  Automatically starts reader mode on mount and stops on unmount.
132
517
 
518
+ **Note:** Call `nfcService.enableReaderMode_ANDROID(flags)` before using this hook on Android.
519
+
133
520
  ```ts
134
521
  useNfc(
135
522
  (tagId: string) => { ... },
136
523
  {
137
- flags?: number,
138
524
  cooldownMs?: number
139
525
  }
140
526
  );
141
527
  ```
142
528
 
529
+ **Example:**
530
+ ```ts
531
+ import { nfcService } from "@spencerls/react-native-nfc";
532
+ import { NfcAdapter } from "react-native-nfc-manager";
533
+
534
+ // Call once at app startup
535
+ nfcService.enableReaderMode_ANDROID(NfcAdapter.FLAG_READER_NFC_V);
536
+
537
+ // Then use the hook
538
+ useNfc((tagId) => {
539
+ console.log("Scanned:", tagId);
540
+ }, { cooldownMs: 800 });
541
+ ```
542
+
143
543
  ---
144
544
 
145
545
  ## useNfcState()
@@ -156,8 +556,13 @@ const { mode, tag } = useNfcState();
156
556
 
157
557
  Manual control over reader mode.
158
558
 
559
+ **Note:** Call `nfcService.enableReaderMode_ANDROID(flags)` before calling `start()` on Android.
560
+
159
561
  ```ts
160
562
  const { start, stop } = useNfcReader();
563
+
564
+ // start signature:
565
+ start(onTag?: (tag: TagEvent) => void, options?: { cooldownMs?: number })
161
566
  ```
162
567
 
163
568
  ---
@@ -200,12 +605,55 @@ TagEvent
200
605
 
201
606
  ---
202
607
 
608
+ # Usage Patterns
609
+
610
+ ## High-Level vs Low-Level
611
+
612
+ ### High-Level (Recommended)
613
+
614
+ Use `nfc.*` namespace operations for most tasks. These automatically manage technology sessions.
615
+
616
+ ```ts
617
+ // ✅ Simple and clean
618
+ const data = await nfc.v.readBlock(0);
619
+ await nfc.ndef.write(records);
620
+ ```
621
+
622
+ ### Low-Level (Advanced)
623
+
624
+ Use tag modules (`nfcTag`, `nfcVTag`, `nfcNdefTag`) inside `withTechnology` for complex operations.
625
+
626
+ ```ts
627
+ // ✅ Advanced: custom multi-block read
628
+ await nfc.service.withTechnology(nfcVTag.tech, async () => {
629
+ const tag = await nfcTag.getTag();
630
+ if (!tag?.id) throw new Error("No NFC-V tag detected");
631
+
632
+ const buffer = new Uint8Array();
633
+ let offset = 0;
634
+
635
+ // Read blocks 0, 2, 4, 6
636
+ for (let i = 0; i < 8; i += 2) {
637
+ const block = await nfcVTag.readBlock(tag.id, i);
638
+ buffer.set(block, offset);
639
+ offset += block.length;
640
+ }
641
+
642
+ return buffer;
643
+ });
644
+ ```
645
+
646
+ ---
647
+
203
648
  # Internal Notes
204
649
 
650
+ - **Android reader mode flags** must be configured via `enableReaderMode_ANDROID()` before starting reader mode or using technology sessions.
205
651
  - iOS automatically restarts reader mode after each scan.
206
652
  - Android waits for cooldown before accepting next scan.
207
- - Technology sessions interrupt reader mode safely.
653
+ - Technology sessions interrupt reader mode safely and auto-restart when done.
208
654
  - `NfcTech` enums must be used. Do not pass raw strings.
655
+ - High-level `nfc.*` operations automatically manage technology sessions.
656
+ - Low-level tag modules require manual `withTechnology` wrapping.
209
657
 
210
658
  ---
211
659