Reading NFC tags for React Native (Android only)
This project has the goal of making it easy (or easier) to scan NFC tags and read the NDEF records they contain.
To read the NDEF data it makes use of the library ndef-tools-for-android.
Requirements
This library is compatible and was tested with React Native projects with version >= 0.40.0
Installation
Install the plugin via NPM:
$ npm install react-native-nfc --save
and then link it:
$ react-native link react-native-nfc
Configuration
Take a moment to read this Android documentation about NFC Basics, especially the How NFC Tags are Dispatched to Applications section.
Edit the file AndroidManifest.xml
Add the permission to read NFC data:
Add the following attribute to your <activity>
section to ensure that all NFC intents are delivered to the same activity.
android:launchMode="singleTask"
Add the following intent filters and metadata tag to instruct Android that you want to catch NFC intents that contain NDEF records and generic payloads about NFC tech, as a fallback in case NDEF messages could not be parsed (see here for more info about this).
Create the file android/src/main/res/xml/nfc_tech_filter.xml
and add the following content:
android.nfc.tech.IsoDep android.nfc.tech.NfcA android.nfc.tech.NfcB android.nfc.tech.NfcF android.nfc.tech.NfcV android.nfc.tech.Ndef android.nfc.tech.NdefFormatable android.nfc.tech.MifareClassic android.nfc.tech.MifareUltralight
Example AndroidManifest.xml
Usage
What you need to do is to register a listener on the NFC module, like this:
function listener(payload){
// TODO
}
NFC.addListener(listener);
This is a more complex example:
;;; NFC; // ... the rest of the app code
Notice: Once you've integrated the plugin in this way you'll be able to receive the data read via NFC by your Android device. You will receive the data even if your app is closed (or killed) and is started as a consequence of a NFC event. If you want to receive the data in a given time,just change the position where you addListener to NFC,such as doing it in the componentDidMount in a page of your program.
; { superprops; } { return ... ; } { this; } { NFC }
The listener receives a JSON object that has a type property with possible values:
- NfcDataType.NDEF - if the NFC tag contains NDEF data
- NfcDataType.TAG - if the NFC tag did not contain NDEF data (or could not be parsed) hence we get just info about the TAG
NDEF Payload format
Property | Values |
---|---|
type | Always NfcDataType.NDEF |
id | The id of the tag in hex format. |
data | Contains an array of messages. Each message is an array of records. |
NDEF Records format
Each record object contains always the properties type and data.
Here is the list of currently supported records:
Type | Data | Other properties |
---|---|---|
NdefRecordType.TEXT | The text string | encoding and locale |
NdefRecordType.URI | The URI string | - |
NdefRecordType.MIME | Base64 data of the mime data | - |
TAG Payload format
Property | Values |
---|---|
type | Always NfcDataType.TAG |
techList | List of strings about the discoverred tech |
description | string description of the tag useful for debug |
TODO
- Support more record types
- Support writing tags
- Advanced NFC operations