react-native-nfc-manager 3.11.3 → 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
|
@@ -32,9 +32,9 @@ 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
|
|
35
|
+
### **Android 12**
|
|
36
36
|
|
|
37
|
-
|
|
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
39
|
```
|
|
40
40
|
buildscript {
|
|
@@ -47,17 +47,15 @@ buildscript {
|
|
|
47
47
|
}
|
|
48
48
|
```
|
|
49
49
|
|
|
50
|
-
The reason for this is because Android puts new limitation on [PendingIntent](https://developer.android.com/reference/android/app/PendingIntent#FLAG_MUTABLE)
|
|
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
51
|
|
|
52
|
-
>
|
|
52
|
+
> The original issue is [here](https://github.com/revtel/react-native-nfc-manager/issues/469)
|
|
53
53
|
|
|
54
|
-
|
|
54
|
+
BTW, if you don't care about **Android 12** for now, you can use **`v3.11.0`** as a short term solution.
|
|
55
55
|
|
|
56
|
-
|
|
56
|
+
### **[Demo App] NfcOpenReWriter**
|
|
57
57
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
We have a full featured NFC utility app available for download.
|
|
58
|
+
We have a full featured NFC utility app using this library available for download.
|
|
61
59
|
|
|
62
60
|
<a href='https://apps.apple.com/tw/app/nfc-rewriter/id1551243964' target='_blank'>
|
|
63
61
|
<img alt="react-native-nfc-rewriter" src="./images/Apple-App-Store-Icon.png" width="250">
|
|
@@ -71,32 +69,84 @@ We have a full featured NFC utility app available for download.
|
|
|
71
69
|
|
|
72
70
|
It also open sourced in this repo: [React Native NFC ReWriter App](https://github.com/revtel/react-native-nfc-rewriter)
|
|
73
71
|
|
|
74
|
-
##
|
|
72
|
+
## Usage
|
|
75
73
|
|
|
76
|
-
|
|
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:
|
|
77
75
|
|
|
78
76
|
```javascript
|
|
79
|
-
import
|
|
77
|
+
import React from 'react';
|
|
78
|
+
import {View, Text, TouchableOpacity, StyleSheet} from 'react-native';
|
|
79
|
+
import NfcManager, {NfcTech} from 'react-native-nfc-manager';
|
|
80
80
|
|
|
81
81
|
// Pre-step, call this before any NFC operations
|
|
82
|
-
|
|
83
|
-
|
|
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
|
+
);
|
|
84
107
|
}
|
|
85
108
|
|
|
86
|
-
|
|
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() {
|
|
87
138
|
const cleanUp = () => {
|
|
88
139
|
NfcManager.setEventListener(NfcEvents.DiscoverTag, null);
|
|
89
140
|
NfcManager.setEventListener(NfcEvents.SessionClosed, null);
|
|
90
141
|
};
|
|
91
142
|
|
|
92
|
-
return new Promise(
|
|
143
|
+
return new Promise(resolve => {
|
|
93
144
|
let tagFound = null;
|
|
94
145
|
|
|
95
|
-
NfcManager.setEventListener(NfcEvents.DiscoverTag,
|
|
146
|
+
NfcManager.setEventListener(NfcEvents.DiscoverTag, tag => {
|
|
96
147
|
tagFound = tag;
|
|
97
148
|
resolve(tagFound);
|
|
98
|
-
NfcManager.
|
|
99
|
-
NfcManager.unregisterTagEvent().catch(() => 0);
|
|
149
|
+
NfcManager.unregisterTagEvent();
|
|
100
150
|
});
|
|
101
151
|
|
|
102
152
|
NfcManager.setEventListener(NfcEvents.SessionClosed, () => {
|
|
@@ -111,120 +161,102 @@ function readNdef() {
|
|
|
111
161
|
}
|
|
112
162
|
```
|
|
113
163
|
|
|
114
|
-
|
|
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:
|
|
169
|
+
|
|
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)
|
|
182
|
+
|
|
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
|
|
115
191
|
|
|
116
|
-
|
|
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))
|
|
117
193
|
|
|
118
|
-
|
|
194
|
+
4. clean up your tech registration through `NfcManager.cancelTechnology`
|
|
119
195
|
|
|
120
|
-
|
|
121
|
-
2. select the proper NFC technology handler, which is implemented as getter in main `NfcManager` object, including:
|
|
122
|
-
- ndefHandler
|
|
123
|
-
- nfcAHandler
|
|
124
|
-
- isoDepHandler
|
|
125
|
-
- iso15693HandlerIOS
|
|
126
|
-
- mifareClassicHandlerAndroid
|
|
127
|
-
- mifareUltralightHandlerAndroid
|
|
128
|
-
3. call specific methods on the NFC technology handler
|
|
129
|
-
4. clean up your tech registration
|
|
196
|
+
## Advanced Usage Example: NDEF-Writing
|
|
130
197
|
|
|
131
198
|
For example, here's an example to write NDEF:
|
|
132
199
|
|
|
133
200
|
```javascript
|
|
134
201
|
import NfcManager, {NfcTech, Ndef} from 'react-native-nfc-manager';
|
|
135
202
|
|
|
136
|
-
// Pre-step, call this before any NFC operations
|
|
137
|
-
async function initNfc() {
|
|
138
|
-
await NfcManager.start();
|
|
139
|
-
}
|
|
140
|
-
|
|
141
203
|
async function writeNdef({type, value}) {
|
|
142
204
|
let result = false;
|
|
143
205
|
|
|
144
206
|
try {
|
|
145
|
-
//
|
|
146
|
-
await NfcManager.requestTechnology(NfcTech.Ndef
|
|
147
|
-
alertMessage: 'Ready to write some NDEF',
|
|
148
|
-
});
|
|
207
|
+
// STEP 1
|
|
208
|
+
await NfcManager.requestTechnology(NfcTech.Ndef);
|
|
149
209
|
|
|
150
210
|
const bytes = Ndef.encodeMessage([Ndef.textRecord('Hello NFC')]);
|
|
151
211
|
|
|
152
212
|
if (bytes) {
|
|
153
|
-
await NfcManager
|
|
154
|
-
.
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
await NfcManager.setAlertMessageIOS('Successfully write NDEF');
|
|
158
|
-
}
|
|
213
|
+
await NfcManager
|
|
214
|
+
.ndefHandler // STEP 2
|
|
215
|
+
.writeNdefMessage(bytes); // STEP 3
|
|
216
|
+
result = true;
|
|
159
217
|
}
|
|
160
|
-
|
|
161
|
-
result = true;
|
|
162
218
|
} catch (ex) {
|
|
163
219
|
console.warn(ex);
|
|
220
|
+
} finally {
|
|
221
|
+
// STEP 4
|
|
222
|
+
NfcManager.cancelTechnologyRequest();
|
|
164
223
|
}
|
|
165
224
|
|
|
166
|
-
// Step 4
|
|
167
|
-
NfcManager.cancelTechnologyRequest().catch(() => 0);
|
|
168
225
|
return result;
|
|
169
226
|
}
|
|
170
227
|
```
|
|
171
228
|
|
|
172
|
-
## Advanced: Mifare Ultralight
|
|
229
|
+
## Advanced Usage Example: Mifare Ultralight
|
|
173
230
|
|
|
174
|
-
Here's
|
|
231
|
+
Here's another example to read a Mifare Ultralight tag:
|
|
175
232
|
|
|
176
233
|
```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
234
|
async function readMifare() {
|
|
235
|
+
let mifarePages = [];
|
|
236
|
+
|
|
185
237
|
try {
|
|
186
|
-
//
|
|
238
|
+
// STEP 1
|
|
187
239
|
let reqMifare = await NfcManager.requestTechnology(
|
|
188
240
|
NfcTech.MifareUltralight,
|
|
189
241
|
);
|
|
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
242
|
|
|
200
|
-
// 2. Read pages
|
|
201
243
|
const readLength = 60;
|
|
202
|
-
let mifarePages = [];
|
|
203
244
|
const mifarePagesRead = await Promise.all(
|
|
204
245
|
[...Array(readLength).keys()].map(async (_, i) => {
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
);
|
|
246
|
+
const pages = await NfcManager
|
|
247
|
+
.mifareUltralightHandlerAndroid // STEP 2
|
|
248
|
+
.mifareUltralightReadPages(i * 4); // STEP 3
|
|
209
249
|
mifarePages.push(pages);
|
|
210
|
-
console.log(`[NFC Read] [INFO] Mifare Page: ${pageOffset}`, pages);
|
|
211
|
-
//await wait(500); // If Mifare Chip is to slow
|
|
212
250
|
}),
|
|
213
251
|
);
|
|
214
|
-
|
|
215
|
-
// 3. Success
|
|
216
|
-
console.log('[NFC Read] [INFO] Success reading Mifare');
|
|
217
|
-
|
|
218
|
-
// 4. Cleanup
|
|
219
|
-
_cleanup();
|
|
220
252
|
} catch (ex) {
|
|
221
|
-
console.warn(
|
|
222
|
-
|
|
253
|
+
console.warn(ex);
|
|
254
|
+
} finally {
|
|
255
|
+
// STEP 4
|
|
256
|
+
NfcManager.cancelTechnologyRequest();
|
|
223
257
|
}
|
|
224
|
-
}
|
|
225
258
|
|
|
226
|
-
|
|
227
|
-
NfcManager.cancelTechnologyRequest().catch(() => 0);
|
|
259
|
+
return mifarePages;
|
|
228
260
|
}
|
|
229
261
|
```
|
|
230
262
|
|
|
@@ -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");
|