NFC
Version and platform compatibility
  • NFC was introduced in version 4.47.0 of the JourneyApps Container.
  • Only reading of tags is supported on iOS. Android supports reads and writes.
  • The Windows container does not have any NFC support currently.

Reading Tags

On Android, tags can be read without explicit user action. On iOS, foreground mode must be triggered specifically.
function init() {
startListening();
}
function resume(from) {
startListening();
}
function scan() {
if (nfc.info().supportsTrigger) {
// iOS
nfc.triggerForegroundRead();
} else {
// No need for the trigger on Android.
dialog('NFC', 'Hold the device against the NFC tag to scan');
}
}
var lastScan = 0;
var busyScan = false;
function startListening() {
if (nfc && nfc.listen) {
console.log('Starting to listen for tags...');
nfc.listen(function(tag) {
// Prevent scanning multiple times in quick succession,
// or opening multiple dialogs over each other.
if (Date.now() - lastScan > 1000 && !busyScan) {
busyScan = true;
try {
console.log('Scanned', JSON.stringify(tag));
// Dialog just for development purposes
if (tag.records.length == 0) {
dialog('Empty Tag', JSON.stringify(tag));
view.scanned_text = null;
} else {
dialog('Tag', JSON.stringify(tag));
if (tag.records[0].text) {
view.scanned_text = tag.records[0].text;
} else {
view.scanned_text = null;
}
}
} finally {
busyScan = false;
lastScan = Date.now();
journey.forceDigest();
}
}
});
}
}

Writing Tags

Platform compatibility
Writing tags is currently only supported on Android.
function write(message) {
if (nfc.info().supportsWrite) {
console.log("Writing tag...");
// nfc.write is an asynchronous method that returns a Promise.
// To block on the write, return the Promise.
nfc.write(message).then(function() {
startListening(); // read listener must be started again after write
dialog("NFC", "Tag written");
journey.forceDigest();
}, function(error) {
startListening(); // read listener must be started again after write
console.log("Tag write error:", error);
dialog("NFC Write Error", error);
journey.forceDigest();
});
dialog('NFC', 'Hold the device against the NFC tag to write');
} else {
dialog('NFC', 'Tag writing not supported');
}
}
function writeText() {
var message = [
nfc.ndef.textRecord(view.text_to_write),
nfc.ndef.textRecord('Date: ' + Date.now().toString()),
];
write(message);
}
function writeUri() {
var message = [
nfc.ndef.uriRecord("https://www.journeyapps.com"),
];
write(message);
}
function writeEmpty() {
var message = [
nfc.ndef.emptyRecord()
];
write(message);
}
Copy link
On this page
Reading Tags
Writing Tags