@exxili/capacitor-nfc 0.0.11 → 0.0.13
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/README.md +274 -103
- package/android/src/main/kotlin/com/exxili/capacitornfc/NFCPlugin.kt +157 -42
- package/dist/esm/definitions.d.ts +81 -8
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.js +225 -44
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/web.d.ts +5 -8
- package/dist/esm/web.js +10 -22
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +234 -65
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +234 -65
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/NFCPlugin/NFCPlugin.swift +71 -18
- package/ios/Sources/NFCPlugin/NFCReader.swift +456 -46
- package/ios/Sources/NFCPlugin/NFCWriter.swift +9 -16
- package/package.json +13 -4
package/README.md
CHANGED
|
@@ -9,32 +9,35 @@ A Capacitor plugin for reading and writing NFC tags on iOS and Android devices.
|
|
|
9
9
|
|
|
10
10
|
## Table of Contents
|
|
11
11
|
|
|
12
|
-
- [
|
|
13
|
-
- [
|
|
14
|
-
- [
|
|
15
|
-
- [
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
- [
|
|
19
|
-
- [
|
|
20
|
-
- [
|
|
21
|
-
- [
|
|
22
|
-
|
|
23
|
-
- [
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
- [
|
|
30
|
-
|
|
31
|
-
- [
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
- [
|
|
36
|
-
- [
|
|
37
|
-
- [
|
|
12
|
+
- [Capacitor NFC Plugin (@exxili/capacitor-nfc)](#capacitor-nfc-plugin-exxilicapacitor-nfc)
|
|
13
|
+
- [Table of Contents](#table-of-contents)
|
|
14
|
+
- [Installation](#installation)
|
|
15
|
+
- [iOS Setup](#ios-setup)
|
|
16
|
+
- [1. Enable NFC Capability](#1-enable-nfc-capability)
|
|
17
|
+
- [2. Add Usage Description](#2-add-usage-description)
|
|
18
|
+
- [Android Setup](#android-setup)
|
|
19
|
+
- [Usage](#usage)
|
|
20
|
+
- [Reading NFC Tags](#reading-nfc-tags)
|
|
21
|
+
- [Writing NFC Tags](#writing-nfc-tags)
|
|
22
|
+
- [API](#api)
|
|
23
|
+
- [Methods](#methods)
|
|
24
|
+
- [`isSupported()`](#issupported)
|
|
25
|
+
- [`startScan()`](#startscan)
|
|
26
|
+
- [`cancelScan()`](#cancelscan)
|
|
27
|
+
- [`writeNDEF(options: NDEFWriteOptions<T extends string | number[] | Uint8Array = string>)`](#writendefoptions-ndefwriteoptionst-extends-string--number--uint8array--string)
|
|
28
|
+
- [`cancelWriteAndroid()`](#cancelwriteandroid)
|
|
29
|
+
- [Listeners](#listeners)
|
|
30
|
+
- [`onRead(listener: (data: NDEFMessagesTransformable) => void)`](#onreadlistener-data-ndefmessagestransformable--void)
|
|
31
|
+
- [Interfaces](#interfaces)
|
|
32
|
+
- [`NDEFWriteOptions`](#ndefwriteoptions)
|
|
33
|
+
- [`NDEFMessagesTransformable`](#ndefmessagestransformable)
|
|
34
|
+
- [`NDEFMessages`](#ndefmessages)
|
|
35
|
+
- [`NDEFMessage`](#ndefmessage)
|
|
36
|
+
- [`NDEFRecord`](#ndefrecord)
|
|
37
|
+
- [`NFCError`](#nfcerror)
|
|
38
|
+
- [Integration into a Capacitor App](#integration-into-a-capacitor-app)
|
|
39
|
+
- [Example](#example)
|
|
40
|
+
- [License](#license)
|
|
38
41
|
|
|
39
42
|
## Installation
|
|
40
43
|
|
|
@@ -60,6 +63,25 @@ In Xcode:
|
|
|
60
63
|
5. Click the `+ Capability` button.
|
|
61
64
|
6. Add **Near Field Communication Tag Reading**.
|
|
62
65
|
|
|
66
|
+
> **Advanced tag formats:** If you need ISO 7816, ISO 15693, or FeliCa access (to read raw UIDs, system codes, etc.), Apple requires additional entitlements in your provisioning profile and `Info.plist`. The plugin will fall back automatically when they are absent, but to unlock the full feature set add the relevant keys:
|
|
67
|
+
>
|
|
68
|
+
> ```xml
|
|
69
|
+
> <key>com.apple.developer.nfc.readersession.felica.systemcodes</key>
|
|
70
|
+
> <array>
|
|
71
|
+
> <string>12FC</string>
|
|
72
|
+
> <string>0000</string>
|
|
73
|
+
> </array>
|
|
74
|
+
> <key>com.apple.developer.nfc.readersession.iso7816.select-identifiers</key>
|
|
75
|
+
> <array>
|
|
76
|
+
> <string>D2760000850100</string>
|
|
77
|
+
> <string>D2760000850101</string>
|
|
78
|
+
> <string>D2760001180101</string>
|
|
79
|
+
> <string>00000000000000</string>
|
|
80
|
+
> </array>
|
|
81
|
+
> ```
|
|
82
|
+
>
|
|
83
|
+
> Replace the sample identifiers with the values required for your tags. Consult Apple's CoreNFC documentation for the complete list of entitlement keys.
|
|
84
|
+
|
|
63
85
|
### 2. Add Usage Description
|
|
64
86
|
|
|
65
87
|
Add the `NFCReaderUsageDescription` key to your `Info.plist` file to explain why your app needs access to NFC.
|
|
@@ -95,20 +117,40 @@ import { NFC } from '@exxili/capacitor-nfc';
|
|
|
95
117
|
To read NFC tags, you need to listen for `nfcTag` events. On iOS, you must also start the NFC scanning session using `startScan()`.
|
|
96
118
|
|
|
97
119
|
```typescript
|
|
98
|
-
import {NFC, NDEFMessagesTransformable, NFCError} from '@exxili/capacitor-nfc';
|
|
120
|
+
import { NFC, NDEFMessagesTransformable, NFCError } from '@exxili/capacitor-nfc';
|
|
99
121
|
|
|
100
|
-
// Start NFC scanning
|
|
122
|
+
// Start NFC scanning (iOS only)
|
|
101
123
|
NFC.startScan().catch((error) => {
|
|
102
124
|
console.error('Error starting NFC scan:', error);
|
|
103
125
|
});
|
|
104
126
|
|
|
105
127
|
// Listen for NFC tag detection
|
|
106
128
|
NFC.onRead((data: NDEFMessagesTransformable) => {
|
|
107
|
-
|
|
129
|
+
// Text (T) and URI (U) records decoded; others best-effort UTF-8
|
|
130
|
+
const asString = data.string();
|
|
131
|
+
console.log('First record text payload:', asString.messages[0]?.records[0]?.payload);
|
|
132
|
+
|
|
133
|
+
// Raw bytes
|
|
134
|
+
const asUint8 = data.uint8Array();
|
|
135
|
+
console.log('First record raw bytes length:', asUint8.messages[0]?.records[0]?.payload.length);
|
|
136
|
+
|
|
137
|
+
// Access tag information (UID, tech types, etc.)
|
|
138
|
+
const info = asString.tagInfo;
|
|
139
|
+
if (info?.fallback) {
|
|
140
|
+
console.log('Reader fallback mode:', info.fallbackMode, 'Reason:', info.reason);
|
|
141
|
+
} else if (info) {
|
|
142
|
+
console.log('Tag UID:', info.uid);
|
|
143
|
+
console.log('Tag technologies:', info.techTypes);
|
|
144
|
+
console.log('Tag type:', info.type);
|
|
145
|
+
if (info.maxSize) {
|
|
146
|
+
console.log('Max NDEF size:', info.maxSize);
|
|
147
|
+
}
|
|
148
|
+
console.log('Is writable:', info.isWritable);
|
|
149
|
+
}
|
|
108
150
|
});
|
|
109
151
|
|
|
110
152
|
// Handle NFC errors
|
|
111
|
-
NFC.onError(
|
|
153
|
+
NFC.onError((error: NFCError) => {
|
|
112
154
|
console.error('NFC Error:', error);
|
|
113
155
|
});
|
|
114
156
|
```
|
|
@@ -123,9 +165,32 @@ import { NFC, NDEFWriteOptions, NFCError } from '@exxili/capacitor-nfc';
|
|
|
123
165
|
const message: NDEFWriteOptions = {
|
|
124
166
|
records: [
|
|
125
167
|
{
|
|
126
|
-
type: 'T', // Text record
|
|
168
|
+
type: 'T', // Well Known Text record. String payload will be encoded as: [status][lang='en'][UTF-8 text]
|
|
127
169
|
payload: 'Hello, NFC!',
|
|
128
170
|
},
|
|
171
|
+
{
|
|
172
|
+
type: 'U', // Well Known URI record. String payload encoded as: [0x00][URI bytes]
|
|
173
|
+
payload: 'https://example.com',
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
type: 'T',
|
|
177
|
+
payload: new Uint8Array([0x01, 0x65, 0x48, 0x69]), // Raw bytes preserved (DO NOT re-format)
|
|
178
|
+
},
|
|
179
|
+
],
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
// For complete control over binary content, use raw mode:
|
|
183
|
+
const rawMessage: NDEFWriteOptions = {
|
|
184
|
+
rawMode: true, // Bypasses automatic Text/URI formatting
|
|
185
|
+
records: [
|
|
186
|
+
{
|
|
187
|
+
type: 'T',
|
|
188
|
+
payload: 'Hello, NFC!', // Written as UTF-8 bytes without Text record prefix
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
type: 'custom',
|
|
192
|
+
payload: new Uint8Array([0x01, 0x02, 0x03, 0x04]), // Exact bytes written to tag
|
|
193
|
+
},
|
|
129
194
|
],
|
|
130
195
|
};
|
|
131
196
|
|
|
@@ -161,12 +226,23 @@ Returns if NFC is supported on the scanning device.
|
|
|
161
226
|
|
|
162
227
|
#### `startScan()`
|
|
163
228
|
|
|
164
|
-
Starts the NFC scanning session on
|
|
229
|
+
Starts the NFC scanning session on **_iOS only_**. Android devices are always in reading mode, so setting up the `nfcTag` listener is sufficient to handle tag reads on Android.
|
|
230
|
+
|
|
231
|
+
The iOS implementation now adapts automatically if the extended CoreNFC entitlements (ISO 7816, ISO 15693, FeliCa) are missing. The plugin first attempts the advanced tag reader so you can access UID/tech info. When iOS reports `Missing required entitlement`, the plugin downgrades to a compatibility mode (ISO 14443 only) and, if necessary, to the classic NDEF reader. A synthetic `nfcTag` event is emitted with `tagInfo.fallback`, `tagInfo.fallbackMode`, and `tagInfo.reason` so your UI can react immediately.
|
|
232
|
+
|
|
233
|
+
You can override the mode explicitly:
|
|
234
|
+
|
|
235
|
+
- `mode: 'auto'` (default) – advanced reader with automatic downgrade and caching.
|
|
236
|
+
- `mode: 'full'` – force a fresh attempt at the advanced reader, resetting cached fallback state.
|
|
237
|
+
- `mode: 'compat'` – skip the advanced probe and jump straight to the ISO 14443 compatibility reader.
|
|
238
|
+
- `mode: 'ndef'` – bypass tag sessions entirely and revert to the legacy NDEF-only reader.
|
|
239
|
+
|
|
240
|
+
Legacy booleans `forceFull`, `forceCompat`, and `forceNDEF` map to the options above for backwards compatibility.
|
|
165
241
|
|
|
166
242
|
**Returns**: `Promise<void>`
|
|
167
243
|
|
|
168
244
|
```typescript
|
|
169
|
-
NFC.startScan()
|
|
245
|
+
NFC.startScan({ mode: 'auto' })
|
|
170
246
|
.then(() => {
|
|
171
247
|
// Scanning started
|
|
172
248
|
})
|
|
@@ -175,17 +251,47 @@ NFC.startScan()
|
|
|
175
251
|
});
|
|
176
252
|
```
|
|
177
253
|
|
|
178
|
-
#### `
|
|
254
|
+
#### `cancelScan()`
|
|
255
|
+
|
|
256
|
+
Immediately invalidates the active iOS CoreNFC reader session (if any). Useful when you want to abort a scan early instead of waiting for the user to cancel or for a tag detection timeout.
|
|
257
|
+
|
|
258
|
+
Platform notes:
|
|
259
|
+
|
|
260
|
+
- iOS: Actively ends the session. Calling `startScan()` again after the returned promise resolves is safe.
|
|
261
|
+
- Android: No-op (Android is always passively listening via foreground dispatch; you generally just ignore future events or control UI state).
|
|
262
|
+
|
|
263
|
+
```typescript
|
|
264
|
+
await NFC.cancelScan(); // iOS: ends session; Android: no-op
|
|
265
|
+
// Optionally restart
|
|
266
|
+
await NFC.startScan();
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
**Returns**: `Promise<void>`
|
|
270
|
+
|
|
271
|
+
#### `writeNDEF(options: NDEFWriteOptions<T extends string | number[] | Uint8Array = string>)`
|
|
179
272
|
|
|
180
273
|
Writes an NDEF message to an NFC tag.
|
|
181
274
|
|
|
182
|
-
Payload may be provided as a string, `Uint8Array`, or an array of numbers.
|
|
275
|
+
Payload may be provided as a string, `Uint8Array`, or an array of numbers.
|
|
276
|
+
|
|
277
|
+
Automatic formatting rules (to aid interoperability):
|
|
278
|
+
|
|
279
|
+
- Text (`type: 'T'` + string payload): encoded per NFC Forum RTD Text spec `[status][lang=en][UTF-8 text]`.
|
|
280
|
+
- URI (`type: 'U'` + string payload): encoded as `[0x00][UTF-8 URI bytes]` (prefix compression not yet applied).
|
|
281
|
+
- Any other `type` + string payload: UTF-8 bytes only (no extra framing).
|
|
282
|
+
- `Uint8Array` or `number[]` payloads are treated as raw bytes and written verbatim (never altered).
|
|
283
|
+
|
|
284
|
+
**Raw Mode**: Set `rawMode: true` to bypass automatic Well Known Type formatting entirely. All string payloads will be written as UTF-8 bytes without Text ('T') or URI ('U') prefixes, giving you complete control over the binary content.
|
|
285
|
+
|
|
286
|
+
If you need full manual control of a Text or URI record, supply raw bytes (number[] / Uint8Array) and the plugin will not modify them.
|
|
287
|
+
|
|
288
|
+
If you attempt to write zero records the promise rejects with `Error("At least one NDEF record is required")`.
|
|
183
289
|
|
|
184
290
|
Android use: since Android has no default UI for reading and writing NFC tags, it is recommended that you add a UI indicator to your application when calling `writeNDEF` and remove it in the `nfcWriteSuccess` listener callback and the `nfcError` listener callback. This will prevent accidental writes to tags that your users intended to read from.
|
|
185
291
|
|
|
186
292
|
**Parameters**:
|
|
187
293
|
|
|
188
|
-
- `options: NDEFWriteOptions<T extends string | number[] | Uint8Array = string>` - The NDEF message to write.
|
|
294
|
+
- `options: NDEFWriteOptions<T extends string | number[] | Uint8Array = string>` - The NDEF message to write. Must include at least one record.
|
|
189
295
|
|
|
190
296
|
**Returns**: `Promise<void>`
|
|
191
297
|
|
|
@@ -209,24 +315,35 @@ Cancels an Android NFC write operation. Android does not have a native UI for NF
|
|
|
209
315
|
|
|
210
316
|
Adds a listener for NFC tag detection events. Returns type `NDEFMessagesTransformable`, which returns the following methods to provide the payload:
|
|
211
317
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
318
|
+
- `string()`: Returns `NDEFMessages<string>`, where all payloads are strings.
|
|
319
|
+
- `base64()`: Returns `NDEFMessages<string>`, where all payloads are the base64-encoded payloads read from the NFC tag.
|
|
320
|
+
- `uint8Array()`: Returns `NDEFMessages<Uint8Array>`, where all payloads are the `Uint8Array` bytes from the NFC tag.
|
|
321
|
+
- `numberArray()`: Returns `NDEFMessages<number[]>`, where all payloads' bytes from the NFC tag are represented as a `number[]`.
|
|
216
322
|
|
|
217
323
|
**Parameters**:
|
|
218
324
|
|
|
219
325
|
- `listener: (data: NDEFMessagesTransformable) => void` - The function to call when an NFC tag is detected.
|
|
220
326
|
|
|
221
|
-
**Returns**: `void`
|
|
327
|
+
**Returns**: `() => void` Unsubscribe function to remove just this listener.
|
|
222
328
|
|
|
223
329
|
```typescript
|
|
224
|
-
NFC.onRead((data
|
|
225
|
-
|
|
330
|
+
const offRead = NFC.onRead((data) => {
|
|
331
|
+
const textRecords = data.string(); // Decoded string representation
|
|
332
|
+
const base64Records = data.base64(); // Original base64 payloads
|
|
333
|
+
const bytesRecords = data.uint8Array(); // Uint8Array payloads
|
|
334
|
+
const numArrayRecords = data.numberArray(); // number[] representation
|
|
335
|
+
console.log(textRecords);
|
|
226
336
|
});
|
|
337
|
+
|
|
338
|
+
// Later (component unmount / cleanup)
|
|
339
|
+
offRead();
|
|
227
340
|
```
|
|
228
341
|
|
|
229
|
-
|
|
342
|
+
> Tip: Register listeners once per screen/component and always dispose them when unmounting to avoid duplicate callbacks.
|
|
343
|
+
|
|
344
|
+
````
|
|
345
|
+
|
|
346
|
+
#### On Error
|
|
230
347
|
|
|
231
348
|
Adds a listener for NFC error events.
|
|
232
349
|
|
|
@@ -234,29 +351,23 @@ Adds a listener for NFC error events.
|
|
|
234
351
|
|
|
235
352
|
- `listener: (error: NFCError) => void` - The function to call when an NFC error occurs.
|
|
236
353
|
|
|
237
|
-
**Returns**: `
|
|
238
|
-
|
|
239
|
-
```typescript
|
|
240
|
-
NFC.onError((error: NFCError) => {
|
|
241
|
-
console.error('NFC Error:', error);
|
|
242
|
-
});
|
|
243
|
-
```
|
|
354
|
+
**Returns**: `() => void` Unsubscribe function.
|
|
244
355
|
|
|
245
|
-
####
|
|
356
|
+
#### On Error
|
|
246
357
|
|
|
247
|
-
Adds a listener for NFC
|
|
358
|
+
Adds a listener for NFC error events.
|
|
248
359
|
|
|
249
360
|
**Parameters**:
|
|
250
361
|
|
|
251
|
-
- `listener: () => void` - The function to call when an
|
|
362
|
+
- `listener: (error: NFCError) => void` - The function to call when an NFC error occurs.
|
|
252
363
|
|
|
253
|
-
**Returns**: `
|
|
364
|
+
**Returns**: `() => void` Unsubscribe function.
|
|
254
365
|
|
|
255
366
|
```typescript
|
|
256
|
-
NFC.
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
367
|
+
const offError = NFC.onError(err => console.error('NFC Error:', err));
|
|
368
|
+
// later
|
|
369
|
+
offError();
|
|
370
|
+
````
|
|
260
371
|
|
|
261
372
|
### Interfaces
|
|
262
373
|
|
|
@@ -267,6 +378,11 @@ Options for writing an NDEF message.
|
|
|
267
378
|
```typescript
|
|
268
379
|
interface NDEFWriteOptions<T extends string | number[] | Uint8Array = string> {
|
|
269
380
|
records: NDEFRecord<T>[];
|
|
381
|
+
/**
|
|
382
|
+
* When true, bypasses automatic Well Known Type formatting (Text 'T' and URI 'U' prefixes).
|
|
383
|
+
* All payloads are written as raw bytes without additional framing.
|
|
384
|
+
*/
|
|
385
|
+
rawMode?: boolean;
|
|
270
386
|
}
|
|
271
387
|
```
|
|
272
388
|
|
|
@@ -274,17 +390,17 @@ interface NDEFWriteOptions<T extends string | number[] | Uint8Array = string> {
|
|
|
274
390
|
|
|
275
391
|
Returned by `onRead` and includes the following methods to provide the payload:
|
|
276
392
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
393
|
+
- `string()`: Returns `NDEFMessages<string>`, where all payloads are strings.
|
|
394
|
+
- `base64()`: Returns `NDEFMessages<string>`, where all payloads are the base64-encoded payloads read from the NFC tag.
|
|
395
|
+
- `uint8Array()`: Returns `NDEFMessages<Uint8Array>`, where all payloads are the `Uint8Array` bytes from the NFC tag.
|
|
396
|
+
- `numberArray()`: Returns `NDEFMessages<number[]>`, where all payloads bytes from the NFC tag represented as a `number[]`.
|
|
281
397
|
|
|
282
398
|
```typescript
|
|
283
399
|
interface NDEFMessagesTransformable {
|
|
284
|
-
base64: ()=> NDEFMessages
|
|
285
|
-
uint8Array: ()=> NDEFMessages<Uint8Array>;
|
|
286
|
-
string: ()=> NDEFMessages
|
|
287
|
-
numberArray: ()=> NDEFMessages<number[]>;
|
|
400
|
+
base64: () => NDEFMessages<string>; // Original base64 strings
|
|
401
|
+
uint8Array: () => NDEFMessages<Uint8Array>; // Raw bytes
|
|
402
|
+
string: () => NDEFMessages<string>; // Decoded (T & U handled, others UTF-8 best-effort)
|
|
403
|
+
numberArray: () => NDEFMessages<number[]>; // Raw bytes as number[]
|
|
288
404
|
}
|
|
289
405
|
```
|
|
290
406
|
|
|
@@ -295,6 +411,68 @@ Data received from an NFC tag.
|
|
|
295
411
|
```typescript
|
|
296
412
|
interface NDEFMessages {
|
|
297
413
|
messages: NDEFMessage[];
|
|
414
|
+
tagInfo?: TagInfo;
|
|
415
|
+
}
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
#### `TagInfo`
|
|
419
|
+
|
|
420
|
+
Information about the NFC tag that was read.
|
|
421
|
+
|
|
422
|
+
```typescript
|
|
423
|
+
interface TagInfo {
|
|
424
|
+
/**
|
|
425
|
+
* The unique identifier of the tag (UID) as a hex string
|
|
426
|
+
*/
|
|
427
|
+
uid?: string;
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* The NFC tag technology types supported
|
|
431
|
+
*/
|
|
432
|
+
techTypes?: string[];
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* The maximum size of NDEF message that can be written to this tag (if applicable)
|
|
436
|
+
*/
|
|
437
|
+
maxSize?: number;
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Whether the tag is writable
|
|
441
|
+
*/
|
|
442
|
+
isWritable?: boolean;
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* The tag type (e.g., "ISO14443-4", "MifareClassic", etc.)
|
|
446
|
+
*/
|
|
447
|
+
type?: string;
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Present when the plugin downgraded capabilities for compatibility.
|
|
451
|
+
*/
|
|
452
|
+
fallback?: boolean;
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* Which fallback strategy is in use (`compat` or `ndef`).
|
|
456
|
+
*/
|
|
457
|
+
fallbackMode?: 'compat' | 'ndef';
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Reason metadata (e.g., `missing-entitlement`).
|
|
461
|
+
*/
|
|
462
|
+
reason?: string;
|
|
463
|
+
}
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
#### `StartScanOptions`
|
|
467
|
+
|
|
468
|
+
Optional tweaks for the iOS reader behavior.
|
|
469
|
+
|
|
470
|
+
```typescript
|
|
471
|
+
interface StartScanOptions {
|
|
472
|
+
mode?: 'auto' | 'full' | 'compat' | 'ndef';
|
|
473
|
+
forceFull?: boolean;
|
|
474
|
+
forceCompat?: boolean;
|
|
475
|
+
forceNDEF?: boolean;
|
|
298
476
|
}
|
|
299
477
|
```
|
|
300
478
|
|
|
@@ -324,7 +502,7 @@ interface NDEFRecord<T = number[]> {
|
|
|
324
502
|
*/
|
|
325
503
|
payload: T;
|
|
326
504
|
}
|
|
327
|
-
|
|
505
|
+
```
|
|
328
506
|
|
|
329
507
|
#### `NFCError`
|
|
330
508
|
|
|
@@ -363,54 +541,47 @@ To integrate this plugin into your Capacitor app:
|
|
|
363
541
|
Here's a complete example of how to read and write NFC tags in your app:
|
|
364
542
|
|
|
365
543
|
```typescript
|
|
366
|
-
import { NFC,
|
|
544
|
+
import { NFC, NDEFWriteOptions, NFCError, NDEFMessagesTransformable } from '@exxili/capacitor-nfc';
|
|
367
545
|
|
|
368
|
-
// Check if NFC is supported
|
|
546
|
+
// Check if NFC is supported (optional gating logic)
|
|
369
547
|
const { supported } = await NFC.isSupported();
|
|
548
|
+
if (!supported) {
|
|
549
|
+
console.warn('NFC not supported on this device');
|
|
550
|
+
}
|
|
370
551
|
|
|
371
|
-
// Start NFC scanning
|
|
372
|
-
NFC.startScan().catch((
|
|
373
|
-
console.error('Error starting NFC scan:', error);
|
|
374
|
-
});
|
|
552
|
+
// Start NFC scanning (needed on iOS only)
|
|
553
|
+
NFC.startScan().catch((err) => console.error('Failed to start scan', err));
|
|
375
554
|
|
|
376
|
-
//
|
|
377
|
-
NFC.onRead((data:
|
|
378
|
-
const
|
|
379
|
-
const
|
|
380
|
-
|
|
381
|
-
// Print all Uint8Array payloads
|
|
382
|
-
console.log('Received NFC tag:', stringMessages.messages?.at(0)?.records?.at(0).payload); // prints string[]
|
|
383
|
-
console.log('Received NFC tag:', uint8ArrayPayloads.messages?.at(0)?.records?.at(0).payload); // prints Uint8Array[]
|
|
384
|
-
});
|
|
555
|
+
// Read listener returns a transformable wrapper
|
|
556
|
+
const offRead = NFC.onRead((data: NDEFMessagesTransformable) => {
|
|
557
|
+
const textView = data.string(); // NDEFMessages<string>
|
|
558
|
+
const rawBytesView = data.uint8Array(); // NDEFMessages<Uint8Array>
|
|
385
559
|
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
console.
|
|
560
|
+
const firstText = textView.messages[0]?.records[0]?.payload;
|
|
561
|
+
const firstLength = rawBytesView.messages[0]?.records[0]?.payload.length;
|
|
562
|
+
console.log('First text record:', firstText);
|
|
563
|
+
console.log('First record byte length:', firstLength);
|
|
389
564
|
});
|
|
390
565
|
|
|
391
|
-
//
|
|
566
|
+
// Error listener (covers read & write errors)
|
|
567
|
+
const offError = NFC.onError((error: NFCError) => console.error('NFC Error:', error));
|
|
568
|
+
|
|
569
|
+
// Prepare an NDEF message to write (auto-formats Text/URI if payload is string)
|
|
392
570
|
const message: NDEFWriteOptions = {
|
|
393
571
|
records: [
|
|
394
|
-
{
|
|
395
|
-
|
|
396
|
-
payload: 'Hello, NFC!',
|
|
397
|
-
},
|
|
572
|
+
{ type: 'T', payload: 'Hello, NFC!' },
|
|
573
|
+
{ type: 'U', payload: 'https://example.com' },
|
|
398
574
|
],
|
|
399
575
|
};
|
|
400
576
|
|
|
401
|
-
|
|
402
|
-
NFC.writeNDEF(message)
|
|
403
|
-
.then(() => {
|
|
404
|
-
console.log('Write initiated');
|
|
405
|
-
})
|
|
406
|
-
.catch((error) => {
|
|
407
|
-
console.error('Error writing to NFC tag:', error);
|
|
408
|
-
});
|
|
577
|
+
await NFC.writeNDEF(message).catch((err) => console.error('Write failed', err));
|
|
409
578
|
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
579
|
+
const offWrite = NFC.onWrite(() => console.log('Write success'));
|
|
580
|
+
|
|
581
|
+
// Later (cleanup)
|
|
582
|
+
offRead();
|
|
583
|
+
offError();
|
|
584
|
+
offWrite();
|
|
414
585
|
```
|
|
415
586
|
|
|
416
587
|
## License
|