Closed jdelibas closed 8 years ago
The current plugin initialization is pretty kludgy. I think there's a better way with Cordova 3.x but I need to look into it more.
For now, see if you can patch phonegap-nfc.js. Fire an event in the success callback for init()
If that works for you, send a pull request
Hey any progress on this? Don's suggestion help?
@mikeljames I'm using phonegap-nfc with iconic without modification
I create an NFC service that I inject into the main controller so NFC works on every page. I think the $iconicPlatform.ready
implementation has changed since @jdelibas submitted this issue.
angular.module('services', [])
.factory('nfcService', function ($rootScope, $ionicPlatform) {
$ionicPlatform.ready(function () {
var onNfc = function (nfcEvent) {
// read or write NFC tag here
};
// Listen for NDEF tags
nfc.addNdefListener(
onNfc,
function () {
console.log("Listening for NDEF tags.");
},
function (error) {
console.log("addNdefListener: " + error)
}
);
});
});
@don , i am newbie in Ionic, I tried to use your example in my project but i can´t do that. Is it possible to create an codepen or jsfiddle example and share with us?
Thanks.
Hi Don thanks for this. I have since created something very similar. I'll give your one a try. My example below is likely to change. At the moment the requirement is just to read a tag.
` angular.module('angular-ionic-nfc',['ionic']) .factory('NfcService', ['$window', '$q', '$rootScope', '$ionicPlatform', function($window, $q, $scope, $ionicPlatform){ var onTouch = function(){
var deferred = $q.defer();
$ionicPlatform.ready(function(){
if($window.nfc){
$window.nfc.addNdefListener(function(){
deferred.resolve(event.tag);
}, function(){
deferred.notify('added listner for NFC');
}, function(){
deferred.reject('failed to attach NFC event handler');
});
}else{
deferred.reject('NFC Global Object does not exist');
}
});
return deferred.promise;
};
return {
onTouch: onTouch
};
}]);`
@mikeljames thank you for sharing, how do you call the factory in a controller?
Is it possible share how to add the NFC plugin in the project.
I am very newbie..... :(
I'd like to create an Ionic NFC example but I haven't had time yet. In the meantime you could checkout this very simple Angular NFC project
@gdpfran to use the factory/service within your controller firstly import the module to your app.
angular.module('someapp',['dependencyOnModule1','someapp.controllers', 'angular-ionic-nfc']).config(...
angular.module('someapp.controllers', function(NfcService){
NfcService.onTouch().then(function(data){
}, function(error){
}, function(update){
});
});
I have been refactoring this sample, I may have time to push this out as a gist later today
Here's an example app using Ionic and phonegap-nfc https://github.com/don/ionic-nfc-reader
Maybe this is helpful:
Im using Ionic and have created an angularjs service with the nfc plugin functions.
The problem is that the Ionic loads before the NFC plugin does and when it tries to create the angular service which contains the
nfc.addMimeTypeListener
function it throws an undefined error because the plugin still hasnt loaded.This is what the cordova log looks like
As you can see from the log
If there was a custom event in
handleNfcFromIntentFilter()
where theconsole.log()
is then the plugin load could be confirmed like so