js-apis-cardEmulation.md 4.7 KB
Newer Older
A
Annie_wang 已提交
1
# @ohos.nfc.cardEmulation
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 19 20
## FeatureType

Enumerates the NFC card emulation types.

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

A
Annie_wang 已提交
21
| Name| Value| Description|
A
Annie_wang 已提交
22 23 24 25
| -------- | -------- | -------- |
| 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
**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
34

A
Annie_wang 已提交
35 36 37 38 39 40
**Parameters**

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

A
Annie_wang 已提交
41 42
**Return value**

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

A
Annie_wang 已提交
47
## HceService<sup>8+</sup>
A
Annie_wang 已提交
48

A
Annie_wang 已提交
49
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 已提交
50

A
Annie_wang 已提交
51
### startHCE<sup>8+</sup>
A
Annie_wang 已提交
52 53 54

startHCE(aidList: string[]): boolean

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

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

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

**Parameters**

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

A
Annie_wang 已提交
67
### stopHCE<sup>8+</sup>
A
Annie_wang 已提交
68 69 70

stopHCE(): boolean

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

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

A
Annie_wang 已提交
75
**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
76

A
Annie_wang 已提交
77
### on<sup>8+</sup>
A
Annie_wang 已提交
78 79 80

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

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

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

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

**Parameters**

| Name  | Type                   | Mandatory| Description                                        |
| -------- | ----------------------- | ---- | -------------------------------------------- |
| type     | string                  | Yes  | Event type to subscribe to. The value is **hceCmd**.                        |
A
Annie_wang 已提交
92
| 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 已提交
93

A
Annie_wang 已提交
94
### sendResponse<sup>8+</sup>
A
Annie_wang 已提交
95 96 97

sendResponse(responseApdu: number[]): void;

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

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

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

**Parameters**

| Name      | Type    | Mandatory| Description                                              |
| ------------ | -------- | ---- | -------------------------------------------------- |
A
Annie_wang 已提交
108
| 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 已提交
109 110 111 112

**Example**

```js
A
Annie_wang 已提交
113 114 115 116 117 118 119 120 121
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 已提交
122 123 124
var hceService = new cardEmulation.HceService();
hceService.startHCE([
    "F0010203040506", "A0000000041010"
A
Annie_wang 已提交
125 126
]);

A
Annie_wang 已提交
127 128 129 130 131 132 133 134 135 136
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 已提交
137 138 139

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