@iotize/device-com-nfc.cordova 3.1.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.
Files changed (76) hide show
  1. package/LICENSE +31 -0
  2. package/README.md +673 -0
  3. package/bundles/iotize-device-com-nfc.cordova.umd.js +469 -0
  4. package/bundles/iotize-device-com-nfc.cordova.umd.js.map +1 -0
  5. package/esm2015/iotize-device-com-nfc.cordova.js +5 -0
  6. package/esm2015/iotize-device-com-nfc.cordova.js.map +1 -0
  7. package/esm2015/iotize-device-com-nfc.cordova.metadata.json +1 -0
  8. package/esm2015/iotize-device-com-nfc.cordova.ngsummary.json +1 -0
  9. package/esm2015/public_api.js +2 -0
  10. package/esm2015/public_api.js.map +1 -0
  11. package/esm2015/public_api.metadata.json +1 -0
  12. package/esm2015/public_api.ngsummary.json +1 -0
  13. package/esm2015/www/cordova-interface.js +2 -0
  14. package/esm2015/www/cordova-interface.js.map +1 -0
  15. package/esm2015/www/cordova-interface.metadata.json +1 -0
  16. package/esm2015/www/cordova-interface.ngsummary.json +1 -0
  17. package/esm2015/www/errors.js +28 -0
  18. package/esm2015/www/errors.js.map +1 -0
  19. package/esm2015/www/errors.metadata.json +1 -0
  20. package/esm2015/www/errors.ngsummary.json +1 -0
  21. package/esm2015/www/index.js +5 -0
  22. package/esm2015/www/index.js.map +1 -0
  23. package/esm2015/www/index.metadata.json +1 -0
  24. package/esm2015/www/index.ngsummary.json +1 -0
  25. package/esm2015/www/logger.js +3 -0
  26. package/esm2015/www/logger.js.map +1 -0
  27. package/esm2015/www/logger.metadata.json +1 -0
  28. package/esm2015/www/logger.ngsummary.json +1 -0
  29. package/esm2015/www/nfc-com-protocol.js +109 -0
  30. package/esm2015/www/nfc-com-protocol.js.map +1 -0
  31. package/esm2015/www/nfc-com-protocol.metadata.json +1 -0
  32. package/esm2015/www/nfc-com-protocol.ngsummary.json +1 -0
  33. package/esm2015/www/tap-ndef/definitions.js +2 -0
  34. package/esm2015/www/tap-ndef/definitions.js.map +1 -0
  35. package/esm2015/www/tap-ndef/definitions.metadata.json +1 -0
  36. package/esm2015/www/tap-ndef/definitions.ngsummary.json +1 -0
  37. package/esm2015/www/tap-ndef/index.js +3 -0
  38. package/esm2015/www/tap-ndef/index.js.map +1 -0
  39. package/esm2015/www/tap-ndef/index.metadata.json +1 -0
  40. package/esm2015/www/tap-ndef/index.ngsummary.json +1 -0
  41. package/esm2015/www/tap-ndef/parse-ndef-message.js +57 -0
  42. package/esm2015/www/tap-ndef/parse-ndef-message.js.map +1 -0
  43. package/esm2015/www/tap-ndef/parse-ndef-message.metadata.json +1 -0
  44. package/esm2015/www/tap-ndef/parse-ndef-message.ngsummary.json +1 -0
  45. package/fesm2015/iotize-device-com-nfc.cordova.js +196 -0
  46. package/fesm2015/iotize-device-com-nfc.cordova.js.map +1 -0
  47. package/iotize-device-com-nfc.cordova.d.ts +4 -0
  48. package/iotize-device-com-nfc.cordova.metadata.json +1 -0
  49. package/package.json +54 -0
  50. package/plugin.xml +97 -0
  51. package/public_api.d.ts +1 -0
  52. package/src/android/build.gradle +38 -0
  53. package/src/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  54. package/src/android/gradle/wrapper/gradle-wrapper.properties +6 -0
  55. package/src/android/gradlew +172 -0
  56. package/src/android/gradlew.bat +84 -0
  57. package/src/android/local.properties +8 -0
  58. package/src/android/src/com/chariotsolutions/nfc/plugin/JSONBuilder.java +60 -0
  59. package/src/android/src/com/chariotsolutions/nfc/plugin/NfcPlugin.java +1192 -0
  60. package/src/android/src/com/chariotsolutions/nfc/plugin/NfcPluginError.java +15 -0
  61. package/src/android/src/com/chariotsolutions/nfc/plugin/PluginResponse.java +85 -0
  62. package/src/android/src/com/chariotsolutions/nfc/plugin/Util.java +140 -0
  63. package/src/ios/AppDelegate+NFC.swift +51 -0
  64. package/src/ios/ISO15Reader.swift +376 -0
  65. package/src/ios/NFCNDEFDelegate.swift +78 -0
  66. package/src/ios/NFCPlugin-Bridging-Header.h +7 -0
  67. package/src/ios/NFCTapPlugin.swift +251 -0
  68. package/www/cordova-interface.d.ts +18 -0
  69. package/www/errors.d.ts +16 -0
  70. package/www/index.d.ts +4 -0
  71. package/www/logger.d.ts +2 -0
  72. package/www/nfc-com-protocol.d.ts +14 -0
  73. package/www/phonegap-nfc.js +885 -0
  74. package/www/tap-ndef/definitions.d.ts +25 -0
  75. package/www/tap-ndef/index.d.ts +2 -0
  76. package/www/tap-ndef/parse-ndef-message.d.ts +6 -0
