The NFC API allows you to read and write NFC tags. You can also beam to, and receive from, other NFC enabled devices.
Use to
This API uses NDEF (NFC Data Exchange Format) for maximum compatibilty between NFC devices, tag types, and operating systems.
Registers an event listener for any NDEF tag.
addNdefListener(options: callback): Promise <any>
Description:
Removes the previously registered event listener for NDEF tags added via addNdefListener.
removeNdefListener(option: callback): Promise <any>
Removing listeners is not recommended. Instead, consider that your callback can ignore messages you no longer need.
Registers an event listener for tags matching any tag type.
addTagDiscoveredListener(option: callback): Promise <any>
Description
Registers an event listener for NDEF tags matching a specified MIME type.
addMimeTypeListener(mimeType: string, call: callback): Promise <any>
Removing listeners is not recommended. Instead, consider that your callback can ignore messages you no longer need.
Description
nfc.addMimeTypeListener("text/json", *onNfc*); nfc.addMimeTypeListener("text/demo", *onNfc*);
Removes the previously registered event listener added via addMimeTypeListener.
removeMimeTypeListener(mimeType: string, option: callback): Promise <any>
Removing listeners is not recommended. Instead, consider that your callback can ignore messages you no longer need.
Registers an event listener for formatable NDEF tags.
addNdefFormatableListener(option: callback): Promise <any>
Description
Writes an NDEF Message to a NFC tag.
A NDEF Message is an array of one or more NDEF Records
write(message: string): Promise <any>
Description
Makes a NFC tag read only. Warning this is permanent.
makeReadOnly(): Promise <any>
Description
Shares an NDEF Message via peer-to-peer.
A NDEF Message is an array of one or more NDEF Records
share(message: string): Promise <any>
Description
Stop sharing NDEF data via peer-to-peer.
unshare(): Promise <any>
Description
Erase a NDEF tag
erase(): Promise <any>
Description
Send a file to another device via NFC handover.
handover(uri: string): Promise <any>
Description
Stop sharing NDEF data via NFC handover.
stopHandover(): Promise <any>
Description
Check if NFC is available and enabled on this device.
enabled(): Promise <any>
Description
Show the NFC settings on the device.
showSettings(): Promise <any>
Description
Read NFC tags sending the tag data to the success callback.
readerMode(flags: flagReader, option: readCallback): Promise <any>
Description
In reader mode, when a NFC tags is read, the results are returned to read callback as a tag object. Note that the normal event listeners are not used in reader mode. The callback receives the tag object without the event wrapper.
Foreground dispatching and peer-to-peer functions are disabled when reader mode is enabled.
The flags control which tags are scanned. One benefit to reader mode, is the system sounds can be disabled when a NFC tag is scanned by adding the nfc.FLAG_READER_NO_PLATFORM_SOUNDS flag.
Disable NFC reader mode.
disableNfcReaderMode(): Promise <any>
The tag technology functions provide access to I/O operations on a tag. Connect to a tag, send commands with transceive, close the tag.
const DESFIRE_SELECT_PICC = '00 A4 04 00 07 D2 76 00 00 85 01 00'; const DESFIRE_SELECT_AID = '90 5A 00 00 03 AA AA AA 00' async function handleDesfire(nfcEvent) { const tagId = nfc.bytesToHexString(nfcEvent.tag.id); console.log('Processing', tagId); try { await nfc.connect('nfc.tech.IsoDep', 500); console.log('connected to', tagId); let response = await nfc.transceive(DESFIRE_SELECT_PICC); ensureResponseIs('9000', response); response = await nfc.transceive(DESFIRE_SELECT_AID); ensureResponseIs('9100', response); // 91a0 means the requested application not found alert('Selected application AA AA AA'); // more transcieve commands go here } catch (error) { alert(error); } finally { await nfc.close(); console.log('closed'); } } function ensureResponseIs(expectedResponse, buffer) { const responseString = util.arrayBufferToHexString(buffer); if (expectedResponse !== responseString) { const error = 'Expecting ' + expectedResponse + ' but received ' + responseString; throw error; } }
Connect to the tag and enable I/O operations to the tag from this TagTechnology object.
connect(tech: string, timeout?: int): Promise <any>
Send raw command to the tag and receive the response.
transceive(data: string): Promise <any>
The ndef object provides NDEF constants, functions for creating NdefRecords, and functions for converting data.
Represents an NDEF (NFC Data Exchange Format) data message that contains one or more NdefRecords. This API uses an array of NdefRecords to represent an NdefMessage.
Represents a logical (unchunked) NDEF (NFC Data Exchange Format) record.
The ndef object has a function for creating NdefRecords
var type = "text/pg", id = [], payload = nfc.stringToBytes("Hello World"), record = ndef.record(ndef.TNF_MIME_MEDIA, type, id, payload);
There are also helper functions for some types of records
Create a URI record
var record = ndef.uriRecord("http://chariotsolutions.com");
Create a plain text record
var record = ndef.textRecord("Plain text message");
Create a mime type record
var mimeType = "text/pg", payload = "Hello Phongap", record = ndef.mimeMediaRecord(mimeType, nfc.stringToBytes(payload));
Create an Empty record
var record = ndef.emptyRecord();
Create an Application Record (AAR)
var record = ndef.applicationRecord('com.example');
The Ndef object has functions to convert some data types to and from byte arrays.
Events are fired when NFC tags are read. Listeners are added by registering callback functions with the nfc object. For example addNdefListener(myNfcListener, win, fail);
The tag contents are platform dependent.
id and techTypes may be included when scanning a tag
{ type: 'ndef', tag: { "isWritable": true, "id": [4, 96, 117, 74, -17, 34, -128], "techTypes": ["nfc.tech.IsoDep", "nfc.tech.NfcA", "nfc.tech.Ndef"], "type": "NFC Forum Type 4", "canMakeReadOnly": false, "maxSize": 2046, "ndefMessage": [{ "id": [], "type": [116, 101, 120, 116, 47, 112, 103], "payload": [72, 101, 108, 108, 111, 32, 80, 104, 111, 110, 101, 71, 97, 112], "tnf": 2 }] } }
The raw contents of the scanned tags are written to the log before the event is fired. Use adb logcat.
You can also log the tag contents in your event handlers. console.log(JSON.stringify(nfcEvent.tag)) Note that you want to stringify the tag not the event to avoid a circular reference.
interface readCallback { "isWritable": boolean, "id": array, "techTypes": array, "type": string, "canMakeReadOnly": boolean, "maxSize": int, "ndefMessage": [{ "id": array, "type": array, "payload": array, "tnf": int }] }
enum Direction { FLAG_READER_NFC_A: 0x1 FLAG_READER_NFC_B: 0x2 FLAG_READER_NFC_F: 0x4 FLAG_READER_NFC_V: 0x8 FLAG_READER_NFC_BARCODE: 0x10 FLAG_READER_SKIP_NDEF_CHECK: 0x80 FLAG_READER_NO_PLATFORM_SOUNDS: 0x100 }