This project has been moved to the following monorepo: capawesome-team/capacitor-plugins.
Capacitor plugin for reading and writing NFC tags.
This project is available as Sponsorware.
Sponsorware is a release strategy for open-source software that enables developers to be compensated for their open-source work with fewer downsides than traditional open-source funding models. (Source)
This means...
This project is licensed under the terms of the MIT license.
However, we kindly ask you to respect our fair use policy:
A working example can be found here: capawesome-team/capacitor-nfc-demo
Android | iOS |
---|---|
See Getting started with Insiders and follow the instructions to install the plugin.
After that, follow the platform-specific instructions in the sections Android and iOS.
This API requires the following permissions be added to your AndroidManifest.xml
before the application
tag:
<!-- To get access to the NFC hardware. -->
<uses-permission android:name="android.permission.NFC" />
<!-- The minimum SDK version that your application can support. -->
<uses-sdk android:minSdkVersion="10"/>
<!-- (Optional) This will ensure that your app appears in Google Play only for devices with NFC hardware. -->
<uses-feature android:name="android.hardware.nfc" android:required="true" />
If you want to launch your app through an NFC tag, please take a look at the Android documentation.
There you will find which changes you have to apply to your AndroidManifest.xml
(example).
Ensure Near Field Communication Tag Reading
capabilities have been enabled in your application in Xcode.
See Add a capability to a target for more information.
Finally, add the NFCReaderUsageDescription
key to the ios/App/App/Info.plist
file, which tells the user why the app needs to use NFC:
+ <key>NFCReaderUsageDescription</key>
+ <string>The app enables the reading and writing of various NFC tags.</string>
If you want to launch your app through an NFC tag, please take a look at the Core NFC documentation.
The NFC tag requires a URI record (see createNdefUriRecord(...)
) that must contain either a universal link (see Deep Linking with Universal and App Links) or a supported URL scheme.
No configuration required for this plugin.
import { Nfc, NfcUtils, NfcTagTechType } from '@capawesome-team/capacitor-nfc';
const createNdefTextRecord = () => {
const utils = new NfcUtils();
const { record } = utils.createNdefTextRecord({ text: 'Capacitor NFC Plugin' });
return record;
};
const write = async () => {
return new Promise((resolve) => {
const record = createNdefTextRecord();
Nfc.addListener('nfcTagScanned', async (event) => {
await Nfc.write({ message: { records: [record] } });
await Nfc.stopScanSession();
resolve();
});
Nfc.startScanSession();
});
};
const read = async () => {
return new Promise((resolve) => {
Nfc.addListener('nfcTagScanned', async (event) => {
await Nfc.stopScanSession();
resolve(event.nfcTag);
});
Nfc.startScanSession();
});
};
const makeReadOnly = async () => {
return new Promise((resolve) => {
Nfc.addListener('nfcTagScanned', async (event) => {
await Nfc.makeReadOnly();
await Nfc.stopScanSession();
resolve();
});
Nfc.startScanSession();
});
};
const readSignature = async () => {
return new Promise((resolve) => {
Nfc.addListener('nfcTagScanned', async (event) => {
const { response } = await Nfc.transceive({ techType: NfcTagTechType.NfcA, data: [60, 0] });
await Nfc.stopScanSession();
resolve(response);
});
Nfc.startScanSession();
});
};
const erase = async () => {
return new Promise((resolve) => {
Nfc.addListener('nfcTagScanned', async (event) => {
await Nfc.erase();
await Nfc.stopScanSession();
resolve();
});
Nfc.startScanSession();
});
};
const format = async () => {
return new Promise((resolve) => {
Nfc.addListener('nfcTagScanned', async (event) => {
await Nfc.format();
await Nfc.stopScanSession();
resolve();
});
Nfc.startScanSession();
});
};
const isSupported = async () => {
const { isSupported } = await Nfc.isSupported();
return isSupported;
};
const isEnabled = async () => {
const { isEnabled } = await Nfc.isEnabled();
return isEnabled;
};
const openSettings = async () => {
await Nfc.openSettings();
};
const checkPermissions = async () => {
const { nfc } = await Nfc.checkPermissions();
return nfc;
};
const requestPermissions = async () => {
const { nfc } = await Nfc.requestPermissions();
return nfc;
};
const removeAllListeners = async () => {
await Nfc.removeAllListeners();
};
StartScanSessionOptions
|
**Since:** 0.0.1
--------------------
### stopScanSession(...)
```typescript
stopScanSession(options?: StopScanSessionOptions | undefined) => PromiseStopScanSessionOptions
|
**Since:** 0.0.1
--------------------
### write(...)
```typescript
write(options: WriteOptions) => PromiseWriteOptions
|
**Since:** 0.0.1
--------------------
### makeReadOnly()
```typescript
makeReadOnly() => PromiseTransceiveOptions
|
**Returns:** Promise<TransceiveResult>
**Since:** 0.3.0
--------------------
### isSupported()
```typescript
isSupported() => PromisePromise<IsSupportedResult>
**Since:** 0.0.1
--------------------
### isEnabled()
```typescript
isEnabled() => PromisePromise<IsEnabledResult>
**Since:** 0.0.1
--------------------
### openSettings()
```typescript
openSettings() => PromisePromise<PermissionResult>
**Since:** 0.0.1
--------------------
### requestPermissions()
```typescript
requestPermissions() => PromisePromise<PermissionResult>
**Since:** 0.0.1
--------------------
### addListener('nfcTagScanned', ...)
```typescript
addListener(eventName: 'nfcTagScanned', listenerFunc: (event: NfcTagScannedEvent) => void) => Promise'nfcTagScanned'
|
| **`listenerFunc`** | (event: NfcTagScannedEvent) => void
|
**Returns:** Promise<PluginListenerHandle> & PluginListenerHandle
**Since:** 0.0.1
--------------------
### addListener('scanSessionCanceled', ...)
```typescript
addListener(eventName: 'scanSessionCanceled', listenerFunc: () => void) => Promise'scanSessionCanceled'
|
| **`listenerFunc`** | () => void
|
**Returns:** Promise<PluginListenerHandle> & PluginListenerHandle
**Since:** 0.0.1
--------------------
### addListener('scanSessionError', ...)
```typescript
addListener(eventName: 'scanSessionError', listenerFunc: (event: ScanSessionErrorEvent) => void) => Promise'scanSessionError'
|
| **`listenerFunc`** | (event: ScanSessionErrorEvent) => void
|
**Returns:** Promise<PluginListenerHandle> & PluginListenerHandle
**Since:** 0.0.1
--------------------
### removeAllListeners()
```typescript
removeAllListeners() => Promisestring
| A custom message, which is displayed in the NFC Reader Session alert. Only available on iOS. | | 0.0.1 |
| **`techTypes`** | NfcTagTechType[]
| The NFC tag technologies to filter on in this session. Only available on Android. | | 0.0.1 |
| **`mimeTypes`** | string[]
| Mime types to filter on in this session. Only available on Android. | | 0.0.1 |
| **`pollingOptions`** | PollingOption[]
| Type of tags to detect during a polling sequence. Only available on iOS. | [PollingOption.iso14443, PollingOption.iso15693]
| 0.2.0 |
#### StopScanSessionOptions
| Prop | Type | Description | Since |
| ------------------ | ------------------- | -------------------------------------------------------------------------------------------------- | ----- |
| **`errorMessage`** | string
| A custom error message, which is displayed in the NFC Reader Session alert. Only available on iOS. | 0.0.1 |
#### WriteOptions
| Prop | Type | Description | Since |
| ------------- | --------------------------------------------------- | -------------------------- | ----- |
| **`message`** | NdefMessage
| The NDEF message to write. | 0.0.1 |
#### NdefMessage
| Prop | Type | Description | Since |
| ------------- | ------------------------- | -------------------------------- | ----- |
| **`records`** | NdefRecord[]
| The records of the NDEF message. | 0.0.1 |
#### NdefRecord
| Prop | Type | Description | Since |
| ------------- | --------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------ | ----- |
| **`id`** | number[]
| The record identifier as byte array. | 0.0.1 |
| **`payload`** | number[]
| The payload field data as byte array. | 0.0.1 |
| **`tnf`** | TypeNameFormat
| The record type name format which indicates the structure of the value of the `type` field. | 0.0.1 |
| **`type`** | number[]
| The type of the record payload. This should be used in conjunction with the `tnf` field to determine the payload format. | 0.0.1 |
#### TransceiveResult
| Prop | Type | Description | Since |
| -------------- | --------------------- | --------------------------- | ----- |
| **`response`** | number[]
| Bytes received in response. | 0.3.0 |
#### TransceiveOptions
| Prop | Type | Description | Since |
| -------------------------- | --------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- |
| **`techType`** | NfcTagTechType
| The NFC tag technology to connect with. On Android, only `NfcTagTechType.NfcA`, `NfcTagTechType.NfcB`, `NfcTagTechType.NfcF`, `NfcTagTechType.NfcV`, `NfcTagTechType.IsoDep`, `NfcTagTechType.MifareClassic` and `NfcTagTechType.MifareUltralight` are supported. On iOS, only `NfcTagTechType.NfcF`, `NfcTagTechType.NfcV` and `NfcTagTechType.MifareDesfire` are supported. | 0.3.0 |
| **`data`** | number[]
| Bytes to send. | 0.3.0 |
| **`iso15693RequestFlags`** | Iso15693RequestFlag[]
| The request flags for the NFC tag technology type `NfcV` (ISO 15693-3). Only available on iOS 14+ | 0.3.0 |
| **`iso15693CommandCode`** | number
| The custom command code defined by the IC manufacturer for the NFC tag technology type `NfcV` (ISO 15693-3). Valid range is 0xA0 to 0xDF inclusively. Only available on iOS 14+ | 0.3.0 |
#### IsSupportedResult
| Prop | Type | Since |
| ----------------- | -------------------- | ----- |
| **`isSupported`** | boolean
| 0.0.1 |
#### IsEnabledResult
| Prop | Type | Since |
| --------------- | -------------------- | ----- |
| **`isEnabled`** | boolean
| 0.0.1 |
#### PermissionResult
| Prop | Type | Description | Since |
| --------- | ----------------------------------------------------------- | -------------------------------------------------- | ----- |
| **`nfc`** | PermissionState
| Permission state for reading and writing NFC tags. | 0.0.1 |
#### PluginListenerHandle
| Prop | Type |
| ------------ | ----------------------------------------- |
| **`remove`** | () => Promise<void>
|
#### NfcTagScannedEvent
| Prop | Type | Description | Since |
| ------------ | ----------------------------------------- | -------------------- | ----- |
| **`nfcTag`** | NfcTag
| The scanned NFC tag. | 0.0.1 |
#### NfcTag
| Prop | Type | Description | Since |
| --------------------- | --------------------------------------------------- | -------------------------------------------------------------------------------------------------- | ----- |
| **`atqa`** | number[]
| The ATQA/SENS_RES bytes of an NFC A tag. Only available on Android. | 0.0.1 |
| **`applicationData`** | number[]
| The Application Data bytes from ATQB/SENSB_RES of an NFC B tag. Only available on Android and iOS. | 0.0.1 |
| **`barcode`** | number[]
| The barcode bytes of an NfcBarcode tag. Only available on Android. | 0.0.1 |
| **`canMakeReadOnly`** | boolean
| Whether the NDEF tag can be made read-only or not. Only available on Android. | 0.0.1 |
| **`dsfId`** | number[]
| The DSF ID bytes of an NFC V tag. Only available on Android. | 0.0.1 |
| **`hiLayerResponse`** | number[]
| The higher layer response bytes of an ISO-DEP tag. Only available on Android. | 0.0.1 |
| **`historicalBytes`** | number[]
| The historical bytes of an ISO-DEP tag. Only available on Android and iOS. | 0.0.1 |
| **`id`** | number[]
| The tag identifier (low level serial number) which is used for anti-collision and identification. | 0.0.1 |
| **`isWritable`** | boolean
| Whether the NDEF tag is writable or not. Only available on Android and iOS. | 0.0.1 |
| **`manufacturer`** | number[]
| The Manufacturer bytes of an NFC F tag. Only available on Android and iOS. | 0.0.1 |
| **`maxSize`** | number
| The maximum NDEF message size in bytes. Only available on Android and iOS. | 0.0.1 |
| **`message`** | NdefMessage
| The NDEF-formatted message. | 0.0.1 |
| **`protocolInfo`** | number[]
| The Protocol Info bytes from ATQB/SENSB_RES of an NFC B tag. Only available on Android. | 0.0.1 |
| **`responseFlags`** | number[]
| The Response Flag bytes of an NFC V tag. Only available on Android. | 0.0.1 |
| **`sak`** | number[]
| The SAK/SEL_RES bytes of an NFC A tag. Only available on Android. | 0.0.1 |
| **`systemCode`** | number[]
| The System Code bytes of an NFC F tag. Only available on Android and iOS. | 0.0.1 |
| **`techTypes`** | NfcTagTechType[]
| The technologies available in this tag. Only available on Android and iOS. | 0.0.1 |
| **`type`** | NfcTagType
| The NDEF tag type. Only available on Android and iOS. | 0.0.1 |
#### ScanSessionErrorEvent
| Prop | Type | Description | Since |
| ------------- | ------------------- | ------------------ | ----- |
| **`message`** | string
| The error message. | 0.0.1 |
### Type Aliases
#### PermissionState
"denied" | "granted" | "prompt"
### Enums
#### NfcTagTechType
| Members | Value | Description | Since |
| ---------------------- | -------------------------------- | ----------------------------------------------------------------------------- | ----- |
| **`NfcA`** | 'NFC_A'
| The NFC-A (ISO 14443-3A) tag technology. Only available on Android. | 0.0.1 |
| **`NfcB`** | 'NFC_B'
| The NFC-B (ISO 14443-3B) tag technology. Only available on Android. | 0.0.1 |
| **`NfcF`** | 'NFC_F'
| The NFC-F (JIS 6319-4) tag technology. Only available on Android and iOS. | 0.0.1 |
| **`NfcV`** | 'NFC_V'
| The NFC-V (ISO 15693) tag technology. Only available on Android and iOS. | 0.0.1 |
| **`IsoDep`** | 'ISO_DEP'
| The ISO-DEP (ISO 14443-4) tag technology. Only available on Android. | 0.0.1 |
| **`Iso7816`** | 'ISO_7816'
| The ISO 7816 tag technology. Only available on iOS. | 5.1.0 |
| **`Ndef`** | 'NDEF'
| The NDEF tag technology. Only available on Android. | 0.0.1 |
| **`MifareClassic`** | 'MIFARE_CLASSIC'
| The MIFARE Classic tag technology. Only available on Android. | 0.0.1 |
| **`MifareDesfire`** | 'MIFARE_DESFIRE'
| The MIFARE Desfire tag technology. Only available on iOS. | 5.1.0 |
| **`MifarePlus`** | 'MIFARE_PLUS'
| The MIFARE Plus tag technology. Only available on iOS. | 5.1.0 |
| **`MifareUltralight`** | 'MIFARE_ULTRALIGHT'
| The MIFARE Ultralight tag technology. Only available on Android and iOS. | 0.0.1 |
| **`NfcBarcode`** | 'NFC_BARCODE'
| The technology of a tag containing just a barcode. Only available on Android. | 0.0.1 |
| **`NdefFormatable`** | 'NDEF_FORMATABLE'
| The NDEF formatable tag technology. Only available on Android. | 0.0.1 |
#### PollingOption
| Members | Value | Description | Since |
| -------------- | ----------------------- | ------------------------------------------------------------- | ----- |
| **`iso14443`** | 'iso14443'
| The option for detecting ISO 7816-compatible and MIFARE tags. | 0.2.0 |
| **`iso15693`** | 'iso15693'
| The option for detecting ISO 15693 tags. | 0.2.0 |
| **`iso18092`** | 'iso18092'
| The option for detecting FeliCa tags. | 0.2.0 |
#### TypeNameFormat
| Members | Value | Description | Since |
| ----------------- | -------------- | ------------------------------------------------------------------------------------------------------ | ----- |
| **`Empty`** | 0
| An empty record with no type or payload. | 0.0.1 |
| **`WellKnown`** | 1
| A predefined type defined in the RTD specification of the NFC Forum. | 0.0.1 |
| **`MimeMedia`** | 2
| An Internet media type as defined in RFC 2046. | 0.0.1 |
| **`AbsoluteUri`** | 3
| A URI as defined in RFC 3986. | 0.0.1 |
| **`External`** | 4
| A user-defined value that is based on the rules of the NFC Forum Record Type Definition specification. | 0.0.1 |
| **`Unknown`** | 5
| Type is unknown. | 0.0.1 |
| **`Unchanged`** | 6
| Indicates the payload is an intermediate or final chunk of a chunked NDEF Record. | 0.0.1 |
#### Iso15693RequestFlag
| Members | Value | Since |
| ------------------------- | ---------------------------------- | ----- |
| **`address`** | 'address'
| 0.3.0 |
| **`commandSpecificBit8`** | 'commandSpecificBit8'
| 0.3.0 |
| **`dualSubCarriers`** | 'dualSubCarriers'
| 0.3.0 |
| **`highDataRate`** | 'highDataRate'
| 0.3.0 |
| **`option`** | 'option'
| 0.3.0 |
| **`protocolExtension`** | 'protocolExtension'
| 0.3.0 |
| **`select`** | 'select'
| 0.3.0 |
#### NfcTagType
| Members | Value | Description | Since |
| ----------------------- | ---------------------------------- | ---------------------------------- | ----- |
| **`NfcForumType1`** | 'NFC_FORUM_TYPE_1'
| Only available on Android. | 0.0.1 |
| **`NfcForumType2`** | 'NFC_FORUM_TYPE_2'
| Only available on Android. | 0.0.1 |
| **`NfcForumType3`** | 'NFC_FORUM_TYPE_3'
| Only available on Android and iOS. | 0.0.1 |
| **`NfcForumType4`** | 'NFC_FORUM_TYPE_4'
| Only available on Android. | 0.0.1 |
| **`MifareClassic`** | 'MIFARE_CLASSIC'
| Only available on Android. | 0.0.1 |
| **`MifareDesfire`** | 'MIFARE_DESFIRE'
| | 0.0.1 |
| **`MifarePlus`** | 'MIFARE_PLUS'
| Only available on Android. | 0.0.1 |
| **`MifarePro`** | 'MIFARE_PRO'
| Only available on Android. | 0.0.1 |
| **`MifareUltralight`** | 'MIFARE_ULTRALIGHT'
| Only available on Android. | 0.0.1 |
| **`MifareUltralightC`** | 'MIFARE_ULTRALIGHT_C'
| Only available on Android. | 0.0.1 |
See docs/utils/README.md.
See CHANGELOG.md.
See LICENSE.