未验证 提交 482a1964 编写于 作者: O openharmony_ci 提交者: Gitee

!10921 [翻译完成】#I5TEZD

Merge pull request !10921 from Annie_wang/PR10015
# Standard NFC Card Emulation
The **cardEmulation** module implements Near-Field Communication (NFC) card emulation.
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).
> **NOTE**<br>
> 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.
......@@ -12,6 +12,17 @@ The **cardEmulation** module implements Near-Field Communication (NFC) card emul
import cardEmulation from '@ohos.nfc.cardEmulation';
```
## FeatureType
Enumerates the NFC card emulation types.
**System capability**: SystemCapability.Communication.NFC.Core
| Name| Default Value| Description|
| -------- | -------- | -------- |
| HCE | 0 | HCE.|
| UICC | 1 | Subscriber identity module (SIM) card emulation.|
| ESE | 2 | embedded Secure Element (eSE) emulation.|
## cardEmulation.isSupported
......@@ -19,23 +30,31 @@ isSupported(feature: number): boolean
Checks whether a certain type of card emulation is supported.
**Required permissions**: ohos.permission.NFC_CARD_EMULATION
**System capability**: SystemCapability.Communication.NFC.Core
**Parameters**
| Name | Type | Mandatory| Description |
| ------- | -------- | ---- | ----------------------- |
| feature | number | Yes | Card emulation type. For details, see [FeatureType](#featuretype).|
**Return value**
| **Type**| **Description**|
| -------- | -------- |
| boolean | Returns **true** if the card emulation is supported; returns **false** otherwise.|
| boolean | Returns **true** if the card emulation type is supported; returns **false** otherwise.|
## HceService<sup>8+</sup>
Implements Host-based Card Emulation (HCE). Before calling any API in **HceService**, you must use **new cardEmulation.HceService()** to create an **HceService** instance.
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.
### startHCE<sup>8+</sup>
startHCE(aidList: string[]): boolean
Starts HCE.
Starts HCE, including setting the application to be foreground preferred and dynamically registering the application identifier (AID) list.
**Required permissions**: ohos.permission.NFC_CARD_EMULATION
......@@ -45,13 +64,13 @@ Starts HCE.
| Name | Type | Mandatory| Description |
| ------- | -------- | ---- | ----------------------- |
| aidList | string[] | Yes | Application ID (AID) list to be registered for card emulation.|
| aidList | string[] | Yes | AID list to register.|
### stopHCE<sup>8+</sup>
stopHCE(): boolean
Stops HCE.
Stops HCE, including removing the foreground preferred attribute and releasing the dynamically registered AID list.
**Required permissions**: ohos.permission.NFC_CARD_EMULATION
......@@ -61,7 +80,7 @@ Stops HCE.
on(type: "hceCmd", callback: AsyncCallback<number[]>): void;
Subscribes to messages from the peer device after **startHCE()**.
Registers a callback to receive APDUs from the peer card reader.
**Required permissions**: ohos.permission.NFC_CARD_EMULATION
......@@ -72,13 +91,13 @@ Subscribes to messages from the peer device after **startHCE()**.
| 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).|
| callback | AsyncCallback<number[]> | Yes | Callback invoked to return the APDU. Each number in the callback is a hexadecimal number ranging from **0x00** to **0xFF**.|
### sendResponse<sup>8+</sup>
sendResponse(responseApdu: number[]): void;
Sends a response to the peer device.
Sends a response to the peer card reader.
**Required permissions**: ohos.permission.NFC_CARD_EMULATION
......@@ -88,16 +107,25 @@ Sends a response to the peer device.
| Name | Type | Mandatory| Description |
| ------------ | -------- | ---- | -------------------------------------------------- |
| responseApdu | number[] | Yes | Data to send, which is an array that complies with the APDU.|
| 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**.|
**Example**
```js
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.
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));
......@@ -108,4 +136,7 @@ hceService.on("hceCmd", (err, res) => {
console.log('callback => Operation hceCmd failed. Cause: ' + err.data);
}
})
// Stop HCE when the application exits the NFC card emulation.
hceService.stopHCE();
```
# Standard NFC
The **nfcController** module implements Near-Field Communication (NFC).
The **nfcController** module provides APIs for opening and closing Near-Field Communication (NFC) and reading the NFC state.
> **NOTE**<br>
> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
......@@ -12,12 +12,24 @@ The **nfcController** module implements Near-Field Communication (NFC).
import controller from '@ohos.nfc.controller';
```
## NfcState
Enumerates the NFC states.
**System capability**: SystemCapability.Communication.NFC.Core
| Name| Default Value| Description|
| -------- | -------- | -------- |
| STATE_OFF | 1 | NFC is closed (OFF).|
| STATE_TURNING_ON | 2 | NFC is turning on.|
| STATE_ON | 3 | NFC is open (ON).|
| STATE_TURNING_OFF | 4 | NFC is turning off.|
## controller.isNfcAvailable
isNfcAvailable(): boolean
Checks whether NFC is available.
Checks whether the device supports NFC.
**System capability**: SystemCapability.Communication.NFC.Core
......@@ -25,7 +37,7 @@ Checks whether NFC is available.
| **Type**| **Description**|
| -------- | -------- |
| boolean | Returns **true** if NFC is available; returns **false** otherwise.|
| boolean | Returns **true** if the device supports NFC; returns **false** otherwise.|
## controller.openNfc
......@@ -42,7 +54,7 @@ Opens NFC.
| **Type**| **Description**|
| -------- | -------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise. |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
## controller.closeNfc
......@@ -76,7 +88,7 @@ Checks whether NFC is open.
## controller.getNfcState
getNfcState(): NfcState
getNfcState(): [NfcState](#nfcstate)
Obtains the NFC state.
......@@ -86,13 +98,13 @@ Obtains the NFC state.
| **Type**| **Description** |
| -------- | ---------------------- |
| NfcState | NFC state obtained. For details, see [NfcState](#nfcstate).|
| [NfcState](#nfcstate) | NFC state obtained. For details, see [NfcState](#nfcstate).|
## controller.on('nfcStateChange')
on(type: "nfcStateChange", callback: Callback&lt;NfcState&gt;): void
on(type: "nfcStateChange", callback: Callback&lt;[NfcState](#nfcstate)&gt;): void
Subscribes to NFC state changes.
Subscribes to NFC state changes. A callback will be invoked to return the NFC state when the NFC state changes.
**System capability**: SystemCapability.Communication.NFC.Core
......@@ -101,15 +113,15 @@ Subscribes to NFC state changes.
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type to subscribe to. The value is **nfcStateChange**.|
| callback | Callback&lt;NfcState&gt; | Yes| Callback invoked to return the NFC state changes.|
| callback | Callback&lt;[NfcState](#nfcstate)&gt; | Yes| Callback invoked to return the NFC state.|
## controller.off('nfcStateChange')
off(type: "nfcStateChange", callback?: Callback&lt;NfcState&gt;): void
off(type: "nfcStateChange", callback?: Callback&lt;[NfcState](#nfcstate)&gt;): void
Unsubscribes from the NFC state changes.
Unsubscribes from the NFC state changes. The subscriber will not receive NFC state change notifications.
**System capability**: SystemCapability.Communication.NFC.Core
......@@ -118,35 +130,37 @@ Unsubscribes from the NFC state changes.
| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type to unsubscribe from. The value is **nfcStateChange**.|
| callback | Callback&lt;NfcState&gt; | No| Callback used to return the NFC state changes. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.|
| callback | Callback&lt;[NfcState](#nfcstate)&gt; | No| Callback for the NFC state changes. This parameter can be left blank.|
**Example**
```js
import nfcController from '@ohos.nfcController';
var NFC_STATE_NOTIFY = "nfcStateChange";
var recvNfcStateNotifyFunc = result => {
console.info("nfc state receive state: " + result);
import controller from '@ohos.nfc.controller';
// Define a callback key.
var NFC_STATE_CALLBACK_KEY = "nfcStateChange";
// Register the callback to receive NFC state change notifications.
controller.on(NFC_STATE_CALLBACK_KEY, (err, nfcState)=> {
if (err) {
console.log("controller on callback err: " + err);
} else {
console.log("controller on callback nfcState: " + nfcState);
}
});
// Open NFC. Require permission: ohos.permission.MANAGE_SECURE_SETTINGS.
if (!controller.isNfcOpen()) {
var ret = controller.openNfc();
console.log("controller openNfc ret: " + ret);
}
// Subscribe to the NFC state changes.
nfcController.on(NFC_STATE_NOTIFY, recvNfcStateNotifyFunc);
// Unsubscribe from the NFC state changes.
nfcController.off(NFC_STATE_NOTIFY, recvNfcStateNotifyFunc);
```
## NfcState
Enumerates the NFC states.
**System capability**: SystemCapability.Communication.NFC.Core
// Close NFC. Require permission: ohos.permission.MANAGE_SECURE_SETTINGS.
if (controller.isNfcOpen()) {
var ret = controller.closeNfc();
console.log("controller closeNfc ret: " + ret);
}
| Name| Default Value| Description|
| -------- | -------- | -------- |
| STATE_OFF | 1 | Off|
| STATE_TURNING_ON | 2 | Turning on|
| STATE_ON | 3 | On|
| STATE_TURNING_OFF | 4 | Turning off|
// Unregister the callback.
controller.off(NFC_STATE_CALLBACK_KEY);
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册