harmony 鸿蒙@ohos.connectedTag (有源标签)

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

@ohos.connectedTag (有源标签)

本模块提供有源标签的使用,包括初始化有源标签芯片、读取有源标签内容、写入内容到有源标签等。

说明:

本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

import connectedTag from '@ohos.connectedTag';

connectedTag.init

init(): boolean

初始化有源标签芯片。

需要权限:ohos.permission.NFC_TAG

系统能力:SystemCapability.Communication.ConnectedTag

返回值:

类型 说明
boolean true:初始化成功, false:初始化失败。

connectedTag.initialize9+

initialize(): void

初始化有源标签芯片。

需要权限: ohos.permission.NFC_TAG

系统能力: SystemCapability.Communication.ConnectedTag

错误码: 以下错误码的详细介绍请参见NFC错误码

错误码ID 错误信息
3200101 Connected NFC tag running state is abnormal in service.

connectedTag.uninit

uninit(): boolean

卸载有源标签芯片资源。

需要权限:ohos.permission.NFC_TAG

系统能力:SystemCapability.Communication.ConnectedTag

返回值:

类型 说明
boolean true:卸载操作成功, false:卸载操作失败。

connectedTag.uninitialize9+

uninitialize(): void

卸载有源标签芯片资源。

需要权限: ohos.permission.NFC_TAG

系统能力: SystemCapability.Communication.ConnectedTag

错误码: 以下错误码的详细介绍请参见NFC错误码

错误码ID 错误信息
3200101 Connected NFC tag running state is abnormal in service.

connectedTag.readNdefTag

readNdefTag(): Promise<string>

读取有源标签内容,使用promise方式作为异步方法。

需要权限:ohos.permission.NFC_TAG

系统能力:SystemCapability.Communication.ConnectedTag

返回值:

类型 说明
Promise<string> 返回读取有源标签内容。

示例:

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[]>

读取有源标签内容,使用promise方式作为异步方法。

需要权限: ohos.permission.NFC_TAG

系统能力: SystemCapability.Communication.ConnectedTag

返回值:

类型 说明
Promise<number[]> 返回读取有源标签内容。

错误码: 以下错误码的详细介绍请参见NFC错误码

错误码ID 错误信息
3200101 Connected NFC tag running state is abnormal in service.

示例:

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

读取有源标签内容,使用AsyncCallback方式作为异步方法。

需要权限:ohos.permission.NFC_TAG

系统能力:SystemCapability.Communication.ConnectedTag

参数:

参数名 类型 必填 说明
callback AsyncCallback<string> 读取有源标签内容回调函数。

示例:

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

读取有源标签内容,使用AsyncCallback方式作为异步方法。

需要权限: ohos.permission.NFC_TAG

系统能力: SystemCapability.Communication.ConnectedTag

参数:

参数名 类型 必填 说明
callback AsyncCallback<number[]> 读取有源标签内容回调函数。

错误码: 以下错误码的详细介绍请参见NFC错误码

错误码ID 错误信息
3200101 Connected NFC tag running state is abnormal in service.

示例:

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>

写入内容到有源标签,使用promise方式作为异步方法。

需要权限:ohos.permission.NFC_TAG

系统能力:SystemCapability.Communication.ConnectedTag

参数:

参数名 类型 必填 说明
data string 有源标签内容, 长度最大是1024个字节。

返回值:

类型 说明
Promise<void> 无返回值。

示例:

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>

写入内容到有源标签,使用promise方式作为异步方法。

需要权限: ohos.permission.NFC_TAG

系统能力: SystemCapability.Communication.ConnectedTag

参数:

参数名 类型 必填 说明
data number[] 有源标签内容, 由十六进制数字组成,范围从0x00至0xFF。

返回值:

类型 说明
Promise<void> 无返回值。

错误码: 以下错误码的详细介绍请参见NFC错误码

错误码ID 错误信息
3200101 Connected NFC tag running state is abnormal in service.

示例:

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

写入内容到有源标签,使用AsyncCallback方式作为异步方法。

需要权限:ohos.permission.NFC_TAG

系统能力:SystemCapability.Communication.ConnectedTag

参数:

参数名 类型 必填 说明
data string 有源标签内容, 长度最大是1024个字节。
callback AsyncCallback<void> 读取有源标签内容回调函数。

示例:

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

写入内容到有源标签,使用AsyncCallback方式作为异步方法。

需要权限: ohos.permission.NFC_TAG

系统能力: SystemCapability.Communication.ConnectedTag

参数:

参数名 类型 必填 说明
data number[] 有源标签内容, 由十六进制数字组成,范围从0x00至0xFF。
callback AsyncCallback<void> 读取有源标签内容回调函数。

错误码: 以下错误码的详细介绍请参见NFC错误码

错误码ID 错误信息
3200101 Connected NFC tag running state is abnormal in service.

示例:

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

注册NFC场强状态事件。

需要权限:ohos.permission.NFC_TAG

系统能力:SystemCapability.Communication.ConnectedTag

参数:

参数名 类型 必填 说明
type string 固定填”notify”字符串
callback Callback<number> 状态改变回调函数,返回值参见NfcRfType

connectedTag.off(‘notify’)

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

取消NFC场强状态事件的注册。

需要权限:ohos.permission.NFC_TAG

系统能力:SystemCapability.Communication.ConnectedTag

参数:

参数名 类型 必填 说明
type string 固定填”notify”字符串
callback Callback<number> 状态改变回调函数。如果callback不填,将“去注册”该事件关联的所有回调函数。

示例:

import connectedTag from '@ohos.connectedTag';

// Register 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 connecected tag business oprations here...
// connectedTag.writeNdefTag(rawData)
// connectedTag.readNdefTag()

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

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

NfcRfType

表示NFC场强状态的枚举。

系统能力:SystemCapability.Communication.ConnectedTag

名称 说明
NFC_RF_LEAVE 0 NFC离场事件
NFC_RF_ENTER 1 NFC进场事件

你可能感兴趣的鸿蒙文章

harmony 鸿蒙接口

harmony 鸿蒙系统公共事件定义(待停用)

harmony 鸿蒙系统公共事件定义

harmony 鸿蒙开发说明

harmony 鸿蒙企业设备管理概述(仅对系统应用开放)

harmony 鸿蒙BundleStatusCallback

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

harmony 鸿蒙@ohos.distributedBundle (分布式包管理)

harmony 鸿蒙@ohos.bundle (Bundle模块)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (企业设备管理扩展能力)

0  赞