@spencerls/react-native-nfc 1.0.8 → 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 +430 -19
- package/README.md +182 -39
- package/dist/index.d.mts +178 -129
- package/dist/index.d.ts +178 -129
- package/dist/index.js +492 -298
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +492 -299
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
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 {
|
|
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,
|
|
@@ -87,47 +94,409 @@ Attach a state listener; returns an unsubscribe function.
|
|
|
87
94
|
import { nfc } from "@spencerls/react-native-nfc";
|
|
88
95
|
```
|
|
89
96
|
|
|
90
|
-
`nfc` is a namespaced,
|
|
97
|
+
`nfc` is a namespaced, high-level interface that automatically manages technology sessions:
|
|
91
98
|
|
|
92
99
|
```
|
|
93
100
|
nfc.service (NfcService instance)
|
|
94
|
-
nfc.
|
|
95
|
-
nfc.
|
|
101
|
+
nfc.tag.* Basic tag operations
|
|
102
|
+
nfc.v.* NFC-V (ISO15693) operations
|
|
96
103
|
nfc.ndef.* NDEF operations
|
|
97
104
|
```
|
|
98
105
|
|
|
99
106
|
---
|
|
100
107
|
|
|
101
|
-
#
|
|
108
|
+
# Tag Namespace: `nfc.tag`
|
|
109
|
+
|
|
110
|
+
Get basic tag information.
|
|
102
111
|
|
|
103
|
-
|
|
112
|
+
### getTag(tech)
|
|
113
|
+
|
|
114
|
+
Requests technology session and returns tag information.
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
await nfc.tag.getTag(tech: NfcTech | NfcTech[]): Promise<TagEvent>
|
|
118
|
+
```
|
|
104
119
|
|
|
120
|
+
**Example:**
|
|
105
121
|
```ts
|
|
106
|
-
nfc
|
|
107
|
-
|
|
108
|
-
nfc.
|
|
109
|
-
|
|
110
|
-
|
|
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);
|
|
111
127
|
```
|
|
112
128
|
|
|
113
129
|
---
|
|
114
130
|
|
|
115
|
-
# NFC-
|
|
131
|
+
# NFC-V Namespace: `nfc.v`
|
|
132
|
+
|
|
133
|
+
High-level ISO15693 (NFC-V) operations. Automatically manages technology sessions.
|
|
134
|
+
|
|
135
|
+
### readBlock(blockNumber)
|
|
116
136
|
|
|
117
137
|
```ts
|
|
118
|
-
nfc.
|
|
119
|
-
|
|
120
|
-
|
|
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()
|
|
169
|
+
|
|
170
|
+
```ts
|
|
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);
|
|
121
179
|
```
|
|
122
180
|
|
|
123
181
|
---
|
|
124
182
|
|
|
125
183
|
# NDEF Namespace: `nfc.ndef`
|
|
126
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
|
+
|
|
127
447
|
```ts
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
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:**
|
|
493
|
+
```ts
|
|
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
|
+
});
|
|
131
500
|
```
|
|
132
501
|
|
|
133
502
|
---
|
|
@@ -236,13 +605,55 @@ TagEvent
|
|
|
236
605
|
|
|
237
606
|
---
|
|
238
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
|
+
|
|
239
648
|
# Internal Notes
|
|
240
649
|
|
|
241
650
|
- **Android reader mode flags** must be configured via `enableReaderMode_ANDROID()` before starting reader mode or using technology sessions.
|
|
242
651
|
- iOS automatically restarts reader mode after each scan.
|
|
243
652
|
- Android waits for cooldown before accepting next scan.
|
|
244
|
-
- Technology sessions interrupt reader mode safely.
|
|
653
|
+
- Technology sessions interrupt reader mode safely and auto-restart when done.
|
|
245
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.
|
|
246
657
|
|
|
247
658
|
---
|
|
248
659
|
|