1# @ohos.nfc.cardEmulation (标准NFC-cardEmulation) 2 3本模块主要提供NFC卡模拟业务,包括判断支持哪种卡模拟类型,HCE卡模拟的业务实现等。 4 5> **说明:** 6> 7> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9## 导入模块 10 11``` 12import cardEmulation from '@ohos.nfc.cardEmulation'; 13``` 14 15## FeatureType 16 17定义不同的NFC卡模拟类型。 18 19> **说明:** 20> 从 API version 6 开始支持,从 API version 9 开始废弃,建议使用[hasHceCapability](#hashcecapability9)替代。 21 22**系统能力:** SystemCapability.Communication.NFC.CardEmulation 23 24| 名称 | 值 | 说明 | 25| ---- | ---- | -------- | 26| HCE | 0 | HCE 卡模拟。 | 27| UICC | 1 | SIM 卡模拟。 | 28| ESE | 2 | ESE卡模拟。 | 29 30## CardType<sup>9+</sup> 31 32定义卡模拟应用所使用的业务类型,是支付类型,还是其他类型。 33 34**系统能力:** SystemCapability.Communication.NFC.CardEmulation 35 36| 名称 | 值 | 说明 | 37| ------- | --------- | ----------------- | 38| PAYMENT | "payment" | 卡模拟应用所使用的业务是支付类型。 | 39| OTHER | "other" | 卡模拟应用所使用的业务是其他类型。 | 40 41## isSupported 42 43isSupported(feature: number): boolean 44 45是否支持某种类型的卡模拟。 46 47> **说明:** 48> 从 API version 6 开始支持,从 API version 9 开始废弃,建议使用[hasHceCapability](#hashcecapability9)替代。 49 50**系统能力:** SystemCapability.Communication.NFC.CardEmulation 51 52**参数:** 53 54| 参数名 | 类型 | 必填 | 说明 | 55| ------- | ------ | ---- | ---------------------------------------- | 56| feature | number | 是 | 卡模拟类型值,详细请见[FeatureType](#featuretype)枚举值。 | 57 58**返回值:** 59 60| **类型** | **说明** | 61| ------- | -------------------------------------- | 62| boolean | true: 支持该类型卡模拟, false: 不支持该类型卡模拟。 | 63 64## hasHceCapability<sup>9+</sup> 65 66hasHceCapability(): boolean 67 68判断是否支持HCE功能。 69 70**系统能力:** SystemCapability.Communication.NFC.CardEmulation 71 72**需要权限:** ohos.permission.NFC_CARD_EMULATION 73 74**返回值:** 75 76| **类型** | **说明** | 77| ------- | -------------------------------- | 78| boolean | true: 支持HCE, false: 不支持HCE。 | 79 80## isDefaultService<sup>9+</sup> 81 82isDefaultService(elementName: ElementName, type: CardType): boolean 83 84判断指定的应用是否为指定业务类型的默认应用。 85 86**系统能力:** SystemCapability.Communication.NFC.CardEmulation 87 88**需要权限:** ohos.permission.NFC_CARD_EMULATION 89 90**参数:** 91 92| 参数名 | 类型 | 必填 | 说明 | 93| ----------- | ---------------------------------------- | ---- | ----------------------- | 94| elementName | [ElementName](js-apis-bundleManager-elementName.md#elementname) | 是 | 应用的描述,由Bundle名称和组件名称组成。 | 95| type | [CardType](#cardtype9) | 是 | 卡模拟业务类型。 | 96 97**返回值:** 98 99| **类型** | **说明** | 100| ------- | ------------------------------------ | 101| boolean | true: 是默认支付应用, false: 不是默认支付应用。 | 102 103**示例:** 104 105```js 106import cardEmulation from '@ohos.nfc.cardEmulation'; 107 108var isHceSupported = cardEmulation.isSupported(cardEmulation.FeatureType.HCE); 109if (!isHceSupported) { 110 console.log('this device is not supported for HCE, ignore it.'); 111} 112 113var hasHceCap = cardEmulation.hasHceCapability(); 114if (!hasHceCap) { 115 console.log('this device hasHceCapability false, ignore it.'); 116} 117 118var elementName = { 119 "bundleName": "com.example.myapplication", 120 "abilityName": "EntryAbility", 121}; 122var isDefaultService = cardEmulation.isDefaultService(elementName, cardEmulation.CardType.PAYMENT); 123console.log('is the app is default service for this card type: ' + isDefaultService); 124``` 125 126