harmony 鸿蒙tagSession (Standard NFC Tag Session)

  • 2025-06-12
  • 浏览 (4)

tagSession (Standard NFC Tag Session)

The tagSession module provides common APIs for establishing connections and transferring data.

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.

NOTE

If an error is reported while importing the tag module editor, the capabilities of a specific device model may exceed the capability set defined for the default device. To use these capabilities, configure a custom SysCap by following instructions in SystemCapability Development.

Modules to Import

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

TagSession

Provides common APIs for establishing connections and transferring data. TagSession is the base class of all NFC tag technologies.

A child class instance is required to access the following interfaces. You can use get API to obtain a child class instance.

The specific API varies with the NFC tag technology in use. For details, see NFC Tags.

getTagInfo(deprecated)

getTagInfo(): tag.TagInfo

Obtains the tagInfo object provided by the NFC service when the tag is dispatched.

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

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.NFC.Tag

Return value

Type Description
TagInfo Taginfo object obtained.

Example

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

// tagInfo is the object provided by the NFC service when allocating a tag. For details, see tag.TagInfo in @ohos.nfc.tag. 
// getter API, which can be getIsoDep, getNdef, getMifareClassic, and so on.

let tagInfo : TagInfo = tag.getIsoDep(tagInfo).getTagInfo();
console.info("tag tagInfo: " + tagInfo);

connectTag(deprecated)

connectTag(): boolean;

Connects to this tag. Call this API to set up a connection before reading data from or writing data to a tag.

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

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.NFC.Tag

Return value

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

Example

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

// tagInfo is the object provided by the NFC service when allocating a tag. For details, see tag.TagInfo in @ohos.nfc.tag. 
// getter API, which can be getIsoDep, getNdef, getMifareClassic, and so on.

let connectStatus : boolean = tag.getIsoDep(tagInfo).connectTag();
console.info("connectStatus: " + connectStatus);

connect9+

connect(): void;

Connects to this tag. Call this API to set up a connection before reading data from or writing data to a tag.

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.NFC.Tag

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

Error codes

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

ID Error Message
201 Permission denied.
801 Capability not supported.
3100201 The tag running state is abnormal in the service.

Example

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

// tagInfo is the object provided by the NFC service when allocating a tag. For details, see tag.TagInfo in @ohos.nfc.tag. 
// getter API, which can be getIsoDep, getNdef, getMifareClassic, and so on.

try {
    tag.getIsoDep(tagInfo).connect(); 
    console.info("tag connect success");
} catch (businessError) {
    console.error("tag connect businessError: " + businessError);
}

reset()(deprecated)

reset(): void

Resets the connection to this tag.

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

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.NFC.Tag

Example

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

// tagInfo is the object provided by the NFC service when allocating a tag. For details, see tag.TagInfo in @ohos.nfc.tag. 
// getter API, which can be getIsoDep, getNdef, getMifareClassic, and so on.

tag.getIsoDep(tagInfo).reset(); 

resetConnection()9+

resetConnection(): void

Resets the connection to this tag.

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.NFC.Tag

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

Error codes

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

ID Error Message
201 Permission denied.
801 Capability not supported.
3100201 The tag running state is abnormal in the service.

Example

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

// tagInfo is the object provided by the NFC service when allocating a tag. For details, see tag.TagInfo in @ohos.nfc.tag. 
// getter API, which can be getIsoDep, getNdef, getMifareClassic, and so on.

try {
    tag.getIsoDep(tagInfo).resetConnection(); 
    console.info("tag resetConnection success");
} catch (businessError) {
    console.error("tag resetConnection businessError: " + businessError);
}

isTagConnected(deprecated)

isTagConnected(): boolean

Checks whether the tag is connected.

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

System capability: SystemCapability.Communication.NFC.Tag

Return value

Type Description
boolean Returns true if the tag is connected; returns false otherwise.

Example

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

