React project to scan and write into NFC tags using the Web NFC API
REACT NFC Sample App
This is a simple sample app demostrating the usage of the Web NFC API.
To get the Web NFC API working you will need an Android Device with Google Chrome and your web app will need to be hosted using https.
This is the sample app in action.
This project was bootstrapped with Create React App.
If you want to see an Angular app using NFCs look at this video example and visit this repo by the awesome Wassim. Thanks to him I was introduced to the Web NFC API.
WTF is NFC?
NFC stands for Near-Field Communication. NFC is a set of communication protocols for communication between two electronic devices.
Electromagnetic fields can be used to transmit data or induce electrical currents in a receiving device. Passive NFC devices draw power from the fields produced by active devices, but the range is short.
You can buy NFC Tags on Amazon. These tags can contain 540KB of data.
Usages
NFCs can have multiple usages, some of the usages are:
- Making contactless payments like Google and Apple Pay
- Opening a door using your badge
- Opening a link
- Product control in a warehouse
To learn about the usages visit this forum.
Getting Started with the Web NFC API
This project uses 4 methods of the Web NFC API
-
Scan: Returns a Promise resolved if starting NFC scan was successful.
ndef.scan()
-
Reading: An event fired when a new reading is available.
ndef.onreading()
-
Reading Error: An event fired when an error happened during reading.
ndef.onreadingerror()
-
Write: Returns a Promise resolved if writing the message (String, ArrayBuffer or NDEF record) with options was successful.
ndef.write()
Using the Web NFC API methods
Scan, Reading, Reading Error
const scan = async() =>
if ("NDEFReader" in window) {
try {
const ndef = new window.NDEFReader();
await ndef.scan();
console.log("Scan started successfully.");
ndef.onreadingerror = () => {
console.log("Cannot read data from the NFC tag. Try another one?");
};
ndef.onreading = (event) => {
console.log("NDEF message read.");
onReading(event); //Find function below
};
} catch (error) {
console.log(`Error! Scan failed to start: ${error}.`);
}
}
};
The onReading method grabs the message and serial number inside of the NFC tag, the uses the array of reacord inside of the message and decodes the information so its readable to humans.
const onReading = ({message, serialNumber}) => {
console.log(serialNumber);
for (const record of message.records) {
switch (record.recordType) {
case "text":
const textDecoder = new TextDecoder(record.encoding);
console.log("Message": textDecoder.decode(record.data));
break;
case "url":
// TODO: Read URL record with record data.
break;
default:
// TODO: Handle other records with record data.
}
}
};
Write
const onWrite = () => {
try {
const ndef = new window.NDEFReader();
await ndef.write({
records: [{ recordType: "text", data: "Hellow World!" }],
});
console.log(`Value Saved!`);
} catch (error) {
console.log(error);
}
};