From cb8ab12184a63e3a91f937b304ed206a1d8e16ec Mon Sep 17 00:00:00 2001 From: zhangxiuping Date: Mon, 26 Sep 2022 14:56:40 +0800 Subject: [PATCH] Update aps description for nfc tag operations. Signed-off-by: zhangxiuping --- .../reference/apis/js-apis-nfcTag.md | 234 +++++- .../reference/apis/js-apis-nfctech.md | 780 ++++++++++-------- 2 files changed, 672 insertions(+), 342 deletions(-) mode change 100755 => 100644 zh-cn/application-dev/reference/apis/js-apis-nfctech.md diff --git a/zh-cn/application-dev/reference/apis/js-apis-nfcTag.md b/zh-cn/application-dev/reference/apis/js-apis-nfcTag.md index 9e073acac9..0d2e8531dc 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-nfcTag.md +++ b/zh-cn/application-dev/reference/apis/js-apis-nfcTag.md @@ -5,15 +5,94 @@ > ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:** > 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 +## **声明技术** + +开发Tag读写相关应用时,需要在应用的属性配置文件中,声明与NFC相关的属性值,比如,在module.json5文件中,声明下面属性值: +```js +{ + "module": { + // other declared attributes. + + "abilities": [ + { + "skills": [ + { + "actions": [ + // other declared actions, + + // add the nfc tag action + "ohos.nfc.tag.action.TAG_FOUND" + ] + } + ], + "metadata": [ + { + "name": "tag-tech", + "value": "NfcA" + }, + { + "name": "tag-tech", + "value": "IsoDep" + }, + // add other technology if neccessary, + // such as: NfcB/NfcF/NfcV/Ndef/MifareClassic/MifareUL/NdefFormatable + ] + } + ], + "requestPermissions": [ + "name": "ohos.permission.NFC_TAG", + "reason": "tag", + ] + } +} +``` +> **注意:** +1. 声明"actions"字段的内容填写,必须是"ohos.nfc.tag.action.TAG_FOUND",不能更改。 +2. 声明技术时"metadata"中的"name"字段的内容填写,必须是"tag-tech",不能更改。 +3. 声明技术时"metadata"中的"value"字段的内容填写,必须是"NfcA/NfcB/NfcF/NfcV/IsoDep/Ndef/MifareClassic/MifareUL/NdefFormatable"中的一个或多个。填写错误会造成解析失败。 +4. 声明权限时"requestPermissions"中的"name"字段的内容填写,必须是"ohos.permission.NFC_TAG",不能更改。 + ## **导入模块** ```js import tag from '@ohos.nfc.tag'; ``` +## **tag.TagInfo** +在对相关Tag类型卡片进行读写之前,必须先获取TagInfo相关属性值,以确认设备读取到的Tag卡片支持哪些技术类型。这样Tag应用程序才能调用正确的接口和所读取到的Tag卡片进行通信。 +```js +import tag from '@ohos.nfc.tag'; + +onCreate(want, launchParam) { + // add other code here + + // want is initialized by nfc service, contains tag info for this found tag + var tagInfo = tag.getTagInfo(want); + if (tagInfo == undefined) { + console.log("no TagInfo to be created, ignore it."); + return; + } + var isNfcATag = false; + for (var i = 0; i < tagInfo.technology.length; i++) { + if (tagInfo.technology[i] == tag.NFC_A) { + isNfcATag = true; + break; + } + // also check for technology: tag.NFC_B/NFC_F/NFC_V/ISO_DEP/NDEF/MIFARE_CLASSIC/MIFARE_ULTRALIGHT/NDEF_FORMATABLE + } + if (isNfcATag) { + var nfcA = tag.getNfcATag(taginfo); + // other code to read or write this found tag. + } + + // use the same code to handle for "NfcA/NfcB/NfcF/NfcV/IsoDep/Ndef/MifareClassic/MifareUL/NdefFormatable", such as: + // var isoDep = tag.getIsoDepTag(taginfo); +} +``` + ## tag.getNfcATag -getNfcATag(tagInfo: [TagInfo](#taginfo7)): [NfcATag](js-apis-nfctech.md#nfcatag) +getNfcATag(tagInfo: [TagInfo](#taginfo)): [NfcATag](js-apis-nfctech.md#nfcatag) 获取NFC A类型Tag对象,通过该对象可访问NfcA技术类型的Tag。 @@ -29,7 +108,7 @@ getNfcATag(tagInfo: [TagInfo](#taginfo7)): [NfcATag](js-apis-nfctech.md#nfcatag) ## tag.getNfcBTag -getNfcBTag(tagInfo: [TagInfo](#taginfo7)): [NfcBTag](js-apis-nfctech.md#nfcbtag) +getNfcBTag(tagInfo: [TagInfo](#taginfo)): [NfcBTag](js-apis-nfctech.md#nfcbtag) 获取NFC B类型Tag对象,通过该对象可访问NfcB技术类型的Tag。 @@ -45,7 +124,7 @@ getNfcBTag(tagInfo: [TagInfo](#taginfo7)): [NfcBTag](js-apis-nfctech.md#nfcbtag) ## tag.getNfcFTag -getNfcFTag(tagInfo: [TagInfo](#taginfo7)): [NfcFTag](js-apis-nfctech.md#nfcftag) +getNfcFTag(tagInfo: [TagInfo](#taginfo)): [NfcFTag](js-apis-nfctech.md#nfcftag) 获取NFC F类型Tag对象,通过该对象可访问NfcF技术类型的Tag。 @@ -61,7 +140,7 @@ getNfcFTag(tagInfo: [TagInfo](#taginfo7)): [NfcFTag](js-apis-nfctech.md#nfcftag) ## tag.getNfcVTag -getNfcVTag(tagInfo: [TagInfo](#taginfo7)): [NfcVTag](js-apis-nfctech.md#nfcvtag) +getNfcVTag(tagInfo: [TagInfo](#taginfo)): [NfcVTag](js-apis-nfctech.md#nfcvtag) 获取NFC V类型Tag对象,通过该对象可访问NfcV技术类型的Tag。 @@ -77,11 +156,10 @@ getNfcVTag(tagInfo: [TagInfo](#taginfo7)): [NfcVTag](js-apis-nfctech.md#nfcvtag) ## tag.getIsoDepTag9+ -getIsoDepTag(tagInfo: [TagInfo](#taginfo7)): [IsoDepTag](js-apis-nfctech.md#isoDepTag9 ) +getIsoDepTag(tagInfo: [TagInfo](#taginfo)): [IsoDepTag](js-apis-nfctech.md#isoDepTag9 ) 获取IsoDep类型Tag对象,通过该对象可访问Iso Dep技术类型的Tag。 - **需要权限**:ohos.permission.NFC_TAG **系统能力**:SystemCapability.Communication.NFC.Core @@ -94,7 +172,7 @@ getIsoDepTag(tagInfo: [TagInfo](#taginfo7)): [IsoDepTag](js-apis-nfctech.md#isoD ## tag.getNdefTag9+ -getNdefTag(tagInfo: [TagInfo](#taginfo7)): [NdefTag](js-apis-nfctech.md#ndeftag9) +getNdefTag(tagInfo: [TagInfo](#taginfo)): [NdefTag](js-apis-nfctech.md#ndeftag9) 获取Ndef类型Tag对象,通过该对象可访问Ndef技术类型的Tag。 @@ -111,7 +189,7 @@ getNdefTag(tagInfo: [TagInfo](#taginfo7)): [NdefTag](js-apis-nfctech.md#ndeftag9 ## tag.getMifareClassicTag9+ -getMifareClassicTag(tagInfo: [TagInfo](#taginfo7)): [MifareClassicTag](js-apis-nfctech.md#mifareclassictag-9) +getMifareClassicTag(tagInfo: [TagInfo](#taginfo)): [MifareClassicTag](js-apis-nfctech.md#mifareclassictag-9) 获取Mifare Classic类型Tag对象,通过该对象访问Mifare Classic技术类型的Tag。 @@ -127,7 +205,7 @@ getMifareClassicTag(tagInfo: [TagInfo](#taginfo7)): [MifareClassicTag](js-apis-n ## tag.getMifareUltralightTag9+ -getMifareUltralightTag(tagInfo: [TagInfo](#taginfo7)): [MifareUltralightTag](js-apis-nfctech.md#mifareultralighttag9) +getMifareUltralightTag(tagInfo: [TagInfo](#taginfo)): [MifareUltralightTag](js-apis-nfctech.md#mifareultralighttag9) 获取Mifare Ultralight类型Tag对象,通过该对象可访问Mifare Ultralight技术类型的Tag。 @@ -143,7 +221,7 @@ getMifareUltralightTag(tagInfo: [TagInfo](#taginfo7)): [MifareUltralightTag](js- ## tag.getNdefFormatableTag9+ -getNdefFormatableTag(tagInfo: [TagInfo](#taginfo7)): [NdefFormatableTag](js-apis-nfctech.md#ndefformatabletag9) +getNdefFormatableTag(tagInfo: [TagInfo](#taginfo)): [NdefFormatableTag](js-apis-nfctech.md#ndefformatabletag9) 获取Ndef Formatable类型Tag对象,通过该对象可访问Ndef Formatable技术类型的Tag。 @@ -157,9 +235,25 @@ getNdefFormatableTag(tagInfo: [TagInfo](#taginfo7)): [NdefFormatableTag](js-apis | ------------------ | --------------------------| | [NdefFormatableTag](js-apis-nfctech.md#ndefformatabletag) | Ndef Formatable类型Tag对象。 | -## TagInfo7+ +## tag.getTagInfo9+ + +getTagInfo(want: Want): [TagInfo](#taginfo) -nfc服务在调度标签时给出的对象。 +从Want中获取TagInfo,Want是被NFC服务初始化,包含了TagInfo所需的属性值。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC.Core + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| [TagInfo](#taginfo) | TagInfo对象,用于获取不同技术类型的Tag对象。 | + +## TagInfo + +NFC服务在读取到标签时给出的对象,通过改对象属性,应用知道该标签支持哪些技术类型,并使用匹配的技术类型来调用相关接口。 **需要权限**:ohos.permission.NFC_TAG @@ -167,7 +261,117 @@ nfc服务在调度标签时给出的对象。 | **参数名** | **类型** | **说明** | | -------- | -------- | -------- | -| uid9+ | string | 标签的uid。 | -| technology9+ | number[] | 支持的技术类型。 | -| supportedProfiles7+ | number[] | 支持的技术类型。 | +| uid9+ | number[] | 标签的uid,每个number值是十六进制表示,范围是0x00~0xFF。 | +| technology9+ | number[] | 支持的技术类型,每个number值表示所支持技术类型的常量值。 | +| supportedProfiles | number[] | 支持的技术类型,从API9开始不支持,使用technology替代。 | + +## NdefRecord9+ +NDEF标签Record属性的定义,参考NDEF标签技术规范《NFCForum-TS-NDEF_1.0》的定义细节。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC.Core +| **参数名** | **类型** | **说明** | +| -------- | -------- | -------- | +| tnf | number | NDEF Record的TNF(Type Name Field)。 | +| rtdType| number[] | NDEF Record的RTD(Record Type Definition)类型值,每个number十六进制表示,范围是0x00~0xFF。 | +| id | number[] | NDEF Record的ID,每个number十六进制表示,范围是0x00~0xFF。| +| payload | number[] | NDEF Record的PAYLOAD,每个number十六进制表示,范围是0x00~0xFF。 | + +## 技术类型定义 +NFC Tag有多种不同的技术类型,定义常量描述不同的技术类型。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC.Core +| **参数名** | **常量值** | **说明** | +| -------- | -------- | -------- | +| NFC_A | 1 | NFC-A(ISO 14443-3A)技术。| +| NFC_B | 2 | NFC-A(ISO 14443-3B)技术。| +| ISO_DEP | 3 | ISO-DEP(ISO 14443-4)技术。| +| NFC_F | 4 | NFC-F(JIS 6319-4)技术。| +| NFC_V | 5 | NFC-V(ISO 15693)技术。| +| NDEF | 6 | NDEF技术。| +| MIFARE_CLASSIC | 8 | Mifare Classic技术。| +| MIFARE_ULTRALIGHT | 9 | Mifare Utralight技术。| +| NDEF_FORMATABLE9+ | 10 | 可以格式化的NDEF技术。| + +## TnfType9+ +NDEF Record的TNF(Type Name Field)类型值,参考NDEF标签技术规范《NFCForum-TS-NDEF_1.0》的定义细节。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC.Core +| **参数名** | **常量值** | **说明** | +| -------- | -------- | -------- | +| TNF_EMPTY | 0x0 | Empty。| +| TNF_WELL_KNOWN | 0x01 | NFC Forum well-known type [NFC RTD]。| +| TNF_MEDIA | 0x02 | Media-type as defined in RFC 2046 [RFC 2046]。| +| TNF_ABSOLUTE_URI | 0x03 | Absolute URI as defined in RFC 3986 [RFC 3986]。| +| TNF_EXT_APP | 0x04 | NFC Forum external type [NFC RTD]。| +| TNF_UNKNOWN | 0x05 | Unknown。| +| TNF_UNCHANGED | 0x06 | Unchanged (see section 2.3.3)。| + +## RtdType9+ +NDEF Record的RTD(Record Type Definition)类型值,参考NDEF标签技术规范《NFCForum-TS-NDEF_1.0》的定义细节。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC.Core +| **参数名** | **常量值** | **说明** | +| -------- | -------- | -------- | +| RTD_TEXT | 常量 'T' | 文本类型的NDEF Record。| +| RTD_URI | 常量 'U' | URI类型的NDEF Record。| + +## NfcForumType9+ +NFC Forum标准里面Tag类型的定义。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC.Core +| **参数名** | **常量值** | **说明** | +| -------- | -------- | -------- | +| NFC_FORUM_TYPE_1 | 1 | NFC论坛类型1。 | +| NFC_FORUM_TYPE_2 | 2 | NFC论坛类型2。 | +| NFC_FORUM_TYPE_3 | 3 | NFC论坛类型3。 | +| NFC_FORUM_TYPE_4 | 4 | NFC论坛类型4。 | +| MIFARE_CLASSIC | 101 | Mifare Classic类型。 | + +## MifareClassicType9+ +MifareClassic标签类型的定义。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC.Core +| **参数名** | **常量值** | **说明** | +| -------- | -------- | -------- | +| TYPE_UNKNOWN | -1 | 未知Mifare类型。 | +| TYPE_CLASSIC | 0 | Mifare Classic类型。| +| TYPE_PLUS | 1 | Mifare Plus类型。| +| TYPE_PRO | 2 | Mifare Pro类型。 | + +## MifareClassicSize9+ +MifareClassic标签存储大小的定义。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC.Core +| **参数名** | **常量值** | **说明** | +| -------- | -------- | -------- | +| MC_SIZE_MINI | 320 | 每个标签5个扇区,每个扇区4个块。 | +| MC_SIZE_1K | 1024 | 每个标签16个扇区,每个扇区4个块。| +| MC_SIZE_2K | 2048 | 每个标签32个扇区,每个扇区4个块。 | +| MC_SIZE_4K | 4096 | 每个标签40个扇区,每个扇区4个块。| + +### MifareUltralightType9+ +MifareUltralight标签类型的定义。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC.Core +| **参数名** | **常量值** | **说明** | +| -------- | -------- | -------- | +| TYPE_UNKOWN | -1 | 未知的 Mifare 类型。 | +| TYPE_ULTRALIGHT | 1 | Mifare Ultralight类型。| +| TYPE_ULTRALIGHT_C | 2 | Mifare UltralightC 类型。 | \ No newline at end of file diff --git a/zh-cn/application-dev/reference/apis/js-apis-nfctech.md b/zh-cn/application-dev/reference/apis/js-apis-nfctech.md old mode 100755 new mode 100644 index b606e4f15c..3f1870ceb8 --- a/zh-cn/application-dev/reference/apis/js-apis-nfctech.md +++ b/zh-cn/application-dev/reference/apis/js-apis-nfctech.md @@ -33,16 +33,17 @@ getSak(): number | **类型** | **说明** | | ------------------ | --------------------------| -| number | NfcA 标签的SAK值。 | +| number | NfcA 标签的SAK值,十六进制表示,范围是0x00~0xFF。 | **示例:** ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -let sak = tag.getNfcATag(taginfo).getSak(); -console.log("sak:" +sak); +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'nfcA' correctly. + +let sak = nfcA.getSak(); +console.log("nfcA sak: " + sak); ``` ### NfcATag.getAtqa @@ -59,16 +60,16 @@ getAtqa(): number[] | **类型** | **说明** | | ------------------ | --------------------------| -| number[] | NfcA 标签的Atqa值。 | +| number[] | NfcA 标签的Atqa值,每个number十六进制表示,范围是0x00~0xFF。 | **示例:** ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -let atqa = tag.getNfcATag(taginfo).getAtqa(); -console.log("atqa:" +atqa); +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'nfcA' correctly. +let atqa = nfcA.getAtqa(); +console.log("nfcA atqa: " + atqa); ``` ## NfcBTag @@ -93,16 +94,16 @@ getRespAppData(): number[] | **类型** | **说明** | | ------------------ | --------------------------| -| number[] | NfcB 标签的应用程序数据。 | +| number[] | NfcB 标签的应用程序数据,每个number十六进制表示,范围是0x00~0xFF。 | **示例:** ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -let appData = tag.getNfcBTag(taginfo).getRespAppData(); -console.log("appData:" +appData); +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'nfcB' correctly. +let respAppData = nfcB.getRespAppData(); +console.log("nfcB respAppData: " + respAppData); ``` ### NfcBTag.getRespProtocol @@ -119,16 +120,16 @@ getRespProtocol(): number[] | **类型** | **说明** | | ------------------ | --------------------------| -| number[] | NfcB 标签的协议信息。| +| number[] | NfcB 标签的协议信息,每个number十六进制表示,范围是0x00~0xFF。| **示例:** ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -let protocol = tag.getNfcBTag(taginfo).getRespProtocol(); -console.log("appData:" +protocol); +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'nfcB' correctly. +let respProtocol = nfcB.getRespProtocol(); +console.log("nfcB respProtocol: " + respProtocol); ``` ## NfcFTag @@ -153,16 +154,16 @@ getSystemCode(): number[] | **类型** | **说明** | | ------------------ | --------------------------| -| number[] | NfcF 标签的系统代码。| +| number[] | NfcF 标签的系统代码,每个number十六进制表示,范围是0x00~0xFF。| **示例:** ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -let systemCode = tag.getNfcFTag(taginfo).getSystemCode(); -console.log("systemCode:" +systemCode); +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'nfcF' correctly. +let systemCode = nfcF.getSystemCode(); +console.log("nfcF systemCode: " + systemCode); ``` ### NfcFTag.getPmm @@ -179,16 +180,16 @@ getPmm(): number[] | **类型** | **说明** | | ------------------ | --------------------------| -| number[] | NfcF 标签的PMm信息。| +| number[] | NfcF 标签的PMm信息,每个number十六进制表示,范围是0x00~0xFF。| **示例:** ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -let pmm = tag.getNfcFTag(taginfo).getPmm(); -console.log("pmm:" +pmm); +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'nfcF' correctly. +let pmm = nfcF.getPmm(); +console.log("nfcF pmm: " + pmm); ``` ## NfcVTag @@ -213,16 +214,16 @@ getResponseFlags(): number | **类型** | **说明** | | ------------------ | --------------------------| -| number | NfcV 标签的响应标志。| +| number | NfcV 标签的响应标志,十六进制表示,范围是0x00~0xFF。| **示例:** ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -let flags = tag.getNfcVTag(taginfo).getResponseFlags(); -console.log("flags:" +flags); +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'nfcV' correctly. +let responseFlags = nfcV.getResponseFlags(); +console.log("nfcV responseFlags: " + responseFlags); ``` ### NfcvTag.getDsfId @@ -239,16 +240,16 @@ getDsfId(): number | **类型** | **说明** | | ------------------ | --------------------------| -| number | NfcV 标签的数据存储格式标识符。| +| number | NfcV 标签的数据存储格式标识符,十六进制表示,范围是0x00~0xFF。| **示例:** ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -let dsfId = tag.getNfcVTag(taginfo).getDsfId(); -console.log("dsfId:" +dsfId); +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'nfcV' correctly. +let dsfId = nfcV.getDsfId(); +console.log("nfcV dsfId: " + dsfId); ``` ## IsoDepTag9+ @@ -261,7 +262,7 @@ TagSession是所有Nfc tag 技术类型的基类, 提供建立连接和发送 ### IsoDepTag.getHistoricalBytes9+ -getHistoricalBytes(): string +getHistoricalBytes(): number[] 获取标签的历史字节。 @@ -273,21 +274,21 @@ getHistoricalBytes(): string | **类型** | **说明** | | ------------------ | --------------------------| -| string | IsoDepTag 标签的历史字节。| +| number[] | IsoDepTag 标签的历史字节,每个number十六进制表示,范围是0x00~0xFF。| **示例:** ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -let historicalBytes = tag.getIsoDepTag(taginfo).getHistoricalBytes(); -console.log("historicalBytes:" +historicalBytes); +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'isoDep' correctly. +let historicalBytes = isoDep.getHistoricalBytes(); +console.log("isoDep historicalBytes: " + historicalBytes); ``` ### IsoDepTag.getHiLayerResponse9+ -getHiLayerResponse(): string +getHiLayerResponse(): number[] 获取标签的HiLayer响应字节。 @@ -299,23 +300,23 @@ getHiLayerResponse(): string | **类型** | **说明** | | ------------------ | --------------------------| -| string | IsoDepTag 标签的HiLayer响应字节。| +| number[] | IsoDepTag 标签的HiLayer响应字节,每个number十六进制表示,范围是0x00~0xFF。| **示例:** ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -let hiLayerResponse = tag.getIsoDepTag(taginfo).getHiLayerResponse(); -console.log("hiLayerResponse:" +hiLayerResponse); +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'isoDep' correctly. +let hiLayerResponse = isoDep.getHiLayerResponse(); +console.log("isoDep hiLayerResponse: " + hiLayerResponse); ``` ### IsoDepTag.isExtendedApduSupported9+ isExtendedApduSupported(): Promise<boolean> -检查是否支持外部apdu长度,使用promise方式作为异步方法。 +检查是否支持扩展的APDU,使用promise方式作为异步方法。 **需要权限**:ohos.permission.NFC_TAG @@ -332,17 +333,20 @@ isExtendedApduSupported(): Promise<boolean> ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.getIsoDepTag(taginfo).isExtendedApduSupported().then(function (has) { - console.log('has: ' + has) -}) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'isoDep' correctly. +isoDep.isExtendedApduSupported() + .then((data) => { + console.log("isoDep isExtendedApduSupported data: " + data); + }).catch((err)=> { + console.log("isoDep isExtendedApduSupported err: " + err); + }); ``` ### IsoDepTag.isExtendedApduSupported9+ isExtendedApduSupported(callback: AsyncCallback\): void -检查是否支持外部apdu长度,使用callback方式作为异步方法。 +检查是否支持扩展的APDU,使用callback方式作为异步方法。 **需要权限**:ohos.permission.NFC_TAG @@ -357,24 +361,27 @@ isExtendedApduSupported(callback: AsyncCallback\): void ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.getIsoDepTag(taginfo).isExtendedApduSupported(function (error, has) { - console.log(JSON.stringify(error)) - console.log('has: ' + has) -}) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'isoDep' correctly. +isoDep.isExtendedApduSupported((err, data)=> { + if (err) { + console.log("isoDep isExtendedApduSupported err: " + err); + } else { + console.log("isoDep isExtendedApduSupported data: " + data); + } +}); ``` ## NdefTag9+ 提供对已格式化为NDEF的NFC标签的数据和操作的访问,继承自TagSession。 -TagSession是所有Nfc tag 技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)。 +TagSession是所有NFC Tag技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)。 以下是NdefTag的独有接口。 ### NdefTag.createNdefMessage9+ -createNdefMessage(data: string): [NdefMessage](#ndefmessage9) +createNdefMessage(data: number[]): [NdefMessage](#ndefmessage9) 使用原始字节创建ndef消息。 @@ -386,28 +393,30 @@ createNdefMessage(data: string): [NdefMessage](#ndefmessage9) | **参数名** | **类型** | **必填** | **说明** | | -------- | -------- | -------- | -------- | -| data | string | 是 | 字符串类型的原始字节 | +| data | number[] | 是 | 原始字节,每个number十六进制表示,范围是0x00~0xFF | **返回值:** | **类型** | **说明** | | ------------------ | --------------------------| -| [NdefMessage](#ndefmessage9) | Ndef消息 | +| [NdefMessage](#ndefmessage9) | NDEF标签的Message,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 | **示例:** ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -let NdefMessage = tag.NdefTag(taginfo).createNdefMessage(data); +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndef' correctly. +let rawData = [0x00, 0xa4, 0x04, ......]; // change the raw data bytes tobe correct. +let ndefMessage = ndef.createNdefMessage(rawData); +console.log("ndef ndefMessage: " + ndefMessage); ``` ## NdefMessage9+ ### NdefMessage.getNdefRecords9+ -getNdefRecords(): [NdefRecord](#ndefrecord9)[ ] +getNdefRecords(): [NdefRecord](js-apis-nfcTag.md#ndefrecord9)[ ] 获取ndef消息的所有记录。 @@ -419,38 +428,23 @@ getNdefRecords(): [NdefRecord](#ndefrecord9)[ ] | **类型** | **说明** | | ------------------ | --------------------------| -| [NdefRecord](#ndefrecord9)[ ] | Ndef消息所包含的所有记录。 | +| [NdefRecord](js-apis-nfcTag.md#ndefrecord9)[ ] | NDEF标签的Record列表,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 | **示例:** ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -let NdefRecord = tag.NdefTag(taginfo).getNdefRecords(); +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndef' correctly. +let ndefRecords = ndef.getNdefRecords(); +console.log("ndef ndefRecords number: " + ndefRecords.length); ``` -## NdefRecord9+ - -| **参数名** | **类型** | **说明** | -| -------- | -------- | -------- | -| tnf | number | 标签的uid。 | -| [rtdType](#rtdtype9) | string | 支持的技术类型。 | -| id | string | 标签的额外信息。 | -| payload | string | 标签的RF discovery id。 | - -## RtdType9+ - -| **参数名** | **类型** | **说明** | -| -------- | -------- | -------- | -| RTD_TEXT | 'T' | 记录描述文本信息。| -| RTD_URI | 'U' | 存储网络地址,邮件或者电话号码。| - ### NdefTag.createNdefMessage9+ createNdefMessage(ndefRecords: NdefRecord[]): [NdefMessage](#ndefmessage9) -使用记录列表创建ndef消息。 +使用记录列表创建NDEF消息。 **需要权限**:ohos.permission.NFC_TAG @@ -459,21 +453,32 @@ createNdefMessage(ndefRecords: NdefRecord[]): [NdefMessage](#ndefmessage9) **参数:** | **参数名** | **类型** | **必填** | **说明** | | -------- | -------- | -------- | -------- | -| ndefRecords | [NdefRecord](#ndefrecord9)[] | 是 | NdefRecord记录列表。 | +| ndefRecords | [NdefRecord](js-apis-nfcTag.md#ndefrecord9)[] | 是 | NDEF标签的Record列表,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 | **返回值:** | **类型** | **说明** | | ------------------ | --------------------------| -| [NdefMessage](#ndefmessage9) | Ndef消息。| +| [NdefMessage](#ndefmessage9) | NDEF标签的Message,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。| **示例:** ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -let NdefMessage = tag.NdefTag(taginfo).createNdefMessage(ndefRecords); +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndef' correctly. +let ndefRecords = [ + // record format: tnf, rtdType, id, payload + // 1st record: + {tnf: 0x01, rtdType: [0x54], id: [0x01, 0x02, ...], payload: [0x00, 0xa4, 0x04, ...]}, + + // 2nd record: + {tnf: 0x02, rtdType: [0x55], id: [0x03, 0x04, ...], payload: [0x00, 0xa4, 0x04, ...]}, + + // other record if has one ... +]; +let ndefMessage = ndef.createNdefMessage(ndefRecords); +console.log("ndef ndefMessage: " + ndefMessage); ``` ### NdefTag.getNdefTagType9+ @@ -490,22 +495,23 @@ getNdefTagType(): NfcForumType | **类型** | **说明** | | ------------------ | --------------------------| -| [NfcForumType](#nfcforumtype9) | Ndef标签类型。| +| [NfcForumType](js-apis-nfcTag.md#nfcforumtype9) | NDEF标签类型,包括NFC FORUM TYPE 1/2/3/4等。| **示例:** ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -let NfcForumType = tag.NdefTag(taginfo).getNdefTagType(); +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndef' correctly. +let ndefTagType = ndef.getNdefTagType(); +console.log("ndef ndefTagType: " + ndefTagType); ``` ### NdefTag.getNdefMessage9+ getNdefMessage(): NdefMessage -获取发现标签时,从ndef标签读取的ndef消息。 +获取发现NDEF标签时,从标签读取的Message。 **需要权限**:ohos.permission.NFC_TAG @@ -515,22 +521,23 @@ getNdefMessage(): NdefMessage | **类型** | **说明** | | ------------------ | --------------------------| -| [NdefMessage](#ndefmessage9) | Ndef消息。| +| [NdefMessage](#ndefmessage9) | NDEF标签的Message,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。| **示例:** ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -let NdefMessage = tag.NdefTag(taginfo).getNdefMessage(); +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndef' correctly. +let ndefMessage = ndef.getNdefMessage(); +console.log("ndef ndefMessage: " + ndefMessage); ``` ### NdefTag.isNdefWritable9+ isNdefWritable(): Promise<boolean> -检查ndef标签是否可写,使用promise方式作为异步方法。 +检查NDEF标签是否可写,使用promise方式作为异步方法。 **需要权限**:ohos.permission.NFC_TAG @@ -547,10 +554,13 @@ isNdefWritable(): Promise<boolean> ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.NdefTag(taginfo).isNdefWritable().then(function (has) { - console.log(JSON.stringify(has)) -}) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndef' correctly. +ndef.isNdefWritable() + .then((data) => { + console.log("ndef isNdefWritable data: " + data); + }).catch((err)=> { + console.log("ndef isNdefWritable err: " + err); + }); ``` ### NdefTag.isNdefWritable9+ @@ -567,18 +577,21 @@ isNdefWritable(callback: AsyncCallback<boolean>): void; | 参数名 | 类型 | 必填 | 说明 | | -------- | ----------------------- | ---- | -------------------------------------- | -| callback | AsyncCallback\ | 是 | 回调函数,ndef标签可写,返回true。 | +| callback | AsyncCallback\ | 是 | 回调函数,NDEF标签可写,返回true。 | **示例:** ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.NdefTag(taginfo).isNdefWritable(function (error, has) { - console.log(JSON.stringify(error)) - console.log('has: ' + has) -}) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndef' correctly. +ndef.isNdefWritable((err, data)=> { + if (err) { + console.log("ndef isNdefWritable err: " + err); + } else { + console.log("ndef isNdefWritable data: " + data); + } +}); ``` ### NdefTag.readNdef9+ @@ -595,17 +608,20 @@ readNdef(): Promise\ | **类型** | **说明** | | ------------------ | --------------------------| -| Promise\<[NdefMessage](#ndefmessage9)> | 以Promise形式返回从标签中读取到的NdefMessage信息。| +| Promise\<[NdefMessage](#ndefmessage9)> | 以Promise形式返回从NDEF标签中读取到的Message信息。| **示例:** ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.NdefTag(taginfo).readNdef().then(function (ndefMessage) { - console.log(JSON.stringify(ndefMessage)) -}) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndef' correctly. +ndef.readNdef() + .then((data) => { + console.log("ndef readNdef data: " + data); + }).catch((err)=> { + console.log("ndef readNdef err: " + err); + }); ``` ### NdefTag.readNdef9+ @@ -629,11 +645,14 @@ readNdef(callback: AsyncCallback\<[NdefMessage](#ndefmessage9)>): void ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.NdefTag(taginfo).readNdef(function (error, ndefMessage) { - console.log(JSON.stringify(error)) - console.log('ndefMessage: ' + ndefMessage) -}) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndef' correctly. +ndef.readNdef((err, data)=> { + if (err) { + console.log("ndef readNdef err: " + err); + } else { + console.log("ndef readNdef data: " + data); + } +}); ``` ### NdefTag.writeNdef9+ @@ -663,10 +682,15 @@ writeNdef(msg: NdefMessage): Promise\; ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -NdefTag.writeNdef(msg).then(function (netHandle) { - console.log(JSON.stringify(netHandle)) -}) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndef' correctly. +let ndefMessage = ndef.createNdefMessage([0x01, 0x02, ...]); // change the raw data to be correct. + +ndef.writeNdef(ndefMessage) + .then((data) => { + console.log("ndef writeNdef data: " + data); + }).catch((err)=> { + console.log("ndef writeNdef err: " + err); + }); ``` ### NdefTag.writeNdef9+ @@ -691,11 +715,15 @@ writeNdef(msg: NdefMessage, callback: AsyncCallback\): void ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.NdefTag(taginfo).write(msg, function (error, has) { - console.log(JSON.stringify(error)) - console.log('has: ' + has) -}) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndef' correctly. +let ndefMessage = ndef.createNdefMessage([0x01, 0x02, ...]); // change the raw data to be correct. +ndef.writeNdef(ndefMessage, (err, data)=> { + if (err) { + console.log("ndef writeNdef err: " + err); + } else { + console.log("ndef writeNdef data: " + data); + } +}); ``` ### NdefTag.canSetReadOnly9+ @@ -712,17 +740,20 @@ canSetReadOnly(): Promise\ | **类型** | **说明** | | ------------------ | --------------------------| -| Promise<boolean> | true: 标签可设置为只读, false: 标签不可设置为只读。 | +| Promise<boolean> | true: NDEF标签可设置为只读, false: NDEF标签不可设置为只读。 | **示例:** ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.NdefTag(taginfo).canSetReadOnly().then(function (has) { - console.log(JSON.stringify(has)) -}) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndef' correctly. +ndef.canSetReadOnly() + .then((data) => { + console.log("ndef canSetReadOnly data: " + data); + }).catch((err)=> { + console.log("ndef canSetReadOnly err: " + err); + }); ``` ### NdefTag.canSetReadOnly9+ @@ -739,18 +770,21 @@ canSetReadOnly(callback: AsyncCallback<boolean>): void; | 参数名 | 类型 | 必填 | 说明 | | -------- | ----------------------- | ---- | -------------------------------------- | -| callback | AsyncCallback\ | 是 | 回调函数,ndef标签可设置为只读,返回true。 | +| callback | AsyncCallback\ | 是 | 回调函数,NDEF标签可设置为只读,返回true。 | **示例:** ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.NdefTag(taginfo).canSetReadOnly(function (error, has) { - console.log(JSON.stringify(error)) - console.log('has: ' + has) -}) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndef' correctly. +ndef.canSetReadOnly((err, data)=> { + if (err) { + console.log("ndef canSetReadOnly err: " + err); + } else { + console.log("ndef canSetReadOnly data: " + data); + } +}); ``` ### NdefTag.setReadOnly9+ @@ -774,10 +808,13 @@ setReadOnly(): Promise\ ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.NdefTag(taginfo).setReadOnly().then(function (errcode) { - console.log(JSON.stringify(errcode)) -}) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndef' correctly. +ndef.setReadOnly() + .then((data) => { + console.log("ndef setReadOnly data: " + data); + }).catch((err)=> { + console.log("ndef setReadOnly err: " + err); + }); ``` ### NdefTag.setReadOnly9+ @@ -801,16 +838,19 @@ setReadOnly(callback: AsyncCallback): void ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.NdefTag(taginfo).setReadOnly(function (error, errcode) { - console.log(JSON.stringify(error)) - console.log('has: ' + errcode) -}) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndef' correctly. +ndef.setReadOnly((err, data)=> { + if (err) { + console.log("ndef setReadOnly err: " + err); + } else { + console.log("ndef setReadOnly data: " + data); + } +}); ``` ### NdefTag.getNdefTagTypeString9+ -getNdefTagTypeString(type: [NfcForumType](#nfcforumtype9)): string +getNdefTagTypeString(type: [NfcForumType](js-apis-nfcTag.md#nfcforumtype9)): string 将Nfc论坛类型转换为Nfc论坛中定义的字节数组。 @@ -822,36 +862,25 @@ getNdefTagTypeString(type: [NfcForumType](#nfcforumtype9)): string | 参数名 | 类型 | 必填 | 说明 | | -------- | ----------------------- | ---- | -------------------------------------- | -| type | [NfcForumType](#nfcforumtype9) | 是 | NfcForum论坛类型。 | +| type | [NfcForumType](js-apis-nfcTag.md#nfcforumtype9) | 是 | NDEF标签类型,包括NFC FORUM TYPE 1/2/3/4等。 | **返回值:** | **类型** | **说明** | | ------------------ | --------------------------| -| string | Nfc论坛类型字节数组。| +| string | NFC论坛类型的字符串描述。| **示例:** ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -let ndefTypeString= tag.NdefTag(taginfo).getNdefTagTypeString(type); +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndef' correctly. +let ndefTypeString = ndef.getNdefTagTypeString(tag.NFC_FORUM_TYPE_1); +console.log("ndef ndefTypeString: " + ndefTypeString); ``` -## NfcForumType9+ - -**系统能力**:SystemCapability.Communication.NFC.Core - -| **参数名** | **类型** | **说明** | -| -------- | -------- | -------- | -| NFC_FORUM_TYPE_1 | 1 | NFC论坛类型1。 | -| NFC_FORUM_TYPE_2 | 2 | NFC论坛类型2。 | -| NFC_FORUM_TYPE_3 | 3 | NFC论坛类型3。 | -| NFC_FORUM_TYPE_4 | 4 | NFC论坛类型4。 | -| MIFARE_CLASSIC | 101 | Mifare Classic类型。 | - -## MifareClassicTag 9+ +## MifareClassicTag9+ MifareClassicTag提供对MIFARE经典属性和I/O操作的访问,继承自TagSession。 @@ -888,10 +917,15 @@ authenticateSector(sectorIndex: number, key: number[], isKeyA: boolean): Promise ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.MifareClassicTag(taginfo).authenticateSector(sectorIndex, key).then(function (isKeyA) { - console.log(JSON.stringify(isKeyA)) - }) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareClassic' correctly. +let sectorIndex = 1; // change it to be correct index. +let key = [0x04, 0x05, ....]; // change it to be correct key. +mifareClassic.authenticateSector(sectorIndex, key, true); + .then((data) => { + console.log("mifareClassic authenticateSector data: " + data); + }).catch((err)=> { + console.log("mifareClassic authenticateSector err: " + err); + }); ``` ### MifareClassicTag.authenticateSector9+ @@ -918,16 +952,21 @@ authenticateSector(sectorIndex: number, key: number[], isKeyA: boolean, callback ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.MifareClassicTag(taginfo).authenticateSector(sectorIndex, key, function (error, has) { - console.log(JSON.stringify(error)) - console.log('has: ' + has) -}) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareClassic' correctly. +let sectorIndex = 1; // change it to be correct index. +let key = [0x04, 0x05, ....]; // change it to be correct key. +mifareClassic.authenticateSector(sectorIndex, key, true, (err, data)=> { + if (err) { + console.log("mifareClassic authenticateSector err: " + err); + } else { + console.log("mifareClassic authenticateSector data: " + data); + } +}); ``` ### MifareClassicTag.readSingleBlock9+ -readSingleBlock(blockIndex: number): Promise\ +readSingleBlock(blockIndex: number): Promise\ 读取标签中一个块存储的内容,一个块大小为16字节。使用promise方式作为异步方法。 @@ -945,23 +984,27 @@ readSingleBlock(blockIndex: number): Promise\ | **类型** | **说明** | | ------------------ | --------------------------| -| Promise\ | 读取的块数据。 | +| Promise\ | 读取的块数据。 | **示例:** ```js import tag from '@ohos.nfc.tag'; -let data = "xxx"; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.MifareClassicTag(taginfo).readSingleBlock(blockIndex).then(function (data){ - console.log('data: ' + data) - }) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareClassic' correctly. +let blockIndex = 1; // change it to be correct index. +mifareClassic.readSingleBlock(blockIndex, (err, data)=> { + if (err) { + console.log("mifareClassic readSingleBlock err: " + err); + } else { + console.log("mifareClassic readSingleBlock data: " + data); + } +}); ``` ### MifareClassicTag.readSingleBlock9+ -readSingleBlock(blockIndex: number, callback: AsyncCallback\): void +readSingleBlock(blockIndex: number, callback: AsyncCallback\): void 读取标签中一个块存储的内容,一个块大小为16字节。使用callback方式作为异步方法。 @@ -974,24 +1017,27 @@ readSingleBlock(blockIndex: number, callback: AsyncCallback\): void | 参数名 | 类型 | 必填 | 说明 | | -------- | ----------------------- | ---- | -------------------------------------- | | blockIndex | number | 是 | 要读取的块索引 | -| callback | AsyncCallback\ | 是 | 回调函数。 | +| callback | AsyncCallback\ | 是 | 回调函数。 | **示例:** ```js import tag from '@ohos.nfc.tag'; -let data = "xxx"; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.MifareClassicTag(taginfo).readSingleBlock(blockIndex, function (error, data) { - console.log(JSON.stringify(error)) - console.log('data: ' + data) -}) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareClassic' correctly. +let blockIndex = 1; // change it to be correct index. +mifareClassic.readSingleBlock(blockIndex, (err, data)=> { + if (err) { + console.log("mifareClassic readSingleBlock err: " + err); + } else { + console.log("mifareClassic readSingleBlock data: " + data); + } +}); ``` ### MifareClassicTag.writeSingleBlock9+ -writeSingleBlock(blockIndex: number, data: string): Promise\ +writeSingleBlock(blockIndex: number, data: number[]): Promise\ 向标签中一个块存储写入内容,一个块大小为16字节。使用promise方式作为异步方法。 @@ -1004,7 +1050,7 @@ writeSingleBlock(blockIndex: number, data: string): Promise\ | 参数名 | 类型 | 必填 | 说明 | | -------- | ----------------------- | ---- | -------------------------------------- | | blockIndex | number | 是 | 要写入的块索引。 | -| data | string | 是 | 要写入的数据。 | +| data | number[] | 是 | 要写入的数据。 | **返回值:** @@ -1017,16 +1063,21 @@ writeSingleBlock(blockIndex: number, data: string): Promise\ ```js import tag from '@ohos.nfc.tag'; -let data = "xxx"; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.MifareClassicTag(taginfo).writeSingleBlock(blockIndex, data).then(function (errcode){ - console.log(JSON.stringify(errcode)) - }) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareClassic' correctly. +let blockIndex = 1; // change it to be correct index. +let rawData = [0x0a, 0x14, ...]; // change it to be correct data. +mifareClassic.writeSingleBlock(blockIndex, rawData, (err, data)=> { + if (err) { + console.log("mifareClassic writeSingleBlock err: " + err); + } else { + console.log("mifareClassic writeSingleBlock data: " + data); + } +}); ``` ### MifareClassicTag.writeSingleBlock9+ -writeSingleBlock(blockIndex: number, data: string, callback: AsyncCallback\): void +writeSingleBlock(blockIndex: number, data: number[], callback: AsyncCallback\): void 向标签中一个块存储写入内容,一个块大小为16字节。使用callback方式作为异步方法。 @@ -1039,7 +1090,7 @@ writeSingleBlock(blockIndex: number, data: string, callback: AsyncCallback\ | 是 | 回调函数。 | **示例:** @@ -1047,12 +1098,16 @@ writeSingleBlock(blockIndex: number, data: string, callback: AsyncCallback\ { + if (err) { + console.log("mifareClassic writeSingleBlock err: " + err); + } else { + console.log("mifareClassic writeSingleBlock data: " + data); + } +}); ``` ### MifareClassicTag.incrementBlock9+ @@ -1083,10 +1138,16 @@ incrementBlock(blockIndex: number, value: number): Promise\ ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.MifareClassicTag(taginfo).incrementBlock(blockIndex, value).then(function (errcode){ - console.log(JSON.stringify(errcode)) - }) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareClassic' correctly. +let blockIndex = 1; // change it to be correct index. +let value = 0x20; // change it to be correct data. +mifareClassic.incrementBlock(blockIndex, value, (err, data)=> { + if (err) { + console.log("mifareClassic incrementBlock err: " + err); + } else { + console.log("mifareClassic incrementBlock data: " + data); + } +}); ``` ### MifareClassicTag.incrementBlock9+ @@ -1112,11 +1173,16 @@ incrementBlock(blockIndex: number, value: number, callback: AsyncCallback\ { + if (err) { + console.log("mifareClassic incrementBlock err: " + err); + } else { + console.log("mifareClassic incrementBlock data: " + data); + } +}); ``` ### MifareClassicTag.decrementBlock9+ @@ -1147,10 +1213,16 @@ decrementBlock(blockIndex: number, value: number): Promise\ ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.MifareClassicTag(taginfo).decrementBlock(blockIndex, value).then(function (errcode){ - console.log(JSON.stringify(errcode)) - }) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareClassic' correctly. +let blockIndex = 1; // change it to be correct index. +let value = 0x20; // change it to be correct data. +mifareClassic.decrementBlock(blockIndex, value, (err, data)=> { + if (err) { + console.log("mifareClassic decrementBlock err: " + err); + } else { + console.log("mifareClassic decrementBlock data: " + data); + } +}); ``` ### MifareClassicTag.decrementBlock9+ @@ -1176,11 +1248,16 @@ decrementBlock(blockIndex: number, value: number, callback: AsyncCallback\ { + if (err) { + console.log("mifareClassic decrementBlock err: " + err); + } else { + console.log("mifareClassic decrementBlock data: " + data); + } +}); ``` ### MifareClassicTag.transferToBlock9+ @@ -1211,13 +1288,18 @@ transferToBlock(blockIndex: number): Promise\ import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.MifareClassicTag(taginfo).transferToBlock(blockIndex).then(function (errcode){ - console.log(JSON.stringify(errcode)) - }) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareClassic' correctly. +let blockIndex = 1; // change it to be correct index. +mifareClassic.transferToBlock(blockIndex, (err, data)=> { + if (err) { + console.log("mifareClassic transferToBlock err: " + err); + } else { + console.log("mifareClassic transferToBlock data: " + data); + } +}); ``` -### MifareClassicTag.transferToBlock +### MifareClassicTag.transferToBlock9+ transferToBlock(blockIndex: number, callback: AsyncCallback\): void @@ -1239,11 +1321,15 @@ transferToBlock(blockIndex: number, callback: AsyncCallback\): void ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.MifareClassicTag(taginfo).transferToBlock(blockIndex, function (error, errcode) { - console.log(JSON.stringify(error)) - console.log(JSON.stringify(errcode)) -}) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareClassic' correctly. +let blockIndex = 1; // change it to be correct index. +mifareClassic.transferToBlock(blockIndex, (err, data)=> { + if (err) { + console.log("mifareClassic transferToBlock err: " + err); + } else { + console.log("mifareClassic transferToBlock data: " + data); + } +}); ``` ### MifareClassicTag.restoreFromBlock9+ @@ -1274,10 +1360,14 @@ restoreFromBlock(blockIndex: number): Promise\ import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.MifareClassicTag(taginfo).restoreFromBlock(blockIndex).then(function (errcode){ - console.log(JSON.stringify(errcode)) - }) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareClassic' correctly. +let blockIndex = 1; // change it to be correct index. +mifareClassic.restoreFromBlock(blockIndex) + .then((data) => { + console.log("mifareClassic restoreFromBlock data: " + data); + }).catch((err)=> { + console.log("mifareClassic isExtendrestoreFromBlockedApduSupported err: " + err); + }); ``` ### MifareClassicTag.restoreFromBlock9+ @@ -1302,11 +1392,15 @@ restoreFromBlock(blockIndex: number, callback: AsyncCallback\): void ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.MifareClassicTag(taginfo).restoreFromBlock(blockIndex, function (error, errcode) { - console.log(JSON.stringify(error)) - console.log(JSON.stringify(errcode)) -}) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareClassic' correctly. +let blockIndex = 1; // change it to be correct index. +mifareClassic.restoreFromBlock(blockIndex, (err, data)=> { + if (err) { + console.log("mifareClassic restoreFromBlock err: " + err); + } else { + console.log("mifareClassic restoreFromBlock data: " + data); + } +}); ``` ### MifareClassicTag.getSectorCount9+ @@ -1330,8 +1424,9 @@ getSectorCount(): number ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -let setorCount = tag.MifareClassicTag(taginfo).getSectorCount(); +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareClassic' correctly. +let sectorCount = mifareClassic.getSectorCount(); +console.log("mifareClassic sectorCount: " + sectorCount); ``` ### MifareClassicTag.getBlockCountInSector9+ @@ -1361,13 +1456,14 @@ getBlockCountInSector(sectorIndex: number): number ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -let blockNumber = tag.MifareClassicTag(taginfo).getBlockCountInSector(sectorIndex); +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareClassic' correctly. +let blockCountInSector = mifareClassic.getBlockCountInSector(); +console.log("mifareClassic blockCountInSector: " + blockCountInSector); ``` ### MifareClassicTag.getType9+ -getType(): [MifareClassicType](#mifareclassictype9) +getType(): [MifareClassicType](js-apis-nfcTag.md#mifareclassictype9) 获取MifareClassic标签的类型。 @@ -1379,22 +1475,23 @@ getType(): [MifareClassicType](#mifareclassictype9) | **类型** | **说明** | | ------------------ | --------------------------| -| [MifareClassicType](#mifareclassictype9) | MifareClassic标签的类型。| +| [MifareClassicType](js-apis-nfcTag.md#mifareclassictype9) | MifareClassic标签的类型。| **示例:** ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -let type = tag.MifareClassicTag(taginfo).getType(); +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareClassic' correctly. +let getType = mifareClassic.getType(); +console.log("mifareClassic getType: " + getType); ``` ### MifareClassicTag.getTagSize9+ getTagSize(): number -获取标签的大小(字节),具体请参见[MifareTagSize](#mifaretagsize9)。 +获取标签的大小(字节),具体请参见[MifareClassicSize](js-apis-nfcTag.md#mifareclassicsize9)。 **需要权限**:ohos.permission.NFC_TAG @@ -1404,35 +1501,18 @@ getTagSize(): number | **类型** | **说明** | | ------------------ | --------------------------| -| number | 标签的大小,单位为字节,请参见[MifareTagSize](#mifaretagsize9)。| +| number | 标签的大小,单位为字节,请参见[MifareClassicSize](js-apis-nfcTag.md#mifareclassicsize9)。| **示例:** ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -let size = tag.MifareClassicTag(taginfo).getTagSize(); +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareClassic' correctly. +let tagSize = mifareClassic.getTagSize(); +console.log("mifareClassic tagSize: " + tagSize); ``` -## MifareClassicType9+ - -| **参数名** | **值** | **说明** | -| -------- | -------- | -------- | -| TYPE_UNKNOWN | -1 | 未知Mifare类型。 | -| TYPE_CLASSIC | 0 | Mifare Classic类型。| -| TYPE_PLUS | 1 | Mifare Plus类型。| -| TYPE_PRO | 2 | Mifare Pro类型。 | - -## MifareTagSize9+ - -| **参数名** | **值** | **说明** | -| -------- | -------- | -------- | -| MC_SIZE_MINI | 320 | 每个标签5个扇区,每个扇区4个块。 | -| MC_SIZE_1K | 1024 | 每个标签16个扇区,每个扇区4个块。| -| MC_SIZE_2K | 2048 | 每个标签32个扇区,每个扇区4个块。 | -| MC_SIZE_4K | 4096 | 每个标签40个扇区,每个扇区4个块。| - ### MifareClassicTag.isEmulatedTag9+ isEmulatedTag(): boolean @@ -1454,8 +1534,9 @@ isEmulatedTag(): boolean ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -let isEmulated = tag.MifareClassicTag(taginfo).isEmulatedTag(); +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareClassic' correctly. +let isEmulatedTag = mifareClassic.isEmulatedTag(); +console.log("mifareClassic isEmulatedTag: " + isEmulatedTag); ``` ### MifareClassicTag.getBlockIndex9+ @@ -1485,8 +1566,10 @@ getBlockIndex(sectorIndex: number): number ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -let index = tag.MifareClassicTag(taginfo).getBlockIndex(sectorIndex); +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareClassic' correctly. +let sectorIndex = 1; // change it to be correct index. +let blockIndex = mifareClassic.getBlockIndex(sectorIndex); +console.log("mifareClassic blockIndex: " + blockIndex); ``` ### MifareClassicTag.getSectorIndex9+ @@ -1516,8 +1599,10 @@ getSectorIndex(blockIndex: number): number ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -let index = tag.MifareClassicTag(taginfo).getSectorIndex(blockIndex); +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareClassic' correctly. +let blockIndex = 1; // change it to be correct index. +let sectorIndex = mifareClassic.getSectorIndex(blockIndex); +console.log("mifareClassic sectorIndex: " + sectorIndex); ``` ## MifareUltralightTag9+ @@ -1530,7 +1615,7 @@ TagSession是所有Nfc tag 技术类型的基类, 提供建立连接和发送 ### MifareUltralightTag.readMultiplePages9+ -readMultiplePages(pageIndex: number): Promise\ +readMultiplePages(pageIndex: number): Promise\ 阅读4页,共16字节。页面大小为4字节。使用promise方式作为异步方法。 @@ -1548,7 +1633,7 @@ readMultiplePages(pageIndex: number): Promise\ | **类型** | **说明** | | ------------------ | --------------------------| -| Promise\ | 读取的4页的数据。 | +| Promise\ | 读取的4页的数据。 | **示例:** @@ -1556,15 +1641,19 @@ readMultiplePages(pageIndex: number): Promise\ import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.MifareUltralightTag(taginfo).readMultiplePages(pageIndex).then(function (data){ - console.log("data: " + data) - }) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareUltralight' correctly. +let pageIndex = 1; // change it to be correct index. +mifareUltralight.readMultiplePages(pageIndex) + .then((data) => { + console.log("mifareUltralight readMultiplePages data: " + data); + }).catch((err)=> { + console.log("mifareUltralight readMultiplePages err: " + err); + }); ``` ### MifareUltralightTag.readMultiplePages9+ -readMultiplePages(pageIndex: number, callback: AsyncCallback\): void +readMultiplePages(pageIndex: number, callback: AsyncCallback\): void 阅读4页,共16字节。页面大小为4字节。使用callback方式作为异步方法。 @@ -1577,23 +1666,27 @@ readMultiplePages(pageIndex: number, callback: AsyncCallback\): void | 参数名 | 类型 | 必填 | 说明 | | -------- | ----------------------- | ---- | -------------------------------------- | | pageIndex | number | 是 | 要读取页面的索引 | -| callback | AsyncCallback\ | 是 | 回调函数。 | +| callback | AsyncCallback\ | 是 | 回调函数。 | **示例:** ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.MifareUltralightTag(taginfo).readMultiplePages(pageIndex, function (error, data) { - console.log(JSON.stringify(error)) - console.log(JSON.stringify(data)) -}) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareUltralight' correctly. +let pageIndex = 1; // change it to be correct index. +mifareUltralight.readMultiplePages(pageIndex, (err, data)=> { + if (err) { + console.log("mifareUltralight readMultiplePages err: " + err); + } else { + console.log("mifareUltralight readMultiplePages data: " + data); + } +}); ``` ### MifareUltralightTag.writeSinglePages9+ -writeSinglePages(pageIndex: number, data: string): Promise\ +writeSinglePages(pageIndex: number, data: number[]): Promise\ 写入一页数据,页面大小为4字节。使用promise方式作为异步方法。 @@ -1606,7 +1699,7 @@ writeSinglePages(pageIndex: number, data: string): Promise\ | 参数名 | 类型 | 必填 | 说明 | | -------- | ----------------------- | ---- | -------------------------------------- | | pageIndex | number | 是 | 要写入页面的索引。 | -| data | string | 是 | 要写入页面的数据内容。 | +| data | number[] | 是 | 要写入页面的数据内容。 | **返回值:** @@ -1619,15 +1712,20 @@ writeSinglePages(pageIndex: number, data: string): Promise\ ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.MifareUltralightTag(taginfo).writeSinglePages(pageIndex, data).then(function (errcode){ - console.log(JSON.stringify(errcode)) - }) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareUltralight' correctly. +let pageIndex = 1; // change it to be correct index. +let data = [0x01, 0x02, ...]; // change it to be correct raw data. +mifareUltralight.writeSinglePages(pageIndex, data) + .then((data) => { + console.log("mifareUltralight writeSinglePages data: " + data); + }).catch((err)=> { + console.log("mifareUltralight writeSinglePages err: " + err); + }); ``` ### MifareUltralightTag.writeSinglePages9+ -writeSinglePages(pageIndex: number, data: string, callback: AsyncCallback\): void +writeSinglePages(pageIndex: number, data: number[], callback: AsyncCallback\): void 写入一页数据,页面大小为4字节。使用callback方式作为异步方法。 @@ -1640,7 +1738,7 @@ writeSinglePages(pageIndex: number, data: string, callback: AsyncCallback\ |是| 回调函数。 | **示例:** @@ -1648,18 +1746,23 @@ writeSinglePages(pageIndex: number, data: string, callback: AsyncCallback\ { + if (err) { + console.log("mifareUltralight writeSinglePages err: " + err); + } else { + console.log("mifareUltralight writeSinglePages data: " + data); + } +}); ``` ### MifareUltralightTag.getType9+ getType(): MifareUltralightType -获取MifareUltralight标签的类型,以字节形式返回,具体请参见 [MifareUltralightType](#mifareultralighttype9)。 +获取MifareUltralight标签的类型,以字节形式返回,具体请参见 [MifareUltralightType](js-apis-nfcTag.md#mifareultralighttype9)。 **需要权限**:ohos.permission.NFC_TAG @@ -1669,25 +1772,18 @@ getType(): MifareUltralightType | **类型** | **说明** | | ------------------ | --------------------------| -| MifareUltralightType | MifareUltralight标签的类型, 具体请参见 [MifareUltralightType](#mifareultralighttype9)。| +| MifareUltralightType | MifareUltralight标签的类型, 具体请参见 [MifareUltralightType](js-apis-nfcTag.md#mifareultralighttype9)。| **示例:** ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -let type = tag.MifareUltralightType(taginfo).getType(); +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareUltralight' correctly. +let getType = mifareClassic.getType(); +console.log("mifareUltralight getType: " + getType); ``` -### MifareUltralightType9+ - -| **参数名** | **值** | **说明** | -| -------- | -------- | -------- | -| TYPE_UNKOWN | -1 | 未知的 Mifare 类型。 | -| TYPE_ULTRALIGHT | 1 | Mifare Ultralight类型。| -| TYPE_ULTRALIGHT_C | 2 | Mifare UltralightC 类型。 | - ## NdefFormatableTag9+ NdefFormatableTag为NDEF formattable的标签提供格式化操作,继承自TagSession。 @@ -1723,10 +1819,18 @@ format(message: [NdefMessage](#ndefmessage9)): Promise\ ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.NdefFormatableTag(taginfo).format(message).then(function (errcode){ - console.log(JSON.stringify(errcode)) - }) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndef' correctly. +let data = [0x01, 0x02, ...]; // change it to be correct raw data. +let ndefmessage = ndef.createNdefMessage(data); + +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndefFormatable' correctly. +ndefFormatable.format(ndefmessage, (err, data)=> { + if (err) { + console.log("ndefFormatable format err: " + err); + } else { + console.log("ndefFormatable format data: " + data); + } +}); ``` ### NdefFormatableTag.format9+ @@ -1757,11 +1861,18 @@ format(message: [NdefMessage](#ndefmessage9), callback: AsyncCallback\): ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.NdefFormatableTag(taginfo).format(message, function (error, errcode) { - console.log(JSON.stringify(error)) - console.log(JSON.stringify(errcode)) -}) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndef' correctly. +let data = [0x01, 0x02, ...]; // change it to be correct raw data. +let ndefmessage = ndef.createNdefMessage(data); + +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndefFormatable' correctly. +ndefFormatable.format(ndefmessage, (err, data)=> { + if (err) { + console.log("ndefFormatable format err: " + err); + } else { + console.log("ndefFormatable format data: " + data); + } +}); ``` ### NdefFormatableTag.formatReadOnly9+ @@ -1791,10 +1902,18 @@ formatReadOnly(message: [NdefMessage](#ndefmessage9)): Promise\ ```js import tag from '@ohos.nfc.tag'; -// tagInfo is an Object given by nfc service when tag is dispatched. -tag.NdefFormatableTag(taginfo).formatReadOnly(message).then(function (errcode){ - console.log(JSON.stringify(errcode)) - }) +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndef' correctly. +let data = [0x01, 0x02, ...]; // change it to be correct raw data. +let ndefmessage = ndef.createNdefMessage(data); + +// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndefFormatable' correctly. +ndefFormatable.formatReadOnly(ndefmessage, (err, data)=> { + if (err) { + console.log("ndefFormatable formatReadOnly err: " + err); + } else { + console.log("ndefFormatable formatReadOnly data: " + data); + } +}); ``` ### NdefFormatableTag.formatReadOnly9+ @@ -1825,9 +1944,16 @@ formatReadOnly(message: [NdefMessage](#ndefmessage9), callback: AsyncCallback\ { + if (err) { + console.log("ndefFormatable formatReadOnly err: " + err); + } else { + console.log("ndefFormatable formatReadOnly data: " + data); + } +}); ``` -- GitLab