【问题标题】:Trouble with Cordova NFC plugin: nfc has no method 'connect'Cordova NFC 插件出现问题:nfc 没有“连接”方法
【发布时间】:2016-03-29 22:40:11
【问题描述】:

我的 Cordova 应用(适用于 Android,使用 phonegap-nfc 插件)成功接收 NFC 意图并显示标签的 UID,但未调用 onConnected 方法。

这是我的 index.js 文件:

var app = {
        initialize: function() {
            this.bindEvents();
        },
        bindEvents: function() {
            document.addEventListener('deviceready', this.onDeviceReady, false);
        },
        onDeviceReady: function() {
            app.receivedEvent('deviceready');
            nfc.addTagDiscoveredListener(
                app.onNfc,
                function () { },
                function (reason) { app.showText("Ups: " + reason); }
              );
        },
        onNfc: function (nfcEvent) {      
          var tag = nfcEvent.tag;
          app.showText(JSON.stringify(nfcEvent.tag));
          var nfcUid = nfc.bytesToHexString(tag.id);
          app.showText(' nfcEvent.tag = ' + nfcUid);
          nfc.connect(
            app.onConnected, // chipcard connected
            function ()       { app.showText('connection successful'); },
            function (reason) { app.showText('connect failed: ' + reason); }
          );    
        },
        onConnected: function () {
            app.showText('onConnected');
            nfc.transceive(
                "00A400",       // RequestAPDU
                function (data) { // ResponseAPDU
                   app.showText("transceive successful: " + data);
                },
                function (reason) {
                   app.showText("transceive failed: " + reason);
                }
            );
            nfc.close(
               app.onConnected, // remove hander
               function ()       { app.showTxt('close successful'); },
               function (reason) { app.showTxt('close failed: ' + reason); }
            );
        },
        receivedEvent: function(id) {
            var parentElement = document.getElementById(id);
            var listeningElement = parentElement.querySelector('.listening');
            var receivedElement = parentElement.querySelector('.received');

            listeningElement.setAttribute('style', 'display:none;');
            receivedElement.setAttribute('style', 'display:block;');

            console.log('Received Event: ' + id);
        },  
        showText: function(message) {
            var label = document.createTextNode(message),
                lineBreak = document.createElement("br");
            messageDiv.appendChild(lineBreak);         // add a line break
            messageDiv.appendChild(label);             // add the text  
        }
    };

    app.initialize();

我在日志中注意到以下错误:

“Uncaught TypeError: Object # has no method 'connect'”,来源:file:///android_asset/www/js/index.js (48)

表示nfc 没有方法connect()。为什么?文档中有这种方法的描述:https://github.com/jalbersol/phonegap-nfc#nfcconnect

【问题讨论】:

    标签: android cordova nfc


    【解决方案1】:

    您似乎在使用不同版本的 phonegap-nfc 插件。标准 phonegap-nfc 插件(来自 Chariot Solutions,您可以在此处获取它https://github.com/chariotsolutions/phonegap-nfc)确实支持 ISO-DEP 通信方法(连接/断开/transeive)。如果你想使用这些方法,你需要使用来自https://github.com/jalbersol/phonegap-nfc的插件的修改版本。

    【讨论】:

      【解决方案2】:

      我解决了这个问题。这确实是由于插件版本错误。要安装正确的插件,您需要添加到 EV 中的路径“C:\Program Files\Git\bin”和“C:\Program Files\Git\cmd”(当然,在此之前必须安装 Git)。然后您可以使用以下命令添加正确的插件:

      $cordova 插件添加https://github.com/jalbersol/phonegap-nfc.git

      它帮助了我,现在调用了 onConnected 方法。

      【讨论】:

      • 感谢您添加有关如何添加特定版本插件的更多信息!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多