Skip to content
Browse files

Merge pull request #1 from m00sey/master

updates to work with phone gap-nfc 0.2.0
  • Loading branch information...
2 parents a2eb823 + ee160b9 commit 992ab00b6da0d9bf08cd0ef15b2e290e9abd1422 @don committed
View
2 assets/www/index.html
@@ -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.3.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
10 assets/www/main.js
@@ -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 = Ndef.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, Ndef.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);
};
View
115 assets/www/phonegap-nfc-0.1.3.js → assets/www/phonegap-nfc-0.2.0.js
@@ -4,17 +4,17 @@ PhoneGap.addConstructor(
function () {
PhoneGap.exec(
function () {
- console.log("Initialized the NdefPlugin");
+ console.log("Initialized the NfcPlugin");
},
function (reason) {
- alert("Failed to initialize the NdefPlugin " + reason);
+ alert("Failed to initialize the NfcPlugin " + reason);
},
- "NdefPlugin", "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);
- Ndef.concatArray(payload, Ndef.stringToBytes(languageCode));
- Ndef.concatArray(payload, Ndef.stringToBytes(text));
+ nfc.concatArray(payload, nfc.stringToBytes(languageCode));
+ nfc.concatArray(payload, nfc.stringToBytes(text));
- return Ndef.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, Ndef.stringToBytes(text));
+ return ndef.record(ndef.TNF_ABSOLUTE_URI, ndef.RTD_URI, id, nfc.stringToBytes(text));
},
/**
@@ -94,16 +94,55 @@ var Ndef = {
*/
mimeMediaRecord: function (mimeType, payload, id) {
if (!id) { id = []; }
- return Ndef.record(Ndef.TNF_MIME_MEDIA, Ndef.stringToBytes(mimeType), id, payload);
+ return ndef.record(ndef.TNF_MIME_MEDIA, nfc.stringToBytes(mimeType), id, payload);
+ }
+};
+
+var nfc = {
+
+ addTagDiscoveredListener: function (callback, win, fail) {
+ document.addEventListener("tag", callback, false);
+ 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]);
},
+ addNdefListener: function (callback, win, fail) {
+ document.addEventListener("ndef", callback, false);
+ PhoneGap.exec(win, fail, "NfcPlugin", "registerNdef", []);
+ },
+
+ addNdefFormatableListener: function (callback, win, fail) {
+ document.addEventListener("ndef-formatable", callback, false);
+ PhoneGap.exec(win, fail, "NfcPlugin", "registerNdefFormatable", []);
+ },
+
+ write: function (ndefMessage, win, fail) {
+ PhoneGap.exec(win, fail, "NfcPlugin", "writeTag", [ndefMessage]);
+ },
+
+ share: function (ndefMessage, win, fail) {
+ PhoneGap.exec(win, fail, "NfcPlugin", "shareTag", [ndefMessage]);
+ },
+
+ unshare: function (win, fail) {
+ PhoneGap.exec(win, fail, "NfcPlugin", "unshareTag", []);
+ },
+
+ erase: function (win, fail) {
+ PhoneGap.exec(win, fail, "NfcPlugin", "writeTag", [[]]);
+ },
+
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++) {
@@ -111,17 +150,17 @@ var Ndef = {
}
return bytesAsString;
},
-
+
// 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
+ 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
@@ -129,41 +168,23 @@ var Ndef = {
}
// return an array of bytes
return re;
- }
-
-};
-
-navigator.nfc = {
-
- addMimeTypeListener: function (mimeType, callback, win, fail) {
- document.addEventListener("ndef-mime", callback, false);
- PhoneGap.exec(win, fail, "NdefPlugin", "registerMimeType", [mimeType]);
- },
-
- addNdefListener: function (callback, win, fail) {
- document.addEventListener("ndef", callback, false);
- PhoneGap.exec(win, fail, "NdefPlugin", "registerNdef", []);
- },
-
- addNdefFormatableListener: function (callback, win, fail) {
- document.addEventListener("ndef-formatable", callback, false);
- PhoneGap.exec(win, fail, "NdefPlugin", "registerNdefFormatable", []);
- },
-
- writeTag: function (ndefMessage, win, fail) {
- PhoneGap.exec(win, fail, "NdefPlugin", "writeTag", [ndefMessage]);
},
- shareTag: function (ndefMessage, win, fail) {
- PhoneGap.exec(win, fail, "NdefPlugin", "shareTag", [ndefMessage]);
- },
-
- unshareTag: function (win, fail) {
- PhoneGap.exec(win, fail, "NdefPlugin", "unshareTag", []);
- },
-
- eraseTag: function (win, fail) {
- PhoneGap.exec(win, fail, "NdefPlugin", "writeTag", [[]]);
+ bytesToHexString: function (bytes) {
+ var bytesAsHexString = "";
+ for (var i = 0; i < bytes.length; i++) {
+ if(bytes[i] >= 0) {
+ dec = bytes[i];
+ } else {
+ dec = 256 + bytes[i];
+ }
+ hexstring = dec.toString(16);
+ // zero padding
+ if(hexstring.length == 1) {
+ hexstring = "0" + hexstring;
+ }
+ bytesAsHexString += hexstring;
+ }
+ return bytesAsHexString;
}
-
};
View
BIN lib/phonegap-nfc-0.1.3.jar
Binary file not shown.
View
BIN lib/phonegap-nfc-0.2.0.jar
Binary file not shown.
View
2 res/xml/plugins.xml
@@ -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="NdefPlugin" value="com.chariotsolutions.nfc.plugin.NdefPlugin"/>
+ <plugin name="NfcPlugin" value="com.chariotsolutions.nfc.plugin.NfcPlugin"/>
</plugins>

0 comments on commit 992ab00

Please sign in to comment.
Something went wrong with that request. Please try again.