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 { nfcController } from '@kit.ConnectivityKit'; 13``` 14 15## NfcState 16 17Enumerates the NFC states. 18 19**System capability**: SystemCapability.Communication.NFC.Core 20 21**Atomic service API**: This API can be used in atomic services since API version 12. 22 23| Name| Value| Description| 24| -------- | -------- | -------- | 25| STATE_OFF | 1 | NFC is closed (OFF).| 26| STATE_TURNING_ON | 2 | NFC is turning on.| 27| STATE_ON | 3 | NFC is open (ON).| 28| STATE_TURNING_OFF | 4 | NFC is turning off.| 29 30## nfcController.isNfcAvailable<sup>(deprecated)</sup> 31 32isNfcAvailable(): boolean 33 34Checks whether the device supports NFC. 35 36> **NOTE** 37> This API is supported since API version 7 and deprecated since API version 9. Use [canIUse("SystemCapability.Communication.NFC.Core")](../common/init.md#caniuse) instead. 38 39**System capability**: SystemCapability.Communication.NFC.Core 40 41**Return value** 42 43| **Type**| **Description**| 44| -------- | -------- | 45| boolean | Returns **true** if the device supports NFC; returns **false** otherwise.| 46 47 48## nfcController.openNfc<sup>(deprecated)</sup> 49 50openNfc(): boolean 51 52Opens NFC. 53 54> **NOTE** 55> This API is supported since API version 7 and deprecated since API version 9. Use [enableNfc](#nfccontrollerenablenfc9) instead. 56 57**Required permissions**: ohos.permission.MANAGE_SECURE_SETTINGS (available only for system applications) 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## nfcController.enableNfc<sup>9+</sup> 68 69enableNfc(): void 70 71Enables NFC. This API can be called only by system applications. 72 73**Required permissions**: ohos.permission.MANAGE_SECURE_SETTINGS (available only for system applications) 74 75**System capability**: SystemCapability.Communication.NFC.Core 76 77**Error codes** 78 79For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 80 81| ID| Error Message| 82| ------- | -------| 83|201 | Permission denied. | 84|801 | Capability not supported. | 85| 3100101 | NFC state is abnormal in service. | 86 87## nfcController.closeNfc<sup>(deprecated)</sup> 88 89closeNfc(): boolean 90 91Closes NFC. 92 93> **NOTE** 94> This API is supported since API version 7 and deprecated since API version 9. Use [disableNfc](#nfccontrollerdisablenfc9) instead. 95 96**Required permissions**: ohos.permission.MANAGE_SECURE_SETTINGS (available only for system applications) 97 98**System capability**: SystemCapability.Communication.NFC.Core 99 100**Return value** 101 102| **Type**| **Description** | 103| -------- | ------------------------------------------- | 104| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 105 106## nfcController.disableNfc<sup>9+</sup> 107 108disableNfc(): void 109 110Disables NFC. This API can be called only by system applications. 111 112**Required permissions**: ohos.permission.MANAGE_SECURE_SETTINGS (available only for system applications) 113 114**System capability**: SystemCapability.Communication.NFC.Core 115 116**Error codes** 117 118For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 119 120| ID| Error Message| 121| ------- | -------| 122|201 | Permission denied. | 123|801 | Capability not supported. | 124| 3100101 | NFC state is abnormal in service. | 125 126## nfcController.isNfcOpen 127 128isNfcOpen(): boolean 129 130Checks whether NFC is open. 131 132**System capability**: SystemCapability.Communication.NFC.Core 133 134**Atomic service API**: This API can be used in atomic services since API version 12. 135 136**Return value** 137 138| **Type**| **Description** | 139| -------- | ----------------------------------- | 140| boolean | Returns **true** if NFC is open; returns **false** otherwise.| 141 142## nfcController.getNfcState 143 144getNfcState(): [NfcState](#nfcstate) 145 146Obtains the NFC state. 147 148**System capability**: SystemCapability.Communication.NFC.Core 149 150**Atomic service API**: This API can be used in atomic services since API version 12. 151 152**Return value** 153 154| **Type**| **Description** | 155| -------- | ---------------------- | 156| [NfcState](#nfcstate) | NFC state obtained. For details, see [NfcState](#nfcstate).| 157 158## nfcController.on('nfcStateChange') 159 160on(type: 'nfcStateChange', callback: Callback<[NfcState](#nfcstate)>): void 161 162Subscribes to NFC state changes. A callback will be invoked to return the NFC state when the NFC state changes. 163 164**System capability**: SystemCapability.Communication.NFC.Core 165 166**Atomic service API**: This API can be used in atomic services since API version 12. 167 168**Parameters** 169 170| **Name**| **Type**| **Mandatory**| **Description**| 171| -------- | -------- | -------- | -------- | 172| type | string | Yes| Event type. The value is **nfcStateChange**.| 173| callback | Callback<[NfcState](#nfcstate)> | Yes| Callback used to return the NFC state.| 174 175## nfcController.off('nfcStateChange') 176 177off(type: 'nfcStateChange', callback?: Callback<[NfcState](#nfcstate)>): void 178 179Unsubscribes from the NFC state changes. The subscriber will not receive NFC state change notifications. 180 181**System capability**: SystemCapability.Communication.NFC.Core 182 183**Atomic service API**: This API can be used in atomic services since API version 12. 184 185**Parameters** 186 187| **Name**| **Type**| **Mandatory**| **Description**| 188| -------- | -------- | -------- | -------- | 189| type | string | Yes| Event type. The value is **nfcStateChange**.| 190| callback | Callback<[NfcState](#nfcstate)> | 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.| 191 192**Example** 193 194```js 195import { nfcController } from '@kit.ConnectivityKit'; 196 197// Register a callback to receive the NFC state change notification. 198nfcController.on("nfcStateChange", (nfcState : number)=> { 199 console.log("nfcController on callback nfcState: " + nfcState); 200}); 201 202// Open NFC. The caller must have the ohos.permission.MANAGE_SECURE_SETTINGS permission. This permission is available only for system applications. 203if (!nfcController.isNfcOpen()) { 204// Use 'enableNfc' to enable NFC since API version 9. 205 try { 206 nfcController.enableNfc(); 207 console.log("nfcController enableNfc success"); 208 } catch (businessError) { 209 console.error("nfcController enableNfc businessError: " + businessError); 210 } 211} else { 212 console.log("nfcController NFC has been opened"); 213} 214 215// Close NFC. The caller must have the ohos.permission.MANAGE_SECURE_SETTINGS permission. This permission is available only for applications. 216if (nfcController.isNfcOpen()) { 217// Use 'disableNfc' to disable NFC since API version 9. 218 try { 219 nfcController.disableNfc(); 220 console.log("nfcController disableNfc success"); 221 } catch (businessError) { 222 console.error("nfcController disableNfc businessError: " + businessError); 223 } 224} else { 225 console.log("nfcController NFC has been closed"); 226} 227 228// Unregister the callback. 229nfcController.off("nfcStateChange"); 230``` 231