js-apis-cardEmulation.md 3.4 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.
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 15 16 17 18 19 20 21

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


## cardEmulation.isSupported

isSupported(feature: number): boolean

Checks whether a certain type of card emulation is supported.

A
Annie_wang 已提交
22
**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
23 24 25 26 27 28 29

**Return value**

  | **Type**| **Description**|
  | -------- | -------- |
  | boolean | Returns **true** if the card emulation is supported; returns **false** otherwise.|

A
Annie_wang 已提交
30
## HceService<sup>8+</sup>
A
Annie_wang 已提交
31 32 33

Implements Host-based Card Emulation (HCE). Before calling any API in **HceService**, you must use **new cardEmulation.HceService()** to create an **HceService** instance.

A
Annie_wang 已提交
34
### startHCE<sup>8+</sup>
A
Annie_wang 已提交
35 36 37 38 39 40 41

startHCE(aidList: string[]): boolean

Starts HCE.

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

A
Annie_wang 已提交
42
**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
43 44 45 46 47

**Parameters**

| Name | Type    | Mandatory| Description                   |
| ------- | -------- | ---- | ----------------------- |
A
Annie_wang 已提交
48
| aidList | string[] | Yes  | Application ID (AID) list to be registered for card emulation.|
A
Annie_wang 已提交
49

A
Annie_wang 已提交
50
### stopHCE<sup>8+</sup>
A
Annie_wang 已提交
51 52 53 54 55 56 57

stopHCE(): boolean

Stops HCE.

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

A
Annie_wang 已提交
58
**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
59

A
Annie_wang 已提交
60
### on<sup>8+</sup>
A
Annie_wang 已提交
61 62 63 64 65 66 67

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

Subscribes to messages from the peer device after **startHCE()**.

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

A
Annie_wang 已提交
68
**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
69 70 71 72 73 74 75 76

**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 subscribed event. The input parameter is a data array that complies with the Application Protocol Data Unit (APDU).|

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

sendResponse(responseApdu: number[]): void;

A
Annie_wang 已提交
81
Sends a response to the peer device.
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

**Parameters**

| Name      | Type    | Mandatory| Description                                              |
| ------------ | -------- | ---- | -------------------------------------------------- |
| responseApdu | number[] | Yes  | Data to send, which is an array that complies with the APDU.|

**Example**

```js
var hceService = new cardEmulation.HceService();
hceService.startHCE([
    "F0010203040506", "A0000000041010"
])
hceService.stopHCE();
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);
    }
})
```