@icedevml/react-native-host-card-emulation 0.7.0 → 0.7.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 +40 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Host Card Emulation Module for React Native on iOS and Android platforms.
|
|
4
4
|
|
|
5
5
|
> [!NOTE]
|
|
6
|
-
> Pull requests are welcome! Please [report an issue](https://github.com/icedevml/react-native-host-card-emulation/issues) if you
|
|
6
|
+
> Pull requests are welcome! Please [report an issue](https://github.com/icedevml/react-native-host-card-emulation/issues) if you encounter any difficulties.
|
|
7
7
|
|
|
8
8
|
> [!IMPORTANT]
|
|
9
9
|
> As per Apple's policy, **Host Card Emulation feature is only available for users based in the European
|
|
@@ -117,11 +117,11 @@ Afterwards, follow the subsections below for each platform that you need to supp
|
|
|
117
117
|
This module provides a uniform low-level HCE API for both mobile platforms.
|
|
118
118
|
|
|
119
119
|
> [!NOTE]
|
|
120
|
-
> Raw native module's API specification is available in [packages/host-card-emulation/js](https://github.com/icedevml/react-native-host-card-emulation/
|
|
120
|
+
> Raw native module's API specification is available in [packages/host-card-emulation/js](https://github.com/icedevml/react-native-host-card-emulation/tree/master/packages/host-card-emulation/js) subdirectory.
|
|
121
121
|
> Check it out in order to understand what methods you can call against the module and what are the expected parameters/return values.
|
|
122
122
|
|
|
123
123
|
> [!NOTE]
|
|
124
|
-
> See [Demo
|
|
124
|
+
> See [Source Code for Demo Application: NDEF NFC Type 4 Tag Emulator (for iOS/Android)](https://github.com/icedevml/react-native-host-card-emulation/tree/master/packages/demo-ndef-app) example for more insights about the library's API.
|
|
125
125
|
|
|
126
126
|
### Quick start guide
|
|
127
127
|
|
|
@@ -140,7 +140,7 @@ This module provides a uniform low-level HCE API for both mobile platforms.
|
|
|
140
140
|
}
|
|
141
141
|
}, []);
|
|
142
142
|
```
|
|
143
|
-
2. When user indicates that he/she wants to perform the HCE action, call the following function from the button's onClick routine:
|
|
143
|
+
2. When the user indicates that he/she wants to perform the HCE action, call the following function from the button's onClick routine:
|
|
144
144
|
```typescript
|
|
145
145
|
await NativeHCEModule.beginSession();
|
|
146
146
|
```
|
|
@@ -163,8 +163,8 @@ This module provides a uniform low-level HCE API for both mobile platforms.
|
|
|
163
163
|
break;
|
|
164
164
|
|
|
165
165
|
case 'readerDeselected':
|
|
166
|
-
|
|
167
|
-
|
|
166
|
+
NativeHCEModule.setSessionAlertMessage('Lost reader'); // only for iOS, no-op in Android
|
|
167
|
+
break;
|
|
168
168
|
```
|
|
169
169
|
For those events, trigger mechanisms are platform dependent:
|
|
170
170
|
* iOS: The `readerDetected` event will be emitted as soon as the NFC reader's field presence is observed. The `readerDeselected` event will be emitted if the reader is physically disconnected or a non-matching AID is selected by the reader.
|
|
@@ -191,7 +191,7 @@ If you need to utilize `NFCPresentmentIntentAssertion` for enhanced user experie
|
|
|
191
191
|
```typescript
|
|
192
192
|
await NativeHCEModule.acquireExclusiveNFC();
|
|
193
193
|
```
|
|
194
|
-
This function will acquire an exclusive NFC access for 15 seconds.
|
|
194
|
+
This function will acquire an exclusive NFC access for 15 seconds. No system services or other applications will be able to interfere with NFC during that period. For example, the NFC background tag reading will be disabled so it would not generate any distracting notifications.
|
|
195
195
|
|
|
196
196
|
While both session and exclusive NFC access are active, you will receive `readerDetected` events even when the HCE emulation is not started at the time (without any explicit UI being displayed by the system). You can then make a call to `startHCE` to initiate the interaction automatically, without needing the user to click anything beforehand.
|
|
197
197
|
|
|
@@ -200,12 +200,29 @@ This function will throw an exception if:
|
|
|
200
200
|
* you are in the cooldown period where you are not allowed to acquire the presentment intent assertion (cooldown is 15 seconds after the previous assertion had expired);
|
|
201
201
|
* the feature is not supported or the device is not eligible for whatever reason;
|
|
202
202
|
|
|
203
|
+
You can check if you are holding an exclusive NFC access by calling:
|
|
204
|
+
```typescript
|
|
205
|
+
NativeHCEModule.isExclusiveNFC()
|
|
206
|
+
```
|
|
207
|
+
|
|
203
208
|
Whenever you don't need the exclusive NFC access anymore, call the following function to release the `NFCPresentmentIntentAssertion` object:
|
|
204
209
|
```typescript
|
|
205
|
-
|
|
210
|
+
NativeHCEModule.releaseExclusiveNFC();
|
|
206
211
|
```
|
|
207
212
|
|
|
208
|
-
|
|
213
|
+
> [!TIP]
|
|
214
|
+
> Your exclusive NFC access might time out after exceeding certain allowance period imposed by iOS. Thus, you should periodically poll
|
|
215
|
+
> the `NativeHCEModule.isExclusiveNFC()` value to see if it's still granted. You might decide to navigate into a different UI view
|
|
216
|
+
> once your assertion has timed out, or indicate that in some different way.
|
|
217
|
+
|
|
218
|
+
> [!TIP]
|
|
219
|
+
> Use the React's [AppState](https://reactnative.dev/docs/appstate) listener to see when your app goes out to background.
|
|
220
|
+
> In such event, call `NativeHCEModule.releaseExclusiveNFC()` to explicitly release the exclusive NFC assertion.
|
|
221
|
+
>
|
|
222
|
+
> You must do so in order to avoid running into ambigous states, since the operating system is not going to enforce your
|
|
223
|
+
> exclusive NFC access anyway, as your app has gone to background.
|
|
224
|
+
>
|
|
225
|
+
> You can attempt to acquire the exclusive access assertion again once your app has gone back to foreground (active state).
|
|
209
226
|
|
|
210
227
|
### Android: Handle HCE calls when the app is not running
|
|
211
228
|
|
|
@@ -223,7 +240,7 @@ for instance - your app may emulate an NDEF tag even when it's not launched on t
|
|
|
223
240
|
}
|
|
224
241
|
});
|
|
225
242
|
```
|
|
226
|
-
2.
|
|
243
|
+
2. Implement your background HCE handler by passing an event callback to `processBackgroundHCE()`:
|
|
227
244
|
```typescript
|
|
228
245
|
import { Buffer } from 'buffer/';
|
|
229
246
|
import {
|
|
@@ -231,6 +248,8 @@ for instance - your app may emulate an NDEF tag even when it's not launched on t
|
|
|
231
248
|
} from '@icedevml/react-native-host-card-emulation/js/hceBackground';
|
|
232
249
|
|
|
233
250
|
export default async function runBackgroundHCETask(processBackgroundHCE: ProcessBackgroundHCEFunc) {
|
|
251
|
+
// ... initialize any state keeping variables here ...
|
|
252
|
+
|
|
234
253
|
processBackgroundHCE(async (event, respondAPDU) => {
|
|
235
254
|
switch (event.type) {
|
|
236
255
|
/* ... background HCE event handler here ... */
|
|
@@ -238,7 +257,7 @@ for instance - your app may emulate an NDEF tag even when it's not launched on t
|
|
|
238
257
|
});
|
|
239
258
|
}
|
|
240
259
|
```
|
|
241
|
-
3.
|
|
260
|
+
3. Whenever you receive `readerDeselected` event, cleanup any side effects (deallocate resources, reset the state back to initial) -- this is obligatory because lack of proper cleanup will result in bugs and unexpected behavior.
|
|
242
261
|
```typescript
|
|
243
262
|
// inside processBackgroundHCE handler's switch
|
|
244
263
|
case 'readerDeselected':
|
|
@@ -259,7 +278,15 @@ for instance - your app may emulate an NDEF tag even when it's not launched on t
|
|
|
259
278
|
break;
|
|
260
279
|
```
|
|
261
280
|
|
|
281
|
+
> [!WARNING]
|
|
282
|
+
> Please note that under some circumstances you might find that your `runBackgroundHCETask()` is running in a few instances concurrently.
|
|
283
|
+
> An example of such situation could be when you receive a C-APDU and the reader rapidly restarts the NFC field and reselects your application
|
|
284
|
+
> before you manage to respond to the original command. Thus, you should not use any global variables to track the state of your app.
|
|
285
|
+
>
|
|
286
|
+
> The library automatically identifies stale (deselected) background tasks. For such stale tasks, any subsequent calls to `await respondAPDU()`
|
|
287
|
+
> will throw an error to prevent communication glitches.
|
|
288
|
+
|
|
262
289
|
### More resources
|
|
263
290
|
|
|
264
|
-
* [Module's API specification](https://github.com/icedevml/react-native-host-card-emulation/
|
|
265
|
-
* [Demo
|
|
291
|
+
* [Module's API specification](https://github.com/icedevml/react-native-host-card-emulation/tree/master/packages/host-card-emulation/js)
|
|
292
|
+
* [Source Code for Demo Application: NDEF NFC Type 4 Tag Emulator (for iOS/Android)](https://github.com/icedevml/react-native-host-card-emulation/tree/master/packages/demo-ndef-app)
|
package/package.json
CHANGED