我的 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/