Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

updated to work with cordova version of phonegap

  • Loading branch information...
commit 8cce8a2f7c98d069843d0520c98e648dfcba53aa 1 parent 6cd415a
Don Coleman authored
1  .gitignore
@@ -5,3 +5,4 @@ pg.sh
5 5 *.iml
6 6 .idea/
7 7 local.properties
  8 +out/
4 AndroidManifest.xml
... ... @@ -1,8 +1,8 @@
1 1 <?xml version="1.0" encoding="utf-8"?>
2 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 3 package="com.chariotsolutions.nfc.demo.reader"
4   - android:versionCode="1"
5   - android:versionName="1.0">
  4 + android:versionCode="6"
  5 + android:versionName="1.2">
6 6 <uses-sdk android:minSdkVersion="10" />
7 7
8 8 <supports-screens
4,949 assets/www/cordova-1.6.0.js
4,949 additions, 0 deletions not shown
6 assets/www/index.html
@@ -5,8 +5,8 @@
5 5 <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
6 6 <title>NFC Reader Demo</title>
7 7 <link rel="stylesheet" href="master.css" type="text/css" media="screen" title="no title" charset="utf-8">
8   -<script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
9   -<script type="text/javascript" charset="utf-8" src="phonegap-nfc-0.2.0.js"></script>
  8 +<script type="text/javascript" charset="utf-8" src="cordova-1.6.0.js"></script>
  9 +<script type="text/javascript" charset="utf-8" src="phonegap-nfc-0.3.0-SNAPSHOT.js"></script>
10 10 <script type="text/javascript" charset="utf-8" src="main.js"></script>
11 11 </head>
12 12
@@ -15,4 +15,4 @@
15 15 <div id="tagContents">
16 16 </div>
17 17 </body>
18   -</html>
  18 +</html>
