1e41f4b71Sopenharmony_ci# @ohos.nfc.cardEmulation (标准NFC-cardEmulation)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci本模块主要提供NFC卡模拟业务,包括判断支持哪种卡模拟类型,HCE卡模拟的业务实现等。
4e41f4b71Sopenharmony_ciHCE(Host Card Emulation),称为基于主机的卡模拟,表示不依赖安全单元芯片,应用程序模拟NFC卡片,可以通过NFC服务和NFC读卡器通信。
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci> **说明:**
7e41f4b71Sopenharmony_ci>
8e41f4b71Sopenharmony_ci> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci## HCE卡模拟和AID列表的声明定义
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci开发HCE卡模拟相关应用时,需要在应用的属性配置文件中,声明与NFC相关的属性值,比如,在module.json5文件中,声明下面属性值:
13e41f4b71Sopenharmony_ci```json
14e41f4b71Sopenharmony_ci{
15e41f4b71Sopenharmony_ci  "module": {
16e41f4b71Sopenharmony_ci    // other declared attributes.
17e41f4b71Sopenharmony_ci    "abilities": [
18e41f4b71Sopenharmony_ci      {
19e41f4b71Sopenharmony_ci        // other declared attributes.
20e41f4b71Sopenharmony_ci        "skills": [
21e41f4b71Sopenharmony_ci          {
22e41f4b71Sopenharmony_ci            "actions": [
23e41f4b71Sopenharmony_ci              "ohos.nfc.cardemulation.action.HOST_APDU_SERVICE"
24e41f4b71Sopenharmony_ci            ]
25e41f4b71Sopenharmony_ci          }
26e41f4b71Sopenharmony_ci        ],
27e41f4b71Sopenharmony_ci        "metadata": [
28e41f4b71Sopenharmony_ci          {
29e41f4b71Sopenharmony_ci            "name": "payment-aid",
30e41f4b71Sopenharmony_ci            "value": "your payment aid"
31e41f4b71Sopenharmony_ci          },
32e41f4b71Sopenharmony_ci          {
33e41f4b71Sopenharmony_ci            "name": "other-aid",
34e41f4b71Sopenharmony_ci            "value": "your other aid"
35e41f4b71Sopenharmony_ci          }
36e41f4b71Sopenharmony_ci        ]
37e41f4b71Sopenharmony_ci      }
38e41f4b71Sopenharmony_ci    ],
39e41f4b71Sopenharmony_ci    "requestPermissions": [
40e41f4b71Sopenharmony_ci      {
41e41f4b71Sopenharmony_ci        "name": "ohos.permission.NFC_CARD_EMULATION",
42e41f4b71Sopenharmony_ci        // should add variable card_emulation_reason in string.json
43e41f4b71Sopenharmony_ci        "reason": "$string:card_emulation_reason",
44e41f4b71Sopenharmony_ci      }
45e41f4b71Sopenharmony_ci    ]
46e41f4b71Sopenharmony_ci  }
47e41f4b71Sopenharmony_ci}
48e41f4b71Sopenharmony_ci```
49e41f4b71Sopenharmony_ci> **注意:**
50e41f4b71Sopenharmony_ci>1. 声明"actions"字段的内容填写,必须包含"ohos.nfc.cardemulation.action.HOST_APDU_SERVICE",不能更改。
51e41f4b71Sopenharmony_ci>2. 声明aid时,name必须为payment-aid,或者other-aid。填写错误会造成解析失败。
52e41f4b71Sopenharmony_ci>3. 声明权限时"requestPermissions"中的"name"字段的内容填写,必须是"ohos.permission.NFC_CARD_EMULATION",不能更改。
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci## 导入模块
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ci```
57e41f4b71Sopenharmony_ciimport { cardEmulation } from '@kit.ConnectivityKit';
58e41f4b71Sopenharmony_ci```
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci## FeatureType<sup>(deprecated)</sup>
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci定义不同的NFC卡模拟类型。
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci> **说明:**
65e41f4b71Sopenharmony_ci> 从 API version 6 开始支持,从 API version 9 开始废弃,建议使用[hasHceCapability](#hashcecapability9)替代。
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Communication.NFC.CardEmulation
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci| 名称   | 值    | 说明       |
70e41f4b71Sopenharmony_ci| ---- | ---- | -------- |
71e41f4b71Sopenharmony_ci| HCE  | 0    | HCE 卡模拟。 |
72e41f4b71Sopenharmony_ci| UICC | 1    | SIM 卡模拟。 |
73e41f4b71Sopenharmony_ci| ESE  | 2    | ESE 卡模拟。  |
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci## CardType<sup>9+</sup>
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci定义卡模拟应用所使用的业务类型,是支付类型,还是其他类型。
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Communication.NFC.CardEmulation
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci| 名称      | 值         | 说明                |
84e41f4b71Sopenharmony_ci| ------- | --------- | ----------------- |
85e41f4b71Sopenharmony_ci| PAYMENT | "payment" | 卡模拟应用所使用的业务是支付类型。 |
86e41f4b71Sopenharmony_ci| OTHER   | "other"   | 卡模拟应用所使用的业务是其他类型。 |
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci## isSupported<sup>(deprecated)</sup>
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ciisSupported(feature: number): boolean
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci是否支持某种类型的卡模拟。
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ci> **说明:**
95e41f4b71Sopenharmony_ci> 从 API version 6 开始支持,从 API version 9 开始废弃,建议使用[hasHceCapability](#hashcecapability9)替代。
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Communication.NFC.CardEmulation
98e41f4b71Sopenharmony_ci
99e41f4b71Sopenharmony_ci**参数:**
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci| 参数名     | 类型     | 必填   | 说明                                       |
102e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---------------------------------------- |
103e41f4b71Sopenharmony_ci| feature | number | 是    | 卡模拟类型值,详细请见[FeatureType](#featuretypedeprecated)枚举值。 |
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ci**返回值:**
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ci| **类型**  | **说明**                                 |
108e41f4b71Sopenharmony_ci| ------- | -------------------------------------- |
109e41f4b71Sopenharmony_ci| boolean | true: 支持该类型卡模拟,&nbsp;false: 不支持该类型卡模拟。 |
110e41f4b71Sopenharmony_ci
111e41f4b71Sopenharmony_ci**示例:**
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ci```js
114e41f4b71Sopenharmony_ciimport { cardEmulation } from '@kit.ConnectivityKit';
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_cilet isHceSupported: boolean = cardEmulation.isSupported(cardEmulation.FeatureType.HCE);
117e41f4b71Sopenharmony_ciif (!isHceSupported) {
118e41f4b71Sopenharmony_ci    console.log('this device is not supported for HCE, ignore it.');
119e41f4b71Sopenharmony_ci}
120e41f4b71Sopenharmony_ci```
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_ci## hasHceCapability<sup>9+</sup>
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_cihasHceCapability(): boolean
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci判断设备是否支持HCE卡模拟功能。
127e41f4b71Sopenharmony_ci
128e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Communication.NFC.CardEmulation
129e41f4b71Sopenharmony_ci
130e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.NFC_CARD_EMULATION
131e41f4b71Sopenharmony_ci
132e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
133e41f4b71Sopenharmony_ci
134e41f4b71Sopenharmony_ci**返回值:**
135e41f4b71Sopenharmony_ci
136e41f4b71Sopenharmony_ci| **类型**  | **说明**                           |
137e41f4b71Sopenharmony_ci| ------- | -------------------------------- |
138e41f4b71Sopenharmony_ci| boolean | true: 支持HCE,&nbsp;false: 不支持HCE。 |
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci**错误码**:
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
145e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
146e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
147e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_ci**示例:**
150e41f4b71Sopenharmony_ci
151e41f4b71Sopenharmony_ci```js
152e41f4b71Sopenharmony_ciimport { cardEmulation } from '@kit.ConnectivityKit';
153e41f4b71Sopenharmony_ci
154e41f4b71Sopenharmony_cilet isHceSupported: boolean = cardEmulation.isSupported(cardEmulation.FeatureType.HCE);
155e41f4b71Sopenharmony_ciif (!isHceSupported) {
156e41f4b71Sopenharmony_ci    console.log('this device is not supported for HCE, ignore it.');
157e41f4b71Sopenharmony_ci}
158e41f4b71Sopenharmony_ci
159e41f4b71Sopenharmony_cilet hasHceCap: boolean = cardEmulation.hasHceCapability();
160e41f4b71Sopenharmony_ciif (!hasHceCap) {
161e41f4b71Sopenharmony_ci    console.log('this device hasHceCapability false, ignore it.');
162e41f4b71Sopenharmony_ci}
163e41f4b71Sopenharmony_ci```
164e41f4b71Sopenharmony_ci
165e41f4b71Sopenharmony_ci## isDefaultService<sup>9+</sup>
166e41f4b71Sopenharmony_ci
167e41f4b71Sopenharmony_ciisDefaultService(elementName: ElementName, type: CardType): boolean
168e41f4b71Sopenharmony_ci
169e41f4b71Sopenharmony_ci判断指定的应用是否为指定业务类型的默认应用。
170e41f4b71Sopenharmony_ci
171e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Communication.NFC.CardEmulation
172e41f4b71Sopenharmony_ci
173e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.NFC_CARD_EMULATION
174e41f4b71Sopenharmony_ci
175e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
176e41f4b71Sopenharmony_ci
177e41f4b71Sopenharmony_ci**参数:**
178e41f4b71Sopenharmony_ci
179e41f4b71Sopenharmony_ci| 参数名         | 类型                                       | 必填   | 说明                      |
180e41f4b71Sopenharmony_ci| ----------- | ---------------------------------------- | ---- |-------------------------|
181e41f4b71Sopenharmony_ci| elementName | [ElementName](../apis-ability-kit/js-apis-bundle-ElementName.md#elementname) | 是    | 所属应用声明NFC卡模拟能力的页面信息(至少包含bundleName、abilityName这两项的赋值),不可以为空。 |
182e41f4b71Sopenharmony_ci| type        | [CardType](#cardtype9)                   | 是    | 卡模拟业务类型。目前只支持默认支付应用查询。   |
183e41f4b71Sopenharmony_ci
184e41f4b71Sopenharmony_ci**错误码**:
185e41f4b71Sopenharmony_ci
186e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。
187e41f4b71Sopenharmony_ci
188e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 |
189e41f4b71Sopenharmony_ci| -------- | ---------------------------- |
190e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
191e41f4b71Sopenharmony_ci|401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. |
192e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
193e41f4b71Sopenharmony_ci
194e41f4b71Sopenharmony_ci**返回值:**
195e41f4b71Sopenharmony_ci
196e41f4b71Sopenharmony_ci| **类型**  | **说明**                               |
197e41f4b71Sopenharmony_ci| ------- | ------------------------------------ |
198e41f4b71Sopenharmony_ci| boolean | true: 是默认支付应用,&nbsp;false: 不是默认支付应用。 |
199e41f4b71Sopenharmony_ci
200e41f4b71Sopenharmony_ci
201e41f4b71Sopenharmony_ci**示例:**
202e41f4b71Sopenharmony_ci```js
203e41f4b71Sopenharmony_ciimport { cardEmulation } from '@kit.ConnectivityKit';
204e41f4b71Sopenharmony_ciimport { bundleManager, Want } from '@kit.AbilityKit';
205e41f4b71Sopenharmony_ci
206e41f4b71Sopenharmony_ci// init elementName here, bundleName and abilityName are required.
207e41f4b71Sopenharmony_cilet want: Want = {
208e41f4b71Sopenharmony_ci  bundleName: "com.example.myapplication",
209e41f4b71Sopenharmony_ci  moduleName: "entry",
210e41f4b71Sopenharmony_ci  abilityName: "EntryAbility"
211e41f4b71Sopenharmony_ci};
212e41f4b71Sopenharmony_cilet elementName: bundleManager.ElementName = {
213e41f4b71Sopenharmony_ci  bundleName: "com.example.myapplication",
214e41f4b71Sopenharmony_ci  moduleName: "entry",
215e41f4b71Sopenharmony_ci  abilityName: "EntryAbility"
216e41f4b71Sopenharmony_ci};
217e41f4b71Sopenharmony_ci
218e41f4b71Sopenharmony_cilet isDefaultService: boolean = cardEmulation.isDefaultService(elementName, cardEmulation.CardType.PAYMENT);
219e41f4b71Sopenharmony_ci// do something according to the isDefaultService value
220e41f4b71Sopenharmony_ci```
221e41f4b71Sopenharmony_ci
222e41f4b71Sopenharmony_ci
223e41f4b71Sopenharmony_ci## HceService<sup>8+</sup>
224e41f4b71Sopenharmony_ci
225e41f4b71Sopenharmony_ci提供HCE卡模拟的实现,主要包括接收对端读卡设备的APDU数据,并响应APDU数据到对端读卡设备。使用HCE相关接口前,必须先判断设备是否支持HCE卡模拟能力。
226e41f4b71Sopenharmony_ci
227e41f4b71Sopenharmony_ci### startHCE<sup>(deprecated)</sup>
228e41f4b71Sopenharmony_ci
229e41f4b71Sopenharmony_cistartHCE(aidList: string[]): boolean
230e41f4b71Sopenharmony_ci
231e41f4b71Sopenharmony_ci启动HCE业务功能。包括设置当前应用为前台优先,动态注册AID列表。
232e41f4b71Sopenharmony_ci
233e41f4b71Sopenharmony_ci> **说明:**
234e41f4b71Sopenharmony_ci> 从 API version 8 开始支持,从 API version 9 开始废弃,建议使用[start](#start9)替代。
235e41f4b71Sopenharmony_ci
236e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.NFC_CARD_EMULATION
237e41f4b71Sopenharmony_ci
238e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Communication.NFC.CardEmulation
239e41f4b71Sopenharmony_ci
240e41f4b71Sopenharmony_ci**参数:**
241e41f4b71Sopenharmony_ci
242e41f4b71Sopenharmony_ci| 参数名  | 类型     | 必填 | 说明                    |
243e41f4b71Sopenharmony_ci| ------- | -------- | ---- | ----------------------- |
244e41f4b71Sopenharmony_ci| aidList | string[] | 是   | 动态注册卡模拟的AID列表。 |
245e41f4b71Sopenharmony_ci
246e41f4b71Sopenharmony_ci**返回值:**
247e41f4b71Sopenharmony_ci
248e41f4b71Sopenharmony_ci| **类型**  | **说明**                                 |
249e41f4b71Sopenharmony_ci| ------- | -------------------------------------- |
250e41f4b71Sopenharmony_ci| boolean | true: 启动HCE功能或HCE已启动,&nbsp;false: 启动失败。 |
251e41f4b71Sopenharmony_ci
252e41f4b71Sopenharmony_ci### start<sup>9+</sup>
253e41f4b71Sopenharmony_ci
254e41f4b71Sopenharmony_cistart(elementName: [ElementName](../apis-ability-kit/js-apis-bundle-ElementName.md#elementname), aidList: string[]): void
255e41f4b71Sopenharmony_ci
256e41f4b71Sopenharmony_ci启动HCE业务功能。包括设置当前应用为前台优先,动态注册AID列表。
257e41f4b71Sopenharmony_ci
258e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.NFC_CARD_EMULATION
259e41f4b71Sopenharmony_ci
260e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Communication.NFC.CardEmulation
261e41f4b71Sopenharmony_ci
262e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
263e41f4b71Sopenharmony_ci
264e41f4b71Sopenharmony_ci**参数:**
265e41f4b71Sopenharmony_ci
266e41f4b71Sopenharmony_ci| 参数名  | 类型     | 必填 | 说明                    |
267e41f4b71Sopenharmony_ci| ------- | -------- | ---- | ----------------------- |
268e41f4b71Sopenharmony_ci| elementName | [ElementName](../apis-ability-kit/js-apis-bundle-ElementName.md#elementname) | 是   | 所属应用声明NFC卡模拟能力的页面信息(至少包含bundleName、abilityName这两项的赋值),不可以为空。 |
269e41f4b71Sopenharmony_ci| aidList | string[] | 是   | 动态注册卡模拟的AID列表,允许为空。 |
270e41f4b71Sopenharmony_ci
271e41f4b71Sopenharmony_ci**错误码:**
272e41f4b71Sopenharmony_ci
273e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。
274e41f4b71Sopenharmony_ci
275e41f4b71Sopenharmony_ci| 错误码ID | 错误信息|
276e41f4b71Sopenharmony_ci| ------- | -------|
277e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
278e41f4b71Sopenharmony_ci|401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. |
279e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
280e41f4b71Sopenharmony_ci| 3100301 | Card emulation running state is abnormal in service. |
281e41f4b71Sopenharmony_ci
282e41f4b71Sopenharmony_ci### stopHCE<sup>(deprecated)</sup>
283e41f4b71Sopenharmony_ci
284e41f4b71Sopenharmony_cistopHCE(): boolean
285e41f4b71Sopenharmony_ci
286e41f4b71Sopenharmony_ci停止HCE业务功能。包括退出当前应用前台优先,释放动态注册的AID列表,释放hceCmd的订阅。
287e41f4b71Sopenharmony_ci
288e41f4b71Sopenharmony_ci> **说明:**
289e41f4b71Sopenharmony_ci> 从 API version 8 开始支持,从 API version 9 开始废弃,建议使用[stop](#stop9)替代。
290e41f4b71Sopenharmony_ci
291e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.NFC_CARD_EMULATION
292e41f4b71Sopenharmony_ci
293e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Communication.NFC.CardEmulation
294e41f4b71Sopenharmony_ci
295e41f4b71Sopenharmony_ci**返回值:**
296e41f4b71Sopenharmony_ci
297e41f4b71Sopenharmony_ci| **类型**  | **说明**                                 |
298e41f4b71Sopenharmony_ci| ------- | -------------------------------------- |
299e41f4b71Sopenharmony_ci| boolean | true: 禁用HCE功能或HCE已禁用,&nbsp;false: 禁用失败。 |
300e41f4b71Sopenharmony_ci
301e41f4b71Sopenharmony_ci**示例:**
302e41f4b71Sopenharmony_ci
303e41f4b71Sopenharmony_ci示例请参见[on](#on8)接口的示例。
304e41f4b71Sopenharmony_ci
305e41f4b71Sopenharmony_ci### stop<sup>9+</sup>
306e41f4b71Sopenharmony_ci
307e41f4b71Sopenharmony_cistop(elementName: [ElementName](../apis-ability-kit/js-apis-bundleManager-elementName.md#elementname)): void 
308e41f4b71Sopenharmony_ci
309e41f4b71Sopenharmony_ci停止HCE业务功能。包括取消APDU数据接收的订阅,退出当前应用前台优先,释放动态注册的AID列表。应用程序需要在HCE卡模拟页面的onDestroy函数里调用该接口。
310e41f4b71Sopenharmony_ci
311e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.NFC_CARD_EMULATION
312e41f4b71Sopenharmony_ci
313e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Communication.NFC.CardEmulation
314e41f4b71Sopenharmony_ci
315e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
316e41f4b71Sopenharmony_ci
317e41f4b71Sopenharmony_ci**参数:**
318e41f4b71Sopenharmony_ci
319e41f4b71Sopenharmony_ci| 参数名  | 类型     | 必填 | 说明                    |
320e41f4b71Sopenharmony_ci| ------- | -------- | ---- | ----------------------- |
321e41f4b71Sopenharmony_ci| elementName | [ElementName](../apis-ability-kit/js-apis-bundle-ElementName.md#elementname) | 是   | 所属应用声明NFC卡模拟能力的页面信息(至少包含bundleName、abilityName这两项的赋值),不可以为空。 |
322e41f4b71Sopenharmony_ci
323e41f4b71Sopenharmony_ci**错误码:**
324e41f4b71Sopenharmony_ci
325e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。
326e41f4b71Sopenharmony_ci
327e41f4b71Sopenharmony_ci| 错误码ID | 错误信息|
328e41f4b71Sopenharmony_ci| ------- | -------|
329e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
330e41f4b71Sopenharmony_ci|401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. |
331e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
332e41f4b71Sopenharmony_ci| 3100301 | Card emulation running state is abnormal in service. |
333e41f4b71Sopenharmony_ci
334e41f4b71Sopenharmony_ci### on<sup>8+</sup>
335e41f4b71Sopenharmony_ci
336e41f4b71Sopenharmony_cion(type: 'hceCmd', callback: AsyncCallback\<number[]>): void
337e41f4b71Sopenharmony_ci
338e41f4b71Sopenharmony_ci订阅回调,用于接收对端读卡设备发送的APDU数据。应用程序需要在HCE卡模拟页面的onCreate函数里面调用该订阅函数。
339e41f4b71Sopenharmony_ci
340e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.NFC_CARD_EMULATION
341e41f4b71Sopenharmony_ci
342e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Communication.NFC.CardEmulation
343e41f4b71Sopenharmony_ci
344e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
345e41f4b71Sopenharmony_ci
346e41f4b71Sopenharmony_ci**参数:**
347e41f4b71Sopenharmony_ci
348e41f4b71Sopenharmony_ci| 参数名   | 类型                    | 必填 | 说明                                         |
349e41f4b71Sopenharmony_ci| -------- | ----------------------- | ---- | -------------------------------------------- |
350e41f4b71Sopenharmony_ci| type     | string                  | 是   | 要订阅的回调类型,固定填"hceCmd"字符串。                         |
351e41f4b71Sopenharmony_ci| callback | AsyncCallback\<number[]> | 是   | 订阅的事件回调,入参是符合APDU协议的数据,每个number十六进制表示,范围是0x00~0xFF。 |
352e41f4b71Sopenharmony_ci
353e41f4b71Sopenharmony_ci**示例:**
354e41f4b71Sopenharmony_ci```js
355e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit';
356e41f4b71Sopenharmony_ciimport { cardEmulation } from '@kit.ConnectivityKit';
357e41f4b71Sopenharmony_ciimport { AsyncCallback } from '@kit.BasicServicesKit';
358e41f4b71Sopenharmony_ciimport { ElementName } from './bundleManager/ElementName'
359e41f4b71Sopenharmony_ciimport { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
360e41f4b71Sopenharmony_ci
361e41f4b71Sopenharmony_cilet hceService: cardEmulation.HceService = new cardEmulation.HceService();
362e41f4b71Sopenharmony_cilet element: ElementName;
363e41f4b71Sopenharmony_ci
364e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility {
365e41f4b71Sopenharmony_ci  onCreate(want: Want, param: AbilityConstant.LaunchParam) {
366e41f4b71Sopenharmony_ci    hilog.info(0x0000, 'testHce', '%{public}s', 'Ability onCreate');
367e41f4b71Sopenharmony_ci    element = {
368e41f4b71Sopenharmony_ci      bundleName: want.bundleName ?? '',
369e41f4b71Sopenharmony_ci      abilityName: want.abilityName ?? '',
370e41f4b71Sopenharmony_ci      moduleName: want.moduleName
371e41f4b71Sopenharmony_ci    }
372e41f4b71Sopenharmony_ci    const apduCallback: AsyncCallback<number[]> = (err, data) => {
373e41f4b71Sopenharmony_ci      //handle the data and err
374e41f4b71Sopenharmony_ci      console.log("got apdu data");
375e41f4b71Sopenharmony_ci    };
376e41f4b71Sopenharmony_ci    hceService.on('hceCmd', apduCallback);
377e41f4b71Sopenharmony_ci  }
378e41f4b71Sopenharmony_ci  onDestroy() {
379e41f4b71Sopenharmony_ci    hilog.info(0x0000, 'testHce', '%{public}s', 'Ability onDestroy');
380e41f4b71Sopenharmony_ci    hceService.stop(element)
381e41f4b71Sopenharmony_ci  }
382e41f4b71Sopenharmony_ci  // other life cycle method...
383e41f4b71Sopenharmony_ci}
384e41f4b71Sopenharmony_ci```
385e41f4b71Sopenharmony_ci
386e41f4b71Sopenharmony_ci
387e41f4b71Sopenharmony_ci### sendResponse<sup>(deprecated)</sup>
388e41f4b71Sopenharmony_ci
389e41f4b71Sopenharmony_cisendResponse(responseApdu: number[]): void
390e41f4b71Sopenharmony_ci
391e41f4b71Sopenharmony_ci发送APDU数据到对端读卡设备。
392e41f4b71Sopenharmony_ci
393e41f4b71Sopenharmony_ci> **说明:**
394e41f4b71Sopenharmony_ci> 从 API version 8 开始支持,从 API version 9 开始废弃,建议使用[transmit](#transmit9)替代。
395e41f4b71Sopenharmony_ci
396e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.NFC_CARD_EMULATION
397e41f4b71Sopenharmony_ci
398e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Communication.NFC.CardEmulation
399e41f4b71Sopenharmony_ci
400e41f4b71Sopenharmony_ci**参数:**
401e41f4b71Sopenharmony_ci
402e41f4b71Sopenharmony_ci| 参数名       | 类型     | 必填 | 说明                                               |
403e41f4b71Sopenharmony_ci| ------------ | -------- | ---- | -------------------------------------------------- |
404e41f4b71Sopenharmony_ci| responseApdu | number[] | 是   | 发送到对端读卡设备的符合APDU协议的数据,每个number十六进制表示,范围是0x00~0xFF。 |
405e41f4b71Sopenharmony_ci
406e41f4b71Sopenharmony_ci### transmit<sup>9+</sup>
407e41f4b71Sopenharmony_ci
408e41f4b71Sopenharmony_citransmit(response: number[]): Promise\<void>
409e41f4b71Sopenharmony_ci
410e41f4b71Sopenharmony_ci发送APDU数据到对端读卡设备,使用Promise异步回调。应用程序必须在[on](#on8)收到读卡设备发送的APDU数据后,才调用该接口响应数据。
411e41f4b71Sopenharmony_ci
412e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.NFC_CARD_EMULATION
413e41f4b71Sopenharmony_ci
414e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Communication.NFC.CardEmulation
415e41f4b71Sopenharmony_ci
416e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
417e41f4b71Sopenharmony_ci
418e41f4b71Sopenharmony_ci**参数:**
419e41f4b71Sopenharmony_ci
420e41f4b71Sopenharmony_ci| 参数名       | 类型     | 必填 | 说明                                               |
421e41f4b71Sopenharmony_ci| ------------ | -------- | ---- | -------------------------------------------------- |
422e41f4b71Sopenharmony_ci| response | number[] | 是   | 发送到对端读卡设备的符合APDU协议的数据,每个number十六进制表示,范围是0x00~0xFF。 |
423e41f4b71Sopenharmony_ci
424e41f4b71Sopenharmony_ci**返回值:**
425e41f4b71Sopenharmony_ci
426e41f4b71Sopenharmony_ci| **类型**  | **说明**                                 |
427e41f4b71Sopenharmony_ci| ------- | -------------------------------------- |
428e41f4b71Sopenharmony_ci| Promise\<void> | 以Promise形式异步返回发送APDU数据的结果。 |
429e41f4b71Sopenharmony_ci
430e41f4b71Sopenharmony_ci**错误码:**
431e41f4b71Sopenharmony_ci
432e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。
433e41f4b71Sopenharmony_ci
434e41f4b71Sopenharmony_ci| 错误码ID | 错误信息|
435e41f4b71Sopenharmony_ci| ------- | -------|
436e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
437e41f4b71Sopenharmony_ci|401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. |
438e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
439e41f4b71Sopenharmony_ci| 3100301 | Card emulation running state is abnormal in service. |
440e41f4b71Sopenharmony_ci
441e41f4b71Sopenharmony_ci**示例:**
442e41f4b71Sopenharmony_ci```js
443e41f4b71Sopenharmony_ciimport { cardEmulation } from '@kit.ConnectivityKit';
444e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
445e41f4b71Sopenharmony_ci
446e41f4b71Sopenharmony_cilet hceService: cardEmulation.HceService = new cardEmulation.HceService();
447e41f4b71Sopenharmony_ci
448e41f4b71Sopenharmony_ci// the data app wanna send, just a example data
449e41f4b71Sopenharmony_ciconst responseData = [0x1, 0x2];
450e41f4b71Sopenharmony_cihceService.transmit(responseData).then(() => {
451e41f4b71Sopenharmony_ci  // handle the transmit promise
452e41f4b71Sopenharmony_ci  console.log("transmit Promise success.");
453e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
454e41f4b71Sopenharmony_ci  console.log("transmit Promise error:", err);
455e41f4b71Sopenharmony_ci});
456e41f4b71Sopenharmony_ci```
457e41f4b71Sopenharmony_ci
458e41f4b71Sopenharmony_ci### transmit<sup>9+</sup>
459e41f4b71Sopenharmony_ci
460e41f4b71Sopenharmony_citransmit(response: number[], callback: AsyncCallback\<void>): void
461e41f4b71Sopenharmony_ci
462e41f4b71Sopenharmony_ci发送APDU数据到对端读卡设备,使用Callback异步回调。应用程序必须在[on](#on8)收到读卡设备发送的APDU数据后,才调用该接口响应数据。
463e41f4b71Sopenharmony_ci
464e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.NFC_CARD_EMULATION
465e41f4b71Sopenharmony_ci
466e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Communication.NFC.CardEmulation
467e41f4b71Sopenharmony_ci
468e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
469e41f4b71Sopenharmony_ci
470e41f4b71Sopenharmony_ci**参数:**
471e41f4b71Sopenharmony_ci
472e41f4b71Sopenharmony_ci| 参数名  | 类型     | 必填 | 说明                    |
473e41f4b71Sopenharmony_ci| ------- | -------- | ---- | ----------------------- |
474e41f4b71Sopenharmony_ci| response | number[] | 是   | 发送到对端读卡设备的符合APDU协议的数据,每个number十六进制表示,范围是0x00~0xFF。 |
475e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | 是   | 以callback形式异步返回发送APDU数据的结果。 |
476e41f4b71Sopenharmony_ci
477e41f4b71Sopenharmony_ci**错误码:**
478e41f4b71Sopenharmony_ci
479e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。
480e41f4b71Sopenharmony_ci
481e41f4b71Sopenharmony_ci| 错误码ID | 错误信息|
482e41f4b71Sopenharmony_ci| ------- | -------|
483e41f4b71Sopenharmony_ci|201 | Permission denied.                 |
484e41f4b71Sopenharmony_ci|401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. |
485e41f4b71Sopenharmony_ci|801 | Capability not supported.          |
486e41f4b71Sopenharmony_ci| 3100301 | Card emulation running state is abnormal in service. |
487e41f4b71Sopenharmony_ci
488e41f4b71Sopenharmony_ci**示例:**
489e41f4b71Sopenharmony_ci```js
490e41f4b71Sopenharmony_ciimport { cardEmulation } from '@kit.ConnectivityKit';
491e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
492e41f4b71Sopenharmony_ci
493e41f4b71Sopenharmony_cilet hceService: cardEmulation.HceService = new cardEmulation.HceService();
494e41f4b71Sopenharmony_ci
495e41f4b71Sopenharmony_ci// the data app wanna send, just a example data
496e41f4b71Sopenharmony_citry {
497e41f4b71Sopenharmony_ci  const responseData = [0x1, 0x2];
498e41f4b71Sopenharmony_ci
499e41f4b71Sopenharmony_ci  hceService.transmit(responseData, (err : BusinessError)=> {
500e41f4b71Sopenharmony_ci    if (err) {
501e41f4b71Sopenharmony_ci      console.error(`transmit AsyncCallback err Code: ${err.code}, message: ${err.message}`);
502e41f4b71Sopenharmony_ci    } else {
503e41f4b71Sopenharmony_ci      console.log("transmit AsyncCallback success.");
504e41f4b71Sopenharmony_ci    }
505e41f4b71Sopenharmony_ci  });
506e41f4b71Sopenharmony_ci} catch (error) {
507e41f4b71Sopenharmony_ci  console.error(`transmit AsyncCallback catch Code: ${(error as BusinessError).code}, ` +
508e41f4b71Sopenharmony_ci    `message: ${(error as BusinessError).message}`);
509e41f4b71Sopenharmony_ci}
510e41f4b71Sopenharmony_ci```
511e41f4b71Sopenharmony_ci
512