Permalink
Browse files

updates to work with phonegap-nfc 0.2.0

  • Loading branch information...
1 parent 118ceb9 commit 540ddeee62fe37bfa12eb954d3bd317b34dd85cb @m00sey m00sey committed Sep 8, 2011
View
@@ -6,7 +6,7 @@
<title>Rock Paper Scissors</title>
<link rel="stylesheet" href="master.css" type="text/css" media="screen" title="no title" />
<script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
- <script type="text/javascript" charset="utf-8" src="phonegap-nfc-0.1.5.js"></script>
+ <script type="text/javascript" charset="utf-8" src="phonegap-nfc-0.2.0.js"></script>
<script type="text/javascript" charset="utf-8" src="main.js"></script>
</head>
<body>
View
@@ -4,7 +4,7 @@ var choice = null,
mimeType = "game/rockpaperscissors";
function stop() {
- navigator.nfc.unshareTag();
+ nfc.unshare();
listening = false;
// TODO deselect button. blur doesn't work
}
@@ -16,7 +16,7 @@ function onNfc(nfcEvent) {
var tag = nfcEvent.tag,
records = tag.ndefMessage,
- opponentsChoice = navigator.nfc.util.bytesToString(records[0].payload),
+ opponentsChoice = nfc.bytesToString(records[0].payload),
result;
if (choice === opponentsChoice) {
@@ -52,10 +52,10 @@ function message(win, lose) {
function choose(text) {
choice = text;
var ndefMessage = [
- Ndef.mimeMediaRecord(mimeType, navigator.nfc.util.stringToBytes(choice))
+ ndef.mimeMediaRecord(mimeType, nfc.stringToBytes(choice))
];
- navigator.nfc.shareTag(
+ nfc.share(
ndefMessage,
function () {
navigator.notification.vibrate(100);
@@ -81,7 +81,7 @@ var ready = function () {
alert('Failed to register mime type ' + mimeType + ' with NFC');
}
- navigator.nfc.addMimeTypeListener(mimeType, onNfc, win, fail);
+ nfc.addMimeTypeListener(mimeType, onNfc, win, fail);
};
@@ -4,17 +4,17 @@ PhoneGap.addConstructor(
function () {
PhoneGap.exec(
function () {
- console.log("Initialized the NFCPlugin");
+ console.log("Initialized the NfcPlugin");
},
function (reason) {
- alert("Failed to initialize the NFCPlugin " + reason);
+ alert("Failed to initialize the NfcPlugin " + reason);
},
- "NFCPlugin", "init", []
+ "NfcPlugin", "init", []
)
}
);
-var Ndef = {
+var ndef = {
// see android.nfc.NdefRecord for documentation about constants
// http://developer.android.com/reference/android/nfc/NdefRecord.html
TNF_EMPTY: 0x0,
@@ -68,10 +68,10 @@ var Ndef = {
if (!id) { id = []; }
payload.push(languageCode.length);
- navigator.nfc.util.concatArray(payload, navigator.nfc.util.stringToBytes(languageCode));
- navigator.nfc.util.concatArray(payload, navigator.nfc.util.stringToBytes(text));
+ nfc.concatArray(payload, nfc.stringToBytes(languageCode));
+ nfc.concatArray(payload, nfc.stringToBytes(text));
- return NFC.record(Ndef.TNF_WELL_KNOWN, Ndef.RTD_TEXT, id, payload);
+ return NFC.record(ndef.TNF_WELL_KNOWN, ndef.RTD_TEXT, id, payload);
},
/**
@@ -82,7 +82,7 @@ var Ndef = {
*/
uriRecord: function (text, id) {
if (!id) { id = []; }
- return Ndef.record(Ndef.TNF_ABSOLUTE_URI, Ndef.RTD_URI, id, navigator.nfc.util.stringToBytes(text));
+ return ndef.record(ndef.TNF_ABSOLUTE_URI, ndef.RTD_URI, id, nfc.stringToBytes(text));
},
/**
@@ -94,87 +94,48 @@ var Ndef = {
*/
mimeMediaRecord: function (mimeType, payload, id) {
if (!id) { id = []; }
- return Ndef.record(Ndef.TNF_MIME_MEDIA, navigator.nfc.util.stringToBytes(mimeType), id, payload);
+ return ndef.record(ndef.TNF_MIME_MEDIA, nfc.stringToBytes(mimeType), id, payload);
}
};
-var Util = {
- concatArray: function (a1, a2) { // this isn't built in?
- for (var i = 0; i < a2.length; i++) {
- a1.push(a2[i]);
- }
- return a1;
- },
-
- bytesToString: function (bytes) {
- var bytesAsString = "";
- for (var i = 0; i < bytes.length; i++) {
- bytesAsString += String.fromCharCode(bytes[i]);
- }
- return bytesAsString;
- },
+var nfc = {
- // http://stackoverflow.com/questions/1240408/reading-bytes-from-a-javascript-string#1242596
- stringToBytes: function ( str ) {
- var ch, st, re = [];
- for (var i = 0; i < str.length; i++ ) {
- ch = str.charCodeAt(i); // get char
- st = []; // set up "stack"
- do {
- st.push( ch & 0xFF ); // push byte to stack
- ch = ch >> 8; // shift value down by 1 byte
- }
- while ( ch );
- // add stack contents to result
- // done because chars have "wrong" endianness
- re = re.concat( st.reverse() );
- }
- // return an array of bytes
- return re;
- }
-};
-
-navigator.nfc = {
-
- addTagListener: function (callback, win, fail) {
+ addTagDiscoveredListener: function (callback, win, fail) {
document.addEventListener("tag", callback, false);
- PhoneGap.exec(win, fail, "NFCPlugin", "registerTag", []);
+ PhoneGap.exec(win, fail, "NfcPlugin", "registerTag", []);
},
addMimeTypeListener: function (mimeType, callback, win, fail) {
document.addEventListener("ndef-mime", callback, false);
- PhoneGap.exec(win, fail, "NFCPlugin", "registerMimeType", [mimeType]);
+ PhoneGap.exec(win, fail, "NfcPlugin", "registerMimeType", [mimeType]);
},
addNdefListener: function (callback, win, fail) {
document.addEventListener("ndef", callback, false);
- PhoneGap.exec(win, fail, "NFCPlugin", "registerNdef", []);
+ PhoneGap.exec(win, fail, "NfcPlugin", "registerNdef", []);
},
addNdefFormatableListener: function (callback, win, fail) {
document.addEventListener("ndef-formatable", callback, false);
- PhoneGap.exec(win, fail, "NFCPlugin", "registerNdefFormatable", []);
+ PhoneGap.exec(win, fail, "NfcPlugin", "registerNdefFormatable", []);
},
- writeTag: function (ndefMessage, win, fail) {
- PhoneGap.exec(win, fail, "NFCPlugin", "writeTag", [ndefMessage]);
+ write: function (ndefMessage, win, fail) {
+ PhoneGap.exec(win, fail, "NfcPlugin", "writeTag", [ndefMessage]);
},
- shareTag: function (ndefMessage, win, fail) {
- PhoneGap.exec(win, fail, "NFCPlugin", "shareTag", [ndefMessage]);
+ share: function (ndefMessage, win, fail) {
+ PhoneGap.exec(win, fail, "NfcPlugin", "shareTag", [ndefMessage]);
},
- unshareTag: function (win, fail) {
- PhoneGap.exec(win, fail, "NFCPlugin", "unshareTag", []);
+ unshare: function (win, fail) {
+ PhoneGap.exec(win, fail, "NfcPlugin", "unshareTag", []);
},
- eraseTag: function (win, fail) {
- PhoneGap.exec(win, fail, "NFCPlugin", "writeTag", [[]]);
- }
-
-};
+ erase: function (win, fail) {
+ PhoneGap.exec(win, fail, "NfcPlugin", "writeTag", [[]]);
+ },
-navigator.nfc.util = {
concatArray: function (a1, a2) { // this isn't built in?
for (var i = 0; i < a2.length; i++) {
a1.push(a2[i]);
View
Binary file not shown.
View
Binary file not shown.
View
@@ -17,5 +17,5 @@
<plugin name="Temperature" value="com.phonegap.TempListener"/>
<plugin name="FileTransfer" value="com.phonegap.FileTransfer"/>
<plugin name="Capture" value="com.phonegap.Capture"/>
- <plugin name="NFCPlugin" value="com.chariotsolutions.nfc.plugin.NFCPlugin"/>
+ <plugin name="NfcPlugin" value="com.chariotsolutions.nfc.plugin.NfcPlugin"/>
</plugins>

0 comments on commit 540ddee

Please sign in to comment.