Need help upgrading to Ionic Framework 4.0? Get assistance with our Enterprise Migration Services EXPLORE NOW

NFC

Improve this doc

The NFC plugin allows you to read and write NFC tags. You can also beam to, and receive from, other NFC enabled devices.

Use to

This plugin uses NDEF (NFC Data Exchange Format) for maximum compatibilty between NFC devices, tag types, and operating systems.

Repo: https://github.com/chariotsolutions/phonegap-nfc

Installation

  1. Install the Cordova and Ionic Native plugins:
    $ ionic cordova plugin add phonegap-nfc
    $ npm install --save @ionic-native/nfc@4
    
  2. Add this plugin to your app's module

Supported platforms

Usage

import { NFC, Ndef } from '@ionic-native/nfc';

constructor(private nfc: NFC, private ndef: Ndef) { }

...

this.nfc.addNdefListener(() => {
  console.log('successfully attached ndef listener');
}, (err) => {
  console.log('error attaching ndef listener', err);
}).subscribe((event) => {
  console.log('received ndef message. the tag contains: ', event.tag);
  console.log('decoded tag id', this.nfc.bytesToHexString(event.tag.id));

  let message = this.ndef.textRecord('Hello world');
  this.nfc.share([message]).then(onSuccess).catch(onError);
});

Instance Members

FLAG_READER

beginSession(onSuccess, onFailure)

Starts the NFCNDEFReaderSession allowing iOS to scan NFC tags.

Param Type Details
onSuccess
onFailure

Returns: Observable<any>

addNdefListener(onSuccess, onFailure)

Registers an event listener for any NDEF tag.

Param Type Details
onSuccess
onFailure

Returns: Observable<any>

addTagDiscoveredListener(onSuccess, onFailure)

Registers an event listener for tags matching any tag type.

Param Type Details
onSuccess
onFailure

Returns: Observable<any>

addMimeTypeListener(mimeType, onSuccess, onFailure)

Registers an event listener for NDEF tags matching a specified MIME type.

Param Type Details
mimeType
onSuccess
onFailure

Returns: Observable<any>

addNdefFormatableListener(onSuccess, onFailure)

Registers an event listener for formatable NDEF tags.

Param Type Details
onSuccess
onFailure

Returns: Observable<any>

write(message)

Writes an NdefMessage(array of ndef records) to a NFC tag.

Param Type Details
message any[]

Returns: Promise<any>

makeReadOnly()

Makes a NFC tag read only. Warning this is permanent.

Returns: Promise<any>

share(message)

Shares an NDEF Message(array of ndef records) via peer-to-peer.

Param Type Details
message

An array of NDEF Records.

Returns: Promise<any>

unshare()

Stop sharing NDEF data via peer-to-peer.

Returns: Promise<any>

erase()

Erase a NDEF tag

handover(uris)

Send a file to another device via NFC handover.

Param Type Details
uris

A URI as a String, or an array of URIs.

Returns: Promise<any>

stopHandover()

Stop sharing NDEF data via NFC handover.

Returns: Promise<any>

showSettings()

Opens the device’s NFC settings.

Returns: Promise<any>

enabled()

Check if NFC is available and enabled on this device.

Returns: Promise<any>

bytesToString(bytes)

Param Type Details
bytes number[]

Returns: string

stringToBytes(str)

Convert string to byte array.

Param Type Details
str string

Returns: number[]

bytesToHexString(bytes)

Convert byte array to hex string

Param Type Details
bytes number[]

Returns: string

readerMode()

Read NFC tags sending the tag data to the success callback.

API

Native

General