Reputation: 3731
My Cordova app (for Android, using phonegap-nfc plugin) successfully receives an NFC intent and displays the UID of the tag, but the onConnected
method isn't called.
This is my index.js file:
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();
I noticed the following error in the logs:
"Uncaught TypeError: Object # has no method 'connect'", source: file:///android_asset/www/js/index.js (48)
It means that nfc
has no method connect()
. Why? There's a description of this method in the docs: https://github.com/jalbersol/phonegap-nfc#nfcconnect
Upvotes: 1
Views: 1181
Reputation: 3731
I resolved this issue. It was really due to the wrong version of the plugin. To install correct plugin you need to add to path in EV "C:\Program Files\Git\bin" and "C:\Program Files\Git\cmd" (of course, before that Git must be installed). Then you can add correct plugin with the following command:
$ cordova plugin add https://github.com/jalbersol/phonegap-nfc.git
It helped me and now onConnected method is invoked.
Upvotes: 1
Reputation: 40869
You seem to be using a different version of the phonegap-nfc plugin. The standard phonegap-nfc plugin (from Chariot Solutions, you can get it here 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 if you want to use these methods.
Upvotes: 1