Question

I am using PhoneGap build and the chariotsolutions NFC plugin to scan NFC cards, and it was working. However, it started failing after PhoneGap build went down. I can only guess that PhoneGap build is now using a newer version of the plugin. A clean build still makes it crash internally on scan of a card.

I created a new Cordova project with the latest build using the cordova CLI, and installed the chariotsolutions plugin. It is still failing with only the bear bones project, which is outside of PhoneGap build now.

Hardware and software: Android Nexus 7 running Android 4.4.2 Cordova version 3.3

Bear bones project code contains only this onDeviceReady:

initialize: function() {
  this.bindEvents();
},
// Bind Event Listeners
bindEvents: function() {
  document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function() {
console.log('v0.0.3');

document.addEventListener('resume', app.resume, false);
document.addEventListener('pause', app.pause, false);

// Read NDEF formatted NFC Tags
nfc.addNdefListener (
  function (nfcEvent) {
    console.log('nfcEvent '+JSON.stringify(nfcEvent));
    var tag = nfcEvent.tag,
      ndefMessage = tag.ndefMessage;

    // dump the raw json of the message
    // note: real code will need to decode
    // the payload from each record
    console.log(JSON.stringify(ndefMessage));

    // assuming the first record in the message has 
    // a payload that can be converted to a string.
    console.log(nfc.bytesToString(ndefMessage[0].payload).substring(3));
  }, 
  function () { // success callback
    console.log("Waiting for NDEF tag");
  },
  function (error) { // error callback
    console.log("Error adding NDEF listener " + JSON.stringify(error));
  }
);

},

ADB output on launch of the app:

I/ActivityManager(  512): Start proc com.test.myapp for activity com.test.myapp/.myappname: pid=17325 uid=10037 gids={50037, 3003}
I/CordovaLog(17325): Changing log level to DEBUG(3)
I/CordovaLog(17325): Found start page location: index.html
D/Whitelist(17325): Unlimited access to network resources
D/CordovaActivity(17325): CordovaActivity.onCreate()
V/WebViewChromium(17325): Binding Chromium to the background looper Looper (main, tid 1) {4211c938}
I/chromium(17325): [INFO:library_loader_hooks.cc(112)] Chromium logging enabled: level = 0, default verbosity = 0
I/BrowserProcessMain(17325): Initializing chromium process, renderers=0
W/chromium(17325): [WARNING:proxy_service.cc(888)] PAC support disabled because there is no system implementation
D/libEGL  (17325): loaded /system/lib/egl/libEGL_tegra.so
D/libEGL  (17325): loaded /system/lib/egl/libGLESv1_CM_tegra.so
D/libEGL  (17325): loaded /system/lib/egl/libGLESv2_tegra.so
D/CordovaWebView(17325): CordovaWebView is running on device made by: asus
D/JsMessageQueue(17325): Set native->JS mode to 2
D/CordovaActivity(17325): CordovaActivity.init()
D/CordovaWebView(17325): >>> loadUrl(file:///android_asset/www/index.html)
D/PluginManager(17325): init()
D/CordovaWebView(17325): >>> loadUrlNow()
I/CordovaLog(17325): Changing log level to DEBUG(3)
I/CordovaLog(17325): Found start page location: index.html
D/Whitelist(17325): Unlimited access to network resources
D/CordovaActivity(17325): Resuming the App
D/CordovaActivity(17325): CB-3064: The errorUrl is null
D/SoftKeyboardDetect(17325): Ignore this event
D/OpenGLRenderer(17325): Enabling debug mode 0
D/SoftKeyboardDetect(17325): Ignore this event
I/ActivityManager(  512): Displayed com.test.myapp/.myappname: +813ms
D/AndroidRuntime(17309): Shutting down VM
D/dalvikvm(17309): GC_CONCURRENT freed 97K, 15% free 589K/688K, paused 0ms+2ms, total 5ms
D/CordovaActivity(17325): onMessage(onPageStarted,file:///android_asset/www/index.html)
D/CordovaLog(17325): file:///android_asset/www/index.html: Line 25 : Viewport target-densitydpi is not supported.
I/chromium(17325): [INFO:CONSOLE(25)] "Viewport target-densitydpi is not supported.", source: file:///android_asset/www/index.html (25)
D/CordovaWebViewClient(17325): onPageFinished(file:///android_asset/www/index.html)
D/CordovaActivity(17325): onMessage(onPageFinished,file:///android_asset/www/index.html)
D/CordovaActivity(17325): onMessage(spinner,stop)
D/CordovaNetworkManager(17325): Connection Type: wifi
I/chromium(17325): [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported
I/chromium(17325): [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported
D/CordovaNetworkManager(17325): Connection Type: wifi
D/CordovaActivity(17325): onMessage(networkconnection,wifi)
D/CordovaLog(17325): file:///android_asset/www/js/index.js: Line 58 : onDeviceReady v0.0.3
I/chromium(17325): [INFO:CONSOLE(58)] "onDeviceReady v0.0.3", source: file:///android_asset/www/js/index.js (58)
D/NfcPlugin(17325): execute registerNdef
D/CordovaLog(17325): file:///android_asset/www/js/index.js: Line 83 : Waiting for NDEF tag
I/chromium(17325): [INFO:CONSOLE(83)] "Waiting for NDEF tag", source: file:///android_asset/www/js/index.js (83)
D/NfcPlugin(17325): execute init
D/NfcPlugin(17325): Enabling plugin Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.test.myapp/.myappname }
D/NfcDispatcher(  769): Set Foreground Dispatch
D/NfcPlugin(17325): parseMessage Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.test.myapp/.myappname }
D/NfcPlugin(17325): action android.intent.action.MAIN
D/CordovaLog(17325): file:///android_asset/www/plugins/com.chariotsolutions.nfc.plugin/www/phonegap-nfc.js: Line 18 : Initialized the NfcPlugin
I/chromium(17325): [INFO:CONSOLE(18)] "Initialized the NfcPlugin", source: file:///android_asset/www/plugins/com.chariotsolutions.nfc.plugin/www/phonegap-nfc.js (18)
D/CordovaActivity(17325): onMessage(spinner,stop)

ADB output when I scan a card. Seems to pause the app at matched single TECH for some reason, and then reloads Cordova running the onDeviceReady again.

D/NativeNfcTag(  769): Check NDEF Failed - status = 255
D/NativeNfcTag(  769): Check NDEF Failed - status = 255
D/NfcDispatcher(  769): dispatch tag: TAG: Tech [android.nfc.tech.MifareClassic, android.nfc.tech.NfcA, android.nfc.tech.NdefFormatable] message: null
I/ActivityManager(  512): START u0 {flg=0x10008000 cmp=com.android.nfc/.NfcRootActivity (has extras)} from pid 769
D/dalvikvm(  512): GC_FOR_ALLOC freed 564K, 15% free 18611K/21744K, paused 145ms, total 145ms
D/dalvikvm(  512): GC_FOR_ALLOC freed 283K, 15% free 18573K/21744K, paused 144ms, total 144ms
I/NfcDispatcher(  769): matched single TECH
D/CordovaActivity(17325): Paused the application!
D/CordovaWebView(17325): Handle the pause
D/NfcPlugin(17325): onPause Intent {  }
D/NfcPlugin(17325): stopNfc
D/NfcDispatcher(  769): Set Foreground Dispatch
D/CordovaLog(17325): file:///android_asset/www/js/index.js: Line 48 : -- pause event fired -- 
I/chromium(17325): [INFO:CONSOLE(48)] "-- pause event fired -- ", source: file:///android_asset/www/js/index.js (48)
I/ActivityManager(  512): START u0 {act=android.nfc.action.TECH_DISCOVERED cmp=com.widgapp.NFC_ReTAG_FREE/com.widgapp.NFC_ReTag_dispatch_other (has extras)} from pid 769
V/receiver Tag discovered: (15520): TAG: Tech [android.nfc.tech.MifareClassic, android.nfc.tech.NfcA, android.nfc.tech.NdefFormatable] mifare tagid: 8A0924F9
I/ActivityManager(  512): START u0 {flg=0x4000000 cmp=com.widgapp.NFC_ReTAG_FREE/.automode (has extras)} from pid 15520
D/NfcDispatcher(  769): Set Foreground Dispatch
D/NfcDispatcher(  769): Set Foreground Dispatch
D/dalvikvm(15520): GC_CONCURRENT freed 224K, 4% free 7810K/8072K, paused 4ms+3ms, total 42ms
W/InputMethodManagerService(  512): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@429d7908 attribute=null, token = android.os.BinderProxy@427e6bf8
I/CordovaLog(17325): Changing log level to DEBUG(3)
I/CordovaLog(17325): Found start page location: index.html
D/Whitelist(17325): Unlimited access to network resources
D/CordovaActivity(17325): Resuming the App
D/CordovaActivity(17325): CB-3064: The errorUrl is null
D/NfcPlugin(17325): onResume Intent {  }
D/NfcDispatcher(  769): Set Foreground Dispatch
D/CordovaLog(17325): file:///android_asset/www/js/index.js: Line 41 : -- resume event fired -- 
I/chromium(17325): [INFO:CONSOLE(41)] "-- resume event fired -- ", source: file:///android_asset/www/js/index.js (41)
D/NativeNfcTag(  769): Tag lost, restarting polling loop
D/dalvikvm(15037): GC_CONCURRENT freed 475K, 7% free 8271K/8824K, paused 3ms+4ms, total 40ms
D/dalvikvm(15037): WAIT_FOR_CONCURRENT_GC blocked 30ms
D/Finsky  (15037): [1] 5.onFinished: Installation state replication succeeded.

Any help would be appreciated.

Was it helpful?

Solution

The reason it's not working is because the application is looking for an NDEF tag, but you're scanning a non-NDEF tag.

D/NativeNfcTag(  769): Check NDEF Failed - status = 255
D/NativeNfcTag(  769): Check NDEF Failed - status = 255
D/NfcDispatcher(  769): dispatch tag: TAG: Tech [android.nfc.tech.MifareClassic, android.nfc.tech.NfcA, android.nfc.tech.NdefFormatable] message: null

To read a non-NDEF tag, you'll need to add nfc.addNdefFormatableListener or nfc.addTagDiscoveredListener. You can use one event handler, e.g. app.onNFC, for multiple tag types.

You could also use NXP Tag Writer to write a message to the tag, which will also format it as NDEF.

Note that devices with the Broadcom NFC chipset won't read Mifare Classic tags, so you might get errors about non-NDEF messages even when reading a NDEF message on a Mifare Classic tag.

My Nexus 7 (original version) has the NXP NFC chipset. Your code worked, after I removed console.log('nfcEvent '+JSON.stringify(nfcEvent));. The event can't be stringified because of circular references. I think the second generation Nexus 7 uses the Broadcom NFC chipset.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top