@spencerls/react-native-nfc 1.0.8 → 1.0.10

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,13 +5,19 @@ 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,
12
19
  useNfcReader,
13
20
  useNfcTechnology,
14
- NfcProvider
15
21
  } from "@spencerls/react-native-nfc";
16
22
  ```
17
23
 
@@ -87,47 +93,409 @@ Attach a state listener; returns an unsubscribe function.
87
93
  import { nfc } from "@spencerls/react-native-nfc";
88
94
  ```
89
95
 
90
- `nfc` is a namespaced, easy-to-use interface:
96
+ `nfc` is a namespaced, high-level interface that automatically manages technology sessions:
91
97
 
92
98
  ```
93
99
  nfc.service (NfcService instance)
94
- nfc.v.* NFC-V operations
95
- nfc.a.* NFC-A operations
100
+ nfc.tag.* Basic tag operations
101
+ nfc.v.* NFC-V (ISO15693) operations
96
102
  nfc.ndef.* NDEF operations
97
103
  ```
98
104
 
99
105
  ---
100
106
 
101
- # NFC-V Namespace: `nfc.v`
107
+ # Tag Namespace: `nfc.tag`
108
+
109
+ Get basic tag information.
102
110
 
103
- High-level ISO15693 helpers and raw operations.
111
+ ### getTag(tech)
112
+
113
+ Requests technology session and returns tag information.
114
+
115
+ ```ts
116
+ await nfc.tag.getTag(tech: NfcTech | NfcTech[]): Promise<TagEvent>
117
+ ```
104
118
 
119
+ **Example:**
105
120
  ```ts
106
- nfc.v.getSystemInfoNfcV()
107
- nfc.v.readSingleBlock(uid, blockNumber)
108
- nfc.v.readMultipleBlocks(uid, start, count)
109
- nfc.v.writeSingleBlock(uid, blockNumber, data)
110
- nfc.v.getSecurityStatus(uid, blockNumber)
121
+ import { NfcTech } from "react-native-nfc-manager";
122
+
123
+ const tag = await nfc.tag.getTag([NfcTech.NfcA, NfcTech.NfcV, NfcTech.Ndef]);
124
+ console.log("Tag ID:", tag.id);
125
+ console.log("Tag Type:", tag.type);
111
126
  ```
112
127
 
113
128
  ---
114
129
 
115
- # NFC-A Namespace: `nfc.a`
130
+ # NFC-V Namespace: `nfc.v`
131
+
132
+ High-level ISO15693 (NFC-V) operations. Automatically manages technology sessions.
133
+
134
+ ### readBlock(blockNumber)
135
+
136
+ ```ts
137
+ await nfc.v.readBlock(blockNumber: number): Promise<Uint8Array>
138
+ ```
139
+
140
+ ### readBlocks(startBlock, endBlock)
141
+
142
+ ```ts
143
+ await nfc.v.readBlocks(
144
+ startBlock: number,
145
+ endBlock: number
146
+ ): Promise<Uint8Array>
147
+ ```
148
+
149
+ ### writeBlock(blockNumber, data)
116
150
 
117
151
  ```ts
118
- nfc.a.transceive(bytes)
119
- nfc.a.getAtqa()
120
- nfc.a.getSak()
152
+ await nfc.v.writeBlock(
153
+ blockNumber: number,
154
+ data: Uint8Array
155
+ ): Promise<void>
156
+ ```
157
+
158
+ ### writeBlocks(blockNumber, data)
159
+
160
+ ```ts
161
+ await nfc.v.writeBlocks(
162
+ blockNumber: number,
163
+ data: Uint8Array[]
164
+ ): Promise<void>
165
+ ```
166
+
167
+ ### getSystemInfo()
168
+
169
+ ```ts
170
+ await nfc.v.getSystemInfo(): Promise<SystemInfo>
171
+ ```
172
+
173
+ **Example:**
174
+ ```ts
175
+ const info = await nfc.v.getSystemInfo();
176
+ console.log("Blocks:", info.numberOfBlocks);
177
+ console.log("Block size:", info.blockSize);
121
178
  ```
122
179
 
123
180
  ---
124
181
 
125
182
  # NDEF Namespace: `nfc.ndef`