// tagInfo is the object provided by the NFC service when allocating a tag. For details, see tag.TagInfo in @ohos.nfc.tag. 
// getter API, which can be getIsoDep, getNdef, getMifareClassic, and so on.

let isTagConnected = tag.getIsoDep(tagInfo).isTagConnected(); 
console.info("isTagConnected: " + isTagConnected);

isConnected9+

isConnected(): boolean

Checks whether the tag is connected. If you receive a message indicating that the tag has not been connected, call tagSession.connect to connect the tag.

System capability: SystemCapability.Communication.NFC.Tag

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

Return value

Type Description
boolean Returns true if the tag is connected; returns false otherwise.

Error codes

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

ID Error Message
801 Capability not supported.

Example

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

// tagInfo is the object provided by the NFC service when allocating a tag. For details, see tag.TagInfo in @ohos.nfc.tag. 
// getter API, which can be getIsoDep, getNdef, getMifareClassic, and so on.

try {
    let isConnected = tag.getIsoDep(tagInfo).isConnected(); 
    console.info("tag isConnected = " + isConnected);
} catch (businessError) {
    console.error("tag isConnected businessError: " + businessError);
}

getMaxSendLength(deprecated)

getMaxSendLength(): number

Obtains the maximum length of the data that can be sent to this tag.

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

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.NFC.Tag

Return value

Type Description
number Maximum data length obtained. The value cannot be a negative number.

Example

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

// tagInfo is the object provided by the NFC service when allocating a tag. For details, see tag.TagInfo in @ohos.nfc.tag. 
// getter API, which can be getIsoDep, getNdef, getMifareClassic, and so on.

let maxSendLen = tag.getIsoDep(tagInfo).getMaxSendLength(); 
console.info("tag maxSendLen: " + maxSendLen);

getMaxTransmitSize9+

getMaxTransmitSize(): number

Obtains the maximum length of the data that can be sent to this tag.

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.NFC.Tag

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

Return value

Type Description
number Maximum data length obtained. The value cannot be a negative number.

Error codes

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

ID Error Message
201 Permission denied.
801 Capability not supported.
3100201 The tag running state is abnormal in the service.

Example

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

// tagInfo is the object provided by the NFC service when allocating a tag. For details, see tag.TagInfo in @ohos.nfc.tag. 
// getter API, which can be getIsoDep, getNdef, getMifareClassic, and so on.

try {
    let maxTransmitSize = tag.getIsoDep(tagInfo).getMaxTransmitSize(); 
    console.info("tag maxTransmitSize = " + maxTransmitSize);
} catch (businessError) {
    console.error("tag getMaxTransmitSize businessError: " + businessError);
}

getSendDataTimeout(deprecated)

getSendDataTimeout(): number

Obtains the timeout period for sending data to this tag, in milliseconds.

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

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.NFC.Tag

Return value

Type Description
number Timeout period obtained, in milliseconds. The value cannot be a negative number.

Example

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

// tagInfo is the object provided by the NFC service when allocating a tag. For details, see tag.TagInfo in @ohos.nfc.tag. 
// getter API, which can be getIsoDep, getNdef, getMifareClassic, and so on.

let sendDataTimeout = tag.getIsoDep(tagInfo).getSendDataTimeout(); 
console.info("tag sendDataTimeout: " + sendDataTimeout);

getTimeout9+

getTimeout(): number

Obtains the timeout period for sending data to this tag, in milliseconds.

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.NFC.Tag

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

Return value

Type Description
number Timeout period obtained, in milliseconds. The value cannot be a negative number.

Error codes

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

ID Error Message
201 Permission denied.
801 Capability not supported.
3100201 The tag running state is abnormal in the service.

Example

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

// tagInfo is the object provided by the NFC service when allocating a tag. For details, see tag.TagInfo in @ohos.nfc.tag. 
// getter API, which can be getIsoDep, getNdef, getMifareClassic, and so on.

