NFC

Version and platform compatibility

  • NFC support was introduced in version 4.47.0 of the JourneyApps Container.

  • NFC is currently only supported on Android and iOS:

    • An Android, reading tags and writing tags is supported since JourneyApps Container version 4.47.0

    • On iOS, reading tags is supported since JourneyApps Container version 4.47.0, and writing tags is supported since iOS 13 and Container version 22.12.2.

Reading Tags

On Android, tags can be read without explicit user action. On iOS, foreground mode must be triggered specifically.

main.js
function init() {
    startListening();
}

function resume(from) {
    startListening();
}

function scan() {
    if (nfc.info().supportsTrigger) {
        // iOS
        nfc.triggerForegroundRead();
    } else {
        // No need for the trigger on Android.
        notification.info('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) {
                        journey.dialog.simple({
                            title: 'Empty Tag', 
                            message: JSON.stringify(tag)
                        });
                        view.scanned_text = null;
                    } else {
                        journey.dialog.simple({
                            title: 'Tag', 
                            message: JSON.stringify(tag)
                        });
                        if (tag.records[0].text) {
                            view.scanned_text = tag.records[0].text;
                            journey.dialog.simple({
                                title: 'Tag', 
                                message: view.scanned_text
                            });
                        } else {
                            view.scanned_text = null;
                        }
                    }
                } finally {
                    busyScan = false;
                    lastScan = Date.now();
                    journey.forceDigest();
                }
            }
        });
    }
}

Writing Tags

main.js
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
            notification.success("Tag written");
            journey.forceDigest();
        }, function(error) {
            startListening(); // read listener must be started again after write
            console.log("Tag write error:", error);
            journey.dialog.simple({
                title: 'NFC Write Error', 
                message: error
            });
            journey.forceDigest();
        });

        journey.dialog.simple({
            title: 'NFC', 
            message: 'Hold the device against the NFC tag to write'
        });
    } else {
        journey.dialog.error({
            title: 'NFC', 
            message: '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);
}

Last updated