126
183
 
184
+ High-level NDEF operations and builder.
185
+
186
+ ### write(records)
187
+
188
+ Writes NDEF records to a tag.
189
+
190
+ ```ts
191
+ await nfc.ndef.write(records: NdefRecord[]): Promise<void>
192
+ ```
193
+
194
+ ### writeText(text, lang?, encoding?, id?)
195
+
196
+ ```ts
197
+ await nfc.ndef.writeText(
198
+ text: string,
199
+ lang?: ISOLangCode,
200
+ encoding?: "utf8" | "utf16",
201
+ id?: string
202
+ ): Promise<void>
203
+ ```
204
+
205
+ ### writeUri(uri, id?)
206
+
207
+ ```ts
208
+ await nfc.ndef.writeUri(uri: string, id?: string): Promise<void>
209
+ ```
210
+
211
+ ### writeJson(data, id?)
212
+
213
+ ```ts
214
+ await nfc.ndef.writeJson(data: unknown, id?: string): Promise<void>
215
+ ```
216
+
217
+ ### writeMime(mimeType, payload, id?)
218
+
219
+ ```ts
220
+ await nfc.ndef.writeMime(
221
+ mimeType: string,
222
+ payload: string | Uint8Array | number[],
223
+ id?: string
224
+ ): Promise<void>
225
+ ```
226
+
227
+ ### writeExternal(domain, type, payload, id?)
228
+
229
+ ```ts
230
+ await nfc.ndef.writeExternal(
231
+ domain: string,
232
+ type: string,
233
+ payload: string | Uint8Array | number[],
234
+ id?: string
235
+ ): Promise<void>
236
+ ```
237
+
238
+ ### readMessage()
239
+
240
+ ```ts
241
+ await nfc.ndef.readMessage(): Promise<NdefMessageResult>
242
+ ```
243
+
244
+ ### readFull()
245
+
246
+ Reads NDEF message and tag information.
247
+
248
+ ```ts
249
+ await nfc.ndef.readFull(): Promise<{
250
+ message: NdefMessageResult;
251
+ tag: TagEvent;
252
+ }>
253
+ ```
254
+
255
+ ### getStatus()
256
+
127
257
  ```ts
128
- nfc.ndef.parse(bytes)
129
- nfc.ndef.encode(records)
130
- nfc.ndef.utils.*
258
+ await nfc.ndef.getStatus(): Promise<{
259
+ status: NdefStatus;
260
+ capacity: number;
261
+ }>
262
+ ```
263
+
264
+ ### makeReadOnly()
265
+
266
+ ```ts
267
+ await nfc.ndef.makeReadOnly(): Promise<void>
268
+ ```
269
+
270
+ ---
271
+
272
+ # NDEF Builder: `nfc.ndef.Builder`
273
+
274
+ Create NDEF records using a builder pattern.
275
+
276
+ ### Builder.records(builder)
277
+
278
+ ```ts
279
+ nfc.ndef.Builder.records((B) => NdefRecord[]): NdefRecord[]
280
+ ```
281
+
282
+ **Example:**
283
+ ```ts
284
+ const records = nfc.ndef.Builder.records((B) => [
285
+ B.textRecord("Hello, world!"),
286
+ B.uriRecord("https://example.com"),
287
+ B.jsonRecord(JSON.stringify({ key: "value" })),
288
+ ]);
289
+
290
+ await nfc.ndef.write(records);
291
+ ```
292
+
293
+ ### Builder Methods
294
+
295
+ #### textRecord(text, lang?, encoding?, id?)
296
+
297
+ ```ts
298
+ Builder.textRecord(
299
+ text: string,
300
+ lang: ISOLangCode = "en",
301
+ encoding: "utf8" | "utf16" = "utf8",
302
+ id?: string
303
+ ): NdefRecord
304
+ ```
305
+
306
+ #### uriRecord(uri, id?)
307
+
308
+ ```ts
309
+ Builder.uriRecord(uri: string, id?: string): NdefRecord
310
+ ```
311
+
312
+ #### jsonRecord(json, id?)
313
+
314
+ ```ts
315
+ Builder.jsonRecord(
316
+ json: string | Uint8Array | number[],
317
+ id?: string
318
+ ): NdefRecord
319
+ ```
320
+
321
+ #### mimeRecord(mimeType, payload, id?)
322
+
323
+ ```ts
324
+ Builder.mimeRecord(
325
+ mimeType: string,
326
+ payload: string | Uint8Array | number[],
327
+ id?: string
328
+ ): NdefRecord
329
+ ```
330
+
331
+ #### externalRecord(domain, type, payload, id?)
332
+
333
+ ```ts
334
+ Builder.externalRecord(
335
+ domain: string,
336
+ type: string,
337
+ payload: string | Uint8Array | number[],
338
+ id?: string
339
+ ): NdefRecord
340
+ ```
341
+
342
+ #### record(init)
343
+
344
+ Low-level record builder.
345
+
346
+ ```ts
347
+ Builder.record(init: {
348
+ tnf: number;
349
+ type: string | number[];
350
+ id?: string | number[];
351
+ payload?: string | number[];
352
+ }): NdefRecord
353
+ ```
354
+
355
+ ---
356
+
357
+ # Low-Level Tag Modules
358
+
359
+ These modules are used for advanced operations inside `withTechnology` sessions.
360
+
361
+ ## `nfcTag`
362
+
363
+ ```ts
364
+ import { nfcTag } from "@spencerls/react-native-nfc";
365
+ ```
366
+
367
+ ### getTag()
368
+
369
+ Gets the current tag within a technology session.
370
+
371
+ ```ts
372
+ await nfcTag.getTag(): Promise<TagEvent>
373
+ ```
374
+
375
+ **Example:**
376
+ ```ts
377
+ await nfc.service.withTechnology(NfcTech.NfcV, async () => {
378
+ const tag = await nfcTag.getTag();
379
+ console.log("Tag ID:", tag.id);
380
+ });
381
+ ```
382
+
383
+ ---
384
+
385
+ ## `nfcVTag`
386
+
387
+ ```ts
388
+ import { nfcVTag } from "@spencerls/react-native-nfc";
389
+ ```
390
+
391
+ Low-level NFC-V operations. Use inside `withTechnology` sessions.
392
+
393
+ ### Properties
394
+
395
+ - `tech`: NfcTech constant for NFC-V (use with `withTechnology`)
396
+ - `utils`: ISO15693 utility functions
397
+
398
+ ### Methods
399
+
400
+ #### readBlock(tagId, blockNumber)
401
+
402
+ ```ts
403
+ await nfcVTag.readBlock(tagId: string, blockNumber: number): Promise<Uint8Array>
404
+ ```
405
+
406
+ #### readBlocks(tagId, startBlock, endBlock)
407
+
408
+ ```ts
409
+ await nfcVTag.readBlocks(
410
+ tagId: string,
411
+ startBlock: number,
412
+ endBlock: number
413
+ ): Promise<Uint8Array>
414
+ ```
415
+
416
+ #### writeBlock(tagId, blockNumber, data)
417
+
418
+ ```ts
419
+ await nfcVTag.writeBlock(
420
+ tagId: string,
421
+ blockNumber: number,
422
+ data: Uint8Array
423
+ ): Promise<void>
424
+ ```
425
+
426
+ #### writeBlocks(tagId, blockNumber, data)
427
+
428
+ ```ts
429
+ await nfcVTag.writeBlocks(
430
+ tagId: string,
431
+ blockNumber: number,
432
+ data: Uint8Array[]
433
+ ): Promise<void>
434
+ ```
435
+
436
+ #### getSystemInfo()
437
+
438
+ ```ts
439
+ await nfcVTag.getSystemInfo(): Promise<SystemInfo>
440
+ ```
441
+
442
+ #### transceive(bytes)
443
+
444
+ Send raw ISO15693 command.
445
+
446
+ ```ts
447
+ await nfcVTag.transceive(bytes: number[]): Promise<number[]>
448
+ ```
449
+
450
+ **Example:**
451
+ ```ts
452
+ import { nfcTag, nfcVTag } from "@spencerls/react-native-nfc";
453
+
454
+ await nfc.service.withTechnology(nfcVTag.tech, async () => {
455
+ const tag = await nfcTag.getTag();
456
+ if (!tag?.id) throw new Error("No tag detected");
457
+
458
+ const block = await nfcVTag.readBlock(tag.id, 0);
459
+ console.log("Block 0:", block);
460
+ });
461
+ ```
462
+
463
+ ---
464
+
465
+ ## `nfcNdefTag`
466
+
467
+ ```ts
468
+ import { nfcNdefTag } from "@spencerls/react-native-nfc";
469
+ ```
470
+
471
+ Low-level NDEF operations. Use inside `withTechnology` sessions.
472
+
473
+ ### Properties
474
+
475
+ - `tech`: NfcTech constant for NDEF (use with `withTechnology`)
476
+
477
+ ### Methods
478
+
479
+ #### readMessage()
480
+
481
+ ```ts
482
+ await nfcNdefTag.readMessage(): Promise<NdefMessageResult>
483
+ ```
484
+
485
+ #### write(records)
486
+
487
+ ```ts
488
+ await nfcNdefTag.write(records: NdefRecord[]): Promise<void>
489
+ ```
490
+
491
+ **Example:**
492
+ ```ts
493
+ import { nfcNdefTag } from "@spencerls/react-native-nfc";
494
+
495
+ await nfc.service.withTechnology(nfcNdefTag.tech, async () => {
496
+ const message = await nfcNdefTag.readMessage();
497
+ console.log("Records:", message.ndefMessage);
498
+ });
131
499
  ```
