js-apis-nfcController.md 3.9 KB
Newer Older
A
Annie_wang 已提交
1 2
# Standard NFC

A
Annie_wang 已提交
3
The **nfcController** module implements Near-Field Communication (NFC).
A
Annie_wang 已提交
4 5

> **NOTE**<br>
A
Annie_wang 已提交
6
> 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.
A
Annie_wang 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21


## **Modules to Import**

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


## controller.isNfcAvailable

isNfcAvailable(): boolean

Checks whether NFC is available.

A
Annie_wang 已提交
22 23
**System capability**: SystemCapability.Communication.NFC.Core

A
Annie_wang 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
**Return value**

| **Type**| **Description**|
| -------- | -------- |
| boolean | Returns **true** if NFC is available; returns **false** otherwise.|


## controller.openNfc

openNfc(): boolean

Opens NFC.

**Required permissions**: ohos.permission.MANAGE_SECURE_SETTINGS

A
Annie_wang 已提交
39
**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

**Return value**

| **Type**| **Description**|
| -------- | -------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise. |

## controller.closeNfc

closeNfc(): boolean

Closes NFC.

**Required permissions**: ohos.permission.MANAGE_SECURE_SETTINGS

A
Annie_wang 已提交
55
**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68

**Return value**

| **Type**| **Description**                                   |
| -------- | ------------------------------------------- |
| boolean  | Returns **true** if the operation is successful; returns **false** otherwise.|

## controller.isNfcOpen

isNfcOpen(): boolean

Checks whether NFC is open.

A
Annie_wang 已提交
69
**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82

**Return value**

| **Type**| **Description**                           |
| -------- | ----------------------------------- |
| boolean  | Returns **true** if NFC is open; returns **false** otherwise.|

## controller.getNfcState

getNfcState(): NfcState

Obtains the NFC state.

A
Annie_wang 已提交
83
**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
84 85 86 87 88

**Return value**

| **Type**| **Description**              |
| -------- | ---------------------- |
A
Annie_wang 已提交
89
| NfcState | NFC state obtained. For details, see [NfcState](#nfcstate).|
A
Annie_wang 已提交
90 91 92 93 94

## controller.on('nfcStateChange')

on(type: "nfcStateChange", callback: Callback&lt;NfcState&gt;): void

A
Annie_wang 已提交
95
Subscribes to NFC state changes.
A
Annie_wang 已提交
96

A
Annie_wang 已提交
97
**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111

**Parameter**
  
  | **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.|



## controller.off('nfcStateChange')

off(type: "nfcStateChange", callback?: Callback&lt;NfcState&gt;): void

A
Annie_wang 已提交
112
Unsubscribes from the NFC state changes.
A
Annie_wang 已提交
113

A
Annie_wang 已提交
114
**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

**Parameter**
  
  | **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.|
  
**Example**

  ```js
  import nfcController from '@ohos.nfcController';
  
  var NFC_STATE_NOTIFY = "nfcStateChange";
  
  var recvNfcStateNotifyFunc = result => {
      console.info("nfc state receive state: " + result);
  }
  
A
Annie_wang 已提交
134
  // Subscribe to the NFC state changes.
A
Annie_wang 已提交
135 136
  nfcController.on(NFC_STATE_NOTIFY, recvNfcStateNotifyFunc);
  
A
Annie_wang 已提交
137
  // Unsubscribe from the NFC state changes.
A
Annie_wang 已提交
138 139 140 141 142 143 144
  nfcController.off(NFC_STATE_NOTIFY, recvNfcStateNotifyFunc);
  ```

## NfcState

Enumerates the NFC states.

A
Annie_wang 已提交
145 146
**System capability**: SystemCapability.Communication.NFC.Core

A
Annie_wang 已提交
147 148 149 150 151 152
| Name| Default Value| Description|
| -------- | -------- | -------- |
| STATE_OFF | 1 | Off|
| STATE_TURNING_ON | 2 | Turning on|
| STATE_ON | 3      | On|
| STATE_TURNING_OFF | 4      | Turning off|