Reading NFC tags for React Native (Android and iOS)
This project has the goal of making it easy (or easier) to scan NFC tags and read the NDEF records they contain. This is forked from the project react-native-rfid-nfc by SMARTRACTECHNOLOGY.
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-rfid-nfc-scanner --save
and then link it:
$ react-native link react-native-rfid-nfc-scanner
iOS Configuration
info.plist
add the following to info.plist
NFCReaderUsageDescriptionNFC NDEF Reading.
YourAppName.entitlements
The following entry should be created automatically in your YourAppName.entitlements
file once you enable NFC capability
in your app but if not add the following to YourAppName.entitlements
com.apple.developer.nfc.readersession.formats NDEF
Note: If you are having issues getting this to run and you don't have any swift files in your project, just create a new one as simple as this to get the application building with the swift packages (weird bug found while developing)
//// SwiftFile.swift// MyAppName//// Created by User on Date.// Copyright © 2018 Facebook. All rights reserved. import Foundation
Android Configuration
Take a moment to read this Android documentation about NFC Basics, especially the How NFC Tags are Dispatched to Applications section.
The NFC scanner runs in the foreground for in app scanning.
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"
Example AndroidManifest.xml For Foreground Scanning (In App Scanning)
Usage
There are 2 ways to use this component. You can use the controller or the underlying scanner object.
The NdefRfidScanner class
To use the NfcRfidScanner class import and create one:
; const scanner = ; scanner
This will register a method and error handler to run when an RFID card is scanned
Scanner API (Using scanner created above)
scanner
- starts the scanner
scanner
- tells you if the device can use an RFID scanner
scanner
- tells you loading status of the device RFID scanner
scanner
Accessing original NFC Object (Expanded and unified for use with Android and iOS)
The underlying object this was built on is still accessible with
;
You can still access the NFC api which has also been expanded.
NFC;
- Start the scanner (For both android and ios)
NFC;
- Stops the scanner (For android)
NFC;
- tells you if the device can use an RFID scanner
NFC;
- Gets the device status (Described below)
NFC;
- add a scan listener
NFC;
- Remove a specific user (Fixed in iOS)
NFC;
- Remove all scan listeners
Statuses
- waiting - Attempting to start up the NFC scanner
- ready - Scanner is ready to use
- missing - The Scanner attempted to start but could not create a reading session
- unavailable - The scanner is not available on this version of the OS
Response
Upon a successful scan, the callback will recieve a payload in the following format:
IOS Response Sample
Android Response Sample
NFC-V/Other Tags on Android
Error Response
If an error handler is provided the response will look like this:
TODO
- Support more record types in iOS
- Support writing tags
- Advanced NFC operations