Android Cordova NFC插件的问题:NFC没有方法';连接';
我的Cordova应用程序(适用于Android,使用phonegap nfc插件)成功接收nfc意图并显示标记的UID,但未调用Android Cordova NFC插件的问题:NFC没有方法';连接';,android,cordova,nfc,Android,Cordova,Nfc,我的Cordova应用程序(适用于Android,使用phonegap nfc插件)成功接收nfc意图并显示标记的UID,但未调用onConnected方法 这是我的index.js文件: var app = { initialize: function() { this.bindEvents(); }, bindEvents: function() { document.addEventListen
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();
我注意到日志中有以下错误:
“未捕获的TypeError:对象#没有“连接”方法,来源:file:///android_asset/www/js/index.js (48)
这意味着
nfc
没有方法connect()
。为什么?文档中对此方法进行了描述:您似乎正在使用不同版本的phonegap nfc插件。标准phonegap nfc插件(来自Chariot Solutions,您可以在此处获得)不支持ISO-DEP通信方法(连接/断开连接/传输)。如果你想使用这些方法,你需要使用这个插件的修改版本。我解决了这个问题。这真的是由于插件的错误版本。要安装正确的插件,您需要添加到EV“C:\Program Files\Git\bin”和“C:\Program Files\Git\cmd”中的路径中(当然,在安装Git之前)。然后,您可以使用以下命令添加正确的插件:
$cordova插件添加
它帮助了我,现在调用了onConnected方法。感谢您添加关于如何添加该插件特定版本的更多信息!