community-cordova-plugin-nfc 1.5.3 → 1.5.5

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "community-cordova-plugin-nfc",
3
- "version": "1.5.3",
3
+ "version": "1.5.5",
4
4
  "description": "Community Cordova NFC Plugin - Read and write NDEF messages, advanced tag analysis, raw memory access",
5
5
  "main": "./types/index.d.ts",
6
6
  "types": "./types/index.d.ts",
package/plugin.xml CHANGED
@@ -1,4 +1,4 @@
1
- <?xml version="1.5.3" encoding="utf-8"?>
1
+ <?xml version="1.0" encoding="utf-8"?>
2
2
  <!--
3
3
  Community Cordova NFC Plugin
4
4
  Licensed under MIT License
@@ -7,7 +7,7 @@
7
7
  xmlns="http://apache.org/cordova/ns/plugins/1.0"
8
8
  xmlns:android="http://schemas.android.com/apk/res/android"
9
9
  id="community-cordova-plugin-nfc"
10
- version="1.5.3">
10
+ version="1.5.5">
11
11
 
12
12
  <name>NFC</name>
13
13
  <description>Community Cordova NFC Plugin - Read and write NDEF messages, advanced tag analysis, raw memory access</description>
@@ -120,16 +120,15 @@
120
120
  </config-file>
121
121
 
122
122
  <!-- https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_developer_nfc_readersession_formats?language=objc -->
123
+ <!-- Note: NDEF is deprecated/disallowed in iOS SDK 26.1+ (iOS 18+). Use TAG only. -->
123
124
  <config-file parent="com.apple.developer.nfc.readersession.formats" platform="ios" target="*-Debug.plist">
124
125
  <array>
125
- <string>NDEF</string>
126
- <string>TAG</string>
126
+ <string>TAG</string>
127
127
  </array>
128
128
  </config-file>
129
129
  <config-file parent="com.apple.developer.nfc.readersession.formats" platform="ios" target="*-Release.plist">
130
130
  <array>
131
- <string>NDEF</string>
132
- <string>TAG</string>
131
+ <string>TAG</string>
133
132
  </array>
134
133
  </config-file>
135
134
 
@@ -378,7 +378,24 @@ public class NfcPlugin extends CordovaPlugin {
378
378
  private void writeNdefMessage(final NdefMessage message, final Tag tag, final CallbackContext callbackContext) {
379
379
  cordova.getThreadPool().execute(() -> {
380
380
  try {
381
- Ndef ndef = Ndef.get(tag);
381
+ // Try to get the freshest tag available
382
+ Tag freshTag = tag;
383
+ Intent currentIntent = getIntent();
384
+ if (currentIntent != null) {
385
+ Tag intentTag = currentIntent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
386
+ if (intentTag != null) {
387
+ freshTag = intentTag;
388
+ }
389
+ }
390
+ // Also check savedIntent for fresh tag
391
+ if (savedIntent != null) {
392
+ Tag savedTag = savedIntent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
393
+ if (savedTag != null) {
394
+ freshTag = savedTag;
395
+ }
396
+ }
397
+
398
+ Ndef ndef = Ndef.get(freshTag);
382
399
  if (ndef != null) {
383
400
  ndef.connect();
384
401
  if (ndef.isWritable()) {
@@ -394,7 +411,7 @@ public class NfcPlugin extends CordovaPlugin {
394
411
  }
395
412
  ndef.close();
396
413
  } else {
397
- NdefFormatable formatable = NdefFormatable.get(tag);
414
+ NdefFormatable formatable = NdefFormatable.get(freshTag);
398
415
  if (formatable != null) {
399
416
  formatable.connect();
400
417
  formatable.format(message);
@@ -404,6 +421,14 @@ public class NfcPlugin extends CordovaPlugin {
404
421
  callbackContext.error("Tag doesn't support NDEF");
405
422
  }
406
423
  }
424
+ } catch (SecurityException e) {
425
+ // Tag out of date - the tag handle has expired
426
+ String errorMessage = e.getMessage();
427
+ if (errorMessage != null && errorMessage.contains("out of date")) {
428
+ callbackContext.error("Tag connection expired. Please tap the tag again and write immediately.");
429
+ } else {
430
+ callbackContext.error(errorMessage != null ? errorMessage : "Security error writing to tag");
431
+ }
407
432
  } catch (FormatException e) {
408
433
  callbackContext.error(e.getMessage());
409
434
  } catch (TagLostException e) {
@@ -550,13 +575,41 @@ public class NfcPlugin extends CordovaPlugin {
550
575
 
551
576
  if (nfcAdapter != null && !getActivity().isFinishing()) {
552
577
  try {
553
- IntentFilter[] intentFilters = getIntentFilters();
554
- String[][] techLists = getTechLists();
555
- if (intentFilters.length > 0 || techLists.length > 0) {
556
- nfcAdapter.enableForegroundDispatch(getActivity(), getPendingIntent(), intentFilters, techLists);
578
+ IntentFilter[] filters = getIntentFilters();
579
+ String[][] techs = getTechLists();
580
+
581
+ // Validate intent filters - remove any null or invalid entries
582
+ List<IntentFilter> validFilters = new ArrayList<>();
583
+ for (IntentFilter filter : filters) {
584
+ if (filter != null && filter.countActions() > 0) {
585
+ validFilters.add(filter);
586
+ }
587
+ }
588
+ IntentFilter[] cleanFilters = validFilters.toArray(new IntentFilter[0]);
589
+
590
+ // Android requires: if techLists is non-empty, filters must also be non-empty and valid
591
+ // If we have tech lists but no valid filters, add a generic TECH_DISCOVERED filter
592
+ if (techs.length > 0 && cleanFilters.length == 0) {
593
+ cleanFilters = new IntentFilter[] { new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED) };
594
+ }
595
+
596
+ // Only call enableForegroundDispatch if we have valid filters
597
+ if (cleanFilters.length > 0) {
598
+ nfcAdapter.enableForegroundDispatch(getActivity(), getPendingIntent(), cleanFilters, techs);
599
+ } else if (techs.length == 0) {
600
+ // No filters and no tech lists - use null to disable filtering (receive all tags)
601
+ nfcAdapter.enableForegroundDispatch(getActivity(), getPendingIntent(), null, null);
557
602
  }
558
603
  } catch (IllegalStateException e) {
559
604
  Log.w(TAG, "Illegal State Exception starting NFC. Assuming application is terminating.");
605
+ } catch (IllegalArgumentException e) {
606
+ Log.e(TAG, "Illegal Argument Exception starting NFC: " + e.getMessage());
607
+ // Fallback: try with null filters to receive all NFC events
608
+ try {
609
+ nfcAdapter.enableForegroundDispatch(getActivity(), getPendingIntent(), null, null);
610
+ } catch (Exception fallbackEx) {
611
+ Log.e(TAG, "Fallback enableForegroundDispatch also failed: " + fallbackEx.getMessage());
612
+ }
560
613
  }
561
614
  }
562
615
  });
@@ -30,17 +30,30 @@ public class Util {
30
30
  json.put("techTypes", new JSONArray(Arrays.asList(tag.getTechList())));
31
31
  }
32
32
 
33
- json.put("type", translateType(ndef.getType()));
34
- json.put("maxSize", ndef.getMaxSize());
35
- json.put("isWritable", ndef.isWritable());
33
+ // These calls can throw SecurityException if tag is out of date
34
+ try {
35
+ json.put("type", translateType(ndef.getType()));
36
+ json.put("maxSize", ndef.getMaxSize());
37
+ json.put("isWritable", ndef.isWritable());
38
+ } catch (SecurityException e) {
39
+ Log.w(TAG, "Tag out of date, using defaults for type/maxSize/isWritable: " + e.getMessage());
40
+ json.put("type", "Unknown");
41
+ json.put("maxSize", 0);
42
+ json.put("isWritable", false);
43
+ }
44
+
36
45
  json.put("ndefMessage", messageToJSON(ndef.getCachedNdefMessage()));
37
46
  // Workaround for bug in ICS (Android 4.0 and 4.0.1) where
38
47
  // mTag.getTagService(); of the Ndef object sometimes returns null
39
48
  // see http://issues.mroland.at/index.php?do=details&task_id=47
49
+ // Also catch SecurityException for "tag out of date" errors
40
50
  try {
41
51
  json.put("canMakeReadOnly", ndef.canMakeReadOnly());
42
52
  } catch (NullPointerException e) {
43
53
  json.put("canMakeReadOnly", JSONObject.NULL);
54
+ } catch (SecurityException e) {
55
+ Log.w(TAG, "Tag out of date for canMakeReadOnly: " + e.getMessage());
56
+ json.put("canMakeReadOnly", JSONObject.NULL);
44
57
  }
45
58
  } catch (JSONException e) {
46
59
  Log.e(TAG, "Failed to convert ndef into json: " + ndef.toString(), e);