android - Cordova NFC 插件 : nfc has no method 'connect' 出现问题

标签 android cordova nfc

我的 Cordova 应用程序(适用于 Android,使用 phonegap-nfc 插件)成功接收到 NFC Intent 并显示标签的 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 )支持 ISO-DEP 通信方法(连接/断开连接/传输)。您需要使用来自 https://github.com/jalbersol/phonegap-nfc 的插件的修改版本如果你想使用这些方法。

关于android - Cordova NFC 插件 : nfc has no method 'connect' 出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34436628/

相关文章:

android nfc 同时读写/一触

javascript - JS 进度条在 InAppBrowser 中不起作用

ios - iOS 上的 Phonegap 插件不调用 native 代码

jQuery 移动 mobileinit 未触发

javascript - 如何使用查询结果 - Javascript(Azure 数据库)

Android NFC writeNdefMessage 抛出 IOException Tag is not ndef

android - 我们可以将文件从一个 Android 应用传输到另一个吗

javascript - 是否可以使用 HTML/Javascript 按名称启动应用程序?

java - PopBackStack 前一个最后一个 fragment 并检查它是哪个 fragment

android - 设置Android Studio但如何?