问题
I use the phonegap-nfc plugin from chariotsolutions. Reading a NFC tag while the app is running is no problem. I registered the app for intents so it starts when the app is in background and a tag is read. But the NdefListener is not fired. Does anybody know how to get the tag information when the app is started by an intent?
回答1:
You need to add a mime type listener to get the nfcEvent fired by the intent when the application launches.
You can use the same event handler you are using for the NDEF listener, everything is the same except for the event type. You don't need to specify a mime type.
nfc.addMimeTypeListener("", app.onNfc, success, failure);
This is a bit odd, but the way it works because of how events are fired in the plugin implementation.
回答2:
That plugin listens for NFC events, but it only notifies the JS callbacks that have been previously registered from Javascript code. So your web page must have been loaded in the webview first, then cordova's load event must have been fired, and only then you can register callbacks. So I think the plugin alone does not solve your problem.
To do what you need, you could add NFC detection capabilities to the Web Activity (using Java API), then store the readed tag info in persistence (again using Java), and then create a custom Cordova plugin to read this info from javascript in your JS entry point (for instance in the "deviceready" event callback).
来源:https://stackoverflow.com/questions/23059763/chariotsolutions-phonegap-nfc-plugin-intent