4,367 assets/www/phonegap-1.0.0.js
0 additions, 4,367 deletions not shown
190 assets/www/phonegap-nfc-0.3.0-SNAPSHOT.js
... ... @@ -0,0 +1,190 @@
  1 +/*global PhoneGap*/
  2 +
  3 +PhoneGap.addConstructor(
  4 + function () {
  5 + PhoneGap.exec(
  6 + function () {
  7 + console.log("Initialized the NfcPlugin");
  8 + },
  9 + function (reason) {
  10 + alert("Failed to initialize the NfcPlugin " + reason);
  11 + },
  12 + "NfcPlugin", "init", []
  13 + )
  14 + }
  15 +);
  16 +
  17 +var ndef = {
  18 + // see android.nfc.NdefRecord for documentation about constants
  19 + // http://developer.android.com/reference/android/nfc/NdefRecord.html
  20 + TNF_EMPTY: 0x0,
  21 + TNF_WELL_KNOWN: 0x01,
  22 + TNF_MIME_MEDIA: 0x02,
  23 + TNF_ABSOLUTE_URI: 0x03,
  24 + TNF_EXTERNAL_TYPE: 0x04,
  25 + TNF_UNKNOWN: 0x05,
  26 + TNF_UNCHANGED: 0x06,
  27 + TNF_RESERVED: 0x07,
  28 +
  29 + RTD_TEXT: [0x54], // "T"
  30 + RTD_URI: [0x55], // "U"
  31 + RTD_SMART_POSTER: [0x53, 0x70], // "Sp"
  32 + RTD_ALTERNATIVE_CARRIER: [0x61, 0x63], // "ac"
  33 + RTD_HANDOVER_CARRIER: [0x48, 0x63], // "Hc"
  34 + RTD_HANDOVER_REQUEST: [0x48, 0x72], // "Hr"
  35 + RTD_HANDOVER_SELECT: [0x48, 0x73], // "Hs"
  36 +
  37 + /**
  38 + * Creates a JSON representation of a NDEF Record.
  39 + *
  40 + * @tnf 3-bit TNF (Type Name Format) - use one of the TNF_* constants
  41 + * @type byte array, containing zero to 255 bytes, must not be null
  42 + * @id byte array, containing zero to 255 bytes, must not be null
  43 + * @payload byte array, containing zero to (2 ** 32 - 1) bytes, must not be null
  44 + *
  45 + * @returns JSON representation of a NDEF record
  46 + *
  47 + * @see Ndef.textRecord, Ndef.uriRecord and Ndef.mimeMediaRecord for examples
  48 + */
  49 + record: function (tnf, type, id, payload) {
  50 + return {
  51 + tnf: tnf,
  52 + type: type,
  53 + id: id,
  54 + payload: payload
  55 + };
  56 + },
  57 +
  58 + /**
  59 + * Helper that creates a NDEF record containing plain text.
  60 + *
  61 + * @text String
  62 + * @id byte[] (optional)
  63 + */
  64 + textRecord: function (text, id) {
  65 + var languageCode = 'en', // TODO get from browser
  66 + payload = [];
  67 +
  68 + if (!id) { id = []; }
  69 +
  70 + payload.push(languageCode.length);
  71 + nfc.concatArray(payload, nfc.stringToBytes(languageCode));
  72 + nfc.concatArray(payload, nfc.stringToBytes(text));
  73 +
  74 + return NFC.record(ndef.TNF_WELL_KNOWN, ndef.RTD_TEXT, id, payload);
  75 + },
  76 +
  77 + /**
  78 + * Helper that creates a NDEF record containing an absolute URI.
  79 + *
  80 + * @text String
  81 + * @id byte[] (optional)
  82 + */
  83 + uriRecord: function (text, id) {
  84 + if (!id) { id = []; }
  85 + return ndef.record(ndef.TNF_ABSOLUTE_URI, ndef.RTD_URI, id, nfc.stringToBytes(text));
  86 + },
  87 +
  88 + /**
  89 + * Helper that creates a NDEF record containing an mimeMediaRecord.
  90 + *
  91 + * @mimeType String
  92 + * @payload byte[]
  93 + * @id byte[] (optional)
  94 + */
  95 + mimeMediaRecord: function (mimeType, payload, id) {
  96 + if (!id) { id = []; }
  97 + return ndef.record(ndef.TNF_MIME_MEDIA, nfc.stringToBytes(mimeType), id, payload);
  98 + }
  99 +};
  100 +
  101 +var nfc = {
  102 +
  103 + addTagDiscoveredListener: function (callback, win, fail) {
  104 + document.addEventListener("tag", callback, false);
  105 + PhoneGap.exec(win, fail, "NfcPlugin", "registerTag", []);
  106 + },
  107 +
  108 + addMimeTypeListener: function (mimeType, callback, win, fail) {
  109 + document.addEventListener("ndef-mime", callback, false);
  110 + PhoneGap.exec(win, fail, "NfcPlugin", "registerMimeType", [mimeType]);
  111 + },
  112 +
  113 + addNdefListener: function (callback, win, fail) {
  114 + document.addEventListener("ndef", callback, false);
  115 + PhoneGap.exec(win, fail, "NfcPlugin", "registerNdef", []);
  116 + },
  117 +
  118 + addNdefFormatableListener: function (callback, win, fail) {
  119 + document.addEventListener("ndef-formatable", callback, false);
  120 + PhoneGap.exec(win, fail, "NfcPlugin", "registerNdefFormatable", []);
  121 + },
  122 +
  123 + write: function (ndefMessage, win, fail) {
  124 + PhoneGap.exec(win, fail, "NfcPlugin", "writeTag", [ndefMessage]);
  125 + },
  126 +
  127 + share: function (ndefMessage, win, fail) {
  128 + PhoneGap.exec(win, fail, "NfcPlugin", "shareTag", [ndefMessage]);
  129 + },
  130 +
  131 + unshare: function (win, fail) {
  132 + PhoneGap.exec(win, fail, "NfcPlugin", "unshareTag", []);
  133 + },
  134 +
  135 + erase: function (win, fail) {
  136 + PhoneGap.exec(win, fail, "NfcPlugin", "writeTag", [[]]);
  137 + },
  138 +
  139 + concatArray: function (a1, a2) { // this isn't built in?
  140 + for (var i = 0; i < a2.length; i++) {
  141 + a1.push(a2[i]);
  142 + }
  143 + return a1;
  144 + },
  145 +
  146 + bytesToString: function (bytes) {
  147 + var bytesAsString = "";
  148 + for (var i = 0; i < bytes.length; i++) {
  149 + bytesAsString += String.fromCharCode(bytes[i]);
  150 + }
  151 + return bytesAsString;
  152 + },
  153 +
  154 + // http://stackoverflow.com/questions/1240408/reading-bytes-from-a-javascript-string#1242596
  155 + stringToBytes: function ( str ) {
  156 + var ch, st, re = [];
  157 + for (var i = 0; i < str.length; i++ ) {
  158 + ch = str.charCodeAt(i); // get char
  159 + st = []; // set up "stack"
  160 + do {
  161 + st.push( ch & 0xFF ); // push byte to stack
  162 + ch = ch >> 8; // shift value down by 1 byte
  163 + }
  164 + while ( ch );
  165 + // add stack contents to result
  166 + // done because chars have "wrong" endianness
  167 + re = re.concat( st.reverse() );
  168 + }
  169 + // return an array of bytes
  170 + return re;
  171 + },
  172 +
  173 + bytesToHexString: function (bytes) {
  174 + var bytesAsHexString = "";
  175 + for (var i = 0; i < bytes.length; i++) {
  176 + if(bytes[i] >= 0) {
  177 + dec = bytes[i];
  178 + } else {
  179 + dec = 256 + bytes[i];
  180 + }
  181 + hexstring = dec.toString(16);
  182 + // zero padding
  183 + if(hexstring.length == 1) {
  184 + hexstring = "0" + hexstring;
  185 + }
  186 + bytesAsHexString += hexstring;
  187 + }
  188 + return bytesAsHexString;
  189 + }
  190 +};
