Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to read nfc tag UID with PhoneGap? How?

I would like to read an NFC tag's UID, tags don't have any ndef message or anything (empty), just UID. It's easy to do on Android, but Phonegap development is way faster so I would like to know if it's possible to get it working on Phonegap, probably using the phonegap-nfc plugin.

like image 561
rskuja Avatar asked May 22 '12 19:05

rskuja


2 Answers

The phonagap-nfc plugin will allow you to read the tag's UID.

nfc.addTagDiscoveredListener() is good if you need the tag id. As of phonegap-nfc-0.4.0 the tag id is also included with the NDEF listeners when it is available.

You can convert the id to a hex string for display with nfc.bytesToHexString(tag.id)

function ready() {

    function onNfc(nfcEvent) {

        var tag = nfcEvent.tag;
        var tagId = nfc.bytesToHexString(tag.id);
        alert(tagId);

    }

    function win() {
        console.log("Listening for NFC Tags");
    }

    function fail(error) {
        alert("Error adding NFC listener");
    }


    nfc.addTagDiscoveredListener(onNfc, win, fail);
}

function init() {
    document.addEventListener('deviceready', ready, false);
}
like image 92
doncoleman Avatar answered Oct 21 '22 12:10

doncoleman


The answer is in your question: yes, use the NFC plugin. Add a callback for any tag with nfc.addTagDiscoveredListener()

like image 42
dda Avatar answered Oct 21 '22 12:10

dda