openharmony 鸿蒙 js-apis-nfcController

2025-06-12 浏览 (1)

@ohos.nfc.controller (Standard NFC)

The nfcController module provides APIs for opening and closing Near-Field Communication (NFC) and reading the NFC state.

NOTE

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.

Modules to Import

import { nfcController } from '@kit.ConnectivityKit';

NfcState

Enumerates the NFC states.

System capability: SystemCapability.Communication.NFC.Core

Atomic service API: This API can be used in atomic services since API version 12.

NameValueDescription
STATE_OFF1NFC is closed (OFF).
STATE_TURNING_ON2NFC is turning on.
STATE_ON3NFC is open (ON).
STATE_TURNING_OFF4NFC is turning off.

nfcController.isNfcAvailable(deprecated)

isNfcAvailable(): boolean

Checks whether the device supports NFC.

NOTE This API is supported since API version 7 and deprecated since API version 9. Use canIUse("SystemCapability.Communication.NFC.Core") instead.

System capability: SystemCapability.Communication.NFC.Core

Return value

TypeDescription
booleanReturns true if the device supports NFC; returns false otherwise.

nfcController.openNfc(deprecated)

openNfc(): boolean

Opens NFC.

NOTE This API is supported since API version 7 and deprecated since API version 9. Use enableNfc instead.

Required permissions: ohos.permission.MANAGE_SECURE_SETTINGS (available only for system applications)

System capability: SystemCapability.Communication.NFC.Core

Return value

TypeDescription
booleanReturns true if the operation is successful; returns false otherwise.

nfcController.enableNfc9+

enableNfc(): void

Enables NFC. This API can be called only by system applications.

Required permissions: ohos.permission.MANAGE_SECURE_SETTINGS (available only for system applications)

System capability: SystemCapability.Communication.NFC.Core

Error codes

For details about the error codes, see NFC Error Codes.

IDError Message
201Permission denied.
801Capability not supported.
3100101The NFC state is abnormal in the service.

nfcController.closeNfc(deprecated)

closeNfc(): boolean

Closes NFC.

NOTE This API is supported since API version 7 and deprecated since API version 9. Use disableNfc instead.

Required permissions: ohos.permission.MANAGE_SECURE_SETTINGS (available only for system applications)

System capability: SystemCapability.Communication.NFC.Core

Return value

TypeDescription
booleanReturns true if the operation is successful; returns false otherwise.

nfcController.disableNfc9+

disableNfc(): void

Disables NFC. This API can be called only by system applications.

Required permissions: ohos.permission.MANAGE_SECURE_SETTINGS (available only for system applications)

System capability: SystemCapability.Communication.NFC.Core

Error codes

For details about the error codes, see NFC Error Codes.

IDError Message
201Permission denied.
801Capability not supported.
3100101The NFC state is abnormal in the service.

nfcController.isNfcOpen

isNfcOpen(): boolean

Checks whether NFC is open.

System capability: SystemCapability.Communication.NFC.Core

Atomic service API: This API can be used in atomic services since API version 12.

Return value

TypeDescription
booleanReturns true if NFC is open; returns false otherwise.

nfcController.getNfcState

getNfcState(): NfcState

Obtains the NFC state.

System capability: SystemCapability.Communication.NFC.Core

Atomic service API: This API can be used in atomic services since API version 12.

Return value

TypeDescription
NfcStateNFC state obtained. For details, see NfcState.

nfcController.on('nfcStateChange')

on(type: 'nfcStateChange', callback: Callback<NfcState>): void

Subscribes to NFC state changes. A callback will be invoked to return the NFC state when the NFC state changes.

System capability: SystemCapability.Communication.NFC.Core

Atomic service API: This API can be used in atomic services since API version 12.

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is nfcStateChange.
callbackCallback<NfcState>YesCallback used to return the NFC state.

nfcController.off('nfcStateChange')

off(type: 'nfcStateChange', callback?: Callback<NfcState>): void

Unsubscribes from the NFC state changes. The subscriber will not receive NFC state change notifications.

System capability: SystemCapability.Communication.NFC.Core

Atomic service API: This API can be used in atomic services since API version 12.

Parameters

NameTypeMandatoryDescription
typestringYesEvent type. The value is nfcStateChange.
callbackCallback<NfcState>NoCallback for the NFC state changes. This parameter can be left blank. If this parameter is not specified, this API unregisters all callbacks for the specified event.

Example

import { nfcController } from '@kit.ConnectivityKit';

// Register a callback for NFC status change events.
nfcController.on("nfcStateChange", (nfcState : number)=> {
  console.log("nfcController on callback nfcState: " + nfcState);
});

// Declare the ohos.permission.MANAGE_SECURE_SETTINGS permission for enabling NFC. This permission is available only for system applications.
if (!nfcController.isNfcOpen()) {
  // Use enableNfc to enable NFC since API version 9.
  try {
    nfcController.enableNfc();
    console.log("nfcController enableNfc success");
  } catch (businessError) {
    console.error("nfcController enableNfc businessError: " + businessError);
  }
} else {
  console.log("nfcController NFC has been opened");
}

// Declare the ohos.permission.MANAGE_SECURE_SETTINGS permission for disabling NFC. This permission is available only for system applications.
if (nfcController.isNfcOpen()) {
  // Use disableNfc to disable NFC since API version 9.
  try {
    nfcController.disableNfc();
    console.log("nfcController disableNfc success");
  } catch (businessError) {
    console.error("nfcController disableNfc businessError: " + businessError);
  }
} else {
  console.log("nfcController NFC has been closed");
}

// Unregister the callback for NFC status change events.
nfcController.off("nfcStateChange");

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Connectivity Kit (Short-Range Communication Service)

harmony 鸿蒙Bluetooth

harmony 鸿蒙Wifi

harmony 鸿蒙Bluetooth Error Codes

harmony 鸿蒙NFC Error Codes

harmony 鸿蒙SecureElement Error Codes

harmony 鸿蒙Wi-Fi Error Codes

harmony 鸿蒙@ohos.bluetooth.a2dp (Bluetooth A2DP Module) (System API)

harmony 鸿蒙@ohos.bluetooth.a2dp (Bluetooth A2DP Module)

harmony 鸿蒙@ohos.bluetooth.access (Bluetooth Access Module) (System API)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/PWyBSs