132
500
 
133
501
  ---
@@ -210,18 +578,6 @@ await runWithTech([NfcTech.NfcV], async () => {
210
578
 
211
579
  ---
212
580
 
213
- # NfcProvider
214
-
215
- Optional provider that exposes service state to React tree.
216
-
217
- ```tsx
218
- <NfcProvider>
219
- <App />
220
- </NfcProvider>
221
- ```
222
-
223
- ---
224
-
225
581
  # Types
226
582
 
227
583
  All types are exported from `@spencerls/react-native-nfc/nfc`.
@@ -236,13 +592,55 @@ TagEvent
236
592
 
237
593
  ---
238
594
 
595
+ # Usage Patterns
596
+
597
+ ## High-Level vs Low-Level
598
+
599
+ ### High-Level (Recommended)
600
+
601
+ Use `nfc.*` namespace operations for most tasks. These automatically manage technology sessions.
602
+
603
+ ```ts
604
+ // ✅ Simple and clean
605
+ const data = await nfc.v.readBlock(0);
606
+ await nfc.ndef.write(records);
607
+ ```
608
+
609
+ ### Low-Level (Advanced)
610
+
611
+ Use tag modules (`nfcTag`, `nfcVTag`, `nfcNdefTag`) inside `withTechnology` for complex operations.
612
+
613
+ ```ts
614
+ // ✅ Advanced: custom multi-block read
615
+ await nfc.service.withTechnology(nfcVTag.tech, async () => {
616
+ const tag = await nfcTag.getTag();
617
+ if (!tag?.id) throw new Error("No NFC-V tag detected");
618
+
619
+ const buffer = new Uint8Array();
620
+ let offset = 0;
621
+
622
+ // Read blocks 0, 2, 4, 6
623
+ for (let i = 0; i < 8; i += 2) {
624
+ const block = await nfcVTag.readBlock(tag.id, i);
625
+ buffer.set(block, offset);
626
+ offset += block.length;
627
+ }
628
+
629
+ return buffer;
630
+ });
631
+ ```
632
+
633
+ ---
634
+
239
635
  # Internal Notes
240
636
 
241
637
  - **Android reader mode flags** must be configured via `enableReaderMode_ANDROID()` before starting reader mode or using technology sessions.
242
638
  - iOS automatically restarts reader mode after each scan.
243
639
  - Android waits for cooldown before accepting next scan.
244
- - Technology sessions interrupt reader mode safely.
640
+ - Technology sessions interrupt reader mode safely and auto-restart when done.
245
641
  - `NfcTech` enums must be used. Do not pass raw strings.
642
+ - High-level `nfc.*` operations automatically manage technology sessions.
643
+ - Low-level tag modules require manual `withTechnology` wrapping.
246
644
 
247
645
  ---
248
646