How to Write a Text Record
Introduction to Writing Text
Writing a simple text record is one of the most basic and useful functions of Web NFC. This can be used for notes, identifiers, or simple messages that can be read by any NFC-enabled device.
Code Example
To write a text record, you create an instance of NDEFReader and call its write() method with a record of type 'text'. The data for this record is the string you want to write.
async function writeText(text) {
if (!('NDEFReader' in window)) {
console.log('Web NFC is not supported by this browser.');
return;
}
try {
const writer = new NDEFReader();
await writer.write({
records: [{ recordType: 'text', data: text }]
});
console.log('Text record written successfully!');
} catch (error) {
console.error('Error writing text record:', error);
}
}
// Example usage:
// writeText('Hello, World!');Use Cases
- Storing a simple ID or serial number.
- Leaving a digital note on a physical object.
- Providing a small piece of information without needing a network connection.