react-native-nfc-manager 3.11.0 → 3.11.4

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,51 +32,121 @@ 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
- ## Also See
35
+ ### **Android 12**
36
36
 
37
- ### [Demo App] NfcOpenReWriter
37
+ We start to support Android 12 from `v3.11.1`, and you will need to update `compileSdkVersion` to `31`, otherwise the build will fail:
38
38
 
39
- We have a full featured NFC utility app available for download.
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) which says `Starting with Build.VERSION_CODES.S, it will be required to explicitly specify the mutability of PendingIntents`
51
+
52
+ > The original issue is [here](https://github.com/revtel/react-native-nfc-manager/issues/469)
53
+
54
+ BTW, if you don't care about **Android 12** for now, you can use **`v3.11.0`** as a short term solution.
55
+
56
+ ### **[Demo App] NfcOpenReWriter**
57
+
58
+ We have a full featured NFC utility app using this library available for download.
40
59
 
41
60
  <a href='https://apps.apple.com/tw/app/nfc-rewriter/id1551243964' target='_blank'>
42
61
  <img alt="react-native-nfc-rewriter" src="./images/Apple-App-Store-Icon.png" width="250">
43
62
  </a>
44
- <br/>
45
63
 
46
- It also open sourced in this repo: [React Native NFC ReWriter App](https://github.com/revtel/react-native-nfc-rewriter)
47
-
48
- ### [Bonus] Reconnect.js
64
+ </br>
49
65
 
50
- Super useful library to help you share states between `sibling` and `nested` React components.
66
+ <a href='https://play.google.com/store/apps/details?id=com.washow.nfcopenrewriter' target='_blank'>
67
+ <img alt="react-native-nfc-rewriter" src="./images/google-play-icon.jpeg" width="250">
68
+ </a>
51
69
 
52
- [Check it out!](https://github.com/revtel/reconnect.js)
70
+ It also open sourced in this repo: [React Native NFC ReWriter App](https://github.com/revtel/react-native-nfc-rewriter)
53
71
 
54
- ## Basic Usage
72
+ ## Usage
55
73
 
56
- If all you want to do is to read `NDEF` data, you can use this example:
74
+ The simplest (and most common) use case for this library is to read `NFC` tags containing `NDEF`, which can be achieved via the following codes:
57
75
 
58
76
  ```javascript
59
- import NfcManager, {NfcEvents} from 'react-native-nfc-manager';
77
+ import React from 'react';
78
+ import {View, Text, TouchableOpacity, StyleSheet} from 'react-native';
79
+ import NfcManager, {NfcTech} from 'react-native-nfc-manager';
60
80
 
61
81
  // Pre-step, call this before any NFC operations
62
- async function initNfc() {
63
- await NfcManager.start();
82
+ NfcManager.start();
83
+
84
+ function App() {
85
+ async function readNdef() {
86
+ try {
87
+ // register for the NFC tag with NDEF in it
88
+ await NfcManager.requestTechnology(NfcTech.Ndef);
89
+ // the resolved tag object will contain `ndefMessage` property
90
+ const tag = await NfcManager.getTag();
91
+ console.warn('Tag found', tag);
92
+ } catch (ex) {
93
+ console.warn('Oops!', ex);
94
+ } finally {
95
+ // stop the nfc scanning
96
+ NfcManager.cancelTechnologyRequest();
97
+ }
98
+ }
99
+
100
+ return (
101
+ <View style={styles.wrapper}>
102
+ <TouchableOpacity onPress={readNdef}>
103
+ <Text>Scan a Tag</Text>
104
+ </TouchableOpacity>
105
+ </View>
106
+ );
64
107
  }
65
108
 
66
- function readNdef() {
109
+ const styles = StyleSheet.create({
110
+ wrapper: {
111
+ flex: 1,
112
+ justifyContent: 'center',
113
+ alignItems: 'center',
114
+ },
115
+ });
116
+
117
+ export default App;
118
+ ```
119
+
120
+ Pleaes notice when running above codes, iOS and Android has different behaviors:
121
+ - iOS will pop up a system scanning UI
122
+ - Android provides **NO** system scanning UI
123
+
124
+ Regarding the system scannning UI, both platforms should be able to scan your NFC tags succesfully and print out its content.
125
+
126
+ ### Old Style (registerTagEvent) To Scan NFC Tags
127
+
128
+ There's an alterntaive style to scan NFC tags through `NfcManager.registerTagEvent`, like this:
129
+
130
+ ```javascript
131
+ import NfcManager, {NfcTech} from 'react-native-nfc-manager';
132
+
133
+ // The following function resolves to a NFC Tag object using old event listener approach.
134
+ // You can call it like this:
135
+ // `const nfcTag = await listenToNfcEventOnce()`
136
+
137
+ function listenToNfcEventOnce() {
67
138
  const cleanUp = () => {
68
139
  NfcManager.setEventListener(NfcEvents.DiscoverTag, null);
69
140
  NfcManager.setEventListener(NfcEvents.SessionClosed, null);
70
141
  };
71
142
 
72
- return new Promise((resolve) => {
143
+ return new Promise(resolve => {
73
144
  let tagFound = null;
74
145
 
75
- NfcManager.setEventListener(NfcEvents.DiscoverTag, (tag) => {
146
+ NfcManager.setEventListener(NfcEvents.DiscoverTag, tag => {
76
147
  tagFound = tag;
77
148
  resolve(tagFound);
78
- NfcManager.setAlertMessageIOS('NDEF tag found');
79
- NfcManager.unregisterTagEvent().catch(() => 0);
149
+ NfcManager.unregisterTagEvent();
80
150
  });
81
151
 
82
152
  NfcManager.setEventListener(NfcEvents.SessionClosed, () => {
@@ -91,113 +161,103 @@ function readNdef() {
91
161
  }
92
162
  ```
93
163
 
94
- Anything else, ex: write NDEF, send custom command, please read next section.
164
+ As you can see, the above approach is more verbose and hard-to-read, so we recommend using `NfcManager.requestTechnology` instead of `NfcManager.registerTagEvent` in your application.
165
+
166
+ ## Advanced Usage Concept
167
+
168
+ In higher level, there're 4 steps to use this library:
95
169
 
96
- ## Advanced Usage
170
+ 1. request your particular NFC technologies through `NfcManager.requestTechnology`, for example:
171
+ - `Ndef`
172
+ - `NfcA`
173
+ - `NfcB` (Android-only)
174
+ - `NfcF` (Android-only)
175
+ - `NfcV` (Android-only)
176
+ - `IsoDep`
177
+ - `MifareClassic` (Android-only)
178
+ - `MifareUltralight` (Android-only)
179
+ - `MifareIOS` (ios-only)
180
+ - `Iso15693IOS` (ios-only)
181
+ - `FelicaIOS` (ios-only)
97
182
 
98
- In high level, there're 3 steps to perform advanced NFC operations:
183
+ 2. select the proper NFC technology handler, which is implemented as getter in main `NfcManager` object, for example:
184
+ - `ndefHandler` (for `Ndef` tech)
185
+ - `nfcAHandler` (for `NfcA` tech)
186
+ - `isoDepHandler` (for `IsoDep` tech)
187
+ - `iso15693HandlerIOS` (for `Iso15693IOS` tech)
188
+ - `mifareClassicHandlerAndroid` (for `mifareClassic` tech)
189
+ - `mifareUltralightHandlerAndroid` (for `mifareUltralight` tech)
190
+ - ... and so on
99
191
 
100
- 1. request your specific NFC technology
101
- 2. select the proper NFC technology handler, which is implemented as getter in main `NfcManager` object, including:
102
- - ndefHandler
103
- - nfcAHandler
104
- - isoDepHandler
105
- - iso15693HandlerIOS
106
- - mifareClassicHandlerAndroid
107
- - mifareUltralightHandlerAndroid
108
- 3. call specific methods on the NFC technology handler
109
- 4. clean up your tech registration
192
+ 3. call specific methods on the NFC technology handler (for example `NfcManager.ndefHandler.writeNdefMessage`). To view all available methods for some tech handler, check out the [API List]((index.d.ts))
193
+
194
+ 4. clean up your tech registration through `NfcManager.cancelTechnology`
195
+
196
+ ## Advanced Usage Example: NDEF-Writing
110
197
 
111
198
  For example, here's an example to write NDEF:
112
199
 
113
200
  ```javascript
114
201
  import NfcManager, {NfcTech, Ndef} from 'react-native-nfc-manager';
115
202
 
116
- // Pre-step, call this before any NFC operations
117
- async function initNfc() {
118
- await NfcManager.start();
119
- }
120
-
121
203
  async function writeNdef({type, value}) {
122
204
  let result = false;
123
205
 
124
206
  try {
125
- // Step 1
126
- await NfcManager.requestTechnology(NfcTech.Ndef, {
127
- alertMessage: 'Ready to write some NDEF',
128
- });
207
+ // STEP 1
208
+ await NfcManager.requestTechnology(NfcTech.Ndef);
129
209
 
130
210
  const bytes = Ndef.encodeMessage([Ndef.textRecord('Hello NFC')]);
131
211
 
132
212
  if (bytes) {
133
- await NfcManager.ndefHandler // Step2
134
- .writeNdefMessage(bytes); // Step3
135
-
136
- if (Platform.OS === 'ios') {
137
- await NfcManager.setAlertMessageIOS('Successfully write NDEF');
138
- }
213
+ await NfcManager
214
+ .ndefHandler // STEP 2
215
+ .writeNdefMessage(bytes); // STEP 3
216
+ result = true;
139
217
  }
140
-
141
- result = true;
142
218
  } catch (ex) {
143
219
  console.warn(ex);
220
+ } finally {
221
+ // STEP 4
222
+ NfcManager.cancelTechnologyRequest();
144
223
  }
145
224
 
146
- // Step 4
147
- NfcManager.cancelTechnologyRequest().catch(() => 0);
148
225
  return result;
149
226
  }
150
227
  ```
151
228
 
152
- ## Advanced: Mifare Ultralight usage
229
+ ## Advanced Usage Example: Mifare Ultralight
153
230
 
154
- Here's an example to read a Mifare Ultralight tag:
231
+ Here's another example to read a Mifare Ultralight tag:
155
232
 
156
233
  ```javascript
157
- import NfcManager, {NfcTech} from 'react-native-nfc-manager';
158
-
159
- // Pre-step, call this before any NFC operations
160
- async function initNfc() {
161
- await NfcManager.start();
162
- }
163
-
164
234
  async function readMifare() {
235
+ let mifarePages = [];
236
+
165
237
  try {
166
- // 0. Request Mifare technology
167
- let reqMifare = await NfcManager.requestTechnology(NfcTech.MifareUltralight);
168
- if (reqMifare !== 'MifareUltralight') {
169
- throw new Error('[NFC Read] [ERR] Mifare technology could not be requested');
170
- };
171
-
172
- // 1. Get NFC Tag information
173
- const nfcTag = await NfcManager.getTag()
174
- console.log('[NFC Read] [INFO] Tag: ', nfcTag);
175
-
176
- // 2. Read pages
177
- const readLength = 60;
178
- let mifarePages = [];
179
- const mifarePagesRead = await Promise.all([...Array(readLength).keys()].map(async (_, i) => {
180
- const pageOffset = i * 4; // 4 Pages are read at once, so offset should be in steps with length 4
181
- let pages = await NfcManager.mifareUltralightHandlerAndroid.mifareUltralightReadPages(pageOffset);
182
- mifarePages.push(pages);
183
- console.log(`[NFC Read] [INFO] Mifare Page: ${pageOffset}`, pages);
184
- //await wait(500); // If Mifare Chip is to slow
185
- }));
186
-
187
- // 3. Success
188
- console.log('[NFC Read] [INFO] Success reading Mifare');
189
-
190
- // 4. Cleanup
191
- _cleanup();
238
+ // STEP 1
239
+ let reqMifare = await NfcManager.requestTechnology(
240
+ NfcTech.MifareUltralight,
241
+ );
242
+
243
+ const readLength = 60;
244
+ const mifarePagesRead = await Promise.all(
245
+ [...Array(readLength).keys()].map(async (_, i) => {
246
+ const pages = await NfcManager
247
+ .mifareUltralightHandlerAndroid // STEP 2
248
+ .mifareUltralightReadPages(i * 4); // STEP 3
249
+ mifarePages.push(pages);
250
+ }),
251
+ );
192
252
  } catch (ex) {
193
- console.warn('[NFC Read] [ERR] Failed Reading Mifare: ', ex);
194
- _cleanup();
253
+ console.warn(ex);
254
+ } finally {
255
+ // STEP 4
256
+ NfcManager.cancelTechnologyRequest();
195
257
  }
196
- };
197
258
 
198
- function _cleanup() {
199
- NfcManager.cancelTechnologyRequest().catch(() => 0);
200
- };
259
+ return mifarePages;
260
+ }
201
261
  ```
202
262
 
203
263
  To see more examples, please see [React Native NFC ReWriter App](https://github.com/revtel/react-native-nfc-rewriter)
@@ -252,3 +312,4 @@ The plugin provides props for extra customization. Every time you change the pro
252
312
  }
253
313
  }
254
314
  ```
315
+
@@ -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
@@ -987,6 +987,16 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
987
987
  callback.invoke(null, isForegroundEnabled);
988
988
  }
989
989
 
990
+ @ReactMethod
991
+ public void addListener(String eventName) {
992
+ // Keep: Required for RN built in Event Emitter Calls.
993
+ }
994
+
995
+ @ReactMethod
996
+ public void removeListeners(Integer count) {
997
+ // Keep: Required for RN built in Event Emitter Calls.
998
+ }
999
+
990
1000
  @Override
991
1001
  public void onHostResume() {
992
1002
  Log.d(LOG_TAG, "onResume");
@@ -1064,7 +1074,12 @@ class NfcManager extends ReactContextBaseJavaModule implements ActivityEventList
1064
1074
  Activity activity = getCurrentActivity();
1065
1075
  Intent intent = new Intent(activity, activity.getClass());
1066
1076
  intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
1067
- return PendingIntent.getActivity(activity, 0, intent, 0);
1077
+
1078
+ int flag = 0;
1079
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
1080
+ flag = PendingIntent.FLAG_MUTABLE;
1081
+ }
1082
+ return PendingIntent.getActivity(activity, 0, intent, flag);
1068
1083
  }
1069
1084
 
1070
1085
  private IntentFilter[] getIntentFilters() {
package/index.d.ts CHANGED
@@ -63,7 +63,7 @@ declare module 'react-native-nfc-manager' {
63
63
  maxSize?: number;
64
64
  type?: string;
65
65
  techTypes?: string[];
66
- id?: number[];
66
+ id?: string;
67
67
  }
68
68
 
69
69
  export interface RegisterTagEventOpts {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-nfc-manager",
3
- "version": "3.11.0",
3
+ "version": "3.11.4",
4
4
  "description": "A NFC module for react native.",
5
5
  "main": "src/index.js",
6
6
  "repository": {
package/CHANGELOG.md DELETED
@@ -1,98 +0,0 @@
1
- ## [3.0.2](https://github.com/whitedogg13/react-native-nfc-manager/compare/v3.0.1...v3.0.2) (2021-02-22)
2
-
3
-
4
- ### Features
5
-
6
- * **android:** allow requestTechnology resolves to tags even when tech ([8b7f862](https://github.com/whitedogg13/react-native-nfc-manager/commit/8b7f8629a8e46304f2a68d62f01e745047fe01dc))
7
-
8
-
9
-
10
- ## [3.0.1](https://github.com/whitedogg13/react-native-nfc-manager/compare/v3.0.0...v3.0.1) (2021-02-20)
11
-
12
-
13
- ### Bug Fixes
14
-
15
- * [#371](https://github.com/whitedogg13/react-native-nfc-manager/issues/371) missing MIFARE_BLOCK_SIZE for mifareClassicHandlerAndroid ([b136419](https://github.com/whitedogg13/react-native-nfc-manager/commit/b1364197c80ab16542bbb823483a2079a896e169))
16
-
17
-
18
-
19
- # [3.0.0](https://github.com/whitedogg13/react-native-nfc-manager/compare/v3.0.0-0...v3.0.0) (2021-02-18)
20
-
21
-
22
- ### Features
23
-
24
- * expose RTD_URI_PROTOCOLS ([26516a7](https://github.com/whitedogg13/react-native-nfc-manager/commit/26516a7ce8309ade21464df13a8e5c799c0e417c))
25
-
26
-
27
-
28
- # [3.0.0-0](https://github.com/whitedogg13/react-native-nfc-manager/compare/v2.0.0-beta.1...v3.0.0-0) (2021-02-10)
29
-
30
-
31
- ### Bug Fixes
32
-
33
- * disable 18092 polling (cause iOS freeze) ([64115f8](https://github.com/whitedogg13/react-native-nfc-manager/commit/64115f80f8cd93ed5ef3b01987d6c628a5a9127b))
34
- * **ios:** add polling for iso18092 ([b7984a5](https://github.com/whitedogg13/react-native-nfc-manager/commit/b7984a59ea27076d7fd34a2614592f468c80f5e1))
35
- * add callback type null possible ([1371533](https://github.com/whitedogg13/react-native-nfc-manager/commit/13715334ea908b89654b42eba4ec902bcc6f4229))
36
- * cancelTechnology might cause exception if there's a connected tag ([af2f1f9](https://github.com/whitedogg13/react-native-nfc-manager/commit/af2f1f92bbd7bfcf8c604db48b790289c3ca4946))
37
- * cancelTechnology might cause exception if there's a connected tag ([dcb26b8](https://github.com/whitedogg13/react-native-nfc-manager/commit/dcb26b85a955ce17ae633e00f6f189a895e8adcb))
38
- * getNdefMessage should use the same structure for platforms ([73859fb](https://github.com/whitedogg13/react-native-nfc-manager/commit/73859fbac8939277f1e7fc2ad13ececee8c9d4c7))
39
- * import NfcManager from new location ([c95cc76](https://github.com/whitedogg13/react-native-nfc-manager/commit/c95cc76a0a3b52c751c1a0f00895cf1b34b66b28))
40
- * ios isSupported ([adcc966](https://github.com/whitedogg13/react-native-nfc-manager/commit/adcc966c8ed4a565c2800fabc5e5a0cad07ea376))
41
- * promise won't be rejected when calling cancelTechnologyRequest in ([64c9cbd](https://github.com/whitedogg13/react-native-nfc-manager/commit/64c9cbd05e34e975fa4744727859efd08ea610bf))
42
- * reject tech request promise when native iOS cancel button is pressed ([7915b98](https://github.com/whitedogg13/react-native-nfc-manager/commit/7915b98132cf5c6f86eb96dfe242b67bef32e8a7))
43
- * remove getNdefMessage's parameter declaration ([6fc257e](https://github.com/whitedogg13/react-native-nfc-manager/commit/6fc257e61e1da5600a7aac44d9a8a97a7ecc988c))
44
-
45
-
46
- ### Features
47
-
48
- * extract more NfcTech handlers ([0032408](https://github.com/whitedogg13/react-native-nfc-manager/commit/0032408af21cafa3f5f92c16083f914325450524))
49
- * **android:** getNdefStatus ([4cc68ce](https://github.com/whitedogg13/react-native-nfc-manager/commit/4cc68ce233d81c03760b791fc2f568fe4b8415c4))
50
- * **ios:** enhance didDetectTags and add queryNDEFStatus / makeReadOnly ([32dd3be](https://github.com/whitedogg13/react-native-nfc-manager/commit/32dd3be2968abbe095ae42fd12793a9a8c0725c6))
51
- * invalidateSessionWithErrorIOS ([c631110](https://github.com/whitedogg13/react-native-nfc-manager/commit/c631110b6454c08821607014a28b8dffb20fe8b0))
52
- * ios 13 ndef read and write ([31a3ae6](https://github.com/whitedogg13/react-native-nfc-manager/commit/31a3ae6e141e41f3f36a102d5c2bb5ce04ba78a5))
53
- * requestTechnology support multi-techs ([65e8f02](https://github.com/whitedogg13/react-native-nfc-manager/commit/65e8f02b83820cb72e65c8443a607b6dca2c8fae))
54
-
55
-
56
-
57
- # [2.0.0-beta.1](https://github.com/whitedogg13/react-native-nfc-manager/compare/0.0.9...v2.0.0-beta.1) (2019-07-13)
58
-
59
-
60
- ### Bug Fixes
61
-
62
- * call the correct method `mifareUltralightReadPages` (notice the tailing s) ([60feee8](https://github.com/whitedogg13/react-native-nfc-manager/commit/60feee88c2d20bc18af23b3abb6d8cb22fe17e16))
63
- * ios crash on registerTagEvent ([e654d93](https://github.com/whitedogg13/react-native-nfc-manager/commit/e654d9359751327284d20b3bee1139c78657dcb4))
64
- * MifareClassic should also support `transceive` ([dd4a902](https://github.com/whitedogg13/react-native-nfc-manager/commit/dd4a902257a402e5d6d3eae8bd43f9d37c03928b))
65
- * use non-cached ndef for getNdefMessage API ([49f8541](https://github.com/whitedogg13/react-native-nfc-manager/commit/49f85415a5bc6d0218992dc6ba2b9303cee8e3ac))
66
-
67
-
68
- ### Features
69
-
70
- * mifareultralight support ([4a53a13](https://github.com/whitedogg13/react-native-nfc-manager/commit/4a53a135c594ef340927ae053dfc142ebfd0b9b6))
71
- * support `getMaxTransceiveLength` and `setTimeout` ([e759c39](https://github.com/whitedogg13/react-native-nfc-manager/commit/e759c399008e9060ae6f5fe23d7b31a61b1f8037))
72
-
73
-
74
-
75
- ## [0.0.9](https://github.com/whitedogg13/react-native-nfc-manager/compare/0.0.8...0.0.9) (2018-01-02)
76
-
77
-
78
-
79
- ## [0.0.8](https://github.com/whitedogg13/react-native-nfc-manager/compare/0.0.7...0.0.8) (2017-12-09)
80
-
81
-
82
-
83
- ## [0.0.7](https://github.com/whitedogg13/react-native-nfc-manager/compare/0.0.6...0.0.7) (2017-12-04)
84
-
85
-
86
-
87
- ## [0.0.6](https://github.com/whitedogg13/react-native-nfc-manager/compare/0.0.3...0.0.6) (2017-11-17)
88
-
89
-
90
-
91
- ## [0.0.3](https://github.com/whitedogg13/react-native-nfc-manager/compare/0.0.2...0.0.3) (2017-09-27)
92
-
93
-
94
-
95
- ## 0.0.2 (2017-08-01)
96
-
97
-
98
-