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