Skip to content

Commit 0585383

Browse files
authored
Update README.md (#30)
1 parent 4f3bf54 commit 0585383

1 file changed

Lines changed: 63 additions & 26 deletions

File tree

packages/native-hce-module/README.md

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,17 @@ Make sure that your project has it enabled. Note that the New Architecture is en
2323
*(no prerequisite steps are required for Android)*
2424

2525
## Installation
26-
Install `@icedevml/react-native-host-card-emulation` package within your React Native project. Then, follow the subsections below for each platform that you need to support.
26+
Install this package within your React Native project:
27+
28+
```
29+
yarn add @icedevml/react-native-host-card-emulation
30+
```
31+
or using npm
32+
```
33+
npm install --save @icedevml/react-native-host-card-emulation
34+
```
35+
36+
Afterwards, follow the subsections below for each platform that you need to support.
2737

2838
### iOS
2939
1. Make sure to set up a proper signing team and bundle identifier in "Signing & Capabilities" configuration section of your project.
@@ -100,30 +110,46 @@ Install `@icedevml/react-native-host-card-emulation` package within your React N
100110

101111
This module provides a uniform low-level HCE API for both mobile platforms.
102112

103-
1. Subscribe to the event stream (there is a single global event stream for the entire module):
113+
> [!NOTE]
114+
> Raw native module's API specification is available in [packages/native-hce-module/js/NativeHCEModule.ts](https://github.com/icedevml/react-native-host-card-emulation/blob/master/packages/native-hce-module/js/NativeHCEModule.ts).
115+
> Check it out in order to understand what methods you can call against the module and what are the expected parameters/return values.
116+
117+
> [!NOTE]
118+
> See [Demo App's Code (Example Library Usage)](https://github.com/icedevml/react-native-host-card-emulation/blob/master/packages/demo-hce-module-app/App.tsx) example for more insignt about the library's API.
119+
120+
### Quick start guide
121+
122+
1. Subscribe to the event stream. There is a single global event stream, so you can subscribe early and remain subscribed to events for the entire application's lifetime.
104123
```typescript
105-
NativeHCEModule?.onEvent(async (event: HCEModuleEvent) => {
106-
switch (event.type) {
107-
/* ... your handler here ... */
124+
useEffect(() => {
125+
const removeListener = NativeHCEModule?.onEvent(async (event: HCEModuleEvent) => {
126+
switch (event.type) {
127+
/* ... implement event handlers here ... */
128+
}
129+
});
130+
131+
// cleanup the event listener when the effect is unmounted
132+
return () => {
133+
removeListener();
108134
}
109-
});
135+
}, []);
110136
```
111-
2. When user indicates that he wants to perform the HCE action, call the following function from the button's onClick routine:
137+
2. When user indicates that he/she wants to perform the HCE action, call the following function from the button's onClick routine:
112138
```typescript
113139
await NativeHCEModule?.beginSession();
114140
```
115-
This will emit `sessionStarted` event on both platforms. You can start the HCE straight from that event:
141+
This will emit `sessionStarted` event right after the function call, on both iOS and Android platforms. After the session is started, you can decide to start HCE emulation right away in the event handler:
116142
```typescript
117143
// inside NativeHCEModule?.onEvent handler's switch
118144
case 'sessionStarted':
119145
NativeHCEModule?.setSessionAlertMessage('Tap towards the reader'); // only for iOS, no-op in Android
120146
await NativeHCEModule?.startHCE();
121147
break;
122148
```
123-
3. Calling `startHCE()` causes:
124-
* iOS: Launch of the NFC scan system UI. The smartphone will start listening for C-APDUs from the readers right after that UI appears on screen. The `readerDetected` event will be emitted as soon as the NFC tag 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.
125-
* Android: No user interface appears (you have to implement it on your own scanning UI, within your app). The `readerDetected` event will be emitted as soon as the first matching SELECT AID command is observed. The `readerDeselected` event will be emitted if the reader is physically disconnected or a non-matching AID is selected by the reader.
126-
4. Optionally, you can handle `readerDetected` and `readerDeselected events to enhance user's experience.
149+
Calling `await NativeHCEModule?.startHCE()` causes:
150+
* iOS: Your smartphone will start listening for C-APDUs (Command APDUs originating from reader devices) right after that function is called, which will be additionally indicated by the operating system popping out the NFC scanning user interface prompt.
151+
* Android: HCE commands will be forwarded to your application from that point on. No specific user interface is displayed (you have to implement it on your own, within your app).
152+
3. Optionally, you can handle `readerDetected` and `readerDeselected` events to enhance user's experience.
127153
```typescript
128154
// inside NativeHCEModule?.onEvent handler's switch
129155
case 'readerDetected':
@@ -134,29 +160,40 @@ This module provides a uniform low-level HCE API for both mobile platforms.
134160
NativeHCEModule?.setSessionAlertMessage('Lost reader'); // only for iOS, no-op in Android
135161
break;
136162
```
137-
5. Handle CAPDU
163+
For those events, trigger mechanisms are platform dependent:
164+
* iOS: The `readerDetected` event will be emitted as soon as the NFC tag 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.
165+
* Android: The `readerDetected` event will be emitted as soon as the first matching SELECT AID command is observed. The `readerDeselected` event will be emitted if the reader is physically disconnected or a non-matching AID is selected by the reader.
166+
4. Receive incoming C-APDU and respond to it:
138167
```typescript
139168
// inside NativeHCEModule?.onEvent handler's switch
140169
case 'received':
141-
NativeHCEModule?.setSessionAlertMessage('Keep holding the tag');
170+
NativeHCEModule?.setSessionAlertMessage('Keep holding the tag'); // only for iOS, no-op on Android
142171

172+
// decode incoming C-APDU to bytes
143173
const capdu = Buffer.from(event.arg!, 'hex');
144174
console.log('Received C-APDU, capdu.toString('hex'));
145175

146-
// let's say we always want to respond with [0x0A] + Status Word 0x9000
176+
// for the demo purposes, we always want to respond with [0x0A] + status code 0x9000 (success)
147177
await NativeHCEModule?.respondAPDU(Buffer.from([0x0A, 0x90, 0x00], "hex"));
148178
break;
149179
```
150-
6. (iOS) Optionally, if you need to utilize `NFCPresentmentIntentAssertion` then at any time you can call:
151-
```typescript
152-
NativeHCEModule?.acquireExclusiveNFC();
153-
```
154-
Which will throw an exception if the presentment intent assertion was already acquired and is still valid
155-
(usually 15 seconds) or if the cooldown period for acquiring new assertion was not yet expired (usually also 15 seconds).
180+
You don't have to respond to the APDU right away from within the event handler, but please remember that the reader might time out if you will be lingering with the response for too long.
156181
157-
> [!NOTE]
158-
> See [Demo App's Code (Example Library Usage)](https://github.com/icedevml/react-native-host-card-emulation/blob/master/packages/demo-hce-module-app/App.tsx) example for more insignt about the library's API.
182+
### iOS: Acquiring exclusive NFC access
183+
If you need to utilize `NFCPresentmentIntentAssertion` for enhanced user experience, call:
184+
```typescript
185+
NativeHCEModule?.acquireExclusiveNFC();
186+
```
187+
This function will acquire an exclusive NFC access for 15 seconds. On 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.
159188
160-
> [!NOTE]
161-
> Raw native module's API specification is available in [packages/native-hce-module/js/NativeHCEModule.ts](https://github.com/icedevml/react-native-host-card-emulation/blob/master/packages/native-hce-module/js/NativeHCEModule.ts).
162-
> Check it out in order to understand what methods you can call against the module and what are the expected parameters/return values.
189+
This function will throw an exception if:
190+
* the presentment intent assertion was already acquired and is still active;
191+
* 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);
192+
* the feature is not supported or the device is not eligible for whatever reason;
193+
194+
Call `NativeHCEModule?.isExclusiveNFC()` to check if exclusive NFC access is still active.
195+
196+
### More resources
197+
198+
* [Module's API specification](https://github.com/icedevml/react-native-host-card-emulation/blob/master/packages/native-hce-module/js/NativeHCEModule.ts)
199+
* [Demo App's Code (Example Library Usage)](https://github.com/icedevml/react-native-host-card-emulation/blob/master/packages/demo-hce-module-app/App.tsx)

0 commit comments

Comments
 (0)