js-apis-cardEmulation.md 11.1 KB
Newer Older
A
Annie_wang 已提交
1
# @ohos.nfc.cardEmulation (Standard NFC Card Emulation)
A
Annie_wang 已提交
2

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

A
Annie_wang 已提交
5 6
> **NOTE**
>
A
Annie_wang 已提交
7
> 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 已提交
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
## FeatureType

Enumerates the NFC card emulation types.

A
Annie_wang 已提交
19 20 21 22
> **NOTE**
> This parameter is supported since API version 6 and deprecated since API version 9. You are advised to use [hasHceCapability](#hashcecapability9).

**System capability**: SystemCapability.Communication.NFC.CardEmulation
A
Annie_wang 已提交
23

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

## CardType<sup>9+</sup>

Enumerates the types of services used by the card emulation application.
A
Annie_wang 已提交
33

A
Annie_wang 已提交
34 35
**System capability**: SystemCapability.Communication.NFC.CardEmulation

A
Annie_wang 已提交
36 37
| Name     | Value        | Description               |
| ------- | --------- | ----------------- |
A
Annie_wang 已提交
38
| PAYMENT | "payment" | Payment type.|
A
Annie_wang 已提交
39
| OTHER   | "other"   | Other types.|
A
Annie_wang 已提交
40 41

## isSupported
A
Annie_wang 已提交
42 43 44 45 46

isSupported(feature: number): boolean

Checks whether a certain type of card emulation is supported.

A
Annie_wang 已提交
47 48 49 50
> **NOTE**
> This parameter is supported since API version 6 and deprecated since API version 9. You are advised to use [hasHceCapability](#hashcecapability9).

**System capability**: SystemCapability.Communication.NFC.CardEmulation
A
Annie_wang 已提交
51

A
Annie_wang 已提交
52 53
**Parameters**

A
Annie_wang 已提交
54 55 56
| Name    | Type    | Mandatory  | Description                                      |
| ------- | ------ | ---- | ---------------------------------------- |
| feature | number | Yes   | Card emulation type. For details, see [FeatureType](#featuretype).|
A
Annie_wang 已提交
57

A
Annie_wang 已提交
58 59
**Return value**

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

A
Annie_wang 已提交
64
## hasHceCapability<sup>9+</sup>
A
Annie_wang 已提交
65

A
Annie_wang 已提交
66
hasHceCapability(): boolean
A
Annie_wang 已提交
67

A
Annie_wang 已提交
68
Checks whether HCE is supported.
A
Annie_wang 已提交
69

A
Annie_wang 已提交
70
**System capability**: SystemCapability.Communication.NFC.CardEmulation
A
Annie_wang 已提交
71 72 73

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

A
Annie_wang 已提交
74
**Return value**
A
Annie_wang 已提交
75

A
Annie_wang 已提交
76 77
| **Type** | **Description**                          |
| ------- | -------------------------------- |
A
Annie_wang 已提交
78
| boolean | Returns **true** if HCE is supported; returns **false** otherwise.|
A
Annie_wang 已提交
79

A
Annie_wang 已提交
80
## isDefaultService<sup>9+</sup>
A
Annie_wang 已提交
81

A
Annie_wang 已提交
82
isDefaultService(elementName: ElementName, type: CardType): boolean
A
Annie_wang 已提交
83

A
Annie_wang 已提交
84
Checks whether an application is the default application of the specified service type.
A
Annie_wang 已提交
85

A
Annie_wang 已提交
86
**System capability**: SystemCapability.Communication.NFC.CardEmulation
A
Annie_wang 已提交
87 88 89 90 91

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

**Parameters**

A
Annie_wang 已提交
92 93 94 95
| Name        | Type                                      | Mandatory  | Description                     |
| ----------- | ---------------------------------------- | ---- | ----------------------- |
| elementName | [ElementName](js-apis-bundleManager-elementName.md#elementname) | Yes   | Application description, which consists of the bundle name and component name.|
| type        | [CardType](#cardtype9)                   | Yes   | Card emulation service type.               |
A
Annie_wang 已提交
96

A
Annie_wang 已提交
97
**Return value**
A
Annie_wang 已提交
98

A
Annie_wang 已提交
99 100
| **Type** | **Description**                              |
| ------- | ------------------------------------ |
A
Annie_wang 已提交
101
| boolean | Returns **true** if the application is the default payment application; returns **false** otherwise.|
A
Annie_wang 已提交
102

A
Annie_wang 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
## HceService<sup>8+</sup>

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. This API is used only for declaration and cannot be used currently.

### startHCE<sup>8+</sup>

startHCE(aidList: string[]): boolean

Starts HCE, including setting the application to be foreground preferred and dynamically registering the application identifier (AID) list. This API is used only for declaration and cannot be used currently.

> **NOTE**
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [start](#start9).

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

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

**Parameters**

| Name | Type    | Mandatory| Description                   |
| ------- | -------- | ---- | ----------------------- |
| aidList | string[] | Yes  | AID list to register.|

### start<sup>9+</sup>

start(elementName: ElementName, aidList: string[]): void

Starts HCE, including setting the application to be foreground preferred and dynamically registering the application identifier (AID) list. This API is used only for declaration and cannot be used currently.

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

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

**Parameters**

| Name | Type    | Mandatory| Description                   |
| ------- | -------- | ---- | ----------------------- |
| elementName | ElementName | Yes  | Element name of a service capability.|
| aidList | string[] | Yes  | AID list to register.|

**Error codes**

For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).

| ID| Error Message |
| ------- | -------|
| 3100301 | Card emulation running state is abnormal in service. |

### stopHCE<sup>8+</sup>

stopHCE(): boolean

Stops HCE, including removing the foreground preferred attribute and releasing the dynamically registered AID list. This API is used only for declaration and cannot be used currently.

> **NOTE**
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [stop](#stop).

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

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

### stop<sup>9+</sup>

stop(elementName: ElementName): void;

Stops HCE, including removing the foreground preferred attribute and releasing the dynamically registered AID list. This API is used only for declaration and cannot be used currently.

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

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

**Parameters**

| Name | Type    | Mandatory| Description                   |
| ------- | -------- | ---- | ----------------------- |
| elementName | ElementName | Yes  | Element name of a service capability.|

**Error codes**

For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).

| ID| Error Message |
| ------- | -------|
| 3100301 | Card emulation running state is abnormal in service. |

### on<sup>8+</sup>

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

Registers a callback to receive APDUs from the peer card reader. This API is used only for declaration and cannot be used currently.

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

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

**Parameters**

| Name  | Type                   | Mandatory| Description                                        |
| -------- | ----------------------- | ---- | -------------------------------------------- |
| type     | string                  | Yes  | Event type to subscribe to. The value is **hceCmd**.                        |
| callback | AsyncCallback<number[]> | Yes  | Callback invoked to return the APDU, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.|

### sendResponse<sup>8+</sup>

sendResponse(responseApdu: number[]): void;

Sends a response to the peer card reader. This API is used only for declaration and cannot be used currently.

> **NOTE**
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [transmit](#transmit9).

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

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

**Parameters**

| Name      | Type    | Mandatory| Description                                              |
| ------------ | -------- | ---- | -------------------------------------------------- |
| responseApdu | number[] | Yes  | Response APDU sent to the peer card reader. The value consists of hexadecimal numbers ranging from **0x00** to **0xFF**.|

### transmit<sup>9+</sup>

transmit(response: number[]): Promise\<void>;

Sends a response to the peer card reader. This API is used only for declaration and cannot be used currently.

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

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

**Parameters**

| Name      | Type    | Mandatory| Description                                              |
| ------------ | -------- | ---- | -------------------------------------------------- |
| responseApdu | number[] | Yes  | Response APDU sent to the peer card reader. The value consists of hexadecimal numbers ranging from **0x00** to **0xFF**.|

**Return value**

| **Type** | **Description**                                |
| ------- | -------------------------------------- |
| Promise\<void> | Promise that returns no value.|

**Error codes**

For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).

| ID| Error Message |
| ------- | -------|
| 3100301 | Card emulation running state is abnormal in service. |

### transmit<sup>9+</sup>

transmit(response: number[], callback: AsyncCallback\<void>): void;

Sends a response to the peer card reader. This API is used only for declaration and cannot be used currently.

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

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

**Parameters**

| Name | Type    | Mandatory| Description                   |
| ------- | -------- | ---- | ----------------------- |
| responseApdu | number[] | Yes  | Response APDU sent to the peer card reader. The value consists of hexadecimal numbers ranging from **0x00** to **0xFF**.|
| callback | AsyncCallback\<void> | Yes  | Callback that returns no value.|

**Error codes**

For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).

| ID| Error Message |
| ------- | -------|
| 3100301 | Card emulation running state is abnormal in service. |

A
Annie_wang 已提交
279 280 281
**Example**

```js
A
Annie_wang 已提交
282 283 284 285 286 287 288
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.');
}

A
Annie_wang 已提交
289 290 291 292 293 294 295 296 297 298 299
var hasHceCap = cardEmulation.hasHceCapability();
if (!hasHceCap) {
    console.log('this device hasHceCapability false, ignore it.');
}

var elementName = {
    "bundleName": "com.example.myapplication",
    "abilityName": "EntryAbility",
};
var isDefaultService = cardEmulation.isDefaultService(elementName, cardEmulation.CardType.PAYMENT);
console.log('is the app is default service for this card type: ' + isDefaultService);
A
Annie_wang 已提交
300
```