konstantinkrassmann / ngCordova-nfc

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

Missing API delegate nfc.enabled() for feature detection #3

Closed Phoscur closed 9 years ago

Phoscur commented 9 years ago

Also, please use a real world example for the readme if possible

konstantinkrassmann commented 9 years ago

You are right. Added it in 80b3180bbe0a8700932ad199aecd3033e3397c4a , thank you for the hint!

About the example: feel free to add a PR.

I've wrapped the $cordovaNfc into an angular service, which simply provides a function to pass a function and it will attach it as a nfc listener, because i use the mime type listener only. With that, not every single controller needs to carry this load of code:


app.service("NFCService", function ($cordovaNfc, $cordovaNfcUtil) {
   var $nfcInstance; 
   var $nfcUtilInstance;

   var ERROR = {
        "NFC_DISABLED":"NFC_DISABLED",
        "NO_NFC":"NO_NFC"
    };

    this.attachNFCListener = function(fnListener) {
        $cordovaNfc.then(function (nfcInstance) {
            $nfcInstance = nfcInstance;
            //then wait for nfc util to be ready
            return $cordovaNfcUtil;
        })
            .then(function (nfcUtilInstance) {
                $nfcUtilInstance = nfcUtilInstance;

                $nfcInstance.addMimeTypeListener(
                    "Your own MIME type",
                    fnListener
                )
                    .then(function () {
                        //console.log("Successful appended Nfc listener")
                    },
                    function (strError) {
                        if (strError == ERROR.NFC_DISABLED)
                        {
                            //nfc disabled
                        }else if(strError == ERROR.NO_NFC){
                           //No nfc available
                        }
                        console.error("NFCService:Failed appending nfc listener", arguments)
                    });
            });
    };

    return this;
});
Phoscur commented 9 years ago

I tried

    var service = {active: false, status: 'NO_NFC', share: function() {}, listen: function() {}};
      $cordovaNfc.then(function (nfc) {
        nfc.enabled(function () {
          service.active = true;
          service.status = 'NFC_ENABLED';
          console.log('NFC enabled and ready');
        }, function (status) {
          console.log('NFC status', status);
          service.status = status; // NFC_DISABLED | NO_NFC
        });

But I do not get any output at all no matter the real NFC state.

Phoscur commented 9 years ago

Please reopen until fixed.