js-apis-cardEmulation.md 4.8 KB
Newer Older
A
Annie_wang 已提交
1 2
# Standard NFC Card Emulation

A
Annie_wang 已提交
3
The **cardEmulation** module implements Near-Field Communication (NFC) card emulation. You can use the APIs provided by this module to determine the card emulation type supported and implement Host-based Card Emulation (HCE).
A
Annie_wang 已提交
4 5

> **NOTE**<br>
A
Annie_wang 已提交
6
> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
A
Annie_wang 已提交
7 8


A
Annie_wang 已提交
9
## Modules to Import
A
Annie_wang 已提交
10 11 12 13 14

```
import cardEmulation from '@ohos.nfc.cardEmulation';
```

A
Annie_wang 已提交
15 16 17 18 19 20 21 22 23 24 25
## FeatureType

Enumerates the NFC card emulation types.

**System capability**: SystemCapability.Communication.NFC.Core

| Name| Default Value| Description|
| -------- | -------- | -------- |
| HCE | 0 | HCE.|
| UICC | 1 | Subscriber identity module (SIM) card emulation.|
| ESE | 2      | embedded Secure Element (eSE) emulation.|
A
Annie_wang 已提交
26 27 28 29 30 31 32

## cardEmulation.isSupported

isSupported(feature: number): boolean

Checks whether a certain type of card emulation is supported.

A
Annie_wang 已提交
33 34
**Required permissions**: ohos.permission.NFC_CARD_EMULATION

A
Annie_wang 已提交
35
**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
36

A
Annie_wang 已提交
37 38 39 40 41 42
**Parameters**

| Name | Type    | Mandatory| Description                   |
| ------- | -------- | ---- | ----------------------- |
| feature | number | Yes  | Card emulation type. For details, see [FeatureType](#featuretype).|

A
Annie_wang 已提交
43 44 45 46
**Return value**

  | **Type**| **Description**|
  | -------- | -------- |
A
Annie_wang 已提交
47
  | boolean | Returns **true** if the card emulation type is supported; returns **false** otherwise.|
A
Annie_wang 已提交
48

A
Annie_wang 已提交
49
## HceService<sup>8+</sup>
A
Annie_wang 已提交
50

A
Annie_wang 已提交
51
Implements HCE, including receiving Application Protocol Data Units (APDUs) from the peer card reader and sending a response. Before using HCE-related APIs, check whether the device supports HCE.
A
Annie_wang 已提交
52

A
Annie_wang 已提交
53
### startHCE<sup>8+</sup>
A
Annie_wang 已提交
54 55 56

startHCE(aidList: string[]): boolean

A
Annie_wang 已提交
57
Starts HCE, including setting the application to be foreground preferred and dynamically registering the application identifier (AID) list.
A
Annie_wang 已提交
58 59 60

**Required permissions**: ohos.permission.NFC_CARD_EMULATION

A
Annie_wang 已提交
61
**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
62 63 64 65 66

**Parameters**

| Name | Type    | Mandatory| Description                   |
| ------- | -------- | ---- | ----------------------- |
A
Annie_wang 已提交
67
| aidList | string[] | Yes  | AID list to register.|
A
Annie_wang 已提交
68

A
Annie_wang 已提交
69
### stopHCE<sup>8+</sup>
A
Annie_wang 已提交
70 71 72

stopHCE(): boolean

A
Annie_wang 已提交
73
Stops HCE, including removing the foreground preferred attribute and releasing the dynamically registered AID list.
A
Annie_wang 已提交
74 75 76

**Required permissions**: ohos.permission.NFC_CARD_EMULATION

A
Annie_wang 已提交
77
**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
78

A
Annie_wang 已提交
79
### on<sup>8+</sup>
A
Annie_wang 已提交
80 81 82

on(type: "hceCmd", callback: AsyncCallback<number[]>): void;

A
Annie_wang 已提交
83
Registers a callback to receive APDUs from the peer card reader.
A
Annie_wang 已提交
84 85 86

**Required permissions**: ohos.permission.NFC_CARD_EMULATION

A
Annie_wang 已提交
87
**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
88 89 90 91 92 93

**Parameters**

| Name  | Type                   | Mandatory| Description                                        |
| -------- | ----------------------- | ---- | -------------------------------------------- |
| type     | string                  | Yes  | Event type to subscribe to. The value is **hceCmd**.                        |
A
Annie_wang 已提交
94
| callback | AsyncCallback<number[]> | Yes  | Callback invoked to return the APDU. Each number in the callback is a hexadecimal number ranging from **0x00** to **0xFF**.|
A
Annie_wang 已提交
95

A
Annie_wang 已提交
96
### sendResponse<sup>8+</sup>
A
Annie_wang 已提交
97 98 99

sendResponse(responseApdu: number[]): void;

A
Annie_wang 已提交
100
Sends a response to the peer card reader.
A
Annie_wang 已提交
101 102 103

**Required permissions**: ohos.permission.NFC_CARD_EMULATION

A
Annie_wang 已提交
104
**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
105 106 107 108 109

**Parameters**

| Name      | Type    | Mandatory| Description                                              |
| ------------ | -------- | ---- | -------------------------------------------------- |
A
Annie_wang 已提交
110
| responseApdu | number[] | Yes  | Response APDU sent to the peer card reader. Each number of the APDU is a hexadecimal number ranging from **0x00** to **0xFF**.|
A
Annie_wang 已提交
111 112 113 114

**Example**

```js
A
Annie_wang 已提交
115 116 117 118 119 120 121 122 123
import cardEmulation from '@ohos.nfc.cardEmulation';

var isHceSupported = cardEmulation.isSupported(cardEmulation.FeatureType.HCE);
if (!isHceSupported) {
    console.log('this device is not supported for HCE, ignore it.');
    return;
}

// The device supports HCE and transimits APDUs with the remote NFC reader.
A
Annie_wang 已提交
124 125 126
var hceService = new cardEmulation.HceService();
hceService.startHCE([
    "F0010203040506", "A0000000041010"
A
Annie_wang 已提交
127 128
]);

A
Annie_wang 已提交
129 130 131 132 133 134 135 136 137 138
hceService.on("hceCmd", (err, res) => {
    if(err.data === 0) {
        console.log('callback => Operation hceCmd succeeded. Data: ' + JSON.stringify(res));
          hceService.sendResponse([0x00,0xa4,0x04,0x00,
          0x0e,0x32,0x50,0x41,0x59,0x2e,0x53,0x59,0x53,0x2e,0x44,0x44,
          0x46,0x30,0x31,0x00]);
    } else {
        console.log('callback => Operation hceCmd failed. Cause: ' + err.data);
    }
})
A
Annie_wang 已提交
139 140 141

// Stop HCE when the application exits the NFC card emulation.
hceService.stopHCE();
A
Annie_wang 已提交
142
```