Created
April 13, 2018 04:23
-
-
Save don/1e7d3395958db9ecce30a33a36589665 to your computer and use it in GitHub Desktop.
phonegap-nfc-ios
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;"> | |
<meta name="format-detection" content="telephone=no"> | |
<meta name="msapplication-tap-highlight" content="no"> | |
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width"> | |
<link rel="stylesheet" type="text/css" href="css/index.css"> | |
<title>NFC Reader</title> | |
</head> | |
<body> | |
<div class="app"> | |
<h1>PhoneGap NFC</h1> | |
<div id="deviceready" class="blink"> | |
<p class="event listening">Connecting to Device</p> | |
<p class="event received">Device is Ready</p> | |
</div> | |
<div id="buttonDiv"> | |
</div> | |
</div> | |
<script type="text/javascript" src="cordova.js"></script> | |
<script type="text/javascript" src="js/index.js"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = { | |
// Application Constructor | |
initialize: function() { | |
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false); | |
}, | |
// deviceready Event Handler | |
// | |
// Bind any cordova events here. Common events are: | |
// 'pause', 'resume', etc. | |
onDeviceReady: function() { | |
app.receivedEvent('deviceready'); | |
// Read NDEF formatted NFC Tags | |
nfc.addNdefListener ( | |
function (nfcEvent) { | |
var tag = nfcEvent.tag, | |
ndefMessage = tag.ndefMessage; | |
// assuming the first record in the message has | |
// a payload that can be converted to a string. | |
var payloadAsText = function() { | |
navigator.notification.alert( | |
'', | |
{}, | |
nfc.bytesToString(ndefMessage[0].payload).substring(3) | |
); | |
} | |
navigator.notification.alert( | |
JSON.stringify(ndefMessage), | |
payloadAsText, | |
'Raw Tag Data'); | |
}, | |
function () { // success callback | |
console.log("Waiting for NDEF tag"); | |
app.addButton(); | |
}, | |
function (error) { // error callback | |
navigator.notification.alert("Error adding NDEF listener " + JSON.stringify(error)); | |
} | |
); | |
}, | |
// Update DOM on a Received Event | |
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); | |
}, | |
addButton: function() { | |
if (cordova.platformId === 'ios') { | |
var div = document.querySelector('#buttonDiv'); | |
var button = document.createElement('button'); | |
button.innerText = 'Begin Session'; | |
button.onclick = function() { | |
nfc.beginSession( | |
success => console.log('Started session'), | |
error => navigator.notification.alert('Failed to start sessions ' + error) | |
); | |
} | |
div.appendChild(button); | |
} | |
} | |
}; | |
app.initialize(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment