NFC listeners doesn't work

Hi,

i am working on a NFC-Reader app. I already use ng-cordova for some device features like the camera.
Now i want to add the nfc-listeners into the ng-cordova.js.

ng-cordova.js:


angular.module(‘ngCordova.plugins’, [
‘ngCordova.plugins.deviceMotion’,

‘ngCordova.plugins.cordovaNfc’,

‘ngCordova.plugins.prefs’
]);


angular.module(‘ngCordova.plugins.cordovaNfc’, [])
.factory(’$cordovaNfc’, [function () {
console.log(‘init $cordovaNfc!’);
return{
registerHandler: function() {
nfc.addNdefFormatableListener(
function (ndefTable) {
alert(“ndefTable”);
return ndefTable.tag.id;
}
);
nfc.addNdefListener(
function (nfcEvent) {
alert(“nfcEvent”);
return nfcEvent.tag.id;
}
);
nfc.addMimeTypeListener(
function (mimeType) {
alert(“mimeType”);
return mimeType.tag.id;
}
);
nfc.addTagDiscoveredListener(
function (tagDiscover) {
alert(“tagDiscover”);
return tagDiscover.tag.id;
}
);
}
}
}]);

I initiate this code in the app.js:


.run(function($ionicPlatform, $timeout, $cordovaNfc) {
$ionicPlatform.ready(function() {
console.log(‘Platform ready!’);

$cordovaNfc.registerHandler;

if(window.StatusBar) {
  // org.apache.cordova.statusbar required
  StatusBar.styleDefault();
}

});
});

The JS-Logs tells me this:

GET http://localhost:8100/cordova.js 404 (Not Found) localhost/:22
init $cordovaNfc! ng-cordova.js:1402
Platform ready! app.js:51

So I think it wont work because the cordova platform isn’t ready.

Did someone already solved this kind of problem?

Just curious what device are you getting these errors?

Hi,

here are the device details:

device: HTC One

Android-Version: 4.4.2

I don’t think it’s relevant but…

HTC Sense Version: 5.5

HTC SDK API level: 5.69

These are the devices for my next tests:

  • Google Nexus 5
  • Google Nexus 7

Both with the newest Android-Version.

regards
mav

Hmm, what order are you loading ng-cordova and cordova? Most of the time ng-cordova is accidentally put after cordova, so it ends up missing the device ready event

This is the order of the js-file in the index.hrml

<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>

<!-- ng-cordova Plugins -->
<script src="lib/ngCordova/ng-cordova.js"></script>

<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>

<!-- your app's js -->
<script src="js/app.js"></script>

regards
mav

Well this is saying that your cordova isn’t being found, which only happens during development. Hmm, just curious are you actually running these as .apk files or just in the mobile web browser?

Hello,

I found this post when i try to integrate an NFC reader in my project. I have used the code showed at the begining, but it doesn´t work for me. This is a sample of my code: http://codepen.io/gdpfran/debug/fFhLx

What i want to do is to scan NFC tag in some templates of my project. I have my config.xml file which include the followng line of code:

<gap:plugin name=“com.chariotsolutions.nfc.plugin” version=“0.5.1” />

i compile my project using phonegap web compiler.

What is wrong or how can i wrap the plugin?

Thanks!!!

Please, could you help me?

Thanks!

@gdp_fran sorry about the delay, was away for the weekend and had a holiday Monday.

Are you running this on a device or in the browser?

Thank you for the reply, and don´t worry, all of us have weekend and holidays! :wink:

I run the app in my phone, nexus 4 and included in my config.xml the plugin.

Hmm, this wrapper is part of ng-cordova, so it may be best to try to get a hold of @mav and see if he was able to get this to work

The problem ist, that the phonegap nfc plugin is really lazy.

You need to listen to the device ready event and then resolve the module.
For my project i’ve wrote the wrapper myself here: https://github.com/konstantinkrassmann/ngCordova-nfc

Maybe it’s useful for you, too.

2 Likes

Hi Konstantin,
You have a example of this plugin working?
I hope you can help

Greetings