• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Standard NFC Card Emulation
2
3Implements Near-Field Communication (NFC) card emulation.
4
5> **NOTE**<br>
6> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
7
8
9## **Modules to Import**
10
11```
12import cardEmulation from '@ohos.nfc.cardEmulation';
13```
14
15
16## cardEmulation.isSupported
17
18isSupported(feature: number): boolean
19
20Checks whether a certain type of card emulation is supported.
21
22**System capability**: SystemCapability.Communication.NFC
23
24**Return value**
25
26  | **Type**| **Description**|
27  | -------- | -------- |
28  | boolean | Returns **true** if the card emulation is supported; returns **false** otherwise.|
29
30## HceService
31
32Implements Host-based Card Emulation (HCE). Before calling any API in **HceService**, you must use **new cardEmulation.HceService()** to create an **HceService** instance.
33
34### startHCE
35
36startHCE(aidList: string[]): boolean
37
38Starts HCE.
39
40**Required permissions**: ohos.permission.NFC_CARD_EMULATION
41
42**System capability**: SystemCapability.Communication.NFC
43
44**Parameters**
45
46| Name | Type    | Mandatory| Description                   |
47| ------- | -------- | ---- | ----------------------- |
48| aidList | string[] | Yes  | Application ID (AID) list to be registered for card emulation.|
49
50### stopHCE
51
52stopHCE(): boolean
53
54Stops HCE.
55
56**Required permissions**: ohos.permission.NFC_CARD_EMULATION
57
58**System capability**: SystemCapability.Communication.NFC
59
60### on
61
62on(type: "hceCmd", callback: AsyncCallback<number[]>): void;
63
64Subscribes to messages from the peer device after **startHCE()**.
65
66**Required permissions**: ohos.permission.NFC_CARD_EMULATION
67
68**System capability**: SystemCapability.Communication.NFC
69
70**Parameters**
71
72| Name  | Type                   | Mandatory| Description                                        |
73| -------- | ----------------------- | ---- | -------------------------------------------- |
74| type     | string                  | Yes  | Event type to subscribe to. The value is **hceCmd**.                        |
75| callback | AsyncCallback<number[]> | Yes  | Callback invoked to return the subscribed event. The input parameter is a data array that complies with the Application Protocol Data Unit (APDU).|
76
77### sendResponse
78
79sendResponse(responseApdu: number[]): void;
80
81Sends a response to the peer device.
82
83**Required permissions**: ohos.permission.NFC_CARD_EMULATION
84
85**System capability**: SystemCapability.Communication.NFC
86
87**Parameters**
88
89| Name      | Type    | Mandatory| Description                                              |
90| ------------ | -------- | ---- | -------------------------------------------------- |
91| responseApdu | number[] | Yes  | Data to send, which is an array that complies with the APDU.|
92
93**Example**
94
95```js
96var hceService = new cardEmulation.HceService();
97hceService.startHCE([
98    "F0010203040506", "A0000000041010"
99])
100hceService.stopHCE();
101hceService.on("hceCmd", (err, res) => {
102    if(err.data === 0) {
103        console.log('callback => Operation hceCmd succeeded. Data: ' + JSON.stringify(res));
104          hceService.sendResponse([0x00,0xa4,0x04,0x00,
105          0x0e,0x32,0x50,0x41,0x59,0x2e,0x53,0x59,0x53,0x2e,0x44,0x44,
106          0x46,0x30,0x31,0x00]);
107    } else {
108        console.log('callback => Operation hceCmd failed. Cause: ' + err.data);
109    }
110})
111```
112