@@ -0,0 +1,1192 @@
1
+ package com.chariotsolutions.nfc.plugin;
2
+
3
+ import android.app.Activity;
4
+ import android.app.PendingIntent;
5
+ import android.content.Context;
6
+ import android.content.Intent;
7
+ import android.content.IntentFilter;
8
+ import android.content.IntentFilter.MalformedMimeTypeException;
9
+ import android.net.Uri;
10
+ import android.nfc.FormatException;
11
+ import android.nfc.NdefMessage;
12
+ import android.nfc.NdefRecord;
13
+ import android.nfc.NfcAdapter;
14
+ import android.nfc.NfcEvent;
15
+ import android.nfc.Tag;
16
+ import android.nfc.TagLostException;
17
+ import android.nfc.tech.Ndef;
18
+ import android.nfc.tech.NdefFormatable;
19
+ import android.os.Bundle;
20
+ import android.os.Parcelable;
21
+ import android.util.Log;
22
+ import android.widget.Toast;
23
+
24
+ import com.iotize.android.communication.client.impl.IoTizeClient;
25
+ import com.iotize.android.communication.protocol.nfc.NFCIntentParser;
26
+ import com.iotize.android.communication.protocol.nfc.NFCProtocol;
27
+ import com.iotize.android.communication.protocol.nfc.NFCProtocolFactory;
28
+ import com.iotize.android.core.util.Helper;
29
+ import com.iotize.android.device.api.client.EncryptionAlgo;
30
+ import com.iotize.android.device.api.protocol.ProtocolFactory;
31
+ import com.iotize.android.device.device.impl.IoTizeDevice;
32
+
33
+ import org.apache.cordova.CallbackContext;
34
+ import org.apache.cordova.CordovaArgs;
35
+ import org.apache.cordova.CordovaPlugin;
36
+ import org.apache.cordova.PluginResult;
37
+ import org.json.JSONArray;
38
+ import org.json.JSONException;
39
+ import org.json.JSONObject;
40
+
41
+ import java.io.IOException;
42
+ import java.lang.reflect.Field;
43
+ import java.lang.reflect.InvocationTargetException;
44
+ import java.util.ArrayList;
45
+ import java.util.Arrays;
46
+ import java.util.Iterator;
47
+ import java.util.List;
48
+
49
+ import io.reactivex.annotations.NonNull;
50
+ import io.reactivex.annotations.Nullable;
51
+
52
+ // using wildcard imports so we can support Cordova 3.x
53
+
54
+ public class NfcPlugin extends CordovaPlugin implements NfcAdapter.OnNdefPushCompleteCallback {
55
+ private static final String REGISTER_MIME_TYPE = "registerMimeType";
56
+ private static final String REMOVE_MIME_TYPE = "removeMimeType";
57
+ private static final String REGISTER_NDEF = "registerNdef";
58
+ private static final String REMOVE_NDEF = "removeNdef";
59
+ private static final String REGISTER_NDEF_FORMATABLE = "registerNdefFormatable";
60
+ private static final String REGISTER_DEFAULT_TAG = "registerTag";
61
+ private static final String REMOVE_DEFAULT_TAG = "removeTag";
62
+ private static final String WRITE_TAG = "writeTag";
63
+ private static final String MAKE_READ_ONLY = "makeReadOnly";
64
+ private static final String ERASE_TAG = "eraseTag";
65
+ private static final String SHARE_TAG = "shareTag";
66
+ private static final String UNSHARE_TAG = "unshareTag";
67
+ private static final String HANDOVER = "handover"; // Android Beam
68
+ private static final String STOP_HANDOVER = "stopHandover";
69
+ private static final String ENABLED = "enabled";
70
+ private static final String INIT = "init";
71
+ private static final String SHOW_SETTINGS = "showSettings";
72
+
73
+ private static final String NDEF = "ndef";
74
+ private static final String NDEF_MIME = "ndef-mime";
75
+ private static final String NDEF_FORMATABLE = "ndef-formatable";
76
+ private static final String TAG_DEFAULT = "tag";
77
+
78
+ private static final String READER_MODE = "readerMode";
79
+ private static final String DISABLE_READER_MODE = "disableReaderMode";
80
+
81
+ // TagTechnology IsoDep, NfcA, NfcB, NfcV, NfcF, MifareClassic, MifareUltralight
82
+ private static final String CONNECT = "connect";
83
+ private static final String CLOSE = "close";
84
+ private static final String TRANSCEIVE = "transceive";
85
+
86
+ private static final String NFC_TAP_DEVICE = "nfc-tap-device";
87
+ private static final String PREF_ENABLE_TAP_DEVICE_DISCOVERY = "EnableNFCTapDeviceDiscovery";
88
+ private static final String PREF_TAP_DEVICE_MIME_TYPE = "NFCTapDeviceMimeType";
89
+ private static final String PREF_ENABLE_NFC_PAIRING = "EnableNFCPairing";
90
+ private static final String PREF_ENABLE_ENCRYPTION_WITH_NFC = "EnableEncryptionWithNFC";
91
+ private static final String PREF_NFC_PAIRING_DONE_TOAST_MESSAGE = "NFCParingDoneToastMessage";
92
+ private static final String REGISTER_NFC_TAP_DEVICE = "registerTapDevice";
93
+
94
+ // @Nullable
95
+ // private TagTechnology tagTechnology = null;
96
+ // private Class<?> tagTechnologyClass;
97
+
98
+ private static final String CHANNEL = "channel";
99
+
100
+ private static final String STATUS_NFC_OK = "NFC_OK";
101
+ private static final String STATUS_NO_NFC = "NO_NFC";
102
+ private static final String STATUS_NFC_DISABLED = "NFC_DISABLED";
103
+ private static final String STATUS_NDEF_PUSH_DISABLED = "NDEF_PUSH_DISABLED";
104
+
105
+ private static final String TAG = "NfcPlugin";
106
+ private final List<IntentFilter> intentFilters = new ArrayList<>();
107
+ private final ArrayList<String[]> techLists = new ArrayList<>();
108
+
109
+ private NdefMessage p2pMessage = null;
110
+ private PendingIntent pendingIntent = null;
111
+
112
+ private Intent savedIntent = null;
113
+
114
+ private CallbackContext readerModeCallback;
115
+ @Nullable
116
+ private CallbackContext channelCallback;
117
+ private CallbackContext shareTagCallback;
118
+ private CallbackContext handoverCallback;
119
+
120
+ @Nullable
121
+ private NFCProtocol nfcProtocol;
122
+ @Nullable
123
+ private IoTizeDevice mLastTapDiscovered;
124
+ @Nullable
125
+ private Intent mLastTapDiscoveredIntent;
126
+
127
+ @Override
128
+ protected void pluginInitialize() {
129
+ super.pluginInitialize();
130
+ if (isTapDeviceDiscoveryEnabled()) {
131
+ parseMessage();
132
+ }
133
+ }
134
+
135
+ @Override
136
+ public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
137
+
138
+ Log.d(TAG, "execute " + action);
139
+
140
+ // showSettings can be called if NFC is disabled
141
+ // might want to skip this if NO_NFC
142
+ if (action.equalsIgnoreCase(SHOW_SETTINGS)) {
143
+ showSettings(callbackContext);
144
+ return true;
145
+ }
146
+
147
+ // the channel is set up when the plugin starts
148
+ if (action.equalsIgnoreCase(CHANNEL)) {
149
+ channelCallback = callbackContext;
150
+ return true; // short circuit
151
+ }
152
+
153
+ // allow reader mode to be disabled even if nfc is disabled
154
+ if (action.equalsIgnoreCase(DISABLE_READER_MODE)) {
155
+ disableReaderMode(callbackContext);
156
+ return true; // short circuit
157
+ }
158
+
159
+ if (!getNfcStatus().equals(STATUS_NFC_OK)) {
160
+ callbackContext.error(getNfcStatus());
161
+ return true; // short circuit
162
+ }
163
+
164
+ createPendingIntent();
165
+
166
+ if (action.equalsIgnoreCase(READER_MODE)) {
167
+ int flags = data.getInt(0);
168
+ readerMode(flags, callbackContext);
169
+
170
+ } else if (action.equalsIgnoreCase(REGISTER_MIME_TYPE)) {
171
+ registerMimeType(data, callbackContext);
172
+ } else if (action.equalsIgnoreCase(REGISTER_NFC_TAP_DEVICE)) {
173
+ // JSONObject jsonStringOptions = data.getJSONObject(0);
174
+ registerTapDevice(callbackContext);
175
+ } else if (action.equalsIgnoreCase(REMOVE_MIME_TYPE)) {
176
+ removeMimeType(data, callbackContext);
177
+
178
+ } else if (action.equalsIgnoreCase(REGISTER_NDEF)) {
179
+ registerNdef(callbackContext);
180
+ } else if (action.equalsIgnoreCase(REMOVE_NDEF)) {
181
+ removeNdef(callbackContext);
182
+
183
+ } else if (action.equalsIgnoreCase(REGISTER_NDEF_FORMATABLE)) {
184
+ registerNdefFormatable(callbackContext);
185
+
186
+ } else if (action.equals(REGISTER_DEFAULT_TAG)) {
187
+ registerDefaultTag(callbackContext);
188
+
189
+ } else if (action.equals(REMOVE_DEFAULT_TAG)) {
190
+ removeDefaultTag(callbackContext);
191
+
192
+ } else if (action.equalsIgnoreCase(WRITE_TAG)) {
193
+ writeTag(data, callbackContext);
194
+
195
+ } else if (action.equalsIgnoreCase(MAKE_READ_ONLY)) {
196
+ makeReadOnly(callbackContext);
197
+
198
+ } else if (action.equalsIgnoreCase(ERASE_TAG)) {
199
+ eraseTag(callbackContext);
200
+
201
+ } else if (action.equalsIgnoreCase(SHARE_TAG)) {
202
+ shareTag(data, callbackContext);
203
+
204
+ } else if (action.equalsIgnoreCase(UNSHARE_TAG)) {
205
+ unshareTag(callbackContext);
206
+
207
+ } else if (action.equalsIgnoreCase(HANDOVER)) {
208
+ handover(data, callbackContext);
209
+
210
+ } else if (action.equalsIgnoreCase(STOP_HANDOVER)) {
211
+ stopHandover(callbackContext);
212
+
213
+ } else if (action.equalsIgnoreCase(INIT)) {
214
+ init(callbackContext);
215
+
216
+ } else if (action.equalsIgnoreCase(ENABLED)) {
217
+ // status is checked before every call
218
+ // if code made it here, NFC is enabled
219
+ callbackContext.success(STATUS_NFC_OK);
220
+
221
+ } else if (action.equalsIgnoreCase(CONNECT)) {
222
+ String tech = data.getString(0);
223
+ int timeout = data.optInt(1, -1);
224
+ connect(tech, timeout, callbackContext);
225
+
226
+ } else if (action.equalsIgnoreCase(TRANSCEIVE)) {
227
+ CordovaArgs args = new CordovaArgs(data); // execute is using the old signature with JSON data
228
+
229
+ byte[] command = args.getArrayBuffer(0);
230
+ transceive(command, callbackContext);
231
+
232
+ } else if (action.equalsIgnoreCase(CLOSE)) {
233
+ close(callbackContext);
234
+
235
+ } else {
236
+ // invalid action
237
+ return false;
238
+ }
239
+
240
+ return true;
241
+ }
242
+
243
+ private String getNfcStatus() {
244
+ NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
245
+ if (nfcAdapter == null) {
246
+ return STATUS_NO_NFC;
247
+ } else if (!nfcAdapter.isEnabled()) {
248
+ return STATUS_NFC_DISABLED;
249
+ } else {
250
+ return STATUS_NFC_OK;
251
+ }
252
+ }
253
+
254
+ private void readerMode(int flags, CallbackContext callbackContext) {
255
+ Bundle extras = new Bundle(); // not used
256
+ readerModeCallback = callbackContext;
257
+ getActivity().runOnUiThread(() -> {
258
+ NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
259
+ nfcAdapter.enableReaderMode(getActivity(), callback, flags, extras);
260
+ });
261
+
262
+ }
263
+
264
+ private void disableReaderMode(CallbackContext callbackContext) {
265
+ getActivity().runOnUiThread(() -> {
266
+ readerModeCallback = null;
267
+ NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
268
+ if (nfcAdapter != null) {
269
+ nfcAdapter.disableReaderMode(getActivity());
270
+ }
271
+ callbackContext.success();
272
+ });
273
+ }
274
+
275
+ private NfcAdapter.ReaderCallback callback = new NfcAdapter.ReaderCallback() {
276
+ @Override
277
+ public void onTagDiscovered(Tag tag) {
278
+
279
+ JSONObject json;
280
+
281
+ // If the tag supports Ndef, try and return an Ndef message
282
+ List<String> techList = Arrays.asList(tag.getTechList());
283
+ if (techList.contains(Ndef.class.getName())) {
284
+ Ndef ndef = Ndef.get(tag);
285
+ json = Util.ndefToJSON(ndef);
286
+ } else {
287
+ json = Util.tagToJSON(tag);
288
+ }
289
+
290
+ PluginResult result = new PluginResult(PluginResult.Status.OK, json);
291
+ result.setKeepCallback(true);
292
+ readerModeCallback.sendPluginResult(result);
293
+
294
+ }
295
+ };
296
+
297
+ @NonNull
298
+ private NFCIntentParser getIntentParser(Intent intent) {
299
+ NFCIntentParser parser = new NFCIntentParser(intent);
300
+ Tag tag = parser.getTag();
301
+ if (tag == null) {
302
+ Log.wtf(TAG, "Intent has a nfc tag null. Intent = " + intent);
303
+ throw new IllegalArgumentException("NFC tag is null. Retry nfc tap ?");
304
+ }
305
+ return parser;
306
+ }
307
+
308
+ private IoTizeDevice createTapFromIntent(Intent intent) throws Exception {
309
+ Context context = getActivity();
310
+ NFCIntentParser parser = this.getIntentParser(intent);
311
+ ProtocolFactory nfcProtocolFactory = new NFCProtocolFactory(parser.getTag());
312
+ IoTizeDevice tap = IoTizeDevice.fromProtocol(nfcProtocolFactory.create(context));
313
+ tap.connect();
314
+ if (preferences.getBoolean(PREF_ENABLE_NFC_PAIRING, true)) {
315
+ byte[] response = tap.nfcPairing();
316
+ }
317
+ if (preferences.getBoolean(PREF_ENABLE_ENCRYPTION_WITH_NFC, false)) {
318
+ tap.encryption(true);
319
+ }
320
+ return tap;
321
+ }
322
+
323
+ /**
324
+ * @param intent
325
+ * @return true if nfc intent has been handld
326
+ */
327
+ private boolean onTapDeviceDiscoveredIntent(Intent intent) {
328
+ try {
329
+ Log.d(TAG, "creating tap device...");
330
+ IoTizeDevice tap = this.createTapFromIntent(intent);
331
+ mLastTapDiscovered = tap;
332
+ mLastTapDiscoveredIntent = intent;
333
+ String nfcPairingDoneUserFeedback = preferences.getString(PREF_NFC_PAIRING_DONE_TOAST_MESSAGE, "NFC pairing done!");
334
+ if (nfcPairingDoneUserFeedback.length() > 0) {
335
+ Activity activity = cordova.getActivity();
336
+ if (activity != null) {
337
+ activity.runOnUiThread(() -> {
338
+ try {
339
+ Toast.makeText(activity, nfcPairingDoneUserFeedback, Toast.LENGTH_LONG).show();
340
+ } catch (Throwable err) {
341
+ Log.w(TAG, err.getMessage(), err);
342
+ }
343
+ });
344
+ }
345
+ }
346
+ fireTapDeviceEvent(tap, intent);
347
+ return true;
348
+ } catch (Exception e) {
349
+ Log.w(TAG, e.getMessage(), e);
350
+ return false;
351
+ }
352
+
353
+ }
354
+
355
+ private void registerDefaultTag(CallbackContext callbackContext) {
356
+ addTagFilter();
357
+ restartNfc();
358
+ callbackContext.success();
359
+ }
360
+
361
+ private void removeDefaultTag(CallbackContext callbackContext) {
362
+ removeTagFilter();
363
+ restartNfc();
364
+ callbackContext.success();
365
+ }
366
+
367
+ private void registerNdefFormatable(CallbackContext callbackContext) {
368
+ addTechList(new String[]{NdefFormatable.class.getName()});
369
+ restartNfc();
370
+ callbackContext.success();
371
+ }
372
+
373
+ private void registerNdef(CallbackContext callbackContext) {
374
+ addTechList(new String[]{Ndef.class.getName()});
375
+ restartNfc();
376
+ callbackContext.success();
377
+ }
378
+
379
+ private void removeNdef(CallbackContext callbackContext) {
380
+ removeTechList(new String[]{Ndef.class.getName()});
381
+ restartNfc();
382
+ callbackContext.success();
383
+ }
384
+
385
+ private void unshareTag(CallbackContext callbackContext) {
386
+ p2pMessage = null;
387
+ stopNdefPush();
388
+ shareTagCallback = null;
389
+ callbackContext.success();
390
+ }
391
+
392
+ private void init(CallbackContext callbackContext) {
393
+ Log.d(TAG, "Enabling plugin " + getIntent());
394
+
395
+ startNfc();
396
+ if (!recycledIntent()) {
397
+ parseMessage();
398
+ }
399
+ callbackContext.success();
400
+ }
401
+
402
+ private void removeMimeType(JSONArray data, CallbackContext callbackContext) throws JSONException {
403
+ String mimeType = data.getString(0);
404
+ removeIntentFilter(mimeType);
405
+ restartNfc();
406
+ callbackContext.success();
407
+ }
408
+
409
+ private void registerMimeType(JSONArray data, CallbackContext callbackContext) throws JSONException {
410
+ String mimeType = "";
411
+ try {
412
+ mimeType = data.getString(0);
413
+ intentFilters.add(createIntentFilter(mimeType));
414
+ restartNfc();
415
+ callbackContext.success();
416
+ } catch (MalformedMimeTypeException e) {
417
+ callbackContext.error("Invalid MIME Type " + mimeType);
418
+ }
419
+ }
420
+
421
+ private void registerTapDevice(CallbackContext callbackContext) throws JSONException {
422
+ Log.d(TAG, "registerTapDevice");
423
+ if (mLastTapDiscovered != null) {
424
+ Log.d(TAG, "a tap was detected before function call registerTapDevice");
425
+ fireTapDeviceEvent(mLastTapDiscovered, mLastTapDiscoveredIntent);
426
+ }
427
+ callbackContext.success();
428
+ }
429
+
430
+ private void initializeTapDeviceListener() {
431
+ if (this.isTapDeviceDiscoveryEnabled()) {
432
+ addTechList(new String[]{Ndef.class.getName()});
433
+ String mimeType = getTapDeviceMimeType();
434
+ try {
435
+ if (mimeType != null) {
436
+ intentFilters.add(createIntentFilter(mimeType));
437
+ }
438
+ } catch (MalformedMimeTypeException e) {
439
+ Log.e(TAG, "MalformedMimeTypeException " + e.getMessage(), e);
440
+ }
441
+
442
+ }
443
+
444
+ }
445
+
446
+ private String getTapDeviceMimeType() {
447
+ return preferences.getString(PREF_TAP_DEVICE_MIME_TYPE, null);
448
+ }
449
+
450
+ private boolean isTapDeviceDiscoveryEnabled() {
451
+ return preferences.getBoolean(PREF_ENABLE_TAP_DEVICE_DISCOVERY, false);
452
+ }
453
+
454
+ // Cheating and writing an empty record. We may actually be able to erase some tag types.
455
+ private void eraseTag(CallbackContext callbackContext) {
456
+ Tag tag = savedIntent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
457
+ NdefRecord[] records = {
458
+ new NdefRecord(NdefRecord.TNF_EMPTY, new byte[0], new byte[0], new byte[0])
459
+ };
460
+ writeNdefMessage(new NdefMessage(records), tag, callbackContext);
461
+ }
462
+
463
+ private void writeTag(JSONArray data, CallbackContext callbackContext) throws JSONException {
464
+ if (getIntent() == null) { // TODO remove this and handle LostTag
465
+ callbackContext.error("Failed to write tag, received null intent");
466
+ }
467
+
468
+ Tag tag = savedIntent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
469
+ NdefRecord[] records = Util.jsonToNdefRecords(data.getString(0));
470
+ writeNdefMessage(new NdefMessage(records), tag, callbackContext);
471
+ }
472
+
473
+ private void writeNdefMessage(final NdefMessage message, final Tag tag,
474
+ final CallbackContext callbackContext) {
475
+ cordova.getThreadPool().execute(() -> {
476
+ try {
477
+ Ndef ndef = Ndef.get(tag);
478
+ if (ndef != null) {
479
+ ndef.connect();
480
+
481
+ if (ndef.isWritable()) {
482
+ int size = message.toByteArray().length;
483
+ if (ndef.getMaxSize() < size) {
484
+ callbackContext.error("Tag capacity is " + ndef.getMaxSize() +
485
+ " bytes, message is " + size + " bytes.");
486
+ } else {
487
+ ndef.writeNdefMessage(message);
488
+ callbackContext.success();
489
+ }
490
+ } else {
491
+ callbackContext.error("Tag is read only");
492
+ }
493
+ ndef.close();
494
+ } else {
495
+ NdefFormatable formatable = NdefFormatable.get(tag);
496
+ if (formatable != null) {
497
+ formatable.connect();
498
+ formatable.format(message);
499
+ callbackContext.success();
500
+ formatable.close();
501
+ } else {
502
+ callbackContext.error("Tag doesn't support NDEF");
503
+ }
504
+ }
505
+ } catch (FormatException e) {
506
+ callbackContext.error(e.getMessage());
507
+ } catch (TagLostException e) {
508
+ callbackContext.error(e.getMessage());
509
+ } catch (IOException e) {
510
+ callbackContext.error(e.getMessage());
511
+ }
512
+ });
513
+ }
514
+
515
+ private void makeReadOnly(final CallbackContext callbackContext) {
516
+
517
+ if (getIntent() == null) { // Lost Tag
518
+ callbackContext.error("Failed to make tag read only, received null intent");
519
+ return;
520
+ }
521
+
522
+ final Tag tag = savedIntent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
523
+ if (tag == null) {
524
+ callbackContext.error("Failed to make tag read only, tag is null");
525
+ return;
526
+ }
527
+
528
+ cordova.getThreadPool().execute(() -> {
529
+ boolean success = false;
530
+ String message = "Could not make tag read only";
531
+
532
+ Ndef ndef = Ndef.get(tag);
533
+
534
+ try {
535
+ if (ndef != null) {
536
+
537
+ ndef.connect();
538
+
539
+ if (!ndef.isWritable()) {
540
+ message = "Tag is not writable";
541
+ } else if (ndef.canMakeReadOnly()) {
542
+ success = ndef.makeReadOnly();
543
+ } else {
544
+ message = "Tag can not be made read only";
545
+ }
546
+
547
+ } else {
548
+ message = "Tag is not NDEF";
549
+ }
550
+
551
+ } catch (IOException e) {
552
+ Log.e(TAG, "Failed to make tag read only", e);
553
+ if (e.getMessage() != null) {
554
+ message = e.getMessage();
555
+ } else {
556
+ message = e.toString();
557
+ }
558
+ }
559
+
560
+ if (success) {
561
+ callbackContext.success();
562
+ } else {
563
+ callbackContext.error(message);
564
+ }
565
+ });
566
+ }
567
+
568
+ private void shareTag(JSONArray data, CallbackContext callbackContext) throws JSONException {
569
+ NdefRecord[] records = Util.jsonToNdefRecords(data.getString(0));
570
+ this.p2pMessage = new NdefMessage(records);
571
+
572
+ startNdefPush(callbackContext);
573
+ }
574
+
575
+ // setBeamPushUris
576
+ // Every Uri you provide must have either scheme 'file' or scheme 'content'.
577
+ // Note that this takes priority over setNdefPush
578
+ //
579
+ // See http://developer.android.com/reference/android/nfc/NfcAdapter.html#setBeamPushUris(android.net.Uri[],%20android.app.Activity)
580
+ private void handover(JSONArray data, CallbackContext callbackContext) throws JSONException {
581
+
582
+ Uri[] uri = new Uri[data.length()];
583
+
584
+ for (int i = 0; i < data.length(); i++) {
585
+ uri[i] = Uri.parse(data.getString(i));
586
+ }
587
+
588
+ startNdefBeam(callbackContext, uri);
589
+ }
590
+
591
+ private void stopHandover(CallbackContext callbackContext) {
592
+ stopNdefBeam();
593
+ handoverCallback = null;
594
+ callbackContext.success();
595
+ }
596
+
597
+ private void showSettings(CallbackContext callbackContext) {
598
+ if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
599
+ Intent intent = new Intent(android.provider.Settings.ACTION_NFC_SETTINGS);
600
+ getActivity().startActivity(intent);
601
+ } else {
602
+ Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
603
+ getActivity().startActivity(intent);
604
+ }
605
+ callbackContext.success();
606
+ }
607
+
608
+ private void createPendingIntent() {
609
+ if (pendingIntent == null) {
610
+ Activity activity = getActivity();
611
+ Intent intent = new Intent(activity, activity.getClass());
612
+ intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
613
+ pendingIntent = PendingIntent.getActivity(activity, 0, intent, 0);
614
+ }
615
+ }
616
+
617
+ private void addTechList(String[] list) {
618
+ this.addTechFilter();
619
+ this.addToTechList(list);
620
+ }
621
+
622
+ private void removeTechList(String[] list) {
623
+ this.removeTechFilter();
624
+ this.removeFromTechList(list);
625
+ }
626
+
627
+ private void addTechFilter() {
628
+ intentFilters.add(new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED));
629
+ }
630
+
631
+ private void removeTechFilter() {
632
+ Iterator<IntentFilter> iterator = intentFilters.iterator();
633
+ while (iterator.hasNext()) {
634
+ IntentFilter intentFilter = iterator.next();
635
+ if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intentFilter.getAction(0))) {
636
+ iterator.remove();
637
+ }
638
+ }
639
+ }
640
+
641
+ private void addTagFilter() {
642
+ intentFilters.add(new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED));
643
+ }
644
+
645
+ private void removeTagFilter() {
646
+ Iterator<IntentFilter> iterator = intentFilters.iterator();
647
+ while (iterator.hasNext()) {
648
+ IntentFilter intentFilter = iterator.next();
649
+ if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intentFilter.getAction(0))) {
650
+ iterator.remove();
651
+ }
652
+ }
653
+ }
654
+
655
+ private void restartNfc() {
656
+ stopNfc();
657
+ startNfc();
658
+ }
659
+
660
+ private void startNfc() {
661
+ createPendingIntent(); // onResume can call startNfc before execute
662
+
663
+ getActivity().runOnUiThread(() -> {
664
+ NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
665
+
666
+ if (nfcAdapter != null && !getActivity().isFinishing()) {
667
+ try {
668
+ IntentFilter[] intentFilters = getIntentFilters();
669
+ String[][] techLists = getTechLists();
670
+ // don't start NFC unless some intent filters or tech lists have been added,
671
+ // because empty lists act as wildcards and receives ALL scan events
672
+ if (intentFilters.length > 0 || techLists.length > 0) {
673
+ nfcAdapter.enableForegroundDispatch(getActivity(), getPendingIntent(), intentFilters, techLists);
674
+ }
675
+
676
+ if (p2pMessage != null) {
677
+ nfcAdapter.setNdefPushMessage(p2pMessage, getActivity());
678
+ }
679
+ } catch (IllegalStateException e) {
680
+ // issue 110 - user exits app with home button while nfc is initializing
681
+ Log.w(TAG, "Illegal State Exception starting NFC. Assuming application is terminating.");
682
+ }
683
+
684
+ }
685
+ });
686
+ }
687
+
688
+ private void stopNfc() {
689
+ Log.d(TAG, "stopNfc");
690
+ getActivity().runOnUiThread(() -> {
691
+
692
+ NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
693
+
694
+ if (nfcAdapter != null) {
695
+ try {
696
+ nfcAdapter.disableForegroundDispatch(getActivity());
697
+ } catch (IllegalStateException e) {
698
+ // issue 125 - user exits app with back button while nfc
699
+ Log.w(TAG, "Illegal State Exception stopping NFC. Assuming application is terminating.");
700
+ }
701
+ }
702
+ });
703
+ }
704
+
705
+ private void startNdefBeam(final CallbackContext callbackContext, final Uri[] uris) {
706
+ getActivity().runOnUiThread(() -> {
707
+
708
+ NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
709
+
710
+ if (nfcAdapter == null) {
711
+ callbackContext.error(STATUS_NO_NFC);
712
+ } else if (!nfcAdapter.isNdefPushEnabled()) {
713
+ callbackContext.error(STATUS_NDEF_PUSH_DISABLED);
714
+ } else {
715
+ nfcAdapter.setOnNdefPushCompleteCallback(NfcPlugin.this, getActivity());
716
+ try {
717
+ nfcAdapter.setBeamPushUris(uris, getActivity());
718
+
719
+ PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
720
+ result.setKeepCallback(true);
721
+ handoverCallback = callbackContext;
722
+ callbackContext.sendPluginResult(result);
723
+
724
+ } catch (IllegalArgumentException e) {
725
+ callbackContext.error(e.getMessage());
726
+ }
727
+ }
728
+ });
729
+ }
730
+
731
+ private void startNdefPush(final CallbackContext callbackContext) {
732
+ getActivity().runOnUiThread(() -> {
733
+
734
+ NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
735
+
736
+ if (nfcAdapter == null) {
737
+ callbackContext.error(STATUS_NO_NFC);
738
+ } else if (!nfcAdapter.isNdefPushEnabled()) {
739
+ callbackContext.error(STATUS_NDEF_PUSH_DISABLED);
740
+ } else {
741
+ nfcAdapter.setNdefPushMessage(p2pMessage, getActivity());
742
+ nfcAdapter.setOnNdefPushCompleteCallback(NfcPlugin.this, getActivity());
743
+
744
+ PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
745
+ result.setKeepCallback(true);
746
+ shareTagCallback = callbackContext;
747
+ callbackContext.sendPluginResult(result);
748
+ }
749
+ });
750
+ }
751
+
752
+ private void stopNdefPush() {
753
+ getActivity().runOnUiThread(() -> {
754
+
755
+ NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
756
+
757
+ if (nfcAdapter != null) {
758
+ nfcAdapter.setNdefPushMessage(null, getActivity());
759
+ }
760
+
761
+ });
762
+ }
763
+
764
+ private void stopNdefBeam() {
765
+ getActivity().runOnUiThread(() -> {
766
+
767
+ NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
768
+
769
+ if (nfcAdapter != null) {
770
+ nfcAdapter.setBeamPushUris(null, getActivity());
771
+ }
772
+
773
+ });
774
+ }
775
+
776
+ private void addToTechList(String[] techs) {
777
+ techLists.add(techs);
778
+ }
779
+
780
+ private void removeFromTechList(String[] techs) {
781
+ Iterator<String[]> iterator = techLists.iterator();
782
+ while (iterator.hasNext()) {
783
+ String[] list = iterator.next();
784
+ if (Arrays.equals(list, techs)) {
785
+ iterator.remove();
786
+ }
787
+ }
788
+ }
789
+
790
+ private void removeIntentFilter(String mimeType) {
791
+ Iterator<IntentFilter> iterator = intentFilters.iterator();
792
+ while (iterator.hasNext()) {
793
+ IntentFilter intentFilter = iterator.next();
794
+ String mt = intentFilter.getDataType(0);
795
+ if (mimeType.equals(mt)) {
796
+ iterator.remove();
797
+ }
798
+ }
799
+ }
800
+
801
+ private IntentFilter createIntentFilter(String mimeType) throws MalformedMimeTypeException {
802
+ IntentFilter intentFilter = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
803
+ intentFilter.addDataType(mimeType);
804
+ return intentFilter;
805
+ }
806
+
807
+ private PendingIntent getPendingIntent() {
808
+ return pendingIntent;
809
+ }
810
+
811
+ private IntentFilter[] getIntentFilters() {
812
+ return intentFilters.toArray(new IntentFilter[intentFilters.size()]);
813
+ }
814
+
815
+ private String[][] getTechLists() {
816
+ //noinspection ToArrayCallWithZeroLengthArrayArgument
817
+ return techLists.toArray(new String[0][0]);
818
+ }
819
+
820
+ private void parseMessage() {
821
+ cordova.getThreadPool().execute(() -> {
822
+ Log.d(TAG, "parseMessage " + getIntent());
823
+ Intent intent = getIntent();
824
+ String action = intent.getAction();
825
+ Log.d(TAG, "action " + action);
826
+ if (action == null) {
827
+ return;
828
+ }
829
+
830
+ Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
831
+ Parcelable[] messages = intent.getParcelableArrayExtra((NfcAdapter.EXTRA_NDEF_MESSAGES));
832
+
833
+ if (isTapDeviceDiscoveryEnabled() && tag != null) {
834
+ onTapDeviceDiscoveredIntent(intent);
835
+ }
836
+
837
+ if (action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {
838
+ Ndef ndef = Ndef.get(tag);
839
+ fireNdefEvent(NDEF_MIME, ndef, messages);
840
+ Log.d(TAG, "Saving Intent for connect" + intent);
841
+ savedIntent = intent;
842
+
843
+ } else if (action.equals(NfcAdapter.ACTION_TECH_DISCOVERED)) {
844
+ for (String tagTech : tag.getTechList()) {
845
+ Log.d(TAG, tagTech);
846
+ if (tagTech.equals(NdefFormatable.class.getName())) {
847
+ fireNdefFormatableEvent(tag);
848
+ } else if (tagTech.equals(Ndef.class.getName())) { //
849
+ Ndef ndef = Ndef.get(tag);
850
+ fireNdefEvent(NDEF, ndef, messages);
851
+ }
852
+ }
853
+ }
854
+
855
+ if (action.equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
856
+ fireTagEvent(tag);
857
+ }
858
+
859
+ setIntent(new Intent());
860
+ });
861
+ }
862
+
863
+ private boolean notifyWhenNfcPairingDone() {
864
+ return true; // TODO
865
+ }
866
+
867
+ private void sendEvent(String type, JSONObject tag, JSONObject tap) {
868
+ try {
869
+ JSONObject event = new JSONObject();
870
+ event.put("type", type); // TAG_DEFAULT, NDEF, NDEF_MIME, NDEF_FORMATABLE
871
+ event.put("tag", tag); // JSON representing the NFC tag and NDEF messages
872
+ event.put("tap", tap);
873
+ sendEvent(event);
874
+ } catch (JSONException e) {
875
+ Log.e(TAG, "Error sending NFC event through the channel", e);
876
+ }
877
+
878
+ }
879
+
880
+ private void sendEvent(String type, JSONObject tag) {
881
+ try {
882
+ JSONObject event = new JSONObject();
883
+ event.put("type", type); // TAG_DEFAULT, NDEF, NDEF_MIME, NDEF_FORMATABLE
884
+ event.put("tag", tag); // JSON representing the NFC tag and NDEF messages
885
+ sendEvent(event);
886
+ } catch (JSONException e) {
887
+ Log.e(TAG, "Error sending NFC event through the channel", e);
888
+ }
889
+ }
890
+
891
+ // Send the event data through a channel so the JavaScript side can fire the event
892
+ private void sendEvent(JSONObject event) {
893
+ PluginResult result = new PluginResult(PluginResult.Status.OK, event);
894
+ result.setKeepCallback(true);
895
+ if (channelCallback != null) {
896
+ channelCallback.sendPluginResult(result);
897
+ }
898
+ }
899
+
900
+ private void fireNdefEvent(String type, Ndef ndef, Parcelable[] messages) {
901
+ JSONObject json = buildNdefJSON(ndef, messages);
902
+ sendEvent(type, json);
903
+ }
904
+
905
+ private void fireTapDeviceEvent(IoTizeDevice tap, Intent intent) {
906
+ try {
907
+ Log.d(TAG, "fireTapDeviceEvent " + tap);
908
+ Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
909
+ Ndef ndef = Ndef.get(tag);
910
+ Parcelable[] messages = intent.getParcelableArrayExtra((NfcAdapter.EXTRA_NDEF_MESSAGES));
911
+
912
+ sendEvent(NFC_TAP_DEVICE, buildNdefJSON(ndef, messages), buildTapJSON(tap));
913
+ } catch (JSONException e) {
914
+ Log.e(TAG, e.getMessage(), e);
915
+ }
916
+ }
917
+
918
+ private JSONObject buildTapJSON(IoTizeDevice tap) throws JSONException {
919
+ JSONObject tapInfo = new JSONObject();
920
+ tapInfo.put("nfcPairingDone", true); // TODO
921
+ JSONObject encryptionJSON = new JSONObject();
922
+ boolean encryptionEnabled = false;
923
+ if (tap.isEncryptionEnabled()) {
924
+ EncryptionAlgo encryptionAlgo = tap.getClient().getEncryptionAlgo();
925
+ if (encryptionAlgo != null) {
926
+ encryptionEnabled = true;
927
+ encryptionJSON.put("enabled", true);
928
+ JSONObject keysOptions = new JSONObject();
929
+ keysOptions.put("sessionKey", Util.byteArrayToJSON(encryptionAlgo.getKey()));
930
+ int frameCounter = 0;
931
+ try {
932
+ IoTizeClient client = tap.getClient();
933
+ Field field = client.getClass().getDeclaredField("cmdCounter");
934
+ field.setAccessible(true);
935
+ frameCounter = (int) field.get(client);
936
+ } catch (NoSuchFieldException e) {
937
+ Log.w(TAG, e.getMessage(), e);
938
+ } catch (IllegalAccessException e) {
939
+ Log.w(TAG, e.getMessage(), e);
940
+ }
941
+ keysOptions.put("sessionKeyHex", Helper.ByteArrayToHexString(encryptionAlgo.getKey()));
942
+ // keysOptions.put("ivEncode", Util.byteArrayToJSON(());
943
+ // keysOptions.put("ivDecode", Util.byteArrayToJSON(());
944
+
945
+ encryptionJSON.put("keys", keysOptions);
946
+ encryptionJSON.put("frameCounter", frameCounter);
947
+ }
948
+ }
949
+ encryptionJSON.put("enabled", encryptionEnabled);
950
+ tapInfo.put("encryption", encryptionJSON);
951
+ return tapInfo;
952
+ }
953
+
954
+ private void fireNdefFormatableEvent(Tag tag) {
955
+ sendEvent(NDEF_FORMATABLE, Util.tagToJSON(tag));
956
+ }
957
+
958
+ private void fireTagEvent(Tag tag) {
959
+ sendEvent(TAG_DEFAULT, Util.tagToJSON(tag));
960
+ }
961
+
962
+ private JSONObject buildNdefJSON(Ndef ndef, Parcelable[] messages) {
963
+
964
+ JSONObject json = Util.ndefToJSON(ndef);
965
+
966
+ // ndef is null for peer-to-peer
967
+ // ndef and messages are null for ndef format-able
968
+ if (ndef == null && messages != null) {
969
+
970
+ try {
971
+
972
+ if (messages.length > 0) {
973
+ NdefMessage message = (NdefMessage) messages[0];
974
+ json.put("ndefMessage", Util.messageToJSON(message));
975
+ // guessing type, would prefer a more definitive way to determine type
976
+ json.put("type", "NDEF Push Protocol");
977
+ }
978
+
979
+ if (messages.length > 1) {
980
+ Log.wtf(TAG, "Expected one ndefMessage but found " + messages.length);
981
+ }
982
+
983
+ } catch (JSONException e) {
984
+ // shouldn't happen
985
+ Log.e(Util.TAG, "Failed to convert ndefMessage into json", e);
986
+ }
987
+ }
988
+ return json;
989
+ }
990
+
991
+ private boolean recycledIntent() { // TODO this is a kludge, find real solution
992
+
993
+ int flags = getIntent().getFlags();
994
+ if ((flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) {
995
+ Log.i(TAG, "Launched from history, killing recycled intent");
996
+ setIntent(new Intent());
997
+ return true;
998
+ }
999
+ return false;
1000
+ }
1001
+
1002
+ @Override
1003
+ public void onStart() {
1004
+ super.onStart();
1005
+ this.initializeTapDeviceListener();
1006
+ }
1007
+
1008
+ @Override
1009
+ public void onPause(boolean multitasking) {
1010
+ Log.d(TAG, "onPause " + getIntent());
1011
+ super.onPause(multitasking);
1012
+ if (multitasking) {
1013
+ // nfc can't run in background
1014
+ stopNfc();
1015
+ }
1016
+ }
1017
+
1018
+ @Override
1019
+ public void onResume(boolean multitasking) {
1020
+ Log.d(TAG, "onResume " + getIntent());
1021
+ super.onResume(multitasking);
1022
+ startNfc();
1023
+ }
1024
+
1025
+ @Override
1026
+ public void onNewIntent(Intent intent) {
1027
+ Log.d(TAG, "onNewIntent " + intent);
1028
+ super.onNewIntent(intent);
1029
+ setIntent(intent);
1030
+ savedIntent = intent;
1031
+ parseMessage();
1032
+ }
1033
+
1034
+ private Activity getActivity() {
1035
+ return this.cordova.getActivity();
1036
+ }
1037
+
1038
+ private Intent getIntent() {
1039
+ return getActivity().getIntent();
1040
+ }
1041
+
1042
+ private void setIntent(Intent intent) {
1043
+ getActivity().setIntent(intent);
1044
+ }
1045
+
1046
+ @Override
1047
+ public void onNdefPushComplete(NfcEvent event) {
1048
+
1049
+ // handover (beam) take precedence over share tag (ndef push)
1050
+ if (handoverCallback != null) {
1051
+ PluginResult result = new PluginResult(PluginResult.Status.OK, "Beamed Message to Peer");
1052
+ result.setKeepCallback(true);
1053
+ handoverCallback.sendPluginResult(result);
1054
+ } else if (shareTagCallback != null) {
1055
+ PluginResult result = new PluginResult(PluginResult.Status.OK, "Shared Message with Peer");
1056
+ result.setKeepCallback(true);
1057
+ shareTagCallback.sendPluginResult(result);
1058
+ }
1059
+
1060
+ }
1061
+
1062
+ /**
1063
+ * Enable I/O operations to the tag from this TagTechnology object.
1064
+ * *
1065
+ *
1066
+ * @param tech TagTechnology class name e.g. 'android.nfc.tech.IsoDep' or 'android.nfc.tech.NfcV'
1067
+ * @param timeout tag timeout
1068
+ * @param callbackContext Cordova callback context
1069
+ */
1070
+ private void connect(final String tech, final int timeout,
1071
+ final CallbackContext callbackContext) {
1072
+ this.cordova.getThreadPool().execute(() -> {
1073
+ try {
1074
+
1075
+ Tag tag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
1076
+ if (tag == null) {
1077
+ tag = savedIntent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
1078
+ }
1079
+
1080
+ if (tag == null) {
1081
+ Log.e(TAG, "No Tag");
1082
+ callbackContext.error("No Tag");
1083
+ return;
1084
+ }
1085
+
1086
+ // get technologies supported by this tag
1087
+ List<String> techList = Arrays.asList(tag.getTechList());
1088
+ if (techList.contains(tech)) {
1089
+ // use reflection to call the static function Tech.get(tag)
1090
+ // tagTechnologyClass = Class.forName(tech);
1091
+ nfcProtocol = NFCProtocol.create(tag);
1092
+ // Method method = tagTechnologyClass.getMethod("get", Tag.class);
1093
+ // tagTechnology = (TagTechnology) method.invoke(null, tag);
1094
+ }
1095
+
1096
+ if (nfcProtocol == null) {
1097
+ callbackContext.error("Tag does not support " + tech);
1098
+ return;
1099
+ }
1100
+
1101
+ nfcProtocol.connect();
1102
+ setTimeout(timeout);
1103
+ Log.d(TAG, "NFC Connection succesful");
1104
+ callbackContext.success();
1105
+
1106
+ } catch (IOException ex) {
1107
+ Log.e(TAG, "Tag connection failed", ex);
1108
+ callbackContext.error("Tag connection failed");
1109
+
1110
+ // Users should never get these reflection errors
1111
+ } catch (ClassNotFoundException e) {
1112
+ Log.e(TAG, e.getMessage(), e);
1113
+ callbackContext.error(e.getMessage());
1114
+ } catch (NoSuchMethodException e) {
1115
+ Log.e(TAG, e.getMessage(), e);
1116
+ callbackContext.error(e.getMessage());
1117
+ } catch (IllegalAccessException e) {
1118
+ Log.e(TAG, e.getMessage(), e);
1119
+ callbackContext.error(e.getMessage());
1120
+ } catch (InvocationTargetException e) {
1121
+ Log.e(TAG, e.getMessage(), e);
1122
+ callbackContext.error(e.getMessage());
1123
+ } catch (Exception e) {
1124
+ Log.e(TAG, e.getMessage(), e);
1125
+ callbackContext.error(e.getMessage());
1126
+ }
1127
+ });
1128
+ }
1129
+
1130
+ private void setTimeout(int timeout) {
1131
+ if (timeout < 0) {
1132
+ return;
1133
+ }
1134
+ if (nfcProtocol != null) {
1135
+ nfcProtocol.getConfiguration().connectionTimeoutMillis = timeout;
1136
+ }
1137
+ }
1138
+
1139
+ /**
1140
+ * Disable I/O operations to the tag from this TagTechnology object, and release resources.
1141
+ *
1142
+ * @param callbackContext Cordova callback context
1143
+ */
1144
+ private void close(CallbackContext callbackContext) {
1145
+ cordova.getThreadPool().execute(() -> {
1146
+ try {
1147
+ if (nfcProtocol != null && nfcProtocol.isConnected()) {
1148
+ nfcProtocol.disconnect();
1149
+ }
1150
+ callbackContext.success();
1151
+
1152
+ } catch (Exception ex) {
1153
+ Log.e(TAG, "Error closing nfc connection", ex);
1154
+ callbackContext.error("Error closing nfc connection " + ex.getLocalizedMessage());
1155
+ }
1156
+ });
1157
+ }
1158
+
1159
+ /**
1160
+ * Send raw commands to the tag and receive the response.
1161
+ *
1162
+ * @param data byte[] command to be passed to the tag
1163
+ * @param callbackContext Cordova callback context
1164
+ */
1165
+ private void transceive(final byte[] data, final CallbackContext callbackContext) {
1166
+ cordova.getThreadPool().execute(() -> {
1167
+ try {
1168
+ if (nfcProtocol == null) {
1169
+ Log.e(TAG, "No Tech");
1170
+ callbackContext.error("No Tech");
1171
+ return;
1172
+ }
1173
+ if (!nfcProtocol.isConnected()) {
1174
+ Log.e(TAG, "Not connected");
1175
+ callbackContext.error("Not connected");
1176
+ return;
1177
+ }
1178
+
1179
+ // Use reflection so we can support many tag types
1180
+ //Method transceiveMethod = tagTechnologyClass.getMethod("transceive", byte[].class);
1181
+ // @SuppressWarnings("PrimitiveArrayArgumentToVarargsMethod")
1182
+ // byte[] response = (byte[]) transceiveMethod.invoke(tagTechnology, data);
1183
+ byte[] response = nfcProtocol.send(data);
1184
+ callbackContext.success(Helper.ByteArrayToHexString(response));
1185
+ } catch (Exception e) {
1186
+ Log.e(TAG, e.getMessage(), e);
1187
+ callbackContext.error(e.getMessage());
1188
+ }
1189
+ });
1190
+ }
1191
+
1192
+ }