chariotsolutions / phonegap-nfc

PhoneGap NFC Plugin
MIT License
706 stars 559 forks source link

destroy listeners #118

Closed JohnMcLear closed 8 years ago

JohnMcLear commented 10 years ago

Hey @Don I'd like a way to destroy the listeners.

The reason for this is that on my write functionality I'd like to use addTagDiscoveredListener but on my read page I'd like addNdefListener

My app is a single page app, so to do this I'd need to destroy the listener..

Any idea on best practice?

don commented 10 years ago

For Android you're pretty much stuck with the listeners, they're difficult to remove.

nfc.removeNdefListener() is undocumented but implemented for Windows Phone 8 and BlackBerry 7. (The calls will fail on Android and BB10.)

As for best practices, I'd add both listeners all the time. Android will call the most specific listener. If the tag is NDEF it ends up int the NDEF listener. All other tags end up with TagDiscovered. The listener for Tag Discovered can always attempt to write the tag. The listener for NDEF will need to check if it's in read or write mode and do the right thing. But in either case, only one listener gets called on Android.

Is there some code I can see? (Email me)

JohnMcLear commented 10 years ago

Ah okay I will add both listeners, hehe, kinda ghetto tho ;)

mork80 commented 10 years ago

Sorry to resume this issue. Still have doubts on Android on using addMimeTypeListener of this plugin. I submit two scenarios that can happen using a NFC-enabled app. 1) NFC option is OFF, user launch the app, addListener throws an error, user get back in options set NFC option ON and resume app, i execute addListener again, user can read a tag:=> all is semantically correct 2) NFC option is ON, user launch the app, addListener is lauched properly, user get back in options set NFC option OFF and resume the app, , user can't read a tag and doesn't get any notice => ???

What's the best practice in case 2 ? Can i launch the same listener again, without destroying it before ? I know the case 2 is rare but it can happen for unexperienced users.

don commented 10 years ago

Adding the same listener again isn't recommended. It think it will cause problems.

All the NFC calls will return an error if the phone doesn't have NFC or NFC is disabled. There is no specific call to check if NFC is enabled. Maybe I could add one in the future.

Until then, you could try and make a NFC api call on resume and if that fails do something. I don't like the code, but here's the hello world cordova app modified check if NFC is enabled on resume.

var app = {
    initialize: function() {
        this.bindEvents();
    },
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
        document.addEventListener('resume', this.onResume, false);        
    },
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    },
    onResume: function(event) {
        nfc.share([ndef.textRecord("This is a hack.")], 
            function() {
                nfc.unshare(); // unshared if a peer reads this
            },
            function(error) {
                if (error === "NO_NFC") {
                    alert("This device does not have NFC.");
                } else if (error === "NFC_DISABLED") {
                    alert("NFC is disabled, please enable NFC in the setting panel.");
                } else {
                    alert("There was a problem " + error);
                }
            }
        );
    }
};
mork80 commented 10 years ago

Cool, that's the same workaround i've used....and the same fake function call ....:)))) Thank you for fast reply

snjak commented 9 years ago

Is there a solution for removing listeners on android yet?

don commented 9 years ago

There are methods for removing listeners on Android, but it's not recommended. I suggest leaving the listener and ignoring the event of you don't want to process the message.