Cordova NFC 插件有问题:nfc 没有方法 'connect'

Trouble with Cordova NFC plugin: nfc has no method 'connect'

我的 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'", source: file:///android_asset/www/js/index.js (48)

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

您似乎使用了不同版本的 phonegap-nfc 插件。标准的 phonegap-nfc 插件(来自 Chariot Solutions,如果你想使用这些方法,你可以在这里https://github.com/chariotsolutions/phonegap-nfc) does not support the ISO-DEP communication methods (connect/disconnect/transeive). You need use that modified version of the plugin from https://github.com/jalbersol/phonegap-nfc得到它。

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

$ cordova plugin add https://github.com/jalbersol/phonegap-nfc.git

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