• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Standard NFC
2
3Implements Near-Field Communication (NFC).
4
5> **NOTE**<br>
6> 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.
7
8
9## **Modules to Import**
10
11```
12import controller from '@ohos.nfc.controller';
13```
14
15
16## controller.isNfcAvailable
17
18isNfcAvailable(): boolean
19
20Checks whether NFC is available.
21
22**Return value**
23
24| **Type**| **Description**|
25| -------- | -------- |
26| boolean | Returns **true** if NFC is available; returns **false** otherwise.|
27
28
29## controller.openNfc
30
31openNfc(): boolean
32
33Opens NFC.
34
35**Required permissions**: ohos.permission.MANAGE_SECURE_SETTINGS
36
37**System capability**: SystemCapability.Communication.NFC
38
39**Return value**
40
41| **Type**| **Description**|
42| -------- | -------- |
43| boolean | Returns **true** if the operation is successful; returns **false** otherwise. |
44
45## controller.closeNfc
46
47closeNfc(): boolean
48
49Closes NFC.
50
51**Required permissions**: ohos.permission.MANAGE_SECURE_SETTINGS
52
53**System capability**: SystemCapability.Communication.NFC
54
55**Return value**
56
57| **Type**| **Description**                                   |
58| -------- | ------------------------------------------- |
59| boolean  | Returns **true** if the operation is successful; returns **false** otherwise.|
60
61## controller.isNfcOpen
62
63isNfcOpen(): boolean
64
65Checks whether NFC is open.
66
67**System capability**: SystemCapability.Communication.NFC
68
69**Return value**
70
71| **Type**| **Description**                           |
72| -------- | ----------------------------------- |
73| boolean  | Returns **true** if NFC is open; returns **false** otherwise.|
74
75## controller.getNfcState
76
77getNfcState(): NfcState
78
79Obtains the NFC state.
80
81**System capability**: SystemCapability.Communication.NFC
82
83**Return value**
84
85| **Type**| **Description**              |
86| -------- | ---------------------- |
87| NfcState | NFC state obtained. For details, see [NfcState](#nfcstate).|
88
89## controller.on('nfcStateChange')
90
91on(type: "nfcStateChange", callback: Callback&lt;NfcState&gt;): void
92
93Subscribes to NFC state changes.
94
95**System capability**: SystemCapability.Communication.NFC
96
97**Parameter**
98
99  | **Name**| **Type**| **Mandatory**| **Description**|
100  | -------- | -------- | -------- | -------- |
101  | type | string | Yes| Event type to subscribe to. The value is **nfcStateChange**.|
102  | callback | Callback&lt;NfcState&gt; | Yes| Callback invoked to return the NFC state changes.|
103
104
105
106## controller.off('nfcStateChange')
107
108off(type: "nfcStateChange", callback?: Callback&lt;NfcState&gt;): void
109
110Unsubscribes from the NFC state changes.
111
112**System capability**: SystemCapability.Communication.NFC
113
114**Parameter**
115
116  | **Name**| **Type**| **Mandatory**| **Description**|
117  | -------- | -------- | -------- | -------- |
118  | type | string | Yes| Event type to unsubscribe from. The value is **nfcStateChange**.|
119| 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.|
120
121**Example**
122
123  ```js
124  import nfcController from '@ohos.nfcController';
125
126  var NFC_STATE_NOTIFY = "nfcStateChange";
127
128  var recvNfcStateNotifyFunc = result => {
129      console.info("nfc state receive state: " + result);
130  }
131
132  // Subscribe to the NFC state changes.
133  nfcController.on(NFC_STATE_NOTIFY, recvNfcStateNotifyFunc);
134
135  // Unsubscribe from the NFC state changes.
136  nfcController.off(NFC_STATE_NOTIFY, recvNfcStateNotifyFunc);
137  ```
138
139## NfcState
140
141Enumerates the NFC states.
142
143| Name| Default Value| Description|
144| -------- | -------- | -------- |
145| STATE_OFF | 1 | Off|
146| STATE_TURNING_ON | 2 | Turning on|
147| STATE_ON | 3      | On|
148| STATE_TURNING_OFF | 4      | Turning off|
149