chariotsolutions phonegap-nfc插件在扫描时重新启动

时间:2014-05-12 14:16:47

标签: android cordova phonegap-plugins nfc phonegap-build

我正在使用PhoneGap版本和chariotsolutions NFC插件来扫描NFC卡,它正在运行。但是,在PhoneGap构建失败后,它开始失败。我只能猜测PhoneGap版本现在正在使用更新版本的插件。干净的构建仍然会在扫描卡片时内部崩溃。

我使用cordova CLI使用最新版本创建了一个新的Cordova项目,并安装了chariotsolutions插件。它仍然没有只有熊骨头项目,现在在PhoneGap构建之外。

硬件和软件:运行Android 4.4.2的Android Nexus 7 Cordova 版本3.3

Bear bones项目代码仅包含此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输出:

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输出。似乎由于某种原因暂停应用程序matched single TECH,然后重新加载运行onDeviceReady的Cordova。

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.

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:2)

它无效的原因是因为应用程序正在寻找NDEF标记,但您正在扫描非NDEF标记。

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

要阅读非NDEF标记,您需要添加nfc.addNdefFormatableListenernfc.addTagDiscoveredListener。您可以使用一个事件处理程序,例如app.onNFC,适用于多种标签类型。

您还可以使用NXP Tag Writer向标记写入消息,该消息也会将其格式化为NDEF。

请注意,使用Broadcom NFC芯片组的设备无法读取Mifare Classic标签,因此即使在Mifare Classic标签上阅读NDEF消息,您也可能会收到有关非NDEF消息的错误。

我的Nexus 7(原始版本)配备了恩智浦NFC芯片组。删除console.log('nfcEvent '+JSON.stringify(nfcEvent));后,您的代码才有效。由于循环引用,事件无法进行字符串化。我认为第二代Nexus 7使用Broadcom NFC芯片组。