react-native-nfc-manager 3.9.0 → 3.11.1
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 +72 -11
- package/android/src/main/java/community/revteltech/nfc/NfcManager.java +23 -10
- package/app.plugin.js +44 -15
- package/package.json +4 -3
- package/src/NativeNfcManager.js +1 -1
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
|
-
|
|
11
|
+
Made with ❤️ by [whitedogg13](https://github.com/whitedogg13) and [revteltech](https://github.com/revtel)
|
|
12
12
|
|
|
13
13
|
## Install
|
|
14
14
|
|
|
@@ -34,22 +34,21 @@ Please see [here](setup.md)
|
|
|
34
34
|
|
|
35
35
|
## Also See
|
|
36
36
|
|
|
37
|
-
### [Demo App] NfcOpenReWriter
|
|
37
|
+
### [Demo App] NfcOpenReWriter
|
|
38
38
|
|
|
39
39
|
We have a full featured NFC utility app available for download.
|
|
40
40
|
|
|
41
41
|
<a href='https://apps.apple.com/tw/app/nfc-rewriter/id1551243964' target='_blank'>
|
|
42
42
|
<img alt="react-native-nfc-rewriter" src="./images/Apple-App-Store-Icon.png" width="250">
|
|
43
43
|
</a>
|
|
44
|
-
<br/>
|
|
45
44
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
### [Bonus] Reconnect.js
|
|
45
|
+
</br>
|
|
49
46
|
|
|
50
|
-
|
|
47
|
+
<a href='https://play.google.com/store/apps/details?id=com.washow.nfcopenrewriter' target='_blank'>
|
|
48
|
+
<img alt="react-native-nfc-rewriter" src="./images/google-play-icon.jpeg" width="250">
|
|
49
|
+
</a>
|
|
51
50
|
|
|
52
|
-
[
|
|
51
|
+
It also open sourced in this repo: [React Native NFC ReWriter App](https://github.com/revtel/react-native-nfc-rewriter)
|
|
53
52
|
|
|
54
53
|
## Basic Usage
|
|
55
54
|
|
|
@@ -149,6 +148,65 @@ async function writeNdef({type, value}) {
|
|
|
149
148
|
}
|
|
150
149
|
```
|
|
151
150
|
|
|
151
|
+
## Advanced: Mifare Ultralight usage
|
|
152
|
+
|
|
153
|
+
Here's an example to read a Mifare Ultralight tag:
|
|
154
|
+
|
|
155
|
+
```javascript
|
|
156
|
+
import NfcManager, {NfcTech} from 'react-native-nfc-manager';
|
|
157
|
+
|
|
158
|
+
// Pre-step, call this before any NFC operations
|
|
159
|
+
async function initNfc() {
|
|
160
|
+
await NfcManager.start();
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async function readMifare() {
|
|
164
|
+
try {
|
|
165
|
+
// 0. Request Mifare technology
|
|
166
|
+
let reqMifare = await NfcManager.requestTechnology(
|
|
167
|
+
NfcTech.MifareUltralight,
|
|
168
|
+
);
|
|
169
|
+
if (reqMifare !== 'MifareUltralight') {
|
|
170
|
+
throw new Error(
|
|
171
|
+
'[NFC Read] [ERR] Mifare technology could not be requested',
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// 1. Get NFC Tag information
|
|
176
|
+
const nfcTag = await NfcManager.getTag();
|
|
177
|
+
console.log('[NFC Read] [INFO] Tag: ', nfcTag);
|
|
178
|
+
|
|
179
|
+
// 2. Read pages
|
|
180
|
+
const readLength = 60;
|
|
181
|
+
let mifarePages = [];
|
|
182
|
+
const mifarePagesRead = await Promise.all(
|
|
183
|
+
[...Array(readLength).keys()].map(async (_, i) => {
|
|
184
|
+
const pageOffset = i * 4; // 4 Pages are read at once, so offset should be in steps with length 4
|
|
185
|
+
let pages = await NfcManager.mifareUltralightHandlerAndroid.mifareUltralightReadPages(
|
|
186
|
+
pageOffset,
|
|
187
|
+
);
|
|
188
|
+
mifarePages.push(pages);
|
|
189
|
+
console.log(`[NFC Read] [INFO] Mifare Page: ${pageOffset}`, pages);
|
|
190
|
+
//await wait(500); // If Mifare Chip is to slow
|
|
191
|
+
}),
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
// 3. Success
|
|
195
|
+
console.log('[NFC Read] [INFO] Success reading Mifare');
|
|
196
|
+
|
|
197
|
+
// 4. Cleanup
|
|
198
|
+
_cleanup();
|
|
199
|
+
} catch (ex) {
|
|
200
|
+
console.warn('[NFC Read] [ERR] Failed Reading Mifare: ', ex);
|
|
201
|
+
_cleanup();
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function _cleanup() {
|
|
206
|
+
NfcManager.cancelTechnologyRequest().catch(() => 0);
|
|
207
|
+
}
|
|
208
|
+
```
|
|
209
|
+
|
|
152
210
|
To see more examples, please see [React Native NFC ReWriter App](https://github.com/revtel/react-native-nfc-rewriter)
|
|
153
211
|
|
|
154
212
|
## API
|
|
@@ -179,8 +237,9 @@ Next, rebuild your app as described in the ["Adding custom native code"](https:/
|
|
|
179
237
|
|
|
180
238
|
The plugin provides props for extra customization. Every time you change the props or plugins, you'll need to rebuild (and `prebuild`) the native app. If no extra properties are added, defaults will be used.
|
|
181
239
|
|
|
182
|
-
- `nfcPermission` (_string | false_): Sets the iOS `NFCReaderUsageDescription` permission message to the `Info.plist`. Setting `false` will skip adding the permission. Defaults to `Allow $(PRODUCT_NAME) to interact with nearby NFC devices
|
|
183
|
-
- `selectIdentifiers` (_string[]_): Sets the iOS [`com.apple.developer.nfc.readersession.iso7816.select-identifiers`](https://developer.apple.com/documentation/bundleresources/information_property_list/select-identifiers)
|
|
240
|
+
- `nfcPermission` (_string | false_): Sets the iOS `NFCReaderUsageDescription` permission message to the `Info.plist`. Setting `false` will skip adding the permission. Defaults to `Allow $(PRODUCT_NAME) to interact with nearby NFC devices` (Info.plist).
|
|
241
|
+
- `selectIdentifiers` (_string[]_): Sets the iOS [`com.apple.developer.nfc.readersession.iso7816.select-identifiers`](https://developer.apple.com/documentation/bundleresources/information_property_list/select-identifiers) to a list of supported application IDs (Info.plist).
|
|
242
|
+
- `systemCodes` (_string[]_): Sets the iOS [`com.apple.developer.nfc.readersession.felica.systemcodes`](https://developer.apple.com/documentation/bundleresources/information_property_list/systemcodes) to a user provided list of FeliCa™ system codes that the app supports (Info.plist). Each system code must be a discrete value. The wild card value (`0xFF`) isn't allowed.
|
|
184
243
|
|
|
185
244
|
#### Example
|
|
186
245
|
|
|
@@ -192,10 +251,12 @@ The plugin provides props for extra customization. Every time you change the pro
|
|
|
192
251
|
"react-native-nfc-manager",
|
|
193
252
|
{
|
|
194
253
|
"nfcPermission": "Custom permission message",
|
|
195
|
-
"selectIdentifiers": ["A0000002471001"]
|
|
254
|
+
"selectIdentifiers": ["A0000002471001"],
|
|
255
|
+
"systemCodes": ["8008"]
|
|
196
256
|
}
|
|
197
257
|
]
|
|
198
258
|
]
|
|
199
259
|
}
|
|
200
260
|
}
|
|
201
261
|
```
|
|
262
|
+
|
|
@@ -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
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
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
|
-
|
|
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/app.plugin.js
CHANGED
|
@@ -4,7 +4,7 @@ const {
|
|
|
4
4
|
withEntitlementsPlist,
|
|
5
5
|
} = require('@expo/config-plugins');
|
|
6
6
|
|
|
7
|
-
const NFC_READER = '
|
|
7
|
+
const NFC_READER = 'Interact with nearby NFC devices';
|
|
8
8
|
|
|
9
9
|
function withIosPermission(c, props = {}) {
|
|
10
10
|
const {nfcPermission} = props;
|
|
@@ -18,44 +18,73 @@ function withIosPermission(c, props = {}) {
|
|
|
18
18
|
});
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
function
|
|
21
|
+
function addValuesToArray(obj, key, values) {
|
|
22
22
|
if (!Array.isArray(values) || !values.length) {
|
|
23
|
-
return
|
|
23
|
+
return obj;
|
|
24
24
|
}
|
|
25
|
-
if (!Array.isArray(
|
|
26
|
-
|
|
25
|
+
if (!Array.isArray(obj[key])) {
|
|
26
|
+
obj[key] = [];
|
|
27
27
|
}
|
|
28
28
|
// Add the required values
|
|
29
|
-
|
|
29
|
+
obj[key].push(...values);
|
|
30
30
|
|
|
31
31
|
// Remove duplicates
|
|
32
|
-
|
|
32
|
+
obj[key] = [...new Set(obj[key])];
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
// Prevent adding empty arrays to Info.plist or *.entitlements
|
|
35
|
+
if (!obj[key].length) {
|
|
36
|
+
delete obj[key];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return obj;
|
|
35
40
|
}
|
|
36
41
|
|
|
37
|
-
function
|
|
42
|
+
function withIosNfcEntitlement(c) {
|
|
38
43
|
return withEntitlementsPlist(c, (config) => {
|
|
39
44
|
// Add the required formats
|
|
40
|
-
config.modResults =
|
|
45
|
+
config.modResults = addValuesToArray(
|
|
41
46
|
config.modResults,
|
|
42
47
|
'com.apple.developer.nfc.readersession.formats',
|
|
43
48
|
['NDEF', 'TAG'],
|
|
44
49
|
);
|
|
50
|
+
|
|
51
|
+
return config;
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function withIosNfcSelectIdentifiers(c, {selectIdentifiers}) {
|
|
56
|
+
return withInfoPlist(c, (config) => {
|
|
45
57
|
// Add the user defined identifiers
|
|
46
|
-
config.modResults =
|
|
58
|
+
config.modResults = addValuesToArray(
|
|
47
59
|
config.modResults,
|
|
48
60
|
// https://developer.apple.com/documentation/bundleresources/information_property_list/select-identifiers
|
|
49
61
|
'com.apple.developer.nfc.readersession.iso7816.select-identifiers',
|
|
50
62
|
selectIdentifiers || [],
|
|
51
63
|
);
|
|
64
|
+
|
|
65
|
+
return config;
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function withIosNfcSystemCodes(c, {systemCodes}) {
|
|
70
|
+
return withInfoPlist(c, (config) => {
|
|
71
|
+
// Add the user defined identifiers
|
|
72
|
+
config.modResults = addValuesToArray(
|
|
73
|
+
config.modResults,
|
|
74
|
+
// https://developer.apple.com/documentation/bundleresources/information_property_list/systemcodes
|
|
75
|
+
'com.apple.developer.nfc.readersession.felica.systemcodes',
|
|
76
|
+
systemCodes || [],
|
|
77
|
+
);
|
|
78
|
+
|
|
52
79
|
return config;
|
|
53
80
|
});
|
|
54
81
|
}
|
|
55
82
|
|
|
56
|
-
function
|
|
57
|
-
const {nfcPermission, selectIdentifiers} = props;
|
|
58
|
-
config =
|
|
83
|
+
function withNfc(config, props = {}) {
|
|
84
|
+
const {nfcPermission, selectIdentifiers, systemCodes} = props;
|
|
85
|
+
config = withIosNfcEntitlement(config);
|
|
86
|
+
config = withIosNfcSelectIdentifiers(config, {selectIdentifiers});
|
|
87
|
+
config = withIosNfcSystemCodes(config, {systemCodes});
|
|
59
88
|
if (nfcPermission !== false) {
|
|
60
89
|
config = withIosPermission(config, props);
|
|
61
90
|
config = AndroidConfig.Permissions.withPermissions(config, [
|
|
@@ -65,4 +94,4 @@ function withNFC(config, props = {}) {
|
|
|
65
94
|
return config;
|
|
66
95
|
}
|
|
67
96
|
|
|
68
|
-
module.exports =
|
|
97
|
+
module.exports = withNfc;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-nfc-manager",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.11.1",
|
|
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",
|
|
@@ -61,12 +62,12 @@
|
|
|
61
62
|
"typescript": "^4.1.3"
|
|
62
63
|
},
|
|
63
64
|
"dependencies": {
|
|
64
|
-
"@expo/config-plugins": "^
|
|
65
|
+
"@expo/config-plugins": "^3.0.6"
|
|
65
66
|
},
|
|
66
67
|
"scripts": {
|
|
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
|
}
|
package/src/NativeNfcManager.js
CHANGED
|
@@ -8,7 +8,7 @@ function callNative(name, params = []) {
|
|
|
8
8
|
const nativeMethod = NativeNfcManager[name];
|
|
9
9
|
|
|
10
10
|
if (!nativeMethod) {
|
|
11
|
-
throw new Error(`no
|
|
11
|
+
throw new Error(`no such native method: "${name}"`);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
if (!Array.isArray(params)) {
|