community-cordova-plugin-nfc 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,140 @@
1
+ package com.chariotsolutions.nfc.plugin;
2
+
3
+ import android.nfc.NdefMessage;
4
+ import android.nfc.NdefRecord;
5
+ import android.nfc.Tag;
6
+ import android.nfc.tech.Ndef;
7
+ import android.util.Log;
8
+ import org.json.JSONArray;
9
+ import org.json.JSONException;
10
+ import org.json.JSONObject;
11
+
12
+ import java.util.ArrayList;
13
+ import java.util.Arrays;
14
+ import java.util.List;
15
+
16
+ public class Util {
17
+
18
+ static final String TAG = "NfcPlugin";
19
+
20
+ static JSONObject ndefToJSON(Ndef ndef) {
21
+ JSONObject json = new JSONObject();
22
+
23
+ if (ndef != null) {
24
+ try {
25
+
26
+ Tag tag = ndef.getTag();
27
+ // tag is going to be null for NDEF_FORMATABLE until NfcUtil.parseMessage is refactored
28
+ if (tag != null) {
29
+ json.put("id", byteArrayToJSON(tag.getId()));
30
+ json.put("techTypes", new JSONArray(Arrays.asList(tag.getTechList())));
31
+ }
32
+
33
+ json.put("type", translateType(ndef.getType()));
34
+ json.put("maxSize", ndef.getMaxSize());
35
+ json.put("isWritable", ndef.isWritable());
36
+ json.put("ndefMessage", messageToJSON(ndef.getCachedNdefMessage()));
37
+ // Workaround for bug in ICS (Android 4.0 and 4.0.1) where
38
+ // mTag.getTagService(); of the Ndef object sometimes returns null
39
+ // see http://issues.mroland.at/index.php?do=details&task_id=47
40
+ try {
41
+ json.put("canMakeReadOnly", ndef.canMakeReadOnly());
42
+ } catch (NullPointerException e) {
43
+ json.put("canMakeReadOnly", JSONObject.NULL);
44
+ }
45
+ } catch (JSONException e) {
46
+ Log.e(TAG, "Failed to convert ndef into json: " + ndef.toString(), e);
47
+ }
48
+ }
49
+ return json;
50
+ }
51
+
52
+ static JSONObject tagToJSON(Tag tag) {
53
+ JSONObject json = new JSONObject();
54
+
55
+ if (tag != null) {
56
+ try {
57
+ json.put("id", byteArrayToJSON(tag.getId()));
58
+ json.put("techTypes", new JSONArray(Arrays.asList(tag.getTechList())));
59
+ } catch (JSONException e) {
60
+ Log.e(TAG, "Failed to convert tag into json: " + tag.toString(), e);
61
+ }
62
+ }
63
+ return json;
64
+ }
65
+
66
+ static String translateType(String type) {
67
+ String translation;
68
+ if (type.equals(Ndef.NFC_FORUM_TYPE_1)) {
69
+ translation = "NFC Forum Type 1";
70
+ } else if (type.equals(Ndef.NFC_FORUM_TYPE_2)) {
71
+ translation = "NFC Forum Type 2";
72
+ } else if (type.equals(Ndef.NFC_FORUM_TYPE_3)) {
73
+ translation = "NFC Forum Type 3";
74
+ } else if (type.equals(Ndef.NFC_FORUM_TYPE_4)) {
75
+ translation = "NFC Forum Type 4";
76
+ } else {
77
+ translation = type;
78
+ }
79
+ return translation;
80
+ }
81
+
82
+ static NdefRecord[] jsonToNdefRecords(String ndefMessageAsJSON) throws JSONException {
83
+ JSONArray jsonRecords = new JSONArray(ndefMessageAsJSON);
84
+ NdefRecord[] records = new NdefRecord[jsonRecords.length()];
85
+ for (int i = 0; i < jsonRecords.length(); i++) {
86
+ JSONObject record = jsonRecords.getJSONObject(i);
87
+ byte tnf = (byte) record.getInt("tnf");
88
+ byte[] type = jsonToByteArray(record.getJSONArray("type"));
89
+ byte[] id = jsonToByteArray(record.getJSONArray("id"));
90
+ byte[] payload = jsonToByteArray(record.getJSONArray("payload"));
91
+ records[i] = new NdefRecord(tnf, type, id, payload);
92
+ }
93
+ return records;
94
+ }
95
+
96
+ static JSONArray byteArrayToJSON(byte[] bytes) {
97
+ JSONArray json = new JSONArray();
98
+ for (byte aByte : bytes) {
99
+ json.put(aByte);
100
+ }
101
+ return json;
102
+ }
103
+
104
+ static byte[] jsonToByteArray(JSONArray json) throws JSONException {
105
+ byte[] b = new byte[json.length()];
106
+ for (int i = 0; i < json.length(); i++) {
107
+ b[i] = (byte) json.getInt(i);
108
+ }
109
+ return b;
110
+ }
111
+
112
+ static JSONArray messageToJSON(NdefMessage message) {
113
+ if (message == null) {
114
+ return null;
115
+ }
116
+
117
+ List<JSONObject> list = new ArrayList<JSONObject>();
118
+
119
+ for (NdefRecord ndefRecord : message.getRecords()) {
120
+ list.add(recordToJSON(ndefRecord));
121
+ }
122
+
123
+ return new JSONArray(list);
124
+ }
125
+
126
+ static JSONObject recordToJSON(NdefRecord record) {
127
+ JSONObject json = new JSONObject();
128
+ try {
129
+ json.put("tnf", record.getTnf());
130
+ json.put("type", byteArrayToJSON(record.getType()));
131
+ json.put("id", byteArrayToJSON(record.getId()));
132
+ json.put("payload", byteArrayToJSON(record.getPayload()));
133
+ } catch (JSONException e) {
134
+ //Not sure why this would happen, documentation is unclear.
135
+ Log.e(TAG, "Failed to convert ndef record into json: " + record.toString(), e);
136
+ }
137
+ return json;
138
+ }
139
+
140
+ }
@@ -0,0 +1,148 @@
1
+ /*jshint quotmark:double, strict:false, bitwise: false */
2
+ /*global PluginResult */
3
+
4
+ // based on code from Gord Tanner https://github.com/chariotsolutions/phonegap-nfc/pull/32
5
+
6
+ var alphabet = "ABCDEFGHIJKLM" +
7
+ "NOPQRSTUVWXYZ" +
8
+ "abcdefghijklm" +
9
+ "nopqrstuvwxyz" +
10
+ "0123456789+/=";
11
+
12
+ function b64toArray(input) {
13
+ var result = [],
14
+ workValue = [],
15
+ bytes = [],
16
+ offset = 0;
17
+
18
+ while (offset < input.length) {
19
+ for (var i = 0; i < 4; ++i) {
20
+ if (offset >= input.length) {
21
+ workValue[i] = 64;
22
+ } else {
23
+ var index = alphabet.indexOf(input.substring(offset++, offset));
24
+ if (index === -1) {
25
+ --i;
26
+ continue;
27
+ }
28
+ workValue[i] = index;
29
+ }
30
+ }
31
+ bytes[0] = (workValue[0] << 2 | workValue[1] >> 4) & 255;
32
+ bytes[1] = (workValue[1] << 4 | workValue[2] >> 2) & 255;
33
+ bytes[2] = (workValue[2] << 6 | workValue[3]) & 255;
34
+
35
+ if (workValue[3] === 64 && workValue[2] === 64) {
36
+ result.push(bytes[0]);
37
+ } else if (workValue[3] === 64) {
38
+ result.push(bytes[0]);
39
+ result.push(bytes[1]);
40
+ } else {
41
+ result.push(bytes[0]);
42
+ result.push(bytes[1]);
43
+ result.push(bytes[2]);
44
+ }
45
+ }
46
+
47
+ return result;
48
+ }
49
+
50
+ function decodeNdefRecord(encoded) {
51
+
52
+ var ndefRecord = {
53
+ tnf: encoded[0] & 7
54
+ },
55
+ flags = encoded[0],
56
+ isShortRecord = (flags & 16) !== 0, // short record
57
+ hasIdLength = (flags & 8) !== 0, // identification length
58
+ offset = 1,
59
+ typeLength = encoded[offset++],
60
+ idLength = 0,
61
+ payloadLength = 0;
62
+
63
+ if (isShortRecord) {
64
+ payloadLength = encoded[offset++];
65
+ } else {
66
+ for ( var i = 0; i < 4; ++i) {
67
+ payloadLength *= 256;
68
+ payloadLength |= encoded[offset++];
69
+ }
70
+ }
71
+
72
+ if (hasIdLength) {
73
+ idLength = encoded[offset++];
74
+ }
75
+
76
+ ndefRecord.type = encoded.slice(offset, offset + typeLength);
77
+
78
+ offset += typeLength;
79
+ ndefRecord.id = encoded.slice(offset, offset + idLength);
80
+
81
+ offset += idLength;
82
+ ndefRecord.payload = encoded.slice(offset, offset + payloadLength);
83
+
84
+ return ndefRecord;
85
+ }
86
+
87
+ function decode(encoding) {
88
+ var decoded = [];
89
+ var offset = 0;
90
+
91
+ while (offset < encoding.length) {
92
+ var start = offset;
93
+ var remaining = encoding.length - offset;
94
+ var flags = encoding[offset++];
95
+ var minLength = 1 + 1;
96
+ var sr = (flags & 16) !== 0;
97
+ var il = (flags & 8) !== 0;
98
+
99
+ minLength += sr ? 1 : 4;
100
+ minLength += il ? 1 : 0;
101
+ if (minLength <= remaining) {
102
+ var typeLength = encoding[offset++];
103
+ var payloadLength = 0;
104
+ if (sr) {
105
+ payloadLength = encoding[offset++];
106
+ } else {
107
+ for ( var i = 0; i < 4; ++i) {
108
+ payloadLength <<= 8;
109
+ payloadLength |= encoding[offset++];
110
+ }
111
+ }
112
+ var idLength = il ? encoding[offset++] : 0;
113
+ var totalLength = minLength + typeLength + payloadLength + idLength;
114
+ if (totalLength <= remaining) {
115
+ var encoded = encoding.slice(start, start + totalLength);
116
+
117
+ decoded.push(decodeNdefRecord(encoded));
118
+
119
+ offset = start + totalLength;
120
+ }
121
+ }
122
+ }
123
+ return decoded;
124
+ }
125
+
126
+ // called by invoke, when NFC tag is scanned
127
+ var tagListener = function(pluginResult, payloadString) {
128
+ var payload = JSON.parse(payloadString),
129
+ ndefObjectAsString = JSON.stringify(decode(b64toArray(payload.data)));
130
+ pluginResult.callbackOk(ndefObjectAsString, true);
131
+ };
132
+
133
+ module.exports = {
134
+ init: function(success, failure, args, env) {
135
+ // no-op, just here for Android compatibility
136
+ var result = new PluginResult(args, env);
137
+ result.ok();
138
+ },
139
+ registerNdef: function(success, failure, args, env) {
140
+
141
+ var result = new PluginResult(args, env),
142
+ application = window.qnx.webplatform.getApplication(),
143
+ ndefListener = tagListener.bind(null, result);
144
+
145
+ application.invocation.addEventListener("invoked", ndefListener);
146
+ result.noResult(true);
147
+ }
148
+ };
@@ -0,0 +1,40 @@
1
+ //
2
+ // NfcPlugin.h
3
+ // PhoneGap NFC - Cordova Plugin
4
+ //
5
+ // (c) 2107-2020 Don Coleman
6
+
7
+ #ifndef NfcPlugin_h
8
+ #define NfcPlugin_h
9
+
10
+ #import <Cordova/CDV.h>
11
+ #import <CoreNFC/CoreNFC.h>
12
+ #import <WebKit/WebKit.h>
13
+
14
+ @interface NfcPlugin : CDVPlugin <NFCNDEFReaderSessionDelegate, NFCTagReaderSessionDelegate> {
15
+ }
16
+
17
+ // iOS Specific API
18
+
19
+ // deprecated use scanNdef or scanTag
20
+ - (void)beginSession:(CDVInvokedUrlCommand *)command;
21
+ // deprecated use stopScan
22
+ - (void)invalidateSession:(CDVInvokedUrlCommand *)command;
23
+
24
+ // Added iOS 13
25
+ - (void)scanNdef:(CDVInvokedUrlCommand *)command;
26
+ - (void)scanTag:(CDVInvokedUrlCommand *)command;
27
+ - (void)cancelScan:(CDVInvokedUrlCommand *)command;
28
+
29
+ // Standard PhoneGap NFC API
30
+ - (void)registerNdef:(CDVInvokedUrlCommand *)command;
31
+ - (void)removeNdef:(CDVInvokedUrlCommand *)command;
32
+ - (void)enabled:(CDVInvokedUrlCommand *)command;
33
+ - (void)writeTag:(CDVInvokedUrlCommand *)command;
34
+
35
+ // Internal implementation
36
+ - (void)channel:(CDVInvokedUrlCommand *)command;
37
+
38
+ @end
39
+
40
+ #endif