• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.nfc.controller (Standard NFC)
2
3The **nfcController** module provides APIs for opening and closing Near-Field Communication (NFC) and reading the NFC state.
4
5> **NOTE**
6>
7> 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.
8
9## **Modules to Import**
10
11```js
12import controller from '@ohos.nfc.controller';
13```
14
15## NfcState
16
17Enumerates the NFC states.
18
19**System capability**: SystemCapability.Communication.NFC.Core
20
21| Name| Value| Description|
22| -------- | -------- | -------- |
23| STATE_OFF | 1 | NFC is closed (OFF).|
24| STATE_TURNING_ON | 2 | NFC is turning on.|
25| STATE_ON | 3      | NFC is open (ON).|
26| STATE_TURNING_OFF | 4      | NFC is turning off.|
27
28## controller.isNfcAvailable
29
30isNfcAvailable(): boolean
31
32Checks whether the device supports NFC.
33
34> **NOTE**
35> This API is supported since API version 7 and deprecated since API version 9. You are advised to use canIUse("SystemCapability.Communication.NFC.Core").
36
37**System capability**: SystemCapability.Communication.NFC.Core
38
39**Return value**
40
41| **Type**| **Description**|
42| -------- | -------- |
43| boolean | Returns **true** if the device supports NFC; returns **false** otherwise.|
44
45
46## controller.openNfc
47
48openNfc(): boolean
49
50Opens NFC.
51
52> **NOTE**
53> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [enableNfc](#controllerenablenfc9).
54
55**Required permissions**: ohos.permission.MANAGE_SECURE_SETTINGS
56
57**System capability**: SystemCapability.Communication.NFC.Core
58
59**Return value**
60
61| **Type**| **Description**|
62| -------- | -------- |
63| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
64
65## controller.enableNfc<sup>9+</sup>
66
67enableNfc(): boolean
68
69Opens NFC.
70
71**Required permissions**: ohos.permission.MANAGE_SECURE_SETTINGS
72
73**System capability**: SystemCapability.Communication.NFC.Core
74
75**Error codes**
76
77For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
78
79| ID| Error Message|
80| ------- | -------|
81| 3100101 | NFC state is abnormal in service. |
82
83## controller.closeNfc
84
85closeNfc(): boolean
86
87Closes NFC.
88
89> **NOTE**
90> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [disableNfc](#controllerdisablenfc9).
91
92**Required permissions**: ohos.permission.MANAGE_SECURE_SETTINGS
93
94**System capability**: SystemCapability.Communication.NFC.Core
95
96**Return value**
97
98| **Type**| **Description**                                   |
99| -------- | ------------------------------------------- |
100| boolean  | Returns **true** if the operation is successful; returns **false** otherwise.|
101
102## controller.disableNfc<sup>9+</sup>
103
104disableNfc(): boolean
105
106Closes NFC.
107
108**Required permissions**: ohos.permission.MANAGE_SECURE_SETTINGS
109
110**System capability**: SystemCapability.Communication.NFC.Core
111
112**Error codes**
113
114For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
115
116| ID| Error Message|
117| ------- | -------|
118| 3100101 | NFC state is abnormal in service. |
119
120## controller.isNfcOpen
121
122isNfcOpen(): boolean
123
124Checks whether NFC is open.
125
126**System capability**: SystemCapability.Communication.NFC.Core
127
128**Return value**
129
130| **Type**| **Description**                           |
131| -------- | ----------------------------------- |
132| boolean  | Returns **true** if NFC is open; returns **false** otherwise.|
133
134## controller.getNfcState
135
136getNfcState(): [NfcState](#nfcstate)
137
138Obtains the NFC state.
139
140**System capability**: SystemCapability.Communication.NFC.Core
141
142**Return value**
143
144| **Type**| **Description**              |
145| -------- | ---------------------- |
146| [NfcState](#nfcstate) | NFC state obtained. For details, see [NfcState](#nfcstate).|
147
148## controller.on('nfcStateChange')
149
150on(type: "nfcStateChange", callback: Callback&lt;[NfcState](#nfcstate)&gt;): void
151
152Subscribes to NFC state changes. A callback will be invoked to return the NFC state when the NFC state changes.
153
154**System capability**: SystemCapability.Communication.NFC.Core
155
156**Parameters**
157
158| **Name**| **Type**| **Mandatory**| **Description**|
159| -------- | -------- | -------- | -------- |
160| type | string | Yes| Event type to subscribe to. The value is **nfcStateChange**.|
161| callback | Callback&lt;[NfcState](#nfcstate)&gt; | Yes| Callback invoked to return the NFC state.|
162
163## controller.off('nfcStateChange')
164
165off(type: "nfcStateChange", callback?: Callback&lt;[NfcState](#nfcstate)&gt;): void
166
167Unsubscribes from the NFC state changes. The subscriber will not receive NFC state change notifications.
168
169**System capability**: SystemCapability.Communication.NFC.Core
170
171**Parameters**
172
173| **Name**| **Type**| **Mandatory**| **Description**|
174| -------- | -------- | -------- | -------- |
175| type | string | Yes| Event type to unsubscribe from. The value is **nfcStateChange**.|
176| callback | Callback&lt;[NfcState](#nfcstate)&gt; | No| Callback for the NFC state changes. This parameter can be left blank.|
177
178**Example**
179
180```js
181import controller from '@ohos.nfc.controller';
182
183// Register the callback to receive NFC state change notifications.
184controller.on("nfcStateChange", (err, nfcState)=> {
185  if (err) {
186      console.log("controller on callback err: " + err);
187  } else {
188      console.log("controller on callback nfcState: " + nfcState);
189  }
190});
191
192  // Open NFC. Require permission: ohos.permission.MANAGE_SECURE_SETTINGS.
193if (!controller.isNfcOpen()) {
194  var ret = controller.openNfc();
195  console.log("controller openNfc ret: " + ret);
196}
197
198// Use 'enableNfc' to enable NFC from API version 9.
199try {
200    controller.enableNfc();
201    console.log("controller enableNfc success");
202} catch (busiError) {
203    console.log("controller enableNfc busiError: " + busiError);
204}
205
206// Close NFC. Require permission: ohos.permission.MANAGE_SECURE_SETTINGS.
207if (controller.isNfcOpen()) {
208  var ret = controller.closeNfc();
209  console.log("controller closeNfc ret: " + ret);
210}
211
212// Use 'disableNfc' to disable NFC from API version 9.
213try {
214    controller.disableNfc();
215    console.log("controller disableNfc success");
216} catch (busiError) {
217    console.log("controller disableNfc busiError: " + busiError);
218}
219
220// Unregister the callback.
221controller.off("nfcStateChange");
222```
223