BIN  lib/cordova-1.6.0.jar
Binary file not shown
BIN  lib/phonegap-1.0.0.jar
Binary file not shown
BIN  lib/phonegap-nfc-0.2.0.jar
Binary file not shown
BIN  lib/phonegap-nfc-0.3.0-SNAPSHOT.jar
Binary file not shown
2  project.properties
@@ -11,4 +11,4 @@
11 11 #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 12
13 13 # Project target.
14   -target=android-10
  14 +target=android-15
5 res/xml/cordova.xml
... ... @@ -0,0 +1,5 @@
  1 +<?xml version="1.0" encoding="utf-8"?>
  2 +<cordova>
  3 + <access origin="http://127.0.0.1*"/>
  4 + <log level="DEBUG"/>
  5 +</cordova>
34 res/xml/plugins.xml
... ... @@ -1,20 +1,20 @@
1 1 <?xml version="1.0" encoding="utf-8"?>
2 2 <plugins>
3   - <plugin name="App" value="com.phonegap.App"/>
4   - <plugin name="Geolocation" value="com.phonegap.GeoBroker"/>
5   - <plugin name="Device" value="com.phonegap.Device"/>
6   - <plugin name="Accelerometer" value="com.phonegap.AccelListener"/>
7   - <plugin name="Compass" value="com.phonegap.CompassListener"/>
8   - <plugin name="Media" value="com.phonegap.AudioHandler"/>
9   - <plugin name="Camera" value="com.phonegap.CameraLauncher"/>
10   - <plugin name="Contacts" value="com.phonegap.ContactManager"/>
11   - <plugin name="Crypto" value="com.phonegap.CryptoHandler"/>
12   - <plugin name="File" value="com.phonegap.FileUtils"/>
13   - <plugin name="Network Status" value="com.phonegap.NetworkManager"/>
14   - <plugin name="Notification" value="com.phonegap.Notification"/>
15   - <plugin name="Storage" value="com.phonegap.Storage"/>
16   - <plugin name="Temperature" value="com.phonegap.TempListener"/>
17   - <plugin name="FileTransfer" value="com.phonegap.FileTransfer"/>
18   - <plugin name="Capture" value="com.phonegap.Capture"/>
  3 + <plugin name="App" value="org.apache.cordova.App"/>
  4 + <plugin name="Geolocation" value="org.apache.cordova.GeoBroker"/>
  5 + <plugin name="Device" value="org.apache.cordova.Device"/>
  6 + <plugin name="Accelerometer" value="org.apache.cordova.AccelListener"/>
  7 + <plugin name="Compass" value="org.apache.cordova.CompassListener"/>
  8 + <plugin name="Media" value="org.apache.cordova.AudioHandler"/>
  9 + <plugin name="Camera" value="org.apache.cordova.CameraLauncher"/>
  10 + <plugin name="Contacts" value="org.apache.cordova.ContactManager"/>
  11 + <plugin name="File" value="org.apache.cordova.FileUtils"/>
  12 + <plugin name="Network Status" value="org.apache.cordova.NetworkManager"/>
  13 + <plugin name="Notification" value="org.apache.cordova.Notification"/>
  14 + <plugin name="Storage" value="org.apache.cordova.Storage"/>
  15 + <plugin name="Temperature" value="org.apache.cordova.TempListener"/>
  16 + <plugin name="FileTransfer" value="org.apache.cordova.FileTransfer"/>
  17 + <plugin name="Capture" value="org.apache.cordova.Capture"/>
  18 + <plugin name="Battery" value="org.apache.cordova.BatteryListener"/>
19 19 <plugin name="NfcPlugin" value="com.chariotsolutions.nfc.plugin.NfcPlugin"/>
20   -</plugins>
  20 +</plugins>
2  src/com/chariotsolutions/nfc/demo/reader/ReaderDemo.java
... ... @@ -1,6 +1,6 @@
1 1 package com.chariotsolutions.nfc.demo.reader;
2 2
3   -import com.phonegap.*;
  3 +import org.apache.cordova.*;
4 4 import android.os.Bundle;
5 5
6 6 public class ReaderDemo extends DroidGap {

0 comments on commit 8cce8a2

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