harmony 鸿蒙@ohos.connectedTag (Active Tags)

  • 2022-08-09
  • 浏览 (683)

@ohos.connectedTag (Active Tags)

The connectedTag module provides APIs for using active tags. You can use the APIs to initialize the active tag chip and read and write active tags.

NOTE

The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import connectedTag from '@ohos.connectedTag';

connectedTag.init

init(): boolean

Initializes the active tag chip.

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.ConnectedTag

Return value

Type Description
boolean Returns true if the operation is successful; returns false otherwise.

connectedTag.initialize9+

initialize(): void

Initializes the active tag chip.

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.ConnectedTag

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

ID Error Message
3200101 Connected NFC tag running state is abnormal in service.

connectedTag.uninit

uninit(): boolean

Uninitializes the active tag resources.

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.ConnectedTag

Return value

Type Description
boolean Returns true if the operation is successful; returns false otherwise.

connectedTag.uninitialize9+

uninitialize(): void

Uninitializes the active tag resources.

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.ConnectedTag

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

ID Error Message
3200101 Connected NFC tag running state is abnormal in service.

connectedTag.readNdefTag

readNdefTag(): Promise<string>

Reads the content of this active tag. This API uses a promise to return the result.

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.ConnectedTag

Return value

Type Description
Promise<string> Promise used to return the content of the active tag.

Example

import connectedTag from '@ohos.connectedTag';
import { BusinessError } from '@ohos.base';

connectedTag.readNdefTag().then((data) => {
    console.log("connectedTag readNdefTag Promise data = " + data);
}).catch((err: BusinessError)=> {
    console.log("connectedTag readNdefTag Promise err: " + err);
});

connectedTag.read9+

read(): Promise<number[]>

Reads the content of this active tag. This API uses a promise to return the result.

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.ConnectedTag

Return value

Type Description
Promise<number[]> Promise used to return the content of the active tag.

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

ID Error Message
3200101 Connected NFC tag running state is abnormal in service.

Example

import connectedTag from '@ohos.connectedTag';
import { BusinessError } from '@ohos.base';

connectedTag.read().then((data) => {
    console.log("connectedTag read Promise data = " + data);
}).catch((err: BusinessError)=> {
    console.log("connectedTag read Promise err: " + err);
});

connectedTag.readNdefTag

readNdefTag(callback: AsyncCallback<string>): void

Reads the content of this active tag. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.ConnectedTag

Parameters

Name Type Mandatory Description
callback AsyncCallback<string> Yes Callback invoked to return the active tag content obtained.

Example

import connectedTag from '@ohos.connectedTag';

connectedTag.readNdefTag((err, data)=> {
    if (err) {
        console.log("connectedTag readNdefTag AsyncCallback err: " + err);
    } else {
        console.log("connectedTag readNdefTag AsyncCallback data: " + data);
    }
});

connectedTag.read9+

read(callback: AsyncCallback<number[]>): void

Reads the content of this active tag. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.ConnectedTag

Parameters

Name Type Mandatory Description
callback AsyncCallback<number[]> Yes Callback invoked to return the active tag content obtained.

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

ID Error Message
3200101 Connected NFC tag running state is abnormal in service.

Example

import connectedTag from '@ohos.connectedTag';

connectedTag.read((err, data)=> {
    if (err) {
        console.log("connectedTag read AsyncCallback err: " + err);
    } else {
        console.log("connectedTag read AsyncCallback data: " + data);
    }
});

connectedTag.writeNdefTag

writeNdefTag(data: string): Promise<void>

Writes data to this active tag. This API uses a promise to return the result.

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.ConnectedTag

Parameters

Name Type Mandatory Description
data string Yes Data to write. The maximum length is 1024 bytes.

Return value

Type Description
Promise<void> Promise that returns no value.

Example

import connectedTag from '@ohos.connectedTag';
import { BusinessError } from '@ohos.base';

let rawData = "010203"; // change it tobe correct.
connectedTag.writeNdefTag(rawData).then(() => {
    console.log("connectedTag writeNdefTag Promise success.");
}).catch((err: BusinessError)=> {
    console.log("connectedTag writeNdefTag Promise err: " + err);
});

connectedTag.write9+

write(data: number[]): Promise<void>

Writes data to this active tag. This API uses a promise to return the result.

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.ConnectedTag

Parameters

Name Type Mandatory Description
data number[] Yes Data to be written to the active tag. The value is a hexadecimal number ranging from 0x00 to 0xFF.

Return value

Type Description
Promise<void> Promise that returns no value.

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

ID Error Message
3200101 Connected NFC tag running state is abnormal in service.

Example

import connectedTag from '@ohos.connectedTag';
import { BusinessError } from '@ohos.base';

let rawData = [0x01, 0x02, 0x03]; // change it tobe correct.
connectedTag.write(rawData).then(() => {
    console.log("connectedTag write NdefTag Promise success.");
}).catch((err: BusinessError)=> {
    console.log("connectedTag write NdefTag Promise err: " + err);
});

connectedTag.writeNdefTag

writeNdefTag(data: string, callback: AsyncCallback<void>): void

Writes data to this active tag. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.ConnectedTag

Parameters

Name Type Mandatory Description
data string Yes Data to write. The maximum length is 1024 bytes.
callback AsyncCallback<void> Yes Callback invoked to return the active tag content obtained.

Example

import connectedTag from '@ohos.connectedTag';

let rawData = "010203"; // change it tobe correct.
connectedTag.writeNdefTag(rawData, (err)=> {
    if (err) {
        console.log("connectedTag writeNdefTag AsyncCallback err: " + err);
    } else {
        console.log("connectedTag writeNdefTag AsyncCallback success.");
    }
});

connectedTag.write9+

write(data: number[], callback: AsyncCallback<void>): void

Writes data to this active tag. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.ConnectedTag

Parameters

Name Type Mandatory Description
data number[] Yes Data to be written to the active tag. The value is a hexadecimal number ranging from 0x00 to 0xFF.
callback AsyncCallback<void> Yes Callback invoked to return the active tag content obtained.

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

ID Error Message
3200101 Connected NFC tag running state is abnormal in service.

Example

import connectedTag from '@ohos.connectedTag';

let rawData = [0x01, 0x02, 0x03]; // change it tobe correct.
connectedTag.write(rawData, (err)=> {
    if (err) {
        console.log("connectedTag write NdefTag AsyncCallback err: " + err);
    } else {
        console.log("connectedTag write NdefTag AsyncCallback success.");
    }
});

connectedTag.on(‘notify’)

on(type: “notify”, callback: Callback<number>): void

Registers the NFC field strength state events.

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.ConnectedTag

Parameters

Name Type Mandatory Description
type string Yes Event type. The value is notify.
callback Callback<number> Yes Callback used to return the NfcRfType.

connectedTag.off(‘notify’)

off(type: “notify”, callback?: Callback<number>): void

Unregisters the NFC field strength state events.

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.ConnectedTag

Parameters

Name Type Mandatory Description
type string Yes Event type. The value is notify.
callback Callback<number> No Callback used to return the field strength state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.

Example

import connectedTag from '@ohos.connectedTag';

// Register the event.
connectedTag.on("notify", (rfState : number)=> {
  console.log("connectedTag on Callback rfState: " + rfState);
});

let initStatus = connectedTag.init();
console.log("connectedTag init status: " + initStatus);

// Add NFC connected tag business operations here.
// connectedTag.writeNdefTag(rawData)
// connectedTag.readNdefTag()

let uninitStatus = connectedTag.uninit();
console.log("connectedTag uninit status: " + uninitStatus);

// Unregister the event.
connectedTag.off("notify", (rfState : number)=> {
  console.log("connectedTag off Callback rfState: " + rfState);
});

NfcRfType

Enumerates the NFC field strength states.

System capability: SystemCapability.Communication.ConnectedTag

Name Value Description
NFC_RF_LEAVE 0 Field off.
NFC_RF_ENTER 1 Field on.

你可能感兴趣的鸿蒙文章

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)

0  赞