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