Cordova NFC Read card (JavaScript)

Source: Internet
Author: User


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();


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.