diff --git a/zh-cn/application-dev/reference/apis/js-apis-nfcTag.md b/zh-cn/application-dev/reference/apis/js-apis-nfcTag.md old mode 100644 new mode 100755 index 94550568a43da3bf71c1313b18d8a6a6cd1eafbd..57c2da9bb923119b8a31756030abc6b5c53f6409 --- a/zh-cn/application-dev/reference/apis/js-apis-nfcTag.md +++ b/zh-cn/application-dev/reference/apis/js-apis-nfcTag.md @@ -5,19 +5,17 @@ > ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:** > 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 - ## **导入模块** ``` import tag from '@ohos.nfc.tag'; ``` - ## tag.getNfcATag -getNfcATag(tagInfo: TagInfo): NfcATag +getNfcATag(tagInfo: [TagInfo](#TagInfo9+)): [NfcATag](js-apis-nfctech.md#NfcATag) -获取NFC A类型Tag对象,通过该对象访问NfcA技术类型的Tag。 +获取NFC A类型Tag对象,通过该对象可访问NfcA技术类型的Tag。 **需要权限**:ohos.permission.NFC_TAG @@ -27,13 +25,13 @@ getNfcATag(tagInfo: TagInfo): NfcATag | **类型** | **说明** | | -------- | -------- | -| NfcATag | NFC A类型Tag对象 | +| [NfcATag](js-apis-nfctech.md#NfcATag) | NFC A类型Tag对象。 | ## tag.getNfcBTag -getNfcBTag(tagInfo: TagInfo): NfcBTag +getNfcBTag(tagInfo: [TagInfo](#TagInfo9+)): [NfcBTag](js-apis-nfctech.md#NfcBTag) -获取NFC B类型Tag对象,通过该对象访问NfcB技术类型的Tag。 +获取NFC B类型Tag对象,通过该对象可访问NfcB技术类型的Tag。 **需要权限**:ohos.permission.NFC_TAG @@ -43,13 +41,13 @@ getNfcBTag(tagInfo: TagInfo): NfcBTag | **类型** | **说明** | | -------- | ---------------- | -| NfcBTag | NFC B类型Tag对象 | +| [NfcBTag](js-apis-nfctech.md#NfcBTag) | NFC B类型Tag对象。 | ## tag.getNfcFTag -getNfcFTag(tagInfo: TagInfo): NfcFTag +getNfcFTag(tagInfo: [TagInfo](#TagInfo9+)): [NfcFTag](js-apis-nfctech.md#NfcFTag) -获取NFC F类型Tag对象,通过该对象访问NfcF技术类型的Tag。 +获取NFC F类型Tag对象,通过该对象可访问NfcF技术类型的Tag。 **需要权限**:ohos.permission.NFC_TAG @@ -59,13 +57,13 @@ getNfcFTag(tagInfo: TagInfo): NfcFTag | **类型** | **说明** | | -------- | ---------------- | -| NfcFTag | NFC F类型Tag对象 | +| [NfcFTag](js-apis-nfctech.md#NfcFTag) | NFC F类型Tag对象。 | ## tag.getNfcVTag -getNfcVTag(tagInfo: TagInfo): NfcVTag +getNfcVTag(tagInfo: [TagInfo](#TagInfo9+)): [NfcVTag](js-apis-nfctech.md#NfcVTag) -获取NFC V类型Tag对象,通过该对象访问NfcV技术类型的Tag。 +获取NFC V类型Tag对象,通过该对象可访问NfcV技术类型的Tag。 **需要权限**:ohos.permission.NFC_TAG @@ -75,4 +73,99 @@ getNfcVTag(tagInfo: TagInfo): NfcVTag | **类型** | **说明** | | -------- | ---------------- | -| NfcVTag | NFC V类型Tag对象 | +| [NfcVTag](js-apis-nfctech.md#NfcVTag) | NFC V类型Tag对象。 | + +## tag.getIsoDepTag9+ + +getIsoDepTag(tagInfo: [TagInfo](#TagInfo9+)): [IsoDepTag](js-apis-nfctech.md#IsoDepTag ) + +获取IsoDep类型Tag对象,通过该对象可访问Iso Dep技术类型的Tag。 + + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC.Core + +**返回值:** + +| **类型** | **说明** | +| ---------- | ------------------| +| [IsoDepTag](js-apis-nfctech.md#IsoDepTag>) | Iso Dep类型Tag对象。 | + +## tag.getNdefTag9+ + +getNdefTag(tagInfo: [TagInfo](#TagInfo9+)): [NdefTag](js-apis-nfctech.md#NdefTag-5) + +获取Ndef类型Tag对象,通过该对象可访问Ndef技术类型的Tag。 + + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC.Core + +**返回值:** + +| **类型** | **说明** | +| ---------| -------------- | +| [NdefTag](js-apis-nfctech.md#NdefTag) | Ndef类型Tag对象。| + +## tag.getMifareClassicTag9+ + +getMifareClassicTag(tagInfo: [TagInfo](#TagInfo9+)): [MifareClassicTag](js-apis-nfctech.md#MifareClassicTag) + +获取Mifare Classic类型Tag对象,通过该对象访问Mifare Classic技术类型的Tag。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC.Core + +**返回值:** + +| **类型** | **说明** | +| ----------------- | ------------------------| +| [MifareClassicTag](js-apis-nfctech.md#MifareClassicTag) | Mifare Classic类型Tag对象 | + +## tag.getMifareUltralightTag9+ + +getMifareUltralightTag(tagInfo: [TagInfo](#TagInfo9+)): [MifareUltralightTag](js-apis-nfctech.md#MifareUltralightTag) + +获取Mifare Ultralight类型Tag对象,通过该对象可访问Mifare Ultralight技术类型的Tag。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC.Core + +**返回值:** + +| **类型** | **说明** | +| -------------------- | ---------------------------| +| [MifareUltralightTag](js-apis-nfctech.md#MifareUltralightTag) | Mifare Ultralight类型Tag对象 | + +## tag.getNdefFormatableTag9+ + +getNdefFormatableTag(tagInfo: [TagInfo](#TagInfo9+)): [NdefFormatableTag](js-apis-nfctech.md#NdefFormatableTag) + +获取Ndef Formatable类型Tag对象,通过该对象可访问Ndef Formatable技术类型的Tag。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC.Core + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| [NdefFormatableTag](js-apis-nfctech.md#NdefFormatableTag) | Ndef Formatable类型Tag对象。 | + +## TagInfo9+ + +nfc服务在调度标签时给出的对象。 + +| **参数名** | **类型** | **说明** | +| -------- | -------- | -------- | +| uid | string | 标签的uid。 | +| technology | number[] | 支持的技术类型。 | +| extrasData | PacMap[] | 标签的额外信息。 | +| tagRfDiscId | number | 标签的RF discovery id。 | +| remoteTagService | rpc.RemoteObject | 标签服务的rpc远程对象。 | +| supportedProfiles | number[] | 支持的技术类型。 | \ 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 new file mode 100755 index 0000000000000000000000000000000000000000..ee0595c40553502dbcc2f042ed97796425470bae --- /dev/null +++ b/zh-cn/application-dev/reference/apis/js-apis-nfctech.md @@ -0,0 +1,1797 @@ +# 标准NFC-Tag Nfc 技术 + +本模块主要用于采用不同Nfc技术的Tag的读写操作。 + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:** +> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 + +## **导入模块** + +``` +import tag from '@ohos.nfc.tag'; +``` + +## NfcATag + +### 继承 TagSession + +tagSession是所有Nfc tag 技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)。 + +以下是NfcATag的独有接口。 + +### NfcATag.getSak + +getSak(): number + +获取NFC-A标签的SAK值。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC.Core + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| number | NfcA 标签的SAK值。 | + +**示例:** + +```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); +``` + +### NfcATag.getAtqa + +getAtqa(): number[] + +获取NFC-A标签的Atqa值。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC.Core + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| number[] | NfcA 标签的Ataq值。 | + +**示例:** + +```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); +``` + +## NfcBTag + +### 继承 TagSession + +tagSession是所有Nfc tag 技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)。 + +以下是NfcBTag的独有接口。 + +### NfcBTag.getRespAppData + +getRespAppData(): number[] + +获取标签的应用程序数据。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC.Core + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| number[] | NfcB 标签的Resp数据。 | + +**示例:** + +```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); +``` + +### NfcBTag.getRespProtocol + +getRespProtocol(): number[] + +获取标签的协议信息。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC.Core + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| number[] | NfcB 标签的协议信息。| + +**示例:** + +```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); +``` + +## NfcFTag + +### 继承 TagSession + +tagSession是所有Nfc tag 技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)。 + +以下是NfcFTag的独有接口。 + +### NfcFTag.getSystemCode + +getSystemCode(): number[] + +从标签实例获取系统代码。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC.Core + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| number[] | NfcF 标签的协议信息。| + +**示例:** + +```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); +``` + +### NfcFTag.getPmm + +getPmm(): number[] + +从标签实例获取PMm(由IC代码和制造商参数组成)。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| number[] | NfcF 标签的PMm信息。| + +**示例:** +```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); +``` + +## NfcVTag + +### 继承 TagSession + +tagSession是所有Nfc tag 技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)。 + +以下是NfcVTag的独有接口。 + +### NfcvTag.getResponseFlags + +getResponseFlags(): number + +从标签实例实例获取响应标志。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| number | NfcV 标签的响应标志。| + +**示例:** + +```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); +``` + +### NfcvTag.getDsfId + +getDsfId(): number + +从标签实例实例获取数据存储格式标识符(DSFID)。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| number | NfcV 标签的数据存储格式标识符。| + +**示例:** + +```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); +``` + +## IsoDepTag 9+ + +### 继承 TagSession + +tagSession是所有Nfc tag 技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)。 + +以下是IsoDepTag的独有接口。 + +### IsoDepTag.getHistoricalBytes9+ + +getHistoricalBytes(): string + +获取标签的历史字节。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| string | IsoDepTag 标签的历史字节。| + +**示例:** + +```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); +``` + +### IsoDepTag.getHiLayerResponse9+ + +getHiLayerResponse(): string + +获取标签的HiLayer响应字节。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| string | IsoDepTag 标签的HiLayer响应字节。| + +**示例:** + +```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); +``` + +### IsoDepTag.isExtendedApduSupported9+ + +isExtendedApduSupported(): Promise<boolean> + +检查是否支持外部apdu长度,使用promise方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| Promise<boolean> | 检查结果,ture: 支持, false: 不支持。| + +**示例:** + +```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) +}) +``` + +### IsoDepTag.isExtendedApduSupported9+ + +isExtendedApduSupported(callback: AsyncCallback\): void + +检查是否支持外部apdu长度,使用callback方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| callback | AsyncCallback\ | 是 | 回调函数,ture: 支持, false: 不支持。 | + +```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) +}) +``` + +## NdefTag 9+ + +### 继承 TagSession + +tagSession是所有Nfc tag 技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)。 + +以下是NdefTag的独有接口。 + +### NdefTag.createNdefMessage9+ + +createNdefMessage(data: string): [NdefMessage](#NdefMessage9+) + +使用原始字节创建ndef消息。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| **参数名** | **类型** | **必填** | **说明** | +| -------- | -------- | -------- | -------- | +| data | string | 是 | 字符串类型的原始字节 | + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| [NdefMessage](#NdefMessage9+) | Ndef消息 | + +**示例:** + +```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); +``` + +## NdefMessage9+ + +### NdefMessage.getNdefRecords9+ + +getNdefRecords(): [NdefRecord](#NdefRecord9+)[ ] + +获取ndef消息的所有记录。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| [NdefRecord](#NdefRecord9+)[ ] | Ndef消息所包含的所有记录。 | + +**示例:** + +```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(); +``` + +## 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消息。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** +| **参数名** | **类型** | **必填** | **说明** | +| -------- | -------- | -------- | -------- | +| ndefRecords | [NdefRecord](#NdefRecord9+)[] | 是 | NdefRecord记录列表 | + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| [NdefMessage](#NdefMessage9+) | Ndef消息。| + +**示例:** + +```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); +``` + +### NdefTag.getNdefTagType9+ + +getNdefTagType(): NfcForumType + +获取Ndef标签的类型。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| [NfcForumType](#NfcForumType9+) | Ndef标签类型。| + +**示例:** + +```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(); +``` + +### NdefTag.getNdefMessage9+ + +getNdefMessage(): NdefMessage + +获取发现标签时,从ndef标签读取的ndef消息。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| [NdefMessage](#NdefMessage9+) | Ndef消息。| + +**示例:** + +```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(); +``` + +### NdefTag.isNdefWritable9+ + +isNdefWritable(): Promise<boolean> + +检查ndef标签是否可写,使用promise方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| Promise<boolean> | 检查结果,ture: 可写, false: 不可写。| + +**示例:** + +```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)) +}) +``` +### NdefTag.isNdefWritable9+ + +isNdefWritable(callback: AsyncCallback<boolean>): void; + +检查ndef标签是否可写,使用callback方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| 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) +}) +``` + +### NdefTag.readNdef9+ + +readNdef(): Promise\ + +读取标签上的ndef消息,使用promise方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| Promise\ | 以Promise形式返回从标签中读取到的NdefMessage信息。| + +**示例:** + +```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)) +}) +``` +### NdefTag.readNdef9+ + +readNdef(callback: AsyncCallback\): void + +读取标签上的ndef消息,使用callback方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| callback | AsyncCallback\<[NdefMessage](#NdefMessage9+)> | 是 | 回调函数。| + +**示例:** + +```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) +}) +``` +### NdefTag.writeNdef9+ + +writeNdef(msg: NdefMessage): Promise\; + +将ndef消息写入标签,使用promise方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| msg | NdefMessage | 是 | Ndef消息。| + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| Promise\ | 以Promise形式返回,写入执行后的错误代码。如果返回0,则表示成功。 | + +**示例:** + +```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)) +}) +``` +### NdefTag.writeNdef9+ + +writeNdef(msg: NdefMessage, callback: AsyncCallback\): void + +将ndef消息写入此标签,使用callback方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| msg | NdefMessage | 是 | Ndef消息 | +| callback | AsyncCallback\ | 是 | 回调函数。 | + +**示例:** + +```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) +}) +``` +### NdefTag.canSetReadOnly9+ + +canSetReadOnly(): Promise\ + +检查ndef标签是否可以设置为只读,使用promise方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| Promise<boolean> | ture: 标签可设置为只读, false: 标签不可设置为只读。 | + +**示例:** + +```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)) +}) +``` +### NdefTag.canSetReadOnly9+ + +isNdefWritable(callback: AsyncCallback<boolean>): void; + +检查ndef标签是否可以设置为只读,使用callback方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| 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) +}) +``` +### NdefTag.setReadOnly9+ + +setReadOnly(): Promise\ + +将Ndef标签设置为只读,使用promise方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| Promise<number> | 0: 设置成功, 其它: 错误编码。 | + +**示例:** + +```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)) +}) +``` +### NdefTag.setReadOnly9+ + +setReadOnly(callback: AsyncCallback): void + +检查ndef标签是否可以设置为只读,使用callback方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| callback | AsyncCallback\ | 是 | 回调函数。 | + +**示例:** + +```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) +}) +``` +### NdefTag.getNdefTagTypeString9+ + +getNdefTagTypeString(type: [NfcForumType](#NfcForumType9+)): string + +将Nfc论坛类型转换为Nfc论坛中定义的字节数组。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| type | [NfcForumType](#NfcForumType9+) | 是 | NfcForum论坛类型。 | + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| 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); +``` + +## NfcForumType9+ + +| **参数名** | **类型** | **说明** | +| -------- | -------- | -------- | +| 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+ + +### 继承 TagSession + +tagSession是所有Nfc tag 技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)。 + +以下是MifareClassicTag的独有接口。 + +### MifareClassicTag.authenticateSector9+ + +authenticateSector(sectorIndex: number, key: number[], isKeyA: boolean): Promise\ + +使用密钥对扇区进行身份验证,只有身份验证成功的扇区可以进行操作。使用promise方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| sectorIndex | number | 是 | 待验证的扇区索引 | +| key | number[]| 是 | 用于身份验证的密钥(6字节) | +| isKeyA | boolean | 是 | isKeyA标志。true 表示KeyA,false 表示KeyB。| + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| Promise\ | 身份验证结果,成功返回true,失败返回false。 | + +**示例:** + +```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)) + }) +``` + +### MifareClassicTag.authenticateSector9+ + +authenticateSector(sectorIndex: number, key: number[], isKeyA: boolean, callback: AsyncCallback): void + +使用密钥对扇区进行身份验证,只有身份验证成功的扇区可以进行操作。使用callback方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| sectorIndex | number | 是 | 待验证的扇区索引。 | +| key | number[]| 是 | 用于身份验证的密钥(6字节)。 | +| isKeyA | boolean | 是 | isKeyA标志。true 表示KeyA,false 表示KeyB。| +| callback | AsyncCallback\ | 是 | 回调函数。| + +**示例:** + +```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) +}) +``` + +### MifareClassicTag.readSingleBlock9+ + +readSingleBlock(blockIndex: number): Promise\ + +读取标签中一个块存储的内容,一个块大小为16字节。使用promise方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| blockIndex | number | 是 | 要读取的块索引。 | + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| 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) + }) +``` +### MifareClassicTag.readSingleBlock9+ + +readSingleBlock(blockIndex: number, callback: AsyncCallback\): void + +读取标签中一个块存储的内容,一个块大小为16字节。使用callback方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| blockIndex | number | 是 | 要读取的块索引 | +| 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) +}) +``` +### MifareClassicTag.writeSingleBlock9+ + +writeSingleBlock(blockIndex: number, data: string): Promise\ + +向标签中一个块存储写入内容,一个块大小为16字节。使用promise方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| blockIndex | number | 是 | 要写入的块索引。 | +| data | string | 是 | 要写入的数据。 | + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| Promise\ | 执行写入操作返回的错误代码。如果返回0,则表示成功。 | + +**示例:** + +```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)) + }) +``` + +### MifareClassicTag.writeSingleBlock9+ + +writeSingleBlock(blockIndex: number, data: string, callback: AsyncCallback\): void + +向标签中一个块存储写入内容,一个块大小为16字节。使用callback方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| blockIndex | number | 是 | 要写入的块索引 | +| data | string | 是 | 要写入的数据 | +| 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).writeSingleBlock(blockIndex, data, function (error, errcode) { + console.log(JSON.stringify(error)) + console.log(JSON.stringify(errcode)) +}) +``` + +### MifareClassicTag.incrementBlock9+ + +incrementBlock(blockIndex: number, value: number): Promise\ + +增加一块带值的区域块。使用promise方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| blockIndex | number | 是 | 要增加的块索引。 | +| value | number | 是 | 要增加的块数据,非负值。 | + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| Promise\ | 执行新增操作返回的错误代码。如果返回0,则表示成功。 | + +**示例:** + +```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)) + }) +``` + +### MifareClassicTag.incrementBlock9+ + +incrementBlock(blockIndex: number, value: number, callback: AsyncCallback\): void + +增加一块带值的区域块。使用callback方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| blockIndex | number | 是 | 要增加的块索引。 | +| value | number | 是 | 要增加的块数据,非负值。 | +| callback | AsyncCallback\ | 是 | 回调函数。 | + +**示例:** + +```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, function (error, errcode) { + console.log(JSON.stringify(error)) + console.log(JSON.stringify(errcode)) +}) +``` + +### MifareClassicTag.decrementBlock9+ + +decrementBlock(blockIndex: number, value: number): Promise\ + +递减一块带值的区域块。使用promise方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| blockIndex | number | 是 | 要递减的块索引。 | +| value | number | 是 | 要递减的块数据,非负值。 | + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| Promise\ | 执行递减操作返回的错误代码。如果返回0,则表示成功。 | + +**示例:** + +```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)) + }) +``` + +### MifareClassicTag.decrementBlock9+ + +decrementBlock(blockIndex: number, value: number, callback: AsyncCallback\): void + +递减一块带值的区域块。使用callback方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| blockIndex | number | 是 | 要递减的块索引。 | +| value | number | 是 | 要递减的块数据,非负值。 | +| callback | AsyncCallback\ | 是 | 回调函数。 | + +**示例:** + +```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, function (error, errcode) { + console.log(JSON.stringify(error)) + console.log(JSON.stringify(errcode)) +}) +``` + +### MifareClassicTag.transferToBlock9+ + +transferToBlock(blockIndex: number): Promise\ + +将寄存器的值复制到块。使用promise方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| blockIndex | number | 是 | 复制的目的块索引。 | + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| Promise\ | 执行复制操作返回的错误代码。如果返回0,表示成功;否则返回错误码。| + +**示例:** +```js +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)) + }) +``` + +### MifareClassicTag.transferToBlock + +transferToBlock(blockIndex: number, callback: AsyncCallback\): void + +将寄存器的值复制到块。使用callback方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| blockIndex | number | 是 | 复制的目的块索引 | +| callback | AsyncCallback\ | 是 | 回调函数。 | + +**示例:** + +```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)) +}) +``` + +### MifareClassicTag.restoreFromBlock9+ + +restoreFromBlock(blockIndex: number): Promise\ + +将块的值复制到寄存器。使用promise方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| blockIndex | number | 是 | 复制的源块索引。| + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| Promise\ | 执行复制操作返回的错误代码。如果返回0,表示成功;否则返回错误码。| + +**示例:** +```js +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)) + }) +``` + +### MifareClassicTag.restoreFromBlock9+ + +restoreFromBlock(blockIndex: number, callback: AsyncCallback\): void + +将块的值复制到寄存器。使用callback方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| blockIndex | number | 是 | 复制的源块索引 | +| callback | AsyncCallback\ | 是 | 回调函数。| + +**示例:** + +```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)) +}) +``` + +### MifareClassicTag.getSectorCount9+ + +getSectorCount(): number + +获取mifare classic标签中的扇区数。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| 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(); +``` + +### MifareClassicTag.getBlockCountInSector9+ + +getBlockCountInSector(sectorIndex: number): number + +获取扇区中的块数。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| 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); +``` + +### MifareClassicTag.getType9+ + +getType(): [MifareClassicType](#MifareClassicType9+) + +获取MifareClassic标签的类型。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| [MifareClassicType](#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(); +``` + +### MifareClassicTag.getTagSize9+ + +getTagSize(): number + +获取标签的大小(字节),具体请参见[MifareTagSize](#MifareTagSize9+)。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| number | 标签的大小,单位为字节,请参见[MifareTagSize](#MifareTagSize9+)。| + +**示例:** + +```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(); +``` + +## 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 + +检查标签是否已模拟。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| boolean |检查结果,true: 是;false:否。 | + +**示例:** + +```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()(); +``` + +### MifareClassicTag.getBlockIndex9+ + +getBlockIndex(sectorIndex: number): number + +获取特定扇区的第一个块。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| 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); +``` +### MifareClassicTag.getSectorIndex9+ + +getSectorIndex(blockIndex: number): number + +获取扇区索引,该扇区包含特定块。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| 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); +``` + +## MifareUltralightTag 9+ + +### 继承 TagSession + +tagSession是所有Nfc tag 技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)。 + +以下是MifareUltralightTag的独有接口。 + +### MifareUltralightTag.readMultiplePages9+ + +readMultiplePages(pageIndex: number): Promise\ + +阅读4页,共16字节。页面大小为4字节。使用promise方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | ------------------------------ | +| pageIndex | number | 是 | 要读取页面的索引。 | + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| Promise\ | 读取的4页的数据。 | + +**示例:** +```js +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) + }) +``` + +### MifareUltralightTag.readMultiplePages9+ + +readMultiplePages(pageIndex: number, callback: AsyncCallback\): void + +阅读4页,共16字节。页面大小为4字节。使用callback方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| pageIndex | number | 是 | 要读取页面的索引 | +| 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)) +}) +``` + +### MifareUltralightTag.writeSinglePages9+ + +writeSinglePages(pageIndex: number, data: string): Promise\ + +写入一页数据,页面大小为4字节。使用promise方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| pageIndex | number | 是 | 要写入页面的索引。 | +| data | string | 是 | 要写入页面的数据内容。 | + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| Promise\ | 执行写入操作返回的错误代码。如果返回0,则表示成功。 | + +**示例:** +```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)) + }) +``` + +### MifareUltralightTag.writeSinglePages9+ + +writeSinglePages(pageIndex: number, data: string, callback: AsyncCallback\): void + +写入一页数据,页面大小为4字节。使用callback方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | ------------------------ | +| pageIndex | number | 是 | 要写入页面的索引。 | +| data | string | 是 | 要写入页面的数据内容。 | +| callback|AsyncCallback\ |是| 回调函数。 | + +**示例:** + +```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, function (error, errcode) { + console.log(JSON.stringify(error)) + console.log(JSON.stringify(errcode)) +}) +``` + +### MifareUltralightTag.getType9+ + +getType(): MifareUltralightType + +获取MifareUltralight标签的类型,以字节形式返回,具体请参见 [MifareUltralightType](#MifareUltralightType9+)。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| MifareUltralightType | MifareUltralight标签的类型, 具体请参见 [MifareUltralightType](#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(); +``` + +### MifareUltralightType9+ + +| **参数名** | **值** | **说明** | +| -------- | -------- | -------- | +| TYPE_UNKOWN | -1 | 未知的 Mifare 类型。 | +| TYPE_ULTRALIGHT | 1 | Mifare Ultralight类型。| +| TYPE_ULTRALIGHT_C | 2 | Mifare UltralightC 类型。 | + +## NdefFormatableTag 9+ + +### 继承 TagSession + +tagSession是所有Nfc tag 技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)。 + +以下是NdefFormatableTag的独有接口。 + +### NdefFormatableTag.format9+ + +format(message: [NdefMessage](#NdefMessage9+)): Promise\ + +将标签格式化为NDEF标签,然后将NDEF消息写入NDEF标签。使用promise方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| message | [NdefMessage](#NdefMessage9+) | 是 | 格式化成功时要写入的Ndef消息。可以为null,为null时仅格式化标签,不写入内容。 | + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| Promise\ | 执行操作后返回的错误代码。如果返回0,则表示成功。 | + +**示例:** +```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)) + }) +``` + +### NdefFormatableTag.format9+ + +format(message: [NdefMessage](#NdefMessage9+), callback: AsyncCallback\): void + +将标签格式化为NDEF标签,然后将NDEF消息写入NDEF标签。使用callback方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| message | [NdefMessage](#NdefMessage9+) | 是 | 格式化成功时要写入的Ndef消息。可以为null,为null时仅格式化标签,不写入内容。 | +| 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)) +}) +``` + +### NdefFormatableTag.formatReadOnly9+ + +formatReadOnly(message: [NdefMessage](#NdefMessage9+)): Promise\ + +将标签格式化为NDEF标签,然后将NDEF消息写入NDEF标签,之后将标签设置为只读。使用promise方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| message | [NdefMessage](#NdefMessage9+) | 是 | 格式化成功时要写入的Ndef消息。可以为null,为null时仅格式化标签,不写入内容。 | + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| Promise\ | 执行操作后返回的错误代码。如果返回0,则表示成功。 | + +**示例:** +```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)) + }) +``` + +### NdefFormatableTag.formatReadOnly9+ + +formatReadOnly(message: [NdefMessage](#NdefMessage9+), callback: AsyncCallback\): void + +将标签格式化为NDEF标签,然后将NDEF消息写入NDEF标签,之后将标签设置为只读。使用callback方式作为异步方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ----------------------- | ---- | -------------------------------------- | +| message | [NdefMessage](#NdefMessage9+) | 是 | 格式化成功时要写入的Ndef消息。可以为null,为null时仅格式化标签,不写入内容。| +| callback: AsyncCallback\ | 回调函数。 | + +**示例:** + +```js +import tag from '@ohos.nfc.tag'; + +// tagInfo is an Object given by nfc service when tag is dispatched. +tag.NdefFormatableTag(taginfo).formatReadOnly(message, function (error, errcode) { + console.log(JSON.stringify(error)) + console.log(JSON.stringify(errcode)) +}) +``` \ No newline at end of file diff --git a/zh-cn/application-dev/reference/apis/js-apis-tagSession.md b/zh-cn/application-dev/reference/apis/js-apis-tagSession.md new file mode 100755 index 0000000000000000000000000000000000000000..d23deefc56820728a30c2911c8b840560ce847f4 --- /dev/null +++ b/zh-cn/application-dev/reference/apis/js-apis-tagSession.md @@ -0,0 +1,125 @@ +# 标准NFC-Tag TagSession + +本模块是对NFC TagSession的使用说明。 + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:** +> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 + +## **导入模块** + +``` +import tag from '@ohos.nfc.tag'; +``` + +## tagSession + +tagSession是所有[Nfc tag 技术类型](js-apis-nfctech.md)的基类, 提供建立连接和发送数据等共同接口。 + +需要通过其子类来访问以下接口。在下面的示例中 统一用 getXXTag表示获取子类实例的方法。 + +具体使用时,请根据实际采用的Nfc tag技术,选择对应的方法,具体请参见[nfcTag](js-apis-nfcTag.md)文档。 + +### tagSession.connectTag + +connectTag(): boolean; + +和标签建立连接; +在从标签读取数据或将数据写入标签之前,必须调用此方法。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC.Core + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| boolean | 连接建立成功返回 true,失败返回false。 | + +**示例:** + +```js +import tag from '@ohos.nfc.tag'; + +// tagInfo is an Object given by nfc service when tag is dispatched. +let isNfcConnected = tag.getXXXTag(taginfo).connectTag(); +console.log("isNfcConnected:" +isNfcConnected); +``` + +### tagSession.reset() + +reset(): void + +重置与标签的连接,并恢复将数据写入标签的默认超时时间。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC.Core + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| boolean | 方法执行成功返回 true,失败返回false。 | + +**示例:** + +```js +import tag from '@ohos.nfc.tag'; + +// tagInfo is an Object given by nfc service when tag is dispatched. +let reset = tag.getXXXTag(taginfo).reset(); +console.log("reset:" +reset); +``` + +### tagSession.isTagConnected + +isTagConnected(): boolean + +检查是否已与标签建立连接。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC.Core + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| boolean | 已建立连接返回 true,未建立连接返回false。 | + +**示例:** + +```js +import tag from '@ohos.nfc.tag'; + +// tagInfo is an Object given by nfc service when tag is dispatched. +let isTagConnected = tag.getXXXTag(taginfo).isTagConnected(); +console.log("isTagConnected:" +isTagConnected); +``` + +### tagSession.getMaxSendLength + +getMaxSendLength(): number + +查询可以发送到标签的最大数据长度。 + +**需要权限**:ohos.permission.NFC_TAG + +**系统能力**:SystemCapability.Communication.NFC.Core + +**返回值:** + +| **类型** | **说明** | +| ------------------ | --------------------------| +| number | 可以发送到标签的最大数据长度。 | + +**示例:** + +```js +import tag from '@ohos.nfc.tag'; + +// tagInfo is an Object given by nfc service when tag is dispatched. +let mazSendLen = tag.getXXXTag(taginfo).getMaxSendLength(); +console.log("mazSendLen:" +mazSendLen); +``` \ No newline at end of file