react-native-nfc-manager 3.10.0 → 3.11.2

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/README.md CHANGED
@@ -8,7 +8,7 @@ Bring NFC feature to React Native. Inspired by [phonegap-nfc](https://github.com
8
8
 
9
9
  Contributions are welcome!
10
10
 
11
- > We also have a slack channel, you're welcome to chat with us for any issue or idea! [join us here](https://join.slack.com/t/reactnativenf-ewh2625/shared_invite/zt-puz9y22v-3rkORO6_zQe4FmaWm6ku_w)
11
+ Made with ❤️ by [whitedogg13](https://github.com/whitedogg13) and [revteltech](https://github.com/revtel)
12
12
 
13
13
  ## Install
14
14
 
@@ -32,6 +32,27 @@ For Android, it should be properly auto-linked, so you don't need to do anything
32
32
 
33
33
  Please see [here](setup.md)
34
34
 
35
+ ### Android 12
36
+
37
+ To support Android 12, you will need to update `compileSdkVersion` in your `android/build.gradle` like this:
38
+
39
+ ```
40
+ buildscript {
41
+ ext {
42
+ ...
43
+ compileSdkVersion = 31
44
+ ...
45
+ }
46
+ ...
47
+ }
48
+ ```
49
+
50
+ The reason for this is because Android puts new limitation on [PendingIntent](https://developer.android.com/reference/android/app/PendingIntent#FLAG_MUTABLE):
51
+
52
+ > Starting with Build.VERSION_CODES.S, it will be required to explicitly specify the mutability of PendingIntents
53
+
54
+ The original issue is [here](https://github.com/revtel/react-native-nfc-manager/issues/469)
55
+
35
56
  ## Also See
36
57
 
37
58
  ### [Demo App] NfcOpenReWriter
@@ -41,15 +62,14 @@ We have a full featured NFC utility app available for download.
41
62
  <a href='https://apps.apple.com/tw/app/nfc-rewriter/id1551243964' target='_blank'>
42
63
  <img alt="react-native-nfc-rewriter" src="./images/Apple-App-Store-Icon.png" width="250">
43
64
  </a>
44
- <br/>
45
-
46
- It also open sourced in this repo: [React Native NFC ReWriter App](https://github.com/revtel/react-native-nfc-rewriter)
47
65
 
48
- ### [Bonus] Reconnect.js
66
+ </br>
49
67
 
50
- Super useful library to help you share states between `sibling` and `nested` React components.
68
+ <a href='https://play.google.com/store/apps/details?id=com.washow.nfcopenrewriter' target='_blank'>
69
+ <img alt="react-native-nfc-rewriter" src="./images/google-play-icon.jpeg" width="250">
70
+ </a>
51
71
 
52
- [Check it out!](https://github.com/revtel/reconnect.js)
72
+ It also open sourced in this repo: [React Native NFC ReWriter App](https://github.com/revtel/react-native-nfc-rewriter)
53
73
 
54
74
  ## Basic Usage
55
75
 
@@ -149,6 +169,65 @@ async function writeNdef({type, value}) {
149
169
  }
150
170
  ```
151
171
 
172
+ ## Advanced: Mifare Ultralight usage
173
+
174
+ Here's an example to read a Mifare Ultralight tag:
175
+
176
+ ```javascript
177
+ import NfcManager, {NfcTech} from 'react-native-nfc-manager';
178
+
179
+ // Pre-step, call this before any NFC operations
180
+ async function initNfc() {
181
+ await NfcManager.start();
182
+ }
183
+
184
+ async function readMifare() {
185
+ try {
186
+ // 0. Request Mifare technology
187
+ let reqMifare = await NfcManager.requestTechnology(
188
+ NfcTech.MifareUltralight,
189
+ );
190
+ if (reqMifare !== 'MifareUltralight') {
191
+ throw new Error(
192
+ '[NFC Read] [ERR] Mifare technology could not be requested',
193
+ );
194
+ }
195
+
196
+ // 1. Get NFC Tag information
197
+ const nfcTag = await NfcManager.getTag();
198
+ console.log('[NFC Read] [INFO] Tag: ', nfcTag);
199
+
200
+ // 2. Read pages
201
+ const readLength = 60;
202
+ let mifarePages = [];
203
+ const mifarePagesRead = await Promise.all(
204
+ [...Array(readLength).keys()].map(async (_, i) => {
205
+ const pageOffset = i * 4; // 4 Pages are read at once, so offset should be in steps with length 4
206
+ let pages = await NfcManager.mifareUltralightHandlerAndroid.mifareUltralightReadPages(
207
+ pageOffset,
208
+ );
209
+ mifarePages.push(pages);
210
+ console.log(`[NFC Read] [INFO] Mifare Page: ${pageOffset}`, pages);
211
+ //await wait(500); // If Mifare Chip is to slow
212
+ }),
213
+ );
214
+
215
+ // 3. Success
216
+ console.log('[NFC Read] [INFO] Success reading Mifare');
217
+
218
+ // 4. Cleanup
219
+ _cleanup();
220
+ } catch (ex) {
221
+ console.warn('[NFC Read] [ERR] Failed Reading Mifare: ', ex);
222
+ _cleanup();
223
+ }
224
+ }
225
+
226
+ function _cleanup() {
227
+ NfcManager.cancelTechnologyRequest().catch(() => 0);
228
+ }
229
+ ```
230
+
152
231
  To see more examples, please see [React Native NFC ReWriter App](https://github.com/revtel/react-native-nfc-rewriter)
153
232
 
154
233
  ## API
@@ -201,3 +280,4 @@ The plugin provides props for extra customization. Every time you change the pro
201
280
  }
202
281
  }
203
282
  ```
283
+
@@ -14,7 +14,7 @@ buildscript {
14
14
  apply plugin: 'com.android.library'
15
15
 
16
16
  android {
17
- compileSdkVersion safeExtGet('compileSdkVersion', 27)
17
+ compileSdkVersion safeExtGet('compileSdkVersion', 31)
18
18
  defaultConfig {
19
19
  minSdkVersion safeExtGet('minSdkVersion', 16)
20
20
  //noinspection OldTargetApi
@@ -58,6 +58,7 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
58
58
  private static final String ERR_NOT_REGISTERED = "you should requestTagEvent first";
59
59
  private static final String ERR_MULTI_REQ = "You can only issue one request at a time";
60
60
  private static final String ERR_NO_TECH_REQ = "no tech request available";
61
+ private static final String ERR_NO_REFERENCE = "no reference available";
61
62
  private static final String ERR_TRANSCEIVE_FAIL = "transceive fail";
62
63
  private static final String ERR_API_NOT_SUPPORT = "unsupported tag api";
63
64
  private static final String ERR_GET_ACTIVITY_FAIL = "fail to get current activity";
@@ -160,19 +161,22 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
160
161
 
161
162
  @ReactMethod
162
163
  public void getTag(Callback callback) {
163
- synchronized(this) {
164
+ synchronized (this) {
164
165
  if (techRequest != null) {
165
166
  Tag tag = techRequest.getTagHandle();
166
- WritableMap parsed = tag2React(tag);
167
- if (Arrays.asList(tag.getTechList()).contains(Ndef.class.getName())) {
168
- try {
169
- Ndef ndef = Ndef.get(tag);
170
- parsed = ndef2React(ndef, new NdefMessage[] { ndef.getCachedNdefMessage() });
171
- } catch (Exception ex) {
172
- // bypass
167
+ if (tag != null) {
168
+ WritableMap parsed = tag2React(tag);
169
+ if (Arrays.asList(tag.getTechList()).contains(Ndef.class.getName())) {
170
+ try {
171
+ Ndef ndef = Ndef.get(tag);
172
+ parsed = ndef2React(ndef, new NdefMessage[]{ndef.getCachedNdefMessage()});
173
+ } catch (Exception ex) {
174
+ }
173
175
  }
176
+ callback.invoke(null, parsed);
177
+ } else {
178
+ callback.invoke(ERR_NO_REFERENCE);
174
179
  }
175
- callback.invoke(null, parsed);
176
180
  } else {
177
181
  callback.invoke(ERR_NO_TECH_REQ);
178
182
  }
@@ -1060,7 +1064,12 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
1060
1064
  Activity activity = getCurrentActivity();
1061
1065
  Intent intent = new Intent(activity, activity.getClass());
1062
1066
  intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
1063
- return PendingIntent.getActivity(activity, 0, intent, 0);
1067
+
1068
+ int flag = 0;
1069
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
1070
+ flag = PendingIntent.FLAG_MUTABLE;
1071
+ }
1072
+ return PendingIntent.getActivity(activity, 0, intent, flag);
1064
1073
  }
1065
1074
 
1066
1075
  private IntentFilter[] getIntentFilters() {
@@ -1135,6 +1144,10 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
1135
1144
  WritableMap nfcTag = parseNfcIntent(intent);
1136
1145
  if (nfcTag != null) {
1137
1146
  if (isForegroundEnabled) {
1147
+ Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
1148
+ Intent intentNew=new Intent("TagFound");
1149
+ intentNew.putExtra("tag",tagFromIntent);
1150
+ context.sendBroadcast(intentNew);
1138
1151
  sendEvent("NfcManagerDiscoverTag", nfcTag);
1139
1152
  } else {
1140
1153
  sendEvent("NfcManagerDiscoverBackgroundTag", nfcTag);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-nfc-manager",
3
- "version": "3.10.0",
3
+ "version": "3.11.2",
4
4
  "description": "A NFC module for react native.",
5
5
  "main": "src/index.js",
6
6
  "repository": {
@@ -51,6 +51,7 @@
51
51
  "@babel/preset-react": "^7.12.10",
52
52
  "@react-native-community/eslint-config": "^2.0.0",
53
53
  "@release-it/conventional-changelog": "^2.0.1",
54
+ "dotenv-cli": "^4.0.0",
54
55
  "eslint": "^7.19.0",
55
56
  "jest": "^26.6.3",
56
57
  "jest-cli": "^26.6.3",
@@ -67,6 +68,6 @@
67
68
  "lint": "eslint .",
68
69
  "lint:fix": "eslint --fix .",
69
70
  "test": "jest -i",
70
- "release": "release-it"
71
+ "release": "dotenv release-it --"
71
72
  }
72
73
  }