@meysammahfouzi wrote:
I am trying to write nfc data using phonegap-nfc plugin. Here is the
factoryI have:.factory('nfcService', function ($rootScope, $ionicPlatform) { var tag = {}; $ionicPlatform.ready(function() { nfc.addTagDiscoveredListener(function (nfcEvent) { console.log(JSON.stringify(nfcEvent.tag, null, 4)); $rootScope.$apply(function(){ angular.copy(nfcEvent.tag, tag); // if necessary $state.go('some-route') }); }, function () { console.log("Listening for tags."); }, function (reason) { alert("Error adding NFC Listener " + reason); }); nfc.addMimeTypeListener('', function (nfcEvent) { console.log(JSON.stringify(nfcEvent.tag, null, 4)); $rootScope.$apply(function(){ angular.copy(nfcEvent.tag, tag); // if necessary $state.go('some-route') }); }); }); return { tag: tag, clearTag: function () { angular.copy({}, this.tag); } }; })And here is my controller:
.controller('NfcCtrl', function($scope, nfcService){ console.log('inside NfcCtrl'); console.log(JSON.stringify(nfcService)); nfcService.then(function(nfcInstance) { nfcInstance.write = function() { console.log('writing nfc...'); console.log(JSON.stringify(nfcService)); var message = [ ndef.textRecord("hello, world"), ndef.uriRecord("http://github.com/chariotsolutions/phonegap-nfc") ]; console.log('trying to write nfc message...'); nfcService.write(message, function (success) { console.log("Wrote data to tag."); }, function (reason) { console.log("There was a problem " + reason); }); }; }); })I've also included the required javascript file in the
index.htmlfile:<script src="lib/ngCordova-nfc/nfc.js"></script>The
NfcPlugininitializes successfully, but I am getting the following error:0 312690 log Initialized the NfcPlugin 1 312825 log inside NfcCtrl 2 312826 log {"tag":{}} 3 312846 error TypeError: Object #<Object> has no method 'then' at new <anonymous> (http://192.168.1.105:8100/js/controllers.js:28:16) at Object.invoke (http://192.168.1.105:8100/lib/ionic/js/ionic.bundle.js:17762:17) at extend.instance (http://192.168.1.105:8100/lib/ionic/js/ionic.bundle.js:22311:34) at nodeLinkFn (http://192.168.1.105:8100/lib/ionic/js/ionic.bundle.js:21421:36) at compositeLinkFn (http://192.168.1.105:8100/lib/ionic/js/ionic.bundle.js:20853:13) at nodeLinkFn (http://192.168.1.105:8100/lib/ionic/js/ionic.bundle.js:21457:24) at compositeLinkFn (http://192.168.1.105:8100/lib/ionic/js/ionic.bundle.js:20853:13) at publicLinkFn (http://192.168.1.105:8100/lib/ionic/js/ionic.bundle.js:20728:30) at self.appendViewElement (http://192.168.1.105:8100/lib/ionic/js/ionic.bundle.js:56890:5) at Object.switcher.render (http://192.168.1.105:8100/lib/ionic/js/ionic.bundle.js:54995:41), <ion-nav-view name="side-menu21" class="view-container" nav-view-transition="android"> 4 312894 log Listening for tags.How can I access the nfc object to call its
writemethod?
Posts: 1
Participants: 1