Last active
August 29, 2015 14:07
-
-
Save tauzen/ab7314db89dd613fa6cf to your computer and use it in GitHub Desktop.
FirefoxOS tag writing example, requires NFC capable device running FirefoxOS 2.1. App needs to be certified! Privileged APIs comming soon!
This file contains 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
window.addEventListener('DOMContentLoaded', function() { | |
'use strict'; | |
console.log('DOMContentLoaded, checking for NFC'); | |
if (!navigator.mozNfc) { | |
console.log('NFC not available'); | |
return; | |
} | |
navigator.mozSetMessageHandler('activity', (activity) => { | |
var data = activity.source.data; | |
// prepare NDEF record to write using NfcUtils from shared/js/nfc_utils.js | |
var nfcUtils = new NfcUtils(); | |
var tnf = NDEF.TNF_WELL_KNOWN; | |
var type = NDEF.RTD_URI; | |
var url = nfcUtils.fromUTF8('\u0003mozilla.org'); | |
var record = new MozNDEFRecord({tnf:tnf, type:type, payload:url}); | |
// getting the tag | |
var tag = navigator.mozNfc.getNFCTag(data.sessionToken); | |
// writing array of NDEF records (NDEF message) to the tag | |
var request = tag.writeNDEF([record]); | |
request.onsuccess = () => { | |
console.log('wrote data to tag'); | |
}; | |
request.onerror = () => { | |
console.log('failed to write data to tag'); | |
}; | |
return true; | |
}); | |
}); |
This file contains 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 charset="utf-8"> | |
<title>Tag Write</title> | |
<meta name="viewport" content="width=device-width"> | |
<script type="text/javascript" src="shared/js/nfc_utils.js" defer></script> | |
<script type="text/javascript" src="js/app.js" defer></script> | |
</head> | |
<body></body> | |
</html> |
This file contains 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
{ | |
"version": "0.1.0", | |
"name": "Tag write", | |
"description": "Quick example of writing contents on tag", | |
"launch_path": "/index.html", | |
"type": "certified", | |
"permissions": { | |
"nfc": { "access": "readwrite" } | |
}, | |
"activities": { | |
"nfc-ndef-discovered": { | |
"filters": { | |
"type": "empty" | |
} | |
} | |
}, | |
"default_locale": "en" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment