【发布时间】: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
【问题讨论】: