harmony 鸿蒙@ohos.nfc.controller (Standard NFC)

2022-08-09 浏览 (928)

@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 controller from '@ohos.nfc.controller';

NfcState

Enumerates the NFC states.

System capability: SystemCapability.Communication.NFC.Core

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

controller.isNfcAvailable

isNfcAvailable(): boolean

Checks whether the device supports NFC.

NOTE

This API is supported since API version 7 and deprecated since API version 9. You are advised to use canIUse("SystemCapability.Communication.NFC.Core").

System capability: SystemCapability.Communication.NFC.Core

Return value

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

controller.openNfc

openNfc(): boolean

Opens NFC.

NOTE

This API is supported since API version 7 and deprecated since API version 9. You are advised to use enableNfc.

Required permissions: ohos.permission.MANAGE_SECURE_SETTINGS

System capability: SystemCapability.Communication.NFC.Core

Return value

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

controller.enableNfc9+

enableNfc(): void

Enables NFC.

Required permissions: ohos.permission.MANAGE_SECURE_SETTINGS

System capability: SystemCapability.Communication.NFC.Core

Error codes

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

IDError Message
3100101NFC state is abnormal in service.

controller.closeNfc

closeNfc(): boolean

Closes NFC.

NOTE

This API is supported since API version 7 and deprecated since API version 9. You are advised to use disableNfc.

Required permissions: ohos.permission.MANAGE_SECURE_SETTINGS

System capability: SystemCapability.Communication.NFC.Core

Return value

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

controller.disableNfc9+

disableNfc(): void

Disables NFC.

Required permissions: ohos.permission.MANAGE_SECURE_SETTINGS

System capability: SystemCapability.Communication.NFC.Core

Error codes

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

IDError Message
3100101NFC state is abnormal in service.

controller.isNfcOpen

isNfcOpen(): boolean

Checks whether NFC is open.

System capability: SystemCapability.Communication.NFC.Core

Return value

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

controller.getNfcState

getNfcState(): NfcState

Obtains the NFC state.

System capability: SystemCapability.Communication.NFC.Core

Return value

TypeDescription
NfcStateNFC state obtained. For details, see NfcState.

controller.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

Parameters

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

controller.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

Parameters

NameTypeMandatoryDescription
typestringYesEvent type to unsubscribe from. The value is nfcStateChange.
callbackCallback<NfcState>NoCallback 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.

Example

import controller from '@ohos.nfc.controller';

// Register a callback to receive the NFC state change notification.
controller.on("nfcStateChange", (nfcState : number)=> {
  console.log("controller on callback nfcState: " + nfcState);
});

// Open NFC. The ohos.permission.MANAGE_SECURE_SETTINGS permission is required.
if (!controller.isNfcOpen()) {
  let ret = controller.openNfc();
  console.log("controller openNfc ret: " + ret);
}

// Use 'enableNfc' to enable NFC from API version 9.
try {
  controller.enableNfc();
  console.log("controller enableNfc success");
} catch (busiError) {
  console.log("controller enableNfc busiError: " + busiError);
}

// Close NFC. The ohos.permission.MANAGE_SECURE_SETTINGS permission is required.
if (controller.isNfcOpen()) {
  let ret = controller.closeNfc();
  console.log("controller closeNfc ret: " + ret);
}

// Use 'disableNfc' to disable NFC from API version 9.
try {
  controller.disableNfc();
  console.log("controller disableNfc success");
} catch (busiError) {
  console.log("controller disableNfc busiError: " + busiError);
}

// Unregister the callback.
controller.off("nfcStateChange");

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

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