konstantinkrassmann / ngCordova-nfc

Wrapper for the NFC functionality
MIT License
11 stars 4 forks source link

How to remove NdefListener #10

Closed playUruguay closed 7 years ago

playUruguay commented 7 years ago

Hi, i can read NFC tags ok, but i want to turn off the listeners to prevent repeats Here's my controller code:

$cordovaNfc.then(function(nfcInstance){
    vm.data.nfcActive = true;
    nfcInstance.addNdefListener(function(event){
        vm.data.payload = $filter('decodePayload')(event.tag.ndefMessage[0]);

        nfcInstance.removeNdefListener(
               function(event){
                    alert('removeNdefListener 1')
                },
                function(event){
                    alert('removeNdefListener 2')
                },
                function(event){
                    alert('removeNdefListener 3')
                }
        );
    })
    .then(
            function(success){
                vm.data.status = 'success';
            },
            function(err){
                vm.data.status = 'error';
            }
     );
});
konstantinkrassmann commented 7 years ago

Refer to https://github.com/chariotsolutions/phonegap-nfc#nfcremovendeflistener

You have to pass EXACTLY the function you passed to the addNdefListener

var listener = function(event){
        vm.data.payload = $filter('decodePayload')(event.tag.ndefMessage[0]);

        nfcInstance.removeNdefListener(listener);
    };
nfcInstance.addNdefListener(listener )