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

A
Annie_wang 已提交
3
Implements Near-Field Communication (NFC).
A
Annie_wang 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

> **NOTE**<br>
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.


## **Modules to Import**

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


## controller.isNfcAvailable

isNfcAvailable(): boolean

Checks whether NFC is available.

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

**System capability**: SystemCapability.Communication.NFC

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

**System capability**: SystemCapability.Communication.NFC

**Return value**

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

## controller.isNfcOpen

isNfcOpen(): boolean

Checks whether NFC is open.

**System capability**: SystemCapability.Communication.NFC

**Return value**

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

## controller.getNfcState

getNfcState(): NfcState

Obtains the NFC state.

**System capability**: SystemCapability.Communication.NFC

**Return value**

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

## controller.on('nfcStateChange')

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

A
Annie_wang 已提交
93
Subscribes to NFC state changes.
A
Annie_wang 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

**System capability**: SystemCapability.Communication.NFC

**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 已提交
110
Unsubscribes from the NFC state changes.
A
Annie_wang 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

**System capability**: SystemCapability.Communication.NFC

**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 已提交
132
  // Subscribe to the NFC state changes.
A
Annie_wang 已提交
133 134
  nfcController.on(NFC_STATE_NOTIFY, recvNfcStateNotifyFunc);
  
A
Annie_wang 已提交
135
  // Unsubscribe from the NFC state changes.
A
Annie_wang 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148
  nfcController.off(NFC_STATE_NOTIFY, recvNfcStateNotifyFunc);
  ```

## NfcState

Enumerates the NFC states.

| Name| Default Value| Description|
| -------- | -------- | -------- |
| STATE_OFF | 1 | Off|
| STATE_TURNING_ON | 2 | Turning on|
| STATE_ON | 3      | On|
| STATE_TURNING_OFF | 4      | Turning off|