The following code may help you to use the Cordova NFC plug-in function, you understand it, oh, say, this is the front-end HTML JavaScript method, non-Java plug-ins
Prerequisite: Configure Cordova Plug-in method, still not on their own niang.
An id= ' state ' text box or label appears on an HTML page to show NFC status.
The key code is INITNFC, preceded by code generation.
Var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
}
// Bind Event Listeners
/ /
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
}
// deviceready Event Handler
/ /
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
function failure(reason) {
Console.log ("startup error");
$("ාstate"). Text ("start error" + reason);
}
Console.log ("start successful");
//Button event
Document.addeventlistener ("backbutton", eventbackbutton, false); / / return key
Document.addeventlistener ("MenuButton", eventmenubutton, false); / / menu key
Document.addeventlistener ("searchbutton", eventsearchbutton, false); / / search key
InitNFC ();
}
}
/ / return key
function eventBackButton() {
window.location.href = "indexList.html";
}
/ / menu key
function eventMenuButton() {
//Window. Plugins. Toastplugin. Show short ('Menu button clicked! ');
}
/ / search key
function eventSearchButton() {
//Window. Plugins. Toastplugin. Show [short ('search button clicked! ');
}
function initNFC() {
Console.log ("NFC initialization");
if (typeof(nfc) == "undefined") {
$("ාstate"). Text ("your machine does not have NFC function, or NFC function is not turned on");
} else {
//Old system use monitoring
nfc.addTagDiscoveredListener(nfccallback, nfconSuccesscallback, nfcerrorcallback);
//New system use monitoring
nfc.addNdefFormatableListener(nfccallback, nfconSuccesscallback, nfcerrorcallback);
}
}
function nfccallback(nfcEvent) {
$("ාstate"). Text ("NFC read");
var tag = nfcEvent.tag,
o_rfid = nfc.bytesToHexString(tag.id),
rfid = o_rfid.toUpperCase();
checkform(rfid);
//$("#state").text('rfid=' + rfid);
}
function nfconSuccesscallback() { // error callback
$("ාstate"). Text ("NFC is already open");
}
function nfcerrorcallback(error) { // error callback
$("ාstate"). Text ("NFC function error. "+ error";
}
function checkform(ID) {
//Card reading and verification
}
app.initialize();