@iotize/device-com-nfc.cordova 3.5.0 → 3.6.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.
@@ -1 +1 @@
1
- {"version":3,"file":"cordova-interface.js","sourceRoot":"","sources":["../../../../../src/www/cordova-interface.ts"],"names":[],"mappings":"","sourcesContent":["//\n// Copyright 2018 IoTize SAS Inc. Licensed under the MIT license.\n//\n// cordova-interface.ts\n// device-com-ble.cordova BLE Cordova Plugin\n//\n/**\n * Partial typings for phonegap nfc cordova interface class\n */\nexport interface CordovaInterface {\n /**\n * Close current nfc tag\n */\n close(): Promise<void>;\n\n /**\n * Connect to current nfc tag\n */\n connect(tech: string, timeout?: number): Promise<void>;\n\n /**\n * Transeive data using Tap NFC communication protocol\n * @param data ArrayBuffer or string of hex data for transcieve\n */\n transceiveTap(data: ArrayBuffer | string): Promise<string>;\n\n /**\n * Transeive raw data\n * @param data ArrayBuffer or string of hex data for transcieve\n */\n transceive(data: ArrayBuffer | string): Promise<string>;\n\n setTapDeviceDiscoveryEnabled(value: boolean): Promise<void>;\n \n /**\n * Begins a reading session for the given technology.\n * @param tech String representing the technology of the tap to discover.\n */\n beginSessionFromTech(tech: string, alertMessage?: string): Promise<void>;\n\n endSession(): Promise<void>;\n}\n"]}
1
+ {"version":3,"file":"cordova-interface.js","sourceRoot":"","sources":["../../../../../src/www/cordova-interface.ts"],"names":[],"mappings":"","sourcesContent":["//\n// Copyright 2018 IoTize SAS Inc. Licensed under the MIT license.\n//\n// cordova-interface.ts\n// device-com-ble.cordova BLE Cordova Plugin\n//\n/**\n * Partial typings for phonegap nfc cordova interface class\n */\nexport interface CordovaInterface {\n /**\n * Close current nfc tag\n */\n close(): Promise<void>;\n\n /**\n * Connect to current Tap nfc tag\n */\n connect(tech: string, timeout?: number): Promise<void>;\n\n /**\n * Raw NFC connect\n */\n connectRaw(tech: string, timeout?: number): Promise<void>;\n\n /**\n * Transeive data using Tap NFC communication protocol\n * @param data ArrayBuffer or string of hex data for transcieve\n */\n transceiveTap(data: ArrayBuffer | string): Promise<string>;\n\n /**\n * Transeive raw data\n * @param data ArrayBuffer or string of hex data for transcieve\n */\n transceive(data: ArrayBuffer | string): Promise<string>;\n\n setTapDeviceDiscoveryEnabled(value: boolean): Promise<void>;\n\n /**\n * Begins a reading session for the given technology.\n * @param tech String representing the technology of the tap to discover.\n */\n beginSessionFromTech(tech: string, alertMessage?: string): Promise<void>;\n\n endSession(): Promise<void>;\n}\n"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@iotize/device-com-nfc.cordova",
3
3
  "main": "bundles/iotize-device-com-nfc.cordova.umd.js",
4
- "version": "3.5.0",
4
+ "version": "3.6.0",
5
5
  "description": "Near Field Communication (NFC) Plugin. Read and write NDEF messages to NFC tags and share NDEF messages with peers.",
6
6
  "cordova": {
7
7
  "id": "@iotize/device-com-nfc.cordova",
@@ -81,7 +81,8 @@ public class NfcPlugin extends CordovaPlugin implements NfcAdapter.OnNdefPushCom
81
81
  private static final String DISABLE_READER_MODE = "disableReaderMode";
82
82
 
83
83
  // TagTechnology IsoDep, NfcA, NfcB, NfcV, NfcF, MifareClassic, MifareUltralight
84
- private static final String CONNECT = "connect";
84
+ private static final String CONNECT_TAP = "connect";
85
+ private static final String CONNECT_RAW = "connectRaw";
85
86
  private static final String CLOSE = "close";
86
87
  private static final String TRANSCEIVE_TAP = "transceiveTap";
87
88
  private static final String TRANSCEIVE = "transceive";
@@ -94,10 +95,15 @@ public class NfcPlugin extends CordovaPlugin implements NfcAdapter.OnNdefPushCom
94
95
  private static final String PREF_NFC_PAIRING_DONE_TOAST_MESSAGE = "NFCParingDoneToastMessage";
95
96
  private static final String REGISTER_NFC_TAP_DEVICE = "registerTapDevice";
96
97
  private static final String SET_TAP_DEVICE_DISCOVERY_ENABLED = "setTapDeviceDiscoveryEnabled";
98
+ private static final String ANDROID_NFC_TECH_CLASS_PASS = "android.nfc.tech.";
99
+ private static final String DEFAULT_NFC_TECH_CLASS_PASS = ANDROID_NFC_TECH_CLASS_PASS + "NfcV";
97
100
 
98
101
  @Nullable
99
102
  private TagTechnology tagTechnology = null;
100
103
 
104
+ @NonNull
105
+ private String _lastTechName = DEFAULT_NFC_TECH_CLASS_PASS;
106
+
101
107
  @Nullable
102
108
  private Class<?> tagTechnologyClass;
103
109
 
@@ -135,7 +141,7 @@ public class NfcPlugin extends CordovaPlugin implements NfcAdapter.OnNdefPushCom
135
141
  @Override
136
142
  public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
137
143
 
138
- Log.d(TAG, "execute " + action);
144
+ Log.d(TAG, "execute " + action);
139
145
 
140
146
  // showSettings can be called if NFC is disabled
141
147
  // might want to skip this if NO_NFC
@@ -218,10 +224,15 @@ public class NfcPlugin extends CordovaPlugin implements NfcAdapter.OnNdefPushCom
218
224
  // if code made it here, NFC is enabled
219
225
  callbackContext.success(STATUS_NFC_OK);
220
226
 
221
- } else if (action.equalsIgnoreCase(CONNECT)) {
227
+ } else if (action.equalsIgnoreCase(CONNECT_TAP)) {
228
+ String tech = data.getString(0);
229
+ int timeout = data.optInt(1, -1);
230
+ connectTap(tech, timeout, callbackContext);
231
+
232
+ } else if (action.equalsIgnoreCase(CONNECT_RAW)) {
222
233
  String tech = data.getString(0);
223
234
  int timeout = data.optInt(1, -1);
224
- connect(tech, timeout, callbackContext);
235
+ connectRaw(tech, timeout, callbackContext);
225
236
 
226
237
  } else if (action.equalsIgnoreCase(TRANSCEIVE)) {
227
238
  CordovaArgs args = new CordovaArgs(data); // execute is using the old signature with JSON data
@@ -320,11 +331,14 @@ public class NfcPlugin extends CordovaPlugin implements NfcAdapter.OnNdefPushCom
320
331
  NFCIntentParser parser = this.getIntentParser(intent);
321
332
  ProtocolFactory nfcProtocolFactory = new NFCProtocolFactory(parser.getTag());
322
333
  IoTizeDevice tap = IoTizeDevice.fromProtocol(nfcProtocolFactory.create(context));
334
+
335
+ boolean nfcPairingEnabled = preferences.getBoolean(PREF_ENABLE_NFC_PAIRING, true);
336
+ boolean encryptionEnabled = preferences.getBoolean(PREF_ENABLE_ENCRYPTION_WITH_NFC, false);
323
337
  tap.connect();
324
- if (preferences.getBoolean(PREF_ENABLE_NFC_PAIRING, true)) {
338
+ if (nfcPairingEnabled) {
325
339
  byte[] response = tap.nfcPairing();
326
340
  }
327
- if (preferences.getBoolean(PREF_ENABLE_ENCRYPTION_WITH_NFC, false)) {
341
+ if (encryptionEnabled) {
328
342
  tap.encryption(true, true);
329
343
  }
330
344
  return tap;
@@ -623,7 +637,7 @@ public class NfcPlugin extends CordovaPlugin implements NfcAdapter.OnNdefPushCom
623
637
  int pendingIntentFlags = 0;
624
638
  if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
625
639
  pendingIntentFlags = PendingIntent.FLAG_MUTABLE;
626
- }
640
+ }
627
641
  pendingIntent = PendingIntent.getActivity(activity, 0, intent, pendingIntentFlags);
628
642
  }
629
643
  }
@@ -880,7 +894,7 @@ public class NfcPlugin extends CordovaPlugin implements NfcAdapter.OnNdefPushCom
880
894
  if (tagTech.equals(NdefFormatable.class.getName())) {
881
895
  fireNdefFormatableEvent(tag);
882
896
  } else if (tagTech.equals(Ndef.class.getName())) { //
883
- this._fireNdefEvent(tag, messages);
897
+ this._fireNdefEvent(tag, messages);
884
898
  }
885
899
  }
886
900
  }
@@ -947,7 +961,7 @@ public class NfcPlugin extends CordovaPlugin implements NfcAdapter.OnNdefPushCom
947
961
  private void fireNdefEvent(String type, Ndef ndef, Parcelable[] messages) {
948
962
  try {
949
963
  JSONObject json = buildNdefJSON(ndef, messages);
950
- sendEvent(type, json);
964
+ sendEvent(type, json);
951
965
  } catch (Throwable e) {
952
966
  Log.w(TAG, "Failed to fire NDef event", e);
953
967
  }
@@ -1129,18 +1143,22 @@ public class NfcPlugin extends CordovaPlugin implements NfcAdapter.OnNdefPushCom
1129
1143
  * @param timeout tag timeout
1130
1144
  * @param callbackContext Cordova callback context
1131
1145
  */
1132
- private void connect(final String tech, final int timeout,
1133
- final CallbackContext callbackContext) {
1146
+ private void connectRaw(final String tech, final int timeout,
1147
+ final CallbackContext callbackContext) {
1148
+ final String fullTechName = !tech.startsWith(ANDROID_NFC_TECH_CLASS_PASS) ? ANDROID_NFC_TECH_CLASS_PASS + tech : tech;
1134
1149
  this.cordova.getThreadPool().execute(() -> {
1135
1150
  try {
1136
- this._initIntentTag(tech);
1151
+ this._lastTechName = fullTechName;
1152
+ this._initIntentTag(fullTechName);
1137
1153
 
1138
- if (nfcProtocol == null) {
1154
+ if (tagTechnology == null) {
1139
1155
  callbackContext.error("Tag does not support " + tech);
1140
1156
  return;
1141
1157
  }
1142
1158
 
1143
- nfcProtocol.connect();
1159
+ if (!tagTechnology.isConnected()) {
1160
+ tagTechnology.connect();
1161
+ }
1144
1162
  setTimeout(timeout);
1145
1163
  Log.d(TAG, "NFC Connection successful");
1146
1164
  callbackContext.success();
@@ -1154,8 +1172,38 @@ public class NfcPlugin extends CordovaPlugin implements NfcAdapter.OnNdefPushCom
1154
1172
  });
1155
1173
  }
1156
1174
 
1157
- private void _initIntentTag() throws Exception {
1158
- this._initIntentTag("android.nfc.tech.NfcV"); // TODO
1175
+ /**
1176
+ * Perform Tap NFCProtocol connect call
1177
+ *
1178
+ * @param tech TagTechnology class name e.g. 'android.nfc.tech.IsoDep' or 'android.nfc.tech.NfcV'
1179
+ * @param timeout tag timeout
1180
+ * @param callbackContext Cordova callback context
1181
+ */
1182
+ private void connectTap(final String tech, final int timeout,
1183
+ final CallbackContext callbackContext) {
1184
+ final String fullTechName = !tech.startsWith(ANDROID_NFC_TECH_CLASS_PASS) ? ANDROID_NFC_TECH_CLASS_PASS + tech : tech;
1185
+ this.cordova.getThreadPool().execute(() -> {
1186
+ try {
1187
+ this._lastTechName = fullTechName;
1188
+ this._initIntentTag(fullTechName);
1189
+
1190
+ if (nfcProtocol == null) {
1191
+ callbackContext.error("Tag does not support " + tech);
1192
+ return;
1193
+ }
1194
+
1195
+ nfcProtocol.connect();
1196
+ setTimeout(timeout);
1197
+ Log.d(TAG, "NFC Connection successful");
1198
+ callbackContext.success();
1199
+ } catch (IOException ex) {
1200
+ Log.e(TAG, "Tag connection failed", ex);
1201
+ callbackContext.error("Tag connection failed");
1202
+ } catch (Throwable e) {
1203
+ Log.e(TAG, e.getMessage(), e);
1204
+ callbackContext.error(e.getMessage());
1205
+ }
1206
+ });
1159
1207
  }
1160
1208
 
1161
1209
  private void _initIntentTag(final String tech) throws Exception {
@@ -1206,6 +1254,7 @@ public class NfcPlugin extends CordovaPlugin implements NfcAdapter.OnNdefPushCom
1206
1254
  private void close(CallbackContext callbackContext) {
1207
1255
  cordova.getThreadPool().execute(() -> {
1208
1256
  try {
1257
+ _lastTechName = DEFAULT_NFC_TECH_CLASS_PASS;
1209
1258
  if (nfcProtocol != null && nfcProtocol.isConnected()) {
1210
1259
  nfcProtocol.disconnect();
1211
1260
  }
@@ -1267,13 +1316,13 @@ public class NfcPlugin extends CordovaPlugin implements NfcAdapter.OnNdefPushCom
1267
1316
  }
1268
1317
 
1269
1318
  private void _connectIntentTag() throws Exception {
1270
- this._initIntentTag();
1319
+ this._initIntentTag(this._lastTechName);
1271
1320
  tagTechnology.connect();
1272
1321
  }
1273
1322
 
1274
1323
  private void _connectIntentTagIfNeeded() throws Exception {
1275
1324
  if (tagTechnology == null) {
1276
- this._initIntentTag();
1325
+ this._initIntentTag(this._lastTechName);
1277
1326
  tagTechnology.connect();
1278
1327
  }
1279
1328
  }
@@ -7,9 +7,13 @@ export interface CordovaInterface {
7
7
  */
8
8
  close(): Promise<void>;
9
9
  /**
10
- * Connect to current nfc tag
10
+ * Connect to current Tap nfc tag
11
11
  */
12
12
  connect(tech: string, timeout?: number): Promise<void>;
13
+ /**
14
+ * Raw NFC connect
15
+ */
16
+ connectRaw(tech: string, timeout?: number): Promise<void>;
13
17
  /**
14
18
  * Transeive data using Tap NFC communication protocol
15
19
  * @param data ArrayBuffer or string of hex data for transcieve
@@ -512,6 +512,13 @@ var nfc = {
512
512
  });
513
513
  },
514
514
 
515
+
516
+ connectRaw: function(tech, timeout) {
517
+ return new Promise(function(resolve, reject) {
518
+ cordova.exec(resolve, reject, 'NfcPlugin', 'connectRaw', [tech, timeout]);
519
+ });
520
+ },
521
+
515
522
  close: function() {
516
523
  return new Promise(function(resolve, reject) {
517
524
  cordova.exec(resolve, reject, 'NfcPlugin', 'close', []);