Search code examples
angularjscordovaphonegap-pluginsnfcngcordova

ngCordova-nfc (by using phonegap-nfc) example for angularjs project


Please help me about ngCordova-nfc (by using phonegap-nfc). I have a cordova project (no ionic/phonegap). I want to read NFC tag id by using this code. But I can't. Can you help me?

angular.module('app')
    .controller('AppCtrl', ['$scope', '$translate', '$localStorage', '$window', '$cordovaNfc', '$cordovaNfcUtil',
      function(              $scope,   $translate,   $localStorage,   $window, $cordovaNfc, $cordovaNfcUtil ) {

    // NFC
      //alert("Out of NFC");

      $cordovaNfc.then(function(nfcInstance){

            //Use the plugins interface as you go, in a more "angular" way
            nfcInstance.addNdefListener(function(event){
                //Callback when ndef got triggered
                var tag = JSON.stringify(event.tag);

                if (tag.serialNumber) {
                  $scope.tagid = tag.serialNumber;
                  tag.isWritable = !tag.isLocked;
                  tag.canMakeReadOnly = tag.isLockable;
                  alert("Hello"+$scope.tagid);
                }

              })
            .then(
            //Success callback
            function(event){
              alert("bound success");
            },
            //Fail callback
            function(err){
              alert("error");
            });
          });

      $cordovaNfcUtil.then(function(nfcUtil){
        alert( nfcUtil.bytesToString("some bytes") );
      });

Solution

  • Did You try whether your device is nfc supported or not ? and check if nfc is disabled ?? by the following piece of code.

    nfc.enabled(
      function() {alert('yes!')},
      // msg is one of NO_NFC (no hardware support) or NFC_DISABLED (supported but disabled)
      function(msg) {alert(msg)}
    );
    

    And Post the errors you got. Hope it helps you