try {
    let timeout = tag.getIsoDep(tagInfo).getTimeout(); 
    console.info("tag timeout = " + timeout);
} catch (businessError) {
    console.error("tag getTimeout businessError: " + businessError);
}

setSendDataTimeout(deprecated)

setSendDataTimeout(timeout: number): boolean

Sets the maximum time allowed for sending data to this tag, in ms.

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

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.NFC.Tag

Parameters

Name Type Mandatory Description
timeout number Yes Timeout period to set, in milliseconds. The value cannot be a negative number.

Return value

Type Description
boolean Returns true if the timeout period is set successfully; returns false otherwise.

Example

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

// tagInfo is the object provided by the NFC service when allocating a tag. For details, see tag.TagInfo in @ohos.nfc.tag. 
// getter API, which can be getIsoDep, getNdef, getMifareClassic, and so on.

let timeoutMs = 700; // Set the expected timeout interval.
let setStatus = tag.getIsoDep(tagInfo).setSendDataTimeout(timeoutMs); 
console.info("tag setSendDataTimeout setStatus: " + setStatus);

setTimeout9+

setTimeout(timeout: number): void

Sets the maximum time allowed for sending data to this tag, in ms.

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.NFC.Tag

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

Parameters

Name Type Mandatory Description
timeout number Yes Timeout period to set, in milliseconds. The value cannot be a negative number.

Error codes

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

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

Example

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

// tagInfo is the object provided by the NFC service when allocating a tag. For details, see tag.TagInfo in @ohos.nfc.tag. 
// getter API, which can be getIsoDep, getNdef, getMifareClassic, and so on.

let timeoutMs = 700; // Set the expected timeout interval.
try {
    tag.getIsoDep(tagInfo).setTimeout(timeoutMs); 
    console.info("tag setTimeout success");
} catch (businessError) {
    console.error("tag setTimeout businessError: " + businessError);
}

sendData(deprecated)

sendData(data: number[]): Promise

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

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

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.NFC.Tag

Parameters

Name Type Mandatory Description
data number[] Yes Data to send. The data consists of hexadecimal numbers ranging from 0x00 to 0xFF.

Return value

Type Description
Promise Promise used to return the response from the tag. The response consists of hexadecimal numbers ranging from 0x00 to 0xFF.

Example

import tag from '@kit.ConnectivityKit';
import { BusinessError } from '@ohos.base';

// tagInfo is the object provided by the NFC service when allocating a tag. For details, see tag.TagInfo in @ohos.nfc.tag. 
// getter API, which can be getIsoDep, getNdef, getMifareClassic, and so on.

function tagSessionDemo() {
    // Connect the tag if it has not been connected.
    if (!tag.getIsoDep(tagInfo).isTagConnected()) {
        if (!tag.getIsoDep(tagInfo).connectTag()) {
            console.error("tagSession connectTag failed.");
            return;
        }
    }  

    let cmdData = [0x01, 0x02, 0x03, 0x04]; // Set command data correctly.
    tag.getIsoDep(tagInfo).sendData(cmdData).then((response) => {
    console.info("tagSession sendData Promise response: " + response);
    }).catch((err : BusinessError)=> {
    console.error("tagSession sendData Promise err: " + err);
    });
}

sendData(deprecated)

sendData(data: number[], callback: AsyncCallback): void

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

NOTE This parameter is supported since API version 7 and deprecated since API version 9. Use tagSession.transmit instead.

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.NFC.Tag

Parameters

Name Type Mandatory Description
data number[] Yes Data to send. The data consists of hexadecimal numbers ranging from 0x00 to 0xFF.
callback AsyncCallback Yes Callback used to return the response from the tag. The response consists of hexadecimal numbers ranging from 0x00 to 0xFF.

Example

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

// tagInfo is the object provided by the NFC service when allocating a tag. For details, see tag.TagInfo in @ohos.nfc.tag. 
// getter API, which can be getIsoDep, getNdef, getMifareClassic, and so on.

function tagSessionDemo() {
    // Connect the tag if it has not been connected.
    if (!tag.getIsoDep(tagInfo).isTagConnected()) {
        if (!tag.getIsoDep(tagInfo).connectTag()) {
            console.error("tagSession connectTag failed.");
            return;
        }
    }

    let cmdData = [0x01, 0x02, 0x03, 0x04]; // Set command data correctly.
    tag.getIsoDep(tagInfo).sendData(cmdData, (err, response)=> {
        if (err) {
            console.error("tagSession sendData AsyncCallback err: " + err);
        } else {
            console.info("tagSession sendData AsyncCallback response: " + response);
        }
    });
}

transmit9+

transmit(data: number[]): Promise

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

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.NFC.Tag

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

Parameters

Name Type Mandatory Description
data number[] Yes Data to send. The data consists of hexadecimal numbers ranging from 0x00 to 0xFF.

Return value

Type Description
Promise Promise used to return the response from the tag. The response consists of hexadecimal numbers ranging from 0x00 to 0xFF.

Error codes

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

ID Error Message
201 Permission denied.
401 The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed.
801 Capability not supported.
3100201 The tag running state is abnormal in the service.
3100204 The tag I/O operation failed.

Example

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

// tagInfo is the object provided by the NFC service when allocating a tag. For details, see tag.TagInfo in @ohos.nfc.tag. 
// getter API, which can be getIsoDep, getNdef, getMifareClassic, and so on.

function tagSessionDemo() {
// Connect the tag if it has not been connected.
    try {
        if (!tag.getIsoDep(tagInfo).isConnected()) {
            tag.getIsoDep(tagInfo).connect();
        }
    } catch (businessError) {
        console.error("tag connect businessError: " + businessError);
        return;
    }

    let cmdData = [0x01, 0x02, 0x03, 0x04]; // Set command data correctly.
    try {
    tag.getIsoDep(tagInfo).transmit(cmdData).then((response) => {
        console.info("tagSession transmit Promise response: " + response);
    }).catch((err : BusinessError)=> {
        console.error("tagSession transmit Promise err: " + err);
    });
    } catch (businessError) {
        console.error("tag transmit businessError: " + businessError);
        return;
    }
}

transmit9+

transmit(data: number[], callback: AsyncCallback): void

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

Required permissions: ohos.permission.NFC_TAG

System capability: SystemCapability.Communication.NFC.Tag

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

Parameters

Name Type Mandatory Description
data number[] Yes Data to send. The data consists of hexadecimal numbers ranging from 0x00 to 0xFF.
callback AsyncCallback Yes Callback used to return the response from the tag. The response consists of hexadecimal numbers ranging from 0x00 to 0xFF.

Error codes

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

ID Error Message
201 Permission denied.
401 The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed.
801 Capability not supported.
3100201 The tag running state is abnormal in the service.
3100204 The tag I/O operation failed..

Example

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

// tagInfo is the object provided by the NFC service when allocating a tag. For details, see tag.TagInfo in @ohos.nfc.tag. 
// getter API, which can be getIsoDep, getNdef, getMifareClassic, and so on.

function tagSessionDemo() {
    // Connect the tag if it has not been connected.
    try {
        if (!tag.getIsoDep(tagInfo).isConnected()) {
            tag.getIsoDep(tagInfo).connect();
        }
    } catch (businessError) {
        console.error("tag connect businessError: " + businessError);
        return;
    }

    let cmdData = [0x01, 0x02, 0x03, 0x04]; // Set command data correctly.
    try {
        tag.getIsoDep(tagInfo).transmit(cmdData, (err, response)=> {
            if (err) {
                console.error("tagSession transmit AsyncCallback err: " + err);
            } else {
                console.info("tagSession transmit AsyncCallback response: " + response);
            }
        });
    } catch (businessError) {
        console.error("tag transmit businessError: " + businessError);
        return;
    }
}

你可能感兴趣的鸿蒙文章

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)

0  赞