I recently purchased an EL-MF1A RFID 13.56 MHz 1K Card Tag, and I’m experimenting with creating a website that can write a link to an NFC card. Is this possible? Because when I run the website, I immediately get the message ‘NFC is not supported on this device or browser.’ I’m using the latest version of Google Chrome on Android, and my device is a Xiaomi Note 13 Pro which supports NFC.
"use client"
import { useState } from 'react';
export default function Home() {
const [nfcSupported, setNfcSupported] = useState(null);
const [message, setMessage] = useState('');
const writeToNFC = async () => {
if ('NDEFReader' in window) {
try {
const ndef = new NDEFReader();
await ndef.write({
records: [{ recordType: "url", data: "https://www.weekgenz.com/" }]
});
setMessage('Succes write NFC!');
} catch (error) {
setMessage(`Fail to write NFC: ${error}`);
}
} else {
setNfcSupported(false);
setMessage('Browser or Device not Support NFC.');
}
};
return (
<div className='h-screen w-full bg-white flex items-center justify-center'>
<div>
<h1 className='text-black ttext-center'>NFC Writer</h1>
{nfcSupported !== false ? (
<button onClick={writeToNFC} className='text-white bg-black p-4'>Write link to NFC Card</button>
) : (
<p>{message}</p>
)}
{message && <p>{message}</p>}
</div>
</div>
);
}
return (
<div className='h-screen w-full bg-white flex items-center justify-center'>
<div>
<h1 className='text-black ttext-center'>NFC Writer</h1>
{nfcSupported !== false ? (
<button onClick={writeToNFC} className='text-white bg-black p-4'>Write link to NFC Card</button>
) : (
<p>{message}</p>
)}
{message && <p>{message}</p>}
</div>
</div>
);
}
What might be causing this code to fail in writing to the NFC card?
Yes but on a very limited set of browsers
See https://developer.mozilla.org/en-US/docs/Web/API/Web_NFC_API#browser_compatibility
Basically Some Android Browsers and only from a https site.