Skip to content

Commit

Permalink
Merge pull request #1 from m00sey/master
Browse files Browse the repository at this point in the history
updates to work with phone gap-nfc 0.2.0
  • Loading branch information
don committed Sep 13, 2011
2 parents a2eb823 + ee160b9 commit 992ab00
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 54 deletions.
2 changes: 1 addition & 1 deletion assets/www/index.html
Expand Up @@ -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>
Expand Down
10 changes: 5 additions & 5 deletions assets/www/main.js
Expand Up @@ -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
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);

};

Expand Down
115 changes: 68 additions & 47 deletions assets/www/phonegap-nfc-0.1.3.js → assets/www/phonegap-nfc-0.2.0.js
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
},

/**
Expand All @@ -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));
},

/**
Expand All @@ -94,76 +94,97 @@ 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++) {
bytesAsString += String.fromCharCode(bytes[i]);
}
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
re = re.concat( st.reverse() );
}
// 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;
}

};
Binary file removed lib/phonegap-nfc-0.1.3.jar
Binary file not shown.
Binary file added lib/phonegap-nfc-0.2.0.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion res/xml/plugins.xml
Expand Up @@ -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.