openharmony 鸿蒙 js-apis-connectedTag

2025-06-12 浏览 (1)

@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 '@kit.ConnectivityKit';

connectedTag.init

init(): boolean

Initializes the active tag chip.

NOTE

This API is supported since API version 8 and deprecated since API version 9. Use initialize instead.

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.ConnectedTag

Return value

TypeDescription
booleantrue: The initialization is successful. 
false: The initialization fails.

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 Universal Error Codes and NFC Error Codes.

IDError Message
201Permission denied.
801Capability not supported.
3200101Connected 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

TypeDescription
booleantrue: The uninstallation is successful. 
false: The uninstallation fails.

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 Universal Error Codes and NFC Error Codes.

IDError Message
201Permission denied.
801Capability not supported.
3200101Connected 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

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

Example

import { connectedTag } from '@kit.ConnectivityKit';
import { BusinessError } from '@kit.BasicServicesKit';

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

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

Error codes

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

IDError Message
201Permission denied.
801Capability not supported.
3200101Connected NFC tag running state is abnormal in service.

Example

import { connectedTag } from '@kit.ConnectivityKit';
import { BusinessError } from '@kit.BasicServicesKit';

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

NameTypeMandatoryDescription
callbackAsyncCallback<string>YesCallback used to return the active tag content obtained.

Example

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

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

NameTypeMandatoryDescription
callbackAsyncCallback<number[]>YesCallback used to return the active tag content obtained.

Error codes

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

IDError Message
201Permission denied.
801Capability not supported.
3200101Connected NFC tag running state is abnormal in service.

Example

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

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

NameTypeMandatoryDescription
datastringYesData to be written to the active tag. The maximum length is 1024 bytes.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import { connectedTag } from '@kit.ConnectivityKit';
import { BusinessError } from '@kit.BasicServicesKit';

let rawData = "010203"; // change it to be 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

NameTypeMandatoryDescription
datanumber[]YesData to be written to the active tag. The value is a hexadecimal number ranging from 0x00 to 0xFF.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

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

IDError Message
201Permission denied.
401The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed.
801Capability not supported.
3200101Connected NFC tag running state is abnormal in service.

Example

import { connectedTag } from '@kit.ConnectivityKit';
import { BusinessError } from '@kit.BasicServicesKit';

let rawData = [0x01, 0x02, 0x03]; // change it to be 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

NameTypeMandatoryDescription
datastringYesContent of the active tag. The maximum length is 1024 bytes.
callbackAsyncCallback<void>YesCallback used to return the active tag content obtained.

Example

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

let rawData = "010203"; // change it to be 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

NameTypeMandatoryDescription
datanumber[]YesData to be written to the active tag. The value is a hexadecimal number ranging from 0x00 to 0xFF.
callbackAsyncCallback<void>YesCallback used to return the active tag content obtained.

Error codes

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

IDError Message
201Permission denied.
401The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed.
801Capability not supported.
3200101Connected NFC tag running state is abnormal in service.

Example

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

let rawData = [0x01, 0x02, 0x03]; // change it to be 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

NameTypeMandatoryDescription
typestringYesEvent type. This parameter has a fixed value of notify.
callbackCallback<number>YesCallback 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

NameTypeMandatoryDescription
typestringYesEvent type. This parameter has a fixed value of notify.
callbackCallback<number>NoCallback 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 '@kit.ConnectivityKit';

// 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

NameValueDescription
NFC_RF_LEAVE0NFC exit.
NFC_RF_ENTER1NFC entry.

你可能感兴趣的鸿蒙文章

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/UegUfa