googlearchive / chrome-nfc

Chrome App NFC Library
Other
214 stars 69 forks source link

trigger function if tag is detected #24

Open jordaofranca opened 8 years ago

jordaofranca commented 8 years ago

how can I trigger a event when the tag is detected?

shahbazali commented 7 years ago

Hi @jordaofranca

I copied this code & used it for one of my project, Try this it will keep looking for card, as its recursive.

`function haveType4Conversation() { console.info("===== JDR - Starting our Type-4 conversation =====");

console.info("Sending APDU SELECT...");
chrome.nfc.conversation(device, {step: 0}, function (hex, raw, message, check) {
    console.log("Application selected! HCE Response: " + hex);

    console.info("Sending CC SELECT...");
    chrome.nfc.conversation(device, {step: 1}, function (hex, raw, message, check) {
        console.log("CAPABILITY CONTAINER Selected! HCE Response: " + hex);

        console.info("Sending ReadBinary from CC...");
        chrome.nfc.conversation(device, {step: 2}, function (hex, raw, message, check) {
            console.log("READ CAPABILITY CONTAINER header! HCE Response: " + hex);

            console.info("Sending NDEF Select...");
            chrome.nfc.conversation(device, {step: 3}, function (hex, raw, message, check) {
                console.log("NDEF SELECT! HCE Response: " + hex);

                console.info("Sending ReadBinary NLEN...");
                chrome.nfc.conversation(device, {step: 4}, function (hex, raw, message, check) {
                    console.log("NLEN! HCE Response: " + hex);

                    console.info("Sending ReadBinary, get NDEF data...");
                    chrome.nfc.conversation(device, {step: 5}, function (hex, raw, message, check) {

                        window.clearInterval(loopInterval);

                        console.log("NDEF says: " + hex);
                        console.log("NDEF text = " + message);

                        scanMessage.textContent = message;

                        states.selected = 1;

                        setTimeout(function () {
                            states.selected = 0;
                            scanMessage.textContent = "";
                            loopInterval = setInterval(haveType4Conversation, 1750);
                        }, 5000);

                        console.info("Wrapping up, closing session.");
                    });

                });

            });

        });

    });

});

}`

Thanks