提交 a1224011 编写于 作者: Z zhangxiuping

Update the nfc docs for throw error.

Signed-off-by: Nzhangxiuping <zhangxiuping@huawei.com>
上级 c853f403
......@@ -64,29 +64,57 @@ import tag from '@ohos.nfc.tag';
import tag from '@ohos.nfc.tag';
onCreate(want, launchParam) {
// add other code here
// 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) {
var tagInfo;
try {
tag.getTagInfo(want);
} catch (error) {
console.log("tag.getTagInfo catched error: " + error);
}
if (tagInfo == null || tagInfo == undefined) {
console.log("no TagInfo to be created, ignore it.");
return;
}
// get the supported technologies for this found tag.
var isNfcATag = false;
var isIsoDepTag = 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 (tagInfo.technology[i] == tag.ISO_DEP) {
isIsoDepTag = true;
}
// also check for technology: tag.NFC_B/NFC_F/NFC_V/NDEF/MIFARE_CLASSIC/MIFARE_ULTRALIGHT/NDEF_FORMATABLE
}
// use NfcA APIs to access the found tag.
if (isNfcATag) {
var nfcA = tag.getNfcATag(taginfo);
var nfcA;
try {
nfcA = tag.getNfcATag(taginfo);
} catch (error) {
console.log("tag.getNfcATag catched error: " + error);
}
// 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);
// use getIsoDep APIs to access the found tag.
if (isIsoDepTag) {
var isoDep;
try {
isoDep = tag.getIsoDep(taginfo);
} catch (error) {
console.log("tag.getIsoDep catched error: " + error);
}
// other code to read or write this found tag.
}
// use the same code to handle for "NfcA/NfcB/NfcF/NfcV/Ndef/MifareClassic/MifareUL/NdefFormatable".
}
```
......@@ -154,99 +182,135 @@ getNfcVTag(tagInfo: [TagInfo](#taginfo)): [NfcVTag](js-apis-nfctech.md#nfcvtag)
| -------- | ---------------- |
| [NfcVTag](js-apis-nfctech.md#nfcvtag) | NFC V类型Tag对象。 |
## tag.getIsoDepTag<sup>9+</sup>
## tag.getIsoDep<sup>9+</sup>
getIsoDepTag(tagInfo: [TagInfo](#taginfo)): [IsoDepTag](js-apis-nfctech.md#isoDepTag9 )
getIsoDep(tagInfo: [TagInfo](#taginfo)): [IsoDepTag](js-apis-nfctech.md#isoDepTag9 )
获取IsoDep类型Tag对象,通过该对象可访问Iso Dep技术类型的Tag。
**需要权限**:ohos.permission.NFC_TAG
获取IsoDep类型Tag对象,通过该对象可访问支持IsoDep技术类型的Tag。
**系统能力**:SystemCapability.Communication.NFC.Core
**返回值:**
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| --------- | ------------------------- | ---- | ---------------------------------------- |
| taginfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从tag.getTagInfo(want: Want)获取。 |
**返回值:**
| **类型** | **说明** |
| ---------- | ------------------|
| [IsoDepTag](js-apis-nfctech.md#isodeptag9) | Iso Dep类型Tag对象。 |
## tag.getNdefTag<sup>9+</sup>
| [IsoDepTag](js-apis-nfctech.md#isodeptag9) | IsoDep类型Tag对象,通过该对象访问IsoDep类型的相关接口。 |
getNdefTag(tagInfo: [TagInfo](#taginfo)): [NdefTag](js-apis-nfctech.md#ndeftag9)
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state of service is abnormal. |
获取Ndef类型Tag对象,通过该对象可访问Ndef技术类型的Tag。
## tag.getNdef<sup>9+</sup>
getNdef(tagInfo: [TagInfo](#taginfo)): [NdefTag](js-apis-nfctech.md#ndeftag9)
**需要权限**:ohos.permission.NFC_TAG
获取NDEF类型Tag对象,通过该对象可访问支持NDEF技术类型的Tag。
**系统能力**:SystemCapability.Communication.NFC.Core
**返回值:**
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| --------- | ------------------------- | ---- | ---------------------------------------- |
| taginfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从tag.getTagInfo(want: Want)获取。 |
**返回值:**
| **类型** | **说明** |
| ---------| -------------- |
| [NdefTag](js-apis-nfctech.md#ndeftag9) | Ndef类型Tag对象。|
| [NdefTag](js-apis-nfctech.md#ndeftag9) | NDEF类型Tag对象,通过该对象访问NDEF类型的相关接口。|
## tag.getMifareClassicTag<sup>9+</sup>
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state of service is abnormal. |
getMifareClassicTag(tagInfo: [TagInfo](#taginfo)): [MifareClassicTag](js-apis-nfctech.md#mifareclassictag-9)
## tag.getMifareClassic<sup>9+</sup>
获取Mifare Classic类型Tag对象,通过该对象访问Mifare Classic技术类型的Tag。
getMifareClassic(tagInfo: [TagInfo](#taginfo)): [MifareClassicTag](js-apis-nfctech.md#mifareclassictag-9)
**需要权限**:ohos.permission.NFC_TAG
获取MIFARE Classic类型Tag对象,通过该对象访问支持MIFARE Classic技术类型的Tag。
**系统能力**:SystemCapability.Communication.NFC.Core
**返回值:**
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| --------- | ------------------------- | ---- | ---------------------------------------- |
| taginfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从tag.getTagInfo(want: Want)获取。 |
**返回值:**
| **类型** | **说明** |
| ----------------- | ------------------------|
| [MifareClassicTag](js-apis-nfctech.md#mifareclassictag-9) | Mifare Classic类型Tag对象。 |
| [MifareClassicTag](js-apis-nfctech.md#mifareclassictag-9) | MIFARE Classic类型Tag对象,通过该对象访问MIFARE Classic类型的相关接口。 |
## tag.getMifareUltralightTag<sup>9+</sup>
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state of service is abnormal. |
getMifareUltralightTag(tagInfo: [TagInfo](#taginfo)): [MifareUltralightTag](js-apis-nfctech.md#mifareultralighttag9)
## tag.getMifareUltralight<sup>9+</sup>
获取Mifare Ultralight类型Tag对象,通过该对象可访问Mifare Ultralight技术类型的Tag。
getMifareUltralight(tagInfo: [TagInfo](#taginfo)): [MifareUltralightTag](js-apis-nfctech.md#mifareultralighttag9)
**需要权限**:ohos.permission.NFC_TAG
获取MIFARE Ultralight类型Tag对象,通过该对象可访问支持MIFARE Ultralight技术类型的Tag。
**系统能力**:SystemCapability.Communication.NFC.Core
**返回值:**
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| --------- | ------------------------- | ---- | ---------------------------------------- |
| taginfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从tag.getTagInfo(want: Want)获取。 |
**返回值:**
| **类型** | **说明** |
| -------------------- | ---------------------------|
| [MifareUltralightTag](js-apis-nfctech.md#mifareultralighttag9) | Mifare Ultralight类型Tag对象。 |
| [MifareUltralightTag](js-apis-nfctech.md#mifareultralighttag9) | MIFARE Ultralight类型Tag对象,通过该对象访问MIFARE Ultralight类型的相关接口。 |
## tag.getNdefFormatableTag<sup>9+</sup>
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state of service is abnormal. |
getNdefFormatableTag(tagInfo: [TagInfo](#taginfo)): [NdefFormatableTag](js-apis-nfctech.md#ndefformatabletag9)
## tag.getNdefFormatable<sup>9+</sup>
获取Ndef Formatable类型Tag对象,通过该对象可访问Ndef Formatable技术类型的Tag。
getNdefFormatable(tagInfo: [TagInfo](#taginfo)): [NdefFormatableTag](js-apis-nfctech.md#ndefformatabletag9)
**需要权限**:ohos.permission.NFC_TAG
获取NDEF Formatable类型Tag对象,通过该对象可访问支持NDEF Formatable技术类型的Tag。
**系统能力**:SystemCapability.Communication.NFC.Core
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| [NdefFormatableTag](js-apis-nfctech.md#ndefformatabletag) | Ndef Formatable类型Tag对象。 |
| [NdefFormatableTag](js-apis-nfctech.md#ndefformatabletag) | NDEF Formatable类型Tag对象,通过该对象访问NDEF Formatable类型的相关接口。 |
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state of service is abnormal. |
## tag.getTagInfo<sup>9+</sup>
getTagInfo(want: Want): [TagInfo](#taginfo)
getTagInfo(want: [Want](js-apis-application-Want.md#Want)): [TagInfo](#taginfo)
从Want中获取TagInfo,Want是被NFC服务初始化,包含了TagInfo所需的属性值。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC.Core
**返回值:**
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| --------- | ------------------------- | ---- | ---------------------------------------- |
| want | [Want](js-apis-application-Want.md#Want) | 是 | 分发Ability时,在系统onCreate入口函数的参数中获取。 |
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| [TagInfo](#taginfo) | TagInfo对象,用于获取不同技术类型的Tag对象。 |
......@@ -255,8 +319,6 @@ getTagInfo(want: Want): [TagInfo](#taginfo)
NFC服务在读取到标签时给出的对象,通过改对象属性,应用知道该标签支持哪些技术类型,并使用匹配的技术类型来调用相关接口。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC.Core
| **参数名** | **类型** | **说明** |
......@@ -268,8 +330,6 @@ NFC服务在读取到标签时给出的对象,通过改对象属性,应用
## NdefRecord<sup>9+</sup>
NDEF标签Record属性的定义,参考NDEF标签技术规范《NFCForum-TS-NDEF_1.0》的定义细节。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC.Core
| **参数名** | **类型** | **说明** |
| -------- | -------- | -------- |
......@@ -281,8 +341,6 @@ NDEF标签Record属性的定义,参考NDEF标签技术规范《NFCForum-TS-NDE
## 技术类型定义
NFC Tag有多种不同的技术类型,定义常量描述不同的技术类型。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC.Core
| **参数名** | **常量值** | **说明** |
| -------- | -------- | -------- |
......@@ -292,15 +350,13 @@ NFC Tag有多种不同的技术类型,定义常量描述不同的技术类型
| 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技术。|
| MIFARE_CLASSIC | 8 | MIFARE Classic技术。|
| MIFARE_ULTRALIGHT | 9 | MIFARE Utralight技术。|
| NDEF_FORMATABLE<sup>9+</sup> | 10 | 可以格式化的NDEF技术。|
## TnfType<sup>9+</sup>
NDEF Record的TNF(Type Name Field)类型值,参考NDEF标签技术规范《NFCForum-TS-NDEF_1.0》的定义细节。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC.Core
| **参数名** | **常量值** | **说明** |
| -------- | -------- | -------- |
......@@ -315,8 +371,6 @@ NDEF Record的TNF(Type Name Field)类型值,参考NDEF标签技术规范《NFC
## NDEF Record RTD类型定义
NDEF Record的RTD(Record Type Definition)类型值,参考NDEF标签技术规范《NFCForum-TS-NDEF_1.0》的定义细节。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC.Core
| **参数名** | **常量值** | **说明** |
| -------- | -------- | -------- |
......@@ -326,8 +380,6 @@ NDEF Record的RTD(Record Type Definition)类型值,参考NDEF标签技术规
## NfcForumType<sup>9+</sup>
NFC Forum标准里面Tag类型的定义。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC.Core
| **参数名** | **常量值** | **说明** |
| -------- | -------- | -------- |
......@@ -335,25 +387,21 @@ NFC Forum标准里面Tag类型的定义。
| 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类型。 |
| MIFARE_CLASSIC | 101 | MIFARE Classic类型。 |
## MifareClassicType<sup>9+</sup>
MifareClassic标签类型的定义。
**需要权限**:ohos.permission.NFC_TAG
MIFARE Classic标签类型的定义。
**系统能力**:SystemCapability.Communication.NFC.Core
| **参数名** | **常量值** | **说明** |
| -------- | -------- | -------- |
| TYPE_UNKNOWN | -1 | 未知Mifare类型。 |
| TYPE_CLASSIC | 0 | Mifare Classic类型。|
| TYPE_PLUS | 1 | Mifare Plus类型。|
| TYPE_PRO | 2 | Mifare Pro类型。 |
| TYPE_UNKNOWN | 0 | 未知MIFARE类型。 |
| TYPE_CLASSIC | 1 | MIFARE Classic类型。|
| TYPE_PLUS | 2 | MIFARE Plus类型。|
| TYPE_PRO | 3 | MIFARE Pro类型。 |
## MifareClassicSize<sup>9+</sup>
MifareClassic标签存储大小的定义。
**需要权限**:ohos.permission.NFC_TAG
MIFARE Classic标签存储大小的定义。
**系统能力**:SystemCapability.Communication.NFC.Core
| **参数名** | **常量值** | **说明** |
......@@ -363,15 +411,13 @@ MifareClassic标签存储大小的定义。
| MC_SIZE_2K | 2048 | 每个标签32个扇区,每个扇区4个块。 |
| MC_SIZE_4K | 4096 | 每个标签40个扇区,每个扇区4个块。|
### MifareUltralightType<sup>9+</sup>
MifareUltralight标签类型的定义。
**需要权限**:ohos.permission.NFC_TAG
## MifareUltralightType<sup>9+</sup>
MIFARE Ultralight标签类型的定义。
**系统能力**:SystemCapability.Communication.NFC.Core
| **参数名** | **常量值** | **说明** |
| -------- | -------- | -------- |
| TYPE_UNKOWN | -1 | 未知的 Mifare 类型。 |
| TYPE_ULTRALIGHT | 1 | Mifare Ultralight类型。|
| TYPE_ULTRALIGHT_C | 2 | Mifare UltralightC 类型。 |
| TYPE_UNKOWN | 0 | 未知的 MIFARE 类型。 |
| TYPE_ULTRALIGHT | 1 | MIFARE Ultralight类型。|
| TYPE_ULTRALIGHT_C | 2 | MIFARE UltralightC 类型。 |
<!--no_check-->
\ No newline at end of file
......@@ -15,7 +15,7 @@ import tag from '@ohos.nfc.tag';
NfcATag 提供 NFC-A(ISO 14443-3A)技术的属性和I/O操作的访问,继承自TagSession。
TagSession是所有Nfc tag 技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)
TagSession是所有NFC Tag技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)
以下是NfcATag的独有接口。
......@@ -40,8 +40,7 @@ getSak(): number
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'nfcA' correctly.
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'nfcA' correctly.
let sak = nfcA.getSak();
console.log("nfcA sak: " + sak);
```
......@@ -67,7 +66,7 @@ getAtqa(): number[]
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'nfcA' correctly.
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'nfcA' correctly.
let atqa = nfcA.getAtqa();
console.log("nfcA atqa: " + atqa);
```
......@@ -76,7 +75,7 @@ console.log("nfcA atqa: " + atqa);
NfcBTag 提供对NFC-B(ISO 14443-3B)技术的属性和I/O操作的访问,继承自TagSession。
TagSession是所有Nfc tag 技术类型的基类,提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)
TagSession是所有NFC Tag技术类型的基类,提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)
以下是NfcBTag的独有接口。
......@@ -101,7 +100,7 @@ getRespAppData(): number[]
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'nfcB' correctly.
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'nfcB' correctly.
let respAppData = nfcB.getRespAppData();
console.log("nfcB respAppData: " + respAppData);
```
......@@ -127,7 +126,7 @@ getRespProtocol(): number[]
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'nfcB' correctly.
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'nfcB' correctly.
let respProtocol = nfcB.getRespProtocol();
console.log("nfcB respProtocol: " + respProtocol);
```
......@@ -136,7 +135,7 @@ console.log("nfcB respProtocol: " + respProtocol);
NfcFTag 提供对NFC-F(JIS 6319-4)技术的属性和I/O操作的访问,继承自TagSession。
TagSession是所有Nfc tag 技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)
TagSession是所有NFC Tag技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)
以下是NfcFTag的独有接口。
......@@ -161,7 +160,7 @@ getSystemCode(): number[]
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'nfcF' correctly.
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'nfcF' correctly.
let systemCode = nfcF.getSystemCode();
console.log("nfcF systemCode: " + systemCode);
```
......@@ -187,7 +186,7 @@ getPmm(): number[]
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'nfcF' correctly.
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'nfcF' correctly.
let pmm = nfcF.getPmm();
console.log("nfcF pmm: " + pmm);
```
......@@ -196,7 +195,7 @@ console.log("nfcF pmm: " + pmm);
NfcVTag 提供对NFC-V(ISO 15693)技术的属性和I/O操作的访问,继承自TagSession。
TagSession是所有Nfc tag 技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)
TagSession是所有NFC Tag技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)
以下是NfcVTag的独有接口。
......@@ -221,7 +220,7 @@ getResponseFlags(): number
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'nfcV' correctly.
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'nfcV' correctly.
let responseFlags = nfcV.getResponseFlags();
console.log("nfcV responseFlags: " + responseFlags);
```
......@@ -247,7 +246,7 @@ getDsfId(): number
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'nfcV' correctly.
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'nfcV' correctly.
let dsfId = nfcV.getDsfId();
console.log("nfcV dsfId: " + dsfId);
```
......@@ -256,7 +255,7 @@ console.log("nfcV dsfId: " + dsfId);
IsoDepTag 提供对ISO-DEP(ISO 14443-4)技术的属性和I/O操作的访问,继承自TagSession。
TagSession是所有Nfc tag 技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)
TagSession是所有NFC Tag技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)
以下是IsoDepTag的独有接口。
......@@ -264,24 +263,20 @@ TagSession是所有Nfc tag 技术类型的基类, 提供建立连接和发送
getHistoricalBytes(): number[]
获取标签的历史字节。
**需要权限**:ohos.permission.NFC_TAG
获取标签的历史字节,针对基于NfcA通信技术的IsoDep卡片。
**系统能力**:SystemCapability.Communication.NFC
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| number[] | IsoDepTag 标签的历史字节,每个number十六进制表示,范围是0x00~0xFF。|
| number[] | IsoDepTag 标签的历史字节,每个number十六进制表示,范围是0x00~0xFF。如果该IsoDep类型Tag是基于NfcB技术的,则该返回值为空。|
**示例:**
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'isoDep' correctly.
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'isoDep' correctly.
let historicalBytes = isoDep.getHistoricalBytes();
console.log("isoDep historicalBytes: " + historicalBytes);
```
......@@ -290,24 +285,20 @@ console.log("isoDep historicalBytes: " + historicalBytes);
getHiLayerResponse(): number[]
获取标签的HiLayer响应字节。
**需要权限**:ohos.permission.NFC_TAG
获取标签的更高层响应字节,针对基于NfcB通信技术的IsoDep卡片。
**系统能力**:SystemCapability.Communication.NFC
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| number[] | IsoDepTag 标签的HiLayer响应字节,每个number十六进制表示,范围是0x00~0xFF。|
| number[] | IsoDepTag 标签的更高层响应字节,每个number十六进制表示,范围是0x00~0xFF。如果该IsoDep类型Tag是基于NfcA技术的,则该返回值为空。|
**示例:**
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'isoDep' correctly.
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'isoDep' correctly.
let hiLayerResponse = isoDep.getHiLayerResponse();
console.log("isoDep hiLayerResponse: " + hiLayerResponse);
```
......@@ -316,137 +307,368 @@ console.log("isoDep hiLayerResponse: " + hiLayerResponse);
isExtendedApduSupported(): Promise&lt;boolean&gt;
检查是否支持扩展的APDU,使用promise方式作为异步方法。
检查是否支持扩展的APDU,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| Promise&lt;boolean&gt; | 检查结果,true: 支持, false: 不支持。|
**示例:**
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'isoDep' correctly.
isoDep.isExtendedApduSupported()
.then((data) => {
console.log("isoDep isExtendedApduSupported data: " + data);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'isoDep' correctly.
// connect the tag at first if not connected.
if (!isoDep.isTagConnected()) {
if (!isoDep.connectTag()) {
console.log("isoDep connectTag failed.");
return;
}
}
try {
isoDep.isExtendedApduSupported().then((response) => {
console.log("isoDep isExtendedApduSupported Promise response: " + response);
}).catch((err)=> {
console.log("isoDep isExtendedApduSupported err: " + err);
console.log("isoDep isExtendedApduSupported Promise err: " + err);
});
} catch (busiError) {
console.log("isoDep isExtendedApduSupported Promise busiError: " + busiError);
}
```
### IsoDepTag.isExtendedApduSupported<sup>9+</sup>
isExtendedApduSupported(callback: AsyncCallback\<boolean>): void
检查是否支持扩展的APDU,使用callback方式作为异步方法。
检查是否支持扩展的APDU,使用AsyncCallback方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| callback | AsyncCallback\<boolean> | 是 | 回调函数,true: 支持, false: 不支持。 |
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
```js
import tag from '@ohos.nfc.tag';
// 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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'isoDep' correctly.
// connect the tag at first if not connected.
if (!isoDep.isTagConnected()) {
if (!isoDep.connectTag()) {
console.log("isoDep connectTag failed.");
return;
}
});
}
try {
isoDep.isExtendedApduSupported((err, response)=> {
if (err) {
console.log("isoDep isExtendedApduSupported AsyncCallback err: " + err);
} else {
console.log("isoDep isExtendedApduSupported AsyncCallback response: " + response);
}
});
} catch (busiError) {
console.log("isoDep isExtendedApduSupported AsyncCallback busiError: " + busiError);
}
```
## NdefTag<sup>9+</sup>
## NdefMessage<sup>9+</sup>
提供对已格式化为NDEF的NFC标签的数据和操作的访问,继承自TagSession。
### NdefMessage.getNdefRecords<sup>9+</sup>
TagSession是所有NFC Tag技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)
getNdefRecords(): [NdefRecord](js-apis-nfcTag.md#ndefrecord9)[]
以下是NdefTag的独有接口
获取NDEF消息中的所有记录
### NdefTag.createNdefMessage<sup>9+</sup>
**系统能力**:SystemCapability.Communication.NFC
createNdefMessage(data: number[]): [NdefMessage](#ndefmessage9)
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| [NdefRecord](js-apis-nfcTag.md#ndefrecord9)[] | NDEF标签的Record列表,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 |
使用原始字节创建ndef消息。
**示例:**
```js
import tag from '@ohos.nfc.tag';
**需要权限**:ohos.permission.NFC_TAG
// see NdefTag, obtains ndefMessage from ndefTag.createNdefMessage or ndefTag.getNdefMessage.
// var ndefMessage = ndefTag.createNdefMessage(...);
// var ndefMessage = ndefTag.getNdefMessage();
**系统能力**:SystemCapability.Communication.NFC
let ndefRecords = ndefMessage.getNdefRecords();
console.log("ndef ndefRecords number: " + ndefRecords.length);
```
### NdefMessage.makeUriRecord<sup>9+</sup>
makeUriRecord(uri: string): [NdefRecord](js-apis-nfcTag.md#ndefrecord9);
根据输入的URI,构建NDEF标签的Record数据对象。
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| **参数名** | **类型** | **必填** | **说明** |
| -------- | -------- | -------- | -------- |
| data | number[] | 是 | 原始字节,每个number十六进制表示,范围是0x00~0xFF |
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| uri | string | 是 | 写入到NDEF Record里面的数据内容。 |
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| [NdefMessage](#ndefmessage9) | NDEF标签的Message,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 |
| [NdefRecord](js-apis-nfcTag.md#ndefrecord9) | NDEF标签的Record,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// see NdefTag, obtains ndefMessage from ndefTag.createNdefMessage or ndefTag.getNdefMessage. Such as:
// var ndefMessage = ndefTag.createNdefMessage(...);
// var ndefMessage = ndefTag.getNdefMessage();
try {
let uri = "https://gitee.com/openharmony"; // change it to be correct.
let ndefRecord = ndefMessage.makeUriRecord(uri);
if (ndefRecord != undefined) {
console.log("ndefMessage makeUriRecord rtdType: " + ndefRecord.rtdType);
console.log("ndefMessage makeUriRecord payload: " + ndefRecord.payload);
} else {
console.log("ndefMessage makeUriRecord ndefRecord: " + ndefRecord);
}
} catch (busiError) {
console.log("ndefMessage makeUriRecord catched busiError: " + busiError);
}
```
### NdefMessage.makeTextRecord<sup>9+</sup>
makeTextRecord(text: string, locale: string): [NdefRecord](js-apis-nfcTag.md#ndefrecord9);
根据输入的文本数据和编码类型,构建NDEF标签的Record。
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| text | string | 是 | 写入到NDEF Record里面的文本数据内容。 |
| locale | string | 是 | 文本数据内容的编码方式。 |
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| [NdefRecord](js-apis-nfcTag.md#ndefrecord9) | NDEF标签的Record,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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);
// see NdefTag, obtains ndefMessage from ndefTag.createNdefMessage or ndefTag.getNdefMessage. Such as:
// var ndefMessage = ndefTag.createNdefMessage(...);
// var ndefMessage = ndefTag.getNdefMessage();
try {
let text = "Hello World"; // change it to be correct.
let locale = "utf8"; // change it to be correct.
let ndefRecord = ndefMessage.makeTextRecord(text, locale);
if (ndefRecord != undefined) {
console.log("ndefMessage makeTextRecord rtdType: " + ndefRecord.rtdType);
console.log("ndefMessage makeTextRecord payload: " + ndefRecord.payload);
} else {
console.log("ndefMessage makeTextRecord ndefRecord: " + ndefRecord);
}
} catch (busiError) {
console.log("ndefMessage makeTextRecord catched busiError: " + busiError);
}
```
## NdefMessage<sup>9+</sup>
### NdefMessage.getNdefRecords<sup>9+</sup>
### NdefMessage.makeMimeRecord<sup>9+</sup>
getNdefRecords(): [NdefRecord](js-apis-nfcTag.md#ndefrecord9)[ ]
makeMimeRecord(mimeType: string, mimeData: number[]): [NdefRecord](js-apis-nfcTag.md#ndefrecord9);
获取ndef消息的所有记录
根据输入的MIME数据和类型,构建NDEF标签的Record
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| mimeType | string | 是 | MIME数据的类型。 |
| mimeData | number[] | 是 | MIME数据内容。 |
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| [NdefRecord](js-apis-nfcTag.md#ndefrecord9) | NDEF标签的Record,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// see NdefTag, obtains ndefMessage from ndefTag.createNdefMessage or ndefTag.getNdefMessage. Such as:
// var ndefMessage = ndefTag.createNdefMessage(...);
// var ndefMessage = ndefTag.getNdefMessage();
try {
let mimeType = "media"; // change it to be correct.
let mimeData = [0x01, 0x02, 0x03, 0x04]; // change it to be correct.
let ndefRecord = ndefMessage.makeMimeRecord(mimeType, mimeData);
if (ndefRecord != undefined) {
console.log("ndefMessage makeMimeRecord rtdType: " + ndefRecord.rtdType);
console.log("ndefMessage makeMimeRecord payload: " + ndefRecord.payload);
} else {
console.log("ndefMessage makeMimeRecord ndefRecord: " + ndefRecord);
}
} catch (busiError) {
console.log("ndefMessage makeMimeRecord catched busiError: " + busiError);
}
```
### NdefMessage.makeExternalRecord<sup>9+</sup>
makeExternalRecord(domainName: string, serviceName: string, externalData: number[]): [NdefRecord](js-apis-nfcTag.md#ndefrecord9);
根据应用程序特定的外部数据,构建NDEF标签的Record。
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| domainName | string | 是 | 外部数据发布组织的域名,一般是应用程序的包名。 |
| serviceName | string | 是 | 外部数据的指定类型。 |
| externalData | number[] | 是 | 外部数据内容。 |
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| [NdefRecord](js-apis-nfcTag.md#ndefrecord9) | NDEF标签的Record,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// see NdefTag, obtains ndefMessage from ndefTag.createNdefMessage or ndefTag.getNdefMessage. Such as:
// var ndefMessage = ndefTag.createNdefMessage(...);
// var ndefMessage = ndefTag.getNdefMessage();
try {
let domainName = "ohos.nfc.application"; // change it to be correct.
let type = "nfc"; // change it to be correct.
let externalData = [0x01, 0x02, 0x03, 0x04]; // change it to be correct.
let ndefRecord = ndefMessage.makeExternalRecord(domainName, type, externalData);
if (ndefRecord != undefined) {
console.log("ndefMessage makeExternalRecord rtdType: " + ndefRecord.rtdType);
console.log("ndefMessage makeExternalRecord payload: " + ndefRecord.payload);
} else {
console.log("ndefMessage makeExternalRecord ndefRecord: " + ndefRecord);
}
} catch (busiError) {
console.log("ndefMessage makeExternalRecord catched busiError: " + busiError);
}
```
### NdefMessage.messageToBytes<sup>9+</sup>
messageToBytes(ndefMessage: [NdefMessage](#ndefmessage9)): number[];
把输入的NDEF消息数据对象,转换为字节格式的数据。
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| ndefMessage | [NdefMessage](#ndefmessage9) | 是 | NDEF消息数据对象。 |
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| [NdefRecord](js-apis-nfcTag.md#ndefrecord9)[ ] | NDEF标签的Record列表,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 |
| number[] | NDEF消息数据对象,所转换成的字节格式的数据。每个number十六进制表示,范围是0x00~0xFF。 |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// see NdefTag, obtains ndefMessage from ndefTag.createNdefMessage or ndefTag.getNdefMessage. Such as:
// var ndefMessage = ndefTag.createNdefMessage(...);
// var ndefMessage = ndefTag.getNdefMessage();
try {
// the parameter 'ndefMessage' can be different from the instance object.
let rawData = ndefMessage.messageToBytes(ndefMessage);
console.log("ndefMessage messageToBytes rawData: " + rawData);
} catch (busiError) {
console.log("ndefMessage messageToBytes catched busiError: " + busiError);
}
```
## NdefTag<sup>9+</sup>
提供对已格式化为NDEF的NFC标签的数据和操作的访问,继承自TagSession。
TagSession是所有NFC Tag技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)
以下是NdefTag的独有接口。
### NdefTag.createNdefMessage<sup>9+</sup>
createNdefMessage(data: number[]): [NdefMessage](#ndefmessage9)
使用原始字节数据创建NDEF标签的Message。该数据必须符合NDEF Record数据格式,如果不符合格式,则返回的NdeMessage数据对象,所包含的NDE Record列表会为空。
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| **参数名** | **类型** | **必填** | **说明** |
| -------- | -------- | -------- | -------- |
| data | number[] | 是 | 原始字节,每个number十六进制表示,范围是0x00~0xFF。 |
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| [NdefMessage](#ndefmessage9) | NDEF标签的Message,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndef' correctly.
let ndefRecords = ndef.getNdefRecords();
console.log("ndef ndefRecords number: " + ndefRecords.length);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly.
let rawData = [0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]; // change the raw data bytes to be correct.
let ndefMessage;
try {
ndefMessage = ndefTag.createNdefMessage(rawData);
console.log("ndef createNdefMessage, ndefMessage: " + ndefMessage);
} catch (busiError) {
console.log("ndef createNdefMessage busiError: " + busiError);
}
```
### NdefTag.createNdefMessage<sup>9+</sup>
createNdefMessage(ndefRecords: NdefRecord[]): [NdefMessage](#ndefmessage9)
使用记录列表创建NDEF消息。
**需要权限**:ohos.permission.NFC_TAG
使用NDEF Records列表,创建NDEF Message。
**系统能力**:SystemCapability.Communication.NFC
......@@ -456,17 +678,15 @@ createNdefMessage(ndefRecords: NdefRecord[]): [NdefMessage](#ndefmessage9)
| ndefRecords | [NdefRecord](js-apis-nfcTag.md#ndefrecord9)[] | 是 | NDEF标签的Record列表,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 |
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| [NdefMessage](#ndefmessage9) | NDEF标签的Message,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。|
**示例:**
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndef' correctly.
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly.
let ndefRecords = [
// record format: tnf, rtdType, id, payload
// 1st record:
......@@ -477,33 +697,34 @@ let ndefRecords = [
// other record if has one ...
];
let ndefMessage = ndef.createNdefMessage(ndefRecords);
console.log("ndef ndefMessage: " + ndefMessage);
let ndefMessage;
try {
ndefMessage = ndefTag.createNdefMessage(ndefRecords);
console.log("ndef createNdefMessage ndefMessage: " + ndefMessage);
} catch (busiError) {
console.log("ndef createNdefMessage busiError: " + busiError);
}
```
### NdefTag.getNdefTagType<sup>9+</sup>
getNdefTagType(): NfcForumType
获取Ndef标签的类型。
**需要权限**:ohos.permission.NFC_TAG
获取NDEF标签的类型。
**系统能力**:SystemCapability.Communication.NFC
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| [NfcForumType](js-apis-nfcTag.md#nfcforumtype9) | NDEF标签类型,包括NFC FORUM TYPE 1/2/3/4等。|
**示例:**
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndef' correctly.
let ndefTagType = ndef.getNdefTagType();
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly.
let ndefTagType = ndefTag.getNdefTagType();
console.log("ndef ndefTagType: " + ndefTagType);
```
......@@ -513,217 +734,237 @@ getNdefMessage(): NdefMessage
获取发现NDEF标签时,从标签读取的Message。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| [NdefMessage](#ndefmessage9) | NDEF标签的Message,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。|
**示例:**
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndef' correctly.
let ndefMessage = ndef.getNdefMessage();
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly.
let ndefMessage = ndefTag.getNdefMessage();
console.log("ndef ndefMessage: " + ndefMessage);
```
### NdefTag.isNdefWritable<sup>9+</sup>
isNdefWritable(): Promise&lt;boolean&gt;
检查NDEF标签是否可写,使用promise方式作为异步方法。
isNdefWritable(): boolean;
**需要权限**:ohos.permission.NFC_TAG
检查NDEF标签是否可写。在调用写数据接口前,需要先判断是否支持写操作。
**系统能力**:SystemCapability.Communication.NFC
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| Promise&lt;boolean&gt; | 检查结果,true: 可写, false: 不可写。|
| boolean | 检查结果,true: 可写, false: 不可写。|
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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.isNdefWritable<sup>9+</sup>
isNdefWritable(callback: AsyncCallback&lt;boolean&gt;): void;
检查ndef标签是否可写,使用callback方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| callback | AsyncCallback\<boolean> | 是 | 回调函数,NDEF标签可写,返回true。 |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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);
}
});
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly.
var isWritable = ndefTag.isNdefWritable();
console.log("ndef isNdefWritable: " + isWritable);
```
### NdefTag.readNdef<sup>9+</sup>
readNdef(): Promise\<NdefMessage>
读取标签上的ndef消息,使用promise方式作为异步方法。
读取标签上的NDEF消息,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| Promise\<[NdefMessage](#ndefmessage9)> | 以Promise形式返回从NDEF标签中读取到的Message信息。|
| Promise\<[NdefMessage](#ndefmessage9)> | 以Promise形式返回从NDEF标签中读取到的Message数据对象。|
**示例:**
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndef' correctly.
ndef.readNdef()
.then((data) => {
console.log("ndef readNdef data: " + data);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly.
// connect the tag at first if not connected.
if (!ndefTag.isTagConnected()) {
if (!ndefTag.connectTag()) {
console.log("ndefTag connectTag failed.");
return;
}
}
try {
ndefTag.readNdef().then((ndefmessage) => {
console.log("ndef readNdef Promise ndefmessage: " + ndefmessage);
}).catch((err)=> {
console.log("ndef readNdef err: " + err);
console.log("ndef readNdef Promise err: " + err);
});
} catch (busiError) {
console.log("ndef readNdef Promise catched busiError: " + busiError);
}
```
### NdefTag.readNdef<sup>9+</sup>
readNdef(callback: AsyncCallback\<[NdefMessage](#ndefmessage9)>): void
读取标签上的ndef消息,使用callback方式作为异步方法。
读取标签上的NDEF消息,使用AsyncCallback方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| callback | AsyncCallback\<[NdefMessage](#ndefmessage9)> | 是 | 回调函数。|
| callback | AsyncCallback\<[NdefMessage](#ndefmessage9)> | 是 | 回调函数,返回从NDEF标签中读取到的Message信息。|
**示例:**
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly.
// connect the tag at first if not connected.
if (!ndefTag.isTagConnected()) {
if (!ndefTag.connectTag()) {
console.log("ndefTag connectTag failed.");
return;
}
});
}
try {
ndefTag.readNdef((err, ndefmessage)=> {
if (err) {
console.log("ndef readNdef AsyncCallback err: " + err);
} else {
console.log("ndef readNdef AsyncCallback ndefmessage: " + ndefmessage);
}
});
} catch (busiError) {
console.log("ndef readNdef AsyncCallback catched busiError: " + busiError);
}
```
### NdefTag.writeNdef<sup>9+</sup>
writeNdef(msg: NdefMessage): Promise\<number>;
writeNdef(msg: NdefMessage): Promise\<void>;
ndef消息写入标签,使用promise方式作为异步方法。
NDEF Messsage数据对象写入标签,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| msg | NdefMessage | 是 | Ndef消息。|
**返回值:**
| msg | NdefMessage | 是 | NDEF Message数据对象。|
| **类型** | **说明** |
| ------------------ | --------------------------|
| Promise\<number> | 以Promise形式返回,写入执行后的错误代码。如果返回0,则表示成功。 |
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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.
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly.
// ndefMessage created from raw data, such as:
let ndefMessage = ndefTag.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // change the raw data to be correct.
// or ndefMessage created from ndefTag.createNdefMessage(ndefRecords: NdefRecord[])
ndef.writeNdef(ndefMessage)
.then((data) => {
console.log("ndef writeNdef data: " + data);
// connect the tag at first if not connected.
if (!ndefTag.isTagConnected()) {
if (!ndefTag.connectTag()) {
console.log("ndefTag connectTag failed.");
return;
}
}
try {
ndefTag.writeNdef(ndefMessage).then(() => {
console.log("ndef writeNdef Promise success.");
}).catch((err)=> {
console.log("ndef writeNdef err: " + err);
});
} catch (busiError) {
console.log("ndef writeNdef Promise catch busiError: " + busiError);
}
```
### NdefTag.writeNdef<sup>9+</sup>
writeNdef(msg: NdefMessage, callback: AsyncCallback\<number>): void
writeNdef(msg: NdefMessage, callback: AsyncCallback\<void>): void
ndef消息写入此标签,使用callback方式作为异步方法。
NDEF Message数据对象写入此标签,使用AsyncCallback方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| msg | NdefMessage | 是 | Ndef消息 |
| callback | AsyncCallback\<number> | 是 | 回调函数。 |
| msg | NdefMessage | 是 | NDEF Message数据对象。 |
| callback | AsyncCallback\<void> | 是 | 回调函数。 |
**示例:**
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly.
// ndefMessage created from raw data, such as:
let ndefMessage = ndefTag.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // change the raw data to be correct.
// or ndefMessage created from ndefTag.createNdefMessage(ndefRecords: NdefRecord[])
// connect the tag at first if not connected.
if (!ndefTag.isTagConnected()) {
if (!ndefTag.connectTag()) {
console.log("ndefTag connectTag failed.");
return;
}
});
}
try {
ndefTag.writeNdef(ndefMessage, (err)=> {
if (err) {
console.log("ndef writeNdef AsyncCallback err: " + err);
} else {
console.log("ndef writeNdef AsyncCallback success.");
}
});
} catch (busiError) {
console.log("ndef writeNdef AsyncCallback catch busiError: " + busiError);
}
```
### NdefTag.canSetReadOnly<sup>9+</sup>
......@@ -737,659 +978,874 @@ canSetReadOnly(): boolean
**系统能力**:SystemCapability.Communication.NFC
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| boolean| true: NDEF标签可设置为只读, false: NDEF标签不可设置为只读。 |
**示例:**
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndef' correctly.
var canSetReadOnly = ndef.canSetReadOnly();
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly.
var canSetReadOnly = ndefTag.canSetReadOnly();
console.log("ndef canSetReadOnly: " + canSetReadOnly);
```
### NdefTag.setReadOnly<sup>9+</sup>
setReadOnly(): Promise\<number>
setReadOnly(): Promise\<void>
将Ndef标签设置为只读,使用promise方式作为异步方法。
将NDEF标签设置为只读,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| Promise&lt;number&gt; | 0: 设置成功, 其它: 错误编码。 |
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'ndef' correctly.
ndef.setReadOnly()
.then((data) => {
console.log("ndef setReadOnly data: " + data);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly.
// connect the tag at first if not connected.
if (!ndefTag.isTagConnected()) {
if (!ndefTag.connectTag()) {
console.log("ndefTag connectTag failed.");
return;
}
}
try {
ndefTag.setReadOnly().then(() => {
console.log("ndef setReadOnly Promise success.");
}).catch((err)=> {
console.log("ndef setReadOnly err: " + err);
console.log("ndef setReadOnly Promise err: " + err);
});
} catch (busiError) {
console.log("ndef setReadOnly Promise catch busiError: " + busiError);
}
```
### NdefTag.setReadOnly<sup>9+</sup>
setReadOnly(callback: AsyncCallback\<number>): void
setReadOnly(callback: AsyncCallback\<void>): void
将Ndef标签设置为只读,使用callback方式作为异步方法。
将NDEF标签设置为只读,使用AsyncCallback方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| callback | AsyncCallback\<number> | 是 | 回调函数。 |
| callback | AsyncCallback\<void> | 是 | 回调函数。 |
**示例:**
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly.
// connect the tag at first if not connected.
if (!ndefTag.isTagConnected()) {
if (!ndefTag.connectTag()) {
console.log("ndefTag connectTag failed.");
return;
}
});
}
try {
ndefTag.setReadOnly((err)=> {
if (err) {
console.log("ndef setReadOnly AsyncCallback err: " + err);
} else {
console.log("ndef setReadOnly AsyncCallback success.");
}
});
} catch (busiError) {
console.log("ndef setReadOnly AsyncCallback catch busiError: " + busiError);
}
```
### NdefTag.getNdefTagTypeString<sup>9+</sup>
getNdefTagTypeString(type: [NfcForumType](js-apis-nfcTag.md#nfcforumtype9)): string
将Nfc论坛类型转换为Nfc论坛中定义的字节数组。
**需要权限**:ohos.permission.NFC_TAG
将NFC论坛类型,转换为NFC论坛中定义的字符串描述。
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| type | [NfcForumType](js-apis-nfcTag.md#nfcforumtype9) | 是 | NDEF标签类型,包括NFC FORUM TYPE 1/2/3/4等。 |
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| string | NFC论坛类型的字符串描述。|
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly.
try {
let ndefTypeString = ndefTag.getNdefTagTypeString(tag.NFC_FORUM_TYPE_1);
console.log("ndef ndefTypeString: " + ndefTypeString);
} catch (busiError) {
console.log("ndef getNdefTagTypeString catch busiError: " + busiError);
}
```
## MifareClassicTag<sup>9+</sup>
MifareClassicTag提供对MIFARE经典属性和I/O操作的访问,继承自TagSession。
MifareClassicTag提供对MIFARE Classic属性和I/O操作的访问,继承自TagSession。
TagSession是所有Nfc tag 技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)
TagSession是所有NFC Tag技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)
以下是MifareClassicTag的独有接口。
### MifareClassicTag.authenticateSector<sup>9+</sup>
authenticateSector(sectorIndex: number, key: number[], isKeyA: boolean): Promise\<boolean>
authenticateSector(sectorIndex: number, key: number[], isKeyA: boolean): Promise\<void>
使用密钥对扇区进行身份验证,只有身份验证成功的扇区可以进行操作。使用promise方式作为异步方法。
使用密钥对扇区进行身份验证,只有身份验证成功的扇区可以进行操作。使用Promise方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| sectorIndex | number | 是 | 待验证的扇区索引 |
| key | number[]| 是 | 用于身份验证的密钥(6字节) |
| sectorIndex | number | 是 | 待验证的扇区索引,从0开始。 |
| key | number[]| 是 | 用于扇区验证的密钥(6字节)。 |
| isKeyA | boolean | 是 | isKeyA标志。true 表示KeyA,false 表示KeyB。|
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| Promise\<boolean> | 身份验证结果,成功返回true,失败返回false。 |
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
// connect the tag at first if not connected.
if (!mifareClassic.isTagConnected()) {
if (!mifareClassic.connectTag()) {
console.log("mifareClassic connectTag failed.");
return;
}
}
try {
let sectorIndex = 1; // change it to be correct index.
let key = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06] // MUST be 6 bytes, change it to be correct key.
mifareClassic.authenticateSector(sectorIndex, key, true).then(() => {
console.log("mifareClassic authenticateSector Promise success.");
}).catch((err)=> {
console.log("mifareClassic authenticateSector err: " + err);
console.log("mifareClassic authenticateSector Promise err: " + err);
});
} catch (busiError) {
console.log("mifareClassic authenticateSector Promise catch busiError: " + busiError);
}
```
### MifareClassicTag.authenticateSector<sup>9+</sup>
authenticateSector(sectorIndex: number, key: number[], isKeyA: boolean, callback: AsyncCallback\<boolean>): void
authenticateSector(sectorIndex: number, key: number[], isKeyA: boolean, callback: AsyncCallback\<void>): void
使用密钥对扇区进行身份验证,只有身份验证成功的扇区可以进行操作。使用callback方式作为异步方法。
使用密钥对扇区进行身份验证,只有身份验证成功的扇区可以进行操作。使用AsyncCallback方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| sectorIndex | number | 是 | 待验证的扇区索引。 |
| key | number[]| 是 | 用于身份验证的密钥(6字节)。 |
| sectorIndex | number | 是 | 待验证的扇区索引,从0开始。 |
| key | number[]| 是 | 用于扇区验证的密钥(6字节)。 |
| isKeyA | boolean | 是 | isKeyA标志。true 表示KeyA,false 表示KeyB。|
| callback | AsyncCallback\<boolean> | 是 | 回调函数。|
| callback | AsyncCallback\<void> | 是 | 回调函数。|
**示例:**
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
// connect the tag at first if not connected.
if (!mifareClassic.isTagConnected()) {
if (!mifareClassic.connectTag()) {
console.log("mifareClassic connectTag failed.");
return;
}
});
}
try {
let sectorIndex = 1; // change it to be correct index.
let key = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06] // MUST be 6 bytes, change it to be correct key.
mifareClassic.authenticateSector(sectorIndex, key, true, (err)=> {
if (err) {
console.log("mifareClassic authenticateSector AsyncCallback err: " + err);
} else {
console.log("mifareClassic authenticateSector AsyncCallback success.");
}
});
} catch (busiError) {
console.log("mifareClassic authenticateSector AsyncCallback catch busiError: " + busiError);
}
```
### MifareClassicTag.readSingleBlock<sup>9+</sup>
readSingleBlock(blockIndex: number): Promise\<number[]>
读取标签中一个块存储的内容,一个块大小为16字节。使用promise方式作为异步方法。
读取标签中一个块存储的内容,一个块大小为16字节。使用Promise方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| blockIndex | number | 是 | 要读取的块索引。 |
| blockIndex | number | 是 | 要读取的块索引,从0开始。 |
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| Promise\<number[]> | 读取的块数据。 |
**示例:**
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
// connect the tag at first if not connected.
if (!mifareClassic.isTagConnected()) {
if (!mifareClassic.connectTag()) {
console.log("mifareClassic connectTag failed.");
return;
}
});
}
try {
let blockIndex = 1; // change it to be correct index.
mifareClassic.readSingleBlock(blockIndex).then((data) => {
console.log("mifareClassic readSingleBlock Promise data: " + data);
}).catch((err)=> {
console.log("mifareClassic readSingleBlock Promise err: " + err);
});
} catch (busiError) {
console.log("mifareClassic readSingleBlock Promise catch busiError: " + busiError);
}
```
### MifareClassicTag.readSingleBlock<sup>9+</sup>
readSingleBlock(blockIndex: number, callback: AsyncCallback\<number[]>): void
读取标签中一个块存储的内容,一个块大小为16字节。使用callback方式作为异步方法。
读取标签中一个块存储的内容,一个块大小为16字节。使用AsyncCallback方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| blockIndex | number | 是 | 要读取的块索引 |
| callback | AsyncCallback\<number[]> | 是 | 回调函数。 |
| blockIndex | number | 是 | 要读取的块索引,从0开始。 |
| callback | AsyncCallback\<number[]> | 是 | 回调函数,返回读取到的数据。 |
**示例:**
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
// connect the tag at first if not connected.
if (!mifareClassic.isTagConnected()) {
if (!mifareClassic.connectTag()) {
console.log("mifareClassic connectTag failed.");
return;
}
});
}
try {
let blockIndex = 1; // change it to be correct index.
mifareClassic.readSingleBlock(blockIndex, (err, data)=> {
if (err) {
console.log("mifareClassic readSingleBlock AsyncCallback err: " + err);
} else {
console.log("mifareClassic readSingleBlock AsyncCallback data: " + data);
}
});
} catch (busiError) {
console.log("mifareClassic readSingleBlock AsyncCallback catch busiError: " + busiError);
}
```
### MifareClassicTag.writeSingleBlock<sup>9+</sup>
writeSingleBlock(blockIndex: number, data: number[]): Promise\<number>
writeSingleBlock(blockIndex: number, data: number[]): Promise\<void>
向标签中一个块存储写入内容,一个块大小为16字节。使用promise方式作为异步方法。
向标签中一个块存储写入内容,一个块大小为16字节。使用Promise方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| blockIndex | number | 是 | 要写入的块索引。 |
| data | number[] | 是 | 要写入的数据。 |
| blockIndex | number | 是 | 要写入的块索引,从0开始。 |
| data | number[] | 是 | 要写入的数据,大小必须是16个字节。 |
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| Promise\<number> | 执行写入操作返回的错误代码。如果返回0,则表示成功。 |
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
// connect the tag at first if not connected.
if (!mifareClassic.isTagConnected()) {
if (!mifareClassic.connectTag()) {
console.log("mifareClassic connectTag failed.");
return;
}
});
}
try {
let blockIndex = 1; // change it to be correct index.
let rawData = [0x01, 0x02, ..., 0x0F, 0x10]; // MUST be 16 bytes, change it to be correct data.
mifareClassic.writeSingleBlock(blockIndex, rawData).then(() => {
console.log("mifareClassic writeSingleBlock Promise success.");
}).catch((err)=> {
console.log("mifareClassic writeSingleBlock Promise err: " + err);
});
} catch (busiError) {
console.log("mifareClassic writeSingleBlock Promise catch busiError: " + busiError);
}
```
### MifareClassicTag.writeSingleBlock<sup>9+</sup>
writeSingleBlock(blockIndex: number, data: number[], callback: AsyncCallback\<number>): void
writeSingleBlock(blockIndex: number, data: number[], callback: AsyncCallback\<void>): void
向标签中一个块存储写入内容,一个块大小为16字节。使用callback方式作为异步方法。
向标签中一个块存储写入内容,一个块大小为16字节。使用AsyncCallback方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| blockIndex | number | 是 | 要写入的块索引 |
| data | number[] | 是 | 要写入的数据 |
| callback | AsyncCallback\<number> | 是 | 回调函数。 |
| blockIndex | number | 是 | 要写入的块索引,从0开始。 |
| data | number[] | 是 | 要写入的数据,大小必须是16个字节。 |
| callback | AsyncCallback\<void> | 是 | 回调函数。 |
**示例:**
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
// connect the tag at first if not connected.
if (!mifareClassic.isTagConnected()) {
if (!mifareClassic.connectTag()) {
console.log("mifareClassic connectTag failed.");
return;
}
});
}
try {
let blockIndex = 1; // change it to be correct index.
let rawData = [0x01, 0x02, ..., 0x15, 0x16]; // MUST be 16 bytes, change it to be correct data.
mifareClassic.writeSingleBlock(blockIndex, rawData, (err)=> {
if (err) {
console.log("mifareClassic writeSingleBlock AsyncCallback err: " + err);
} else {
console.log("mifareClassic writeSingleBlock AsyncCallback success.");
}
});
} catch (busiError) {
console.log("mifareClassic writeSingleBlock AsyncCallback catch busiError: " + busiError);
}
```
### MifareClassicTag.incrementBlock<sup>9+</sup>
incrementBlock(blockIndex: number, value: number): Promise\<number>
incrementBlock(blockIndex: number, value: number): Promise\<void>
增加一块带值的区域块。使用promise方式作为异步方法。
对指定块的内容,增加指定的数值。使用Promise方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| blockIndex | number | 是 | 要增加的块索引。 |
| value | number | 是 | 要增加的块数据,非负值。 |
| blockIndex | number | 是 | 要指定增加的块索引,从0开始。 |
| value | number | 是 | 要指定增加的数据,非负数。 |
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| Promise\<number> | 执行新增操作返回的错误代码。如果返回0,则表示成功。 |
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
// connect the tag at first if not connected.
if (!mifareClassic.isTagConnected()) {
if (!mifareClassic.connectTag()) {
console.log("mifareClassic connectTag failed.");
return;
}
});
}
try {
let blockIndex = 1; // change it to be correct index.
let value = 0x20; // change it to be correct data.
mifareClassic.incrementBlock(blockIndex, value).then(() => {
console.log("mifareClassic incrementBlock Promise success.");
}).catch((err)=> {
console.log("mifareClassic incrementBlock Promise err: " + err);
});
} catch (busiError) {
console.log("mifareClassic incrementBlock Promise catch busiError: " + busiError);
}
```
### MifareClassicTag.incrementBlock<sup>9+</sup>
incrementBlock(blockIndex: number, value: number, callback: AsyncCallback\<number>): void
incrementBlock(blockIndex: number, value: number, callback: AsyncCallback\<void>): void
增加一块带值的区域块。使用callback方式作为异步方法。
对指定块的内容,增加指定的数值。使用AsyncCallback方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| blockIndex | number | 是 | 要增加的块索引。 |
| value | number | 是 | 要增加的块数据,非负值。 |
| callback | AsyncCallback\<number> | 是 | 回调函数。 |
| blockIndex | number | 是 | 要被运算的块索引,从0开始。 |
| value | number | 是 | 要增加的数值,非负数。 |
| callback | AsyncCallback\<void> | 是 | 回调函数。 |
**示例:**
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
// connect the tag at first if not connected.
if (!mifareClassic.isTagConnected()) {
if (!mifareClassic.connectTag()) {
console.log("mifareClassic connectTag failed.");
return;
}
});
}
try {
let blockIndex = 1; // change it to be correct index.
let value = 0x20; // change it to be correct data.
mifareClassic.incrementBlock(blockIndex, value, (err)=> {
if (err) {
console.log("mifareClassic incrementBlock AsyncCallback err: " + err);
} else {
console.log("mifareClassic incrementBlock AsyncCallback success.");
}
});
} catch (busiError) {
console.log("mifareClassic incrementBlock AsyncCallback catch busiError: " + busiError);
}
```
### MifareClassicTag.decrementBlock<sup>9+</sup>
decrementBlock(blockIndex: number, value: number): Promise\<number>
decrementBlock(blockIndex: number, value: number): Promise\<void>
递减一块带值的区域块。使用promise方式作为异步方法。
对指定块的内容,减少指定的数值。使用Promise方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| blockIndex | number | 是 | 要递减的块索引。 |
| value | number | 是 | 要递减的块数据,非负值。 |
| blockIndex | number | 是 | 要被运算的块索引,从0开始。 |
| value | number | 是 | 要减少的数值,非负数。 |
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| Promise\<number> | 执行递减操作返回的错误代码。如果返回0,则表示成功。 |
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
// connect the tag at first if not connected.
if (!mifareClassic.isTagConnected()) {
if (!mifareClassic.connectTag()) {
console.log("mifareClassic connectTag failed.");
return;
}
});
}
try {
let blockIndex = 1; // change it to be correct index.
let value = 0x20; // change it to be correct data.
mifareClassic.decrementBlock(blockIndex, value).then(() => {
console.log("mifareClassic decrementBlock Promise success.");
}).catch((err)=> {
console.log("mifareClassic decrementBlock Promise err: " + err);
});
} catch (busiError) {
console.log("mifareClassic decrementBlock Promise catch busiError: " + busiError);
}
```
### MifareClassicTag.decrementBlock<sup>9+</sup>
decrementBlock(blockIndex: number, value: number, callback: AsyncCallback\<number>): void
decrementBlock(blockIndex: number, value: number, callback: AsyncCallback\<void>): void
递减一块带值的区域块。使用callback方式作为异步方法。
对指定块的内容,减少指定的数值。使用AsyncCallback方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| blockIndex | number | 是 | 要递减的块索引。 |
| value | number | 是 | 要递减的块数据,非负值。 |
| callback | AsyncCallback\<number> | 是 | 回调函数。 |
| blockIndex | number | 是 | 要被运算的块索引,从0开始。 |
| value | number | 是 | 要减少的数值,非负数。 |
| callback | AsyncCallback\<void> | 是 | 回调函数。 |
**示例:**
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
// connect the tag at first if not connected.
if (!mifareClassic.isTagConnected()) {
if (!mifareClassic.connectTag()) {
console.log("mifareClassic connectTag failed.");
return;
}
});
}
try {
let blockIndex = 1; // change it to be correct index.
let value = 0x20; // change it to be correct data.
mifareClassic.decrementBlock(blockIndex, value, (err)=> {
if (err) {
console.log("mifareClassic decrementBlock AsyncCallback err: " + err);
} else {
console.log("mifareClassic decrementBlock AsyncCallback success.");
}
});
} catch (busiError) {
console.log("mifareClassic decrementBlock AsyncCallback catch busiError: " + busiError);
}
```
### MifareClassicTag.transferToBlock<sup>9+</sup>
transferToBlock(blockIndex: number): Promise\<number>
transferToBlock(blockIndex: number): Promise\<void>
寄存器的值复制到块。使用promise方式作为异步方法。
临时寄存器的值转移到指定的块。使用Promise方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| blockIndex | number | 是 | 复制的目的块索引。 |
| blockIndex | number | 是 | 被操作的块的索引,从0开始。 |
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| Promise\<number> | 执行复制操作返回的错误代码。如果返回0,表示成功;否则返回错误码。|
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
// connect the tag at first if not connected.
if (!mifareClassic.isTagConnected()) {
if (!mifareClassic.connectTag()) {
console.log("mifareClassic connectTag failed.");
return;
}
});
}
try {
let blockIndex = 1; // change it to be correct index.
mifareClassic.transferToBlock(blockIndex).then(() => {
console.log("mifareClassic transferToBlock Promise success.");
}).catch((err)=> {
console.log("mifareClassic transferToBlock Promise err: " + err);
});
} catch (busiError) {
console.log("mifareClassic transferToBlock Promise catch busiError: " + busiError);
}
```
### MifareClassicTag.transferToBlock<sup>9+</sup>
transferToBlock(blockIndex: number, callback: AsyncCallback\<number>): void
transferToBlock(blockIndex: number, callback: AsyncCallback\<void>): void
寄存器的值复制到块。使用callback方式作为异步方法。
临时寄存器的值转移到指定的块。使用AsyncCallback方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| blockIndex | number | 是 | 复制的目的块索引 |
| callback | AsyncCallback\<number> | 是 | 回调函数。 |
| blockIndex | number | 是 | 被操作的块的索引,从0开始。 |
| callback | AsyncCallback\<void> | 是 | 回调函数。 |
**示例:**
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
// connect the tag at first if not connected.
if (!mifareClassic.isTagConnected()) {
if (!mifareClassic.connectTag()) {
console.log("mifareClassic connectTag failed.");
return;
}
});
}
try {
let blockIndex = 1; // change it to be correct index.
mifareClassic.transferToBlock(blockIndex, (err)=> {
if (err) {
console.log("mifareClassic transferToBlock AsyncCallback err: " + err);
} else {
console.log("mifareClassic transferToBlock AsyncCallback success.");
}
});
} catch (busiError) {
console.log("mifareClassic transferToBlock AsyncCallback catch busiError: " + busiError);
}
```
### MifareClassicTag.restoreFromBlock<sup>9+</sup>
restoreFromBlock(blockIndex: number): Promise\<number>
restoreFromBlock(blockIndex: number): Promise\<void>
块的值复制到寄存器。使用promise方式作为异步方法。
指定块的值复制到临时寄存器。使用Promise方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| blockIndex | number | 是 | 复制的源块索引。|
**返回值:**
| blockIndex | number | 是 | 被操作的块的索引,从0开始。|
| **类型** | **说明** |
| ------------------ | --------------------------|
| Promise\<number> | 执行复制操作返回的错误代码。如果返回0,表示成功;否则返回错误码。|
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
// connect the tag at first if not connected.
if (!mifareClassic.isTagConnected()) {
if (!mifareClassic.connectTag()) {
console.log("mifareClassic connectTag failed.");
return;
}
}
try {
let blockIndex = 1; // change it to be correct index.
mifareClassic.restoreFromBlock(blockIndex).then(() => {
console.log("mifareClassic restoreFromBlock Promise success.");
}).catch((err)=> {
console.log("mifareClassic isExtendrestoreFromBlockedApduSupported err: " + err);
console.log("mifareClassic restoreFromBlock Promise err: " + err);
});
} catch (busiError) {
console.log("mifareClassic restoreFromBlock Promise catch busiError: " + busiError);
}
```
### MifareClassicTag.restoreFromBlock<sup>9+</sup>
restoreFromBlock(blockIndex: number, callback: AsyncCallback\<number>): void
restoreFromBlock(blockIndex: number, callback: AsyncCallback\<void>): void
块的值复制到寄存器。使用callback方式作为异步方法。
指定块的值复制到临时寄存器。使用AsyncCallback方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| blockIndex | number | 是 | 复制的源块索引 |
| callback | AsyncCallback\<number> | 是 | 回调函数。|
| blockIndex | number | 是 | 被操作的块的索引,从0开始。 |
| callback | AsyncCallback\<void> | 是 | 回调函数。|
**示例:**
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
// connect the tag at first if not connected.
if (!mifareClassic.isTagConnected()) {
if (!mifareClassic.connectTag()) {
console.log("mifareClassic connectTag failed.");
return;
}
});
}
try {
let blockIndex = 1; // change it to be correct index.
mifareClassic.restoreFromBlock(blockIndex, (err)=> {
if (err) {
console.log("mifareClassic restoreFromBlock AsyncCallback err: " + err);
} else {
console.log("mifareClassic restoreFromBlock AsyncCallback success.");
}
});
} catch (busiError) {
console.log("mifareClassic restoreFromBlock AsyncCallback catch busiError: " + busiError);
}
```
### MifareClassicTag.getSectorCount<sup>9+</sup>
getSectorCount(): number
获取mifare classic标签中的扇区数。
**需要权限**:ohos.permission.NFC_TAG
获取MIFARE Classic标签中的扇区数。
**系统能力**:SystemCapability.Communication.NFC
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| number | 扇区数量。|
| number | 标签中的扇区数量。|
**示例:**
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareClassic' correctly.
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
let sectorCount = mifareClassic.getSectorCount();
console.log("mifareClassic sectorCount: " + sectorCount);
```
......@@ -1398,56 +1854,53 @@ console.log("mifareClassic sectorCount: " + sectorCount);
getBlockCountInSector(sectorIndex: number): number
获取扇区中的块数。
**需要权限**:ohos.permission.NFC_TAG
获取指定扇区中的块数。
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| sectorIndex | number | 是 | 扇区序号。|
| sectorIndex | number | 是 | 扇区序号,从0开始。|
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| number | 该扇区内的块数量。|
**示例:**
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareClassic' correctly.
let blockCountInSector = mifareClassic.getBlockCountInSector();
console.log("mifareClassic blockCountInSector: " + blockCountInSector);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
try {
let sectorIndex = 1; // change it to be correct index.
let blockCnt = mifareClassic.getBlockCountInSector(sectorIndex);
console.log("mifareClassic blockCnt: " + blockCnt);
} catch (busiError) {
console.log("mifareClassic getBlockCountInSector catch busiError: " + busiError);
}
```
### MifareClassicTag.getType<sup>9+</sup>
getType(): [MifareClassicType](js-apis-nfcTag.md#mifareclassictype9)
获取MifareClassic标签的类型。
**需要权限**:ohos.permission.NFC_TAG
获取MIFARE Classic标签的类型。
**系统能力**:SystemCapability.Communication.NFC
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| [MifareClassicType](js-apis-nfcTag.md#mifareclassictype9) | MifareClassic标签的类型。|
**示例:**
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareClassic' correctly.
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
let getType = mifareClassic.getType();
console.log("mifareClassic getType: " + getType);
```
......@@ -1456,24 +1909,20 @@ console.log("mifareClassic getType: " + getType);
getTagSize(): number
获取标签的大小(字节),具体请参见[MifareClassicSize](js-apis-nfcTag.md#mifareclassicsize9)
**需要权限**:ohos.permission.NFC_TAG
获取标签的存储空间大小,具体请参见[MifareClassicSize](js-apis-nfcTag.md#mifareclassicsize9)
**系统能力**:SystemCapability.Communication.NFC
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| number | 标签的大小,单位为字节,请参见[MifareClassicSize](js-apis-nfcTag.md#mifareclassicsize9)。|
**示例:**
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareClassic' correctly.
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
let tagSize = mifareClassic.getTagSize();
console.log("mifareClassic tagSize: " + tagSize);
```
......@@ -1482,24 +1931,20 @@ console.log("mifareClassic tagSize: " + tagSize);
isEmulatedTag(): boolean
检查标签是否已模拟。
**需要权限**:ohos.permission.NFC_TAG
检查标签是不是被模拟的。
**系统能力**:SystemCapability.Communication.NFC
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| boolean |检查结果,true: 是;false:否。 |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareClassic' correctly.
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
let isEmulatedTag = mifareClassic.isEmulatedTag();
console.log("mifareClassic isEmulatedTag: " + isEmulatedTag);
```
......@@ -1508,73 +1953,75 @@ console.log("mifareClassic isEmulatedTag: " + isEmulatedTag);
getBlockIndex(sectorIndex: number): number
获取特定扇区的第一个块。
**需要权限**:ohos.permission.NFC_TAG
获取特定扇区的第一个块的序号。
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| sectorIndex | number | 是 | 扇区序号。 |
| sectorIndex | number | 是 | 扇区序号,从0开始。 |
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| number | 该扇区内的第一个块的序列号。 |
| number | 该扇区内的第一个块的序号,从0开始。 |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
try {
let sectorIndex = 1; // change it to be correct index.
let blockIndex = mifareClassic.getBlockIndex(sectorIndex);
console.log("mifareClassic blockIndex: " + blockIndex);
} catch (busiError) {
console.log("mifareClassic getBlockIndex catch busiError: " + busiError);
}
```
### MifareClassicTag.getSectorIndex<sup>9+</sup>
getSectorIndex(blockIndex: number): number
获取扇区索引,该扇区包含特定块
获取包含指定块号的扇区序号
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| blockIndex | number | 是 | 块序号。 |
| blockIndex | number | 是 | 块序号,从0开始。 |
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| number | 扇区序号。 |
| number | 扇区序号,从0开始。 |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
try {
let blockIndex = 1; // change it to be correct index.
let sectorIndex = mifareClassic.getSectorIndex(blockIndex);
console.log("mifareClassic sectorIndex: " + sectorIndex);
} catch (busiError) {
console.log("mifareClassic getSectorIndex catch busiError: " + busiError);
}
```
## MifareUltralightTag<sup>9+</sup>
MifareUltralightTag 提供对MIFARE超轻属性和I/O操作的访问,继承自TagSession。
MifareUltralightTag 提供对MIFARE Ultralight属性和I/O操作的访问,继承自TagSession。
TagSession是所有Nfc tag 技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)
TagSession是所有NFC Tag技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)
以下是MifareUltralightTag的独有接口。
......@@ -1582,7 +2029,7 @@ TagSession是所有Nfc tag 技术类型的基类, 提供建立连接和发送
readMultiplePages(pageIndex: number): Promise\<number[]>
阅读4页,共16字节。页面大小为4字节。使用promise方式作为异步方法。
读取标签的4页数据,共16字节的数据。每个页面数据大小为4字节。使用Promise方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
......@@ -1592,298 +2039,383 @@ readMultiplePages(pageIndex: number): Promise\<number[]>
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | ------------------------------ |
| pageIndex | number | 是 | 要读取页面的索引。 |
| pageIndex | number | 是 | 要读取页面的索引,从0开始。 |
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| Promise\<number[]> | 读取的4页的数据。 |
| Promise\<number[]> | 读取的4页的数据,共16字节。 |
**示例:**
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareUltralight' correctly.
// connect the tag at first if not connected.
if (!mifareUltralight.isTagConnected()) {
if (!mifareUltralight.connectTag()) {
console.log("mifareUltralight connectTag failed.");
return;
}
}
try {
let pageIndex = 1; // change it to be correct index.
mifareUltralight.readMultiplePages(pageIndex).then((data) => {
console.log("mifareUltralight readMultiplePages Promise data = " + data);
}).catch((err)=> {
console.log("mifareUltralight readMultiplePages err: " + err);
console.log("mifareUltralight readMultiplePages Promise err: " + err);
});
} catch (busiError) {
console.log("mifareUltralight readMultiplePages Promise catch busiError: " + busiError);
}
```
### MifareUltralightTag.readMultiplePages<sup>9+</sup>
readMultiplePages(pageIndex: number, callback: AsyncCallback\<number[]>): void
阅读4页,共16字节。页面大小为4字节。使用callback方式作为异步方法。
读取标签的4页数据,共16字节的数据。每个页面数据大小为4字节。使用AsyncCallback方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| pageIndex | number | 是 | 要读取页面的索引 |
| callback | AsyncCallback\<number[]> | 是 | 回调函数。 |
| pageIndex | number | 是 | 要读取页面的索引,从0开始。 |
| callback | AsyncCallback\<number[]> | 是 | 回调函数,返回读取到的数据,共16字节。 |
**示例:**
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareUltralight' correctly.
// connect the tag at first if not connected.
if (!mifareUltralight.isTagConnected()) {
if (!mifareUltralight.connectTag()) {
console.log("mifareUltralight connectTag failed.");
return;
}
});
}
try {
let pageIndex = 1; // change it to be correct index.
mifareUltralight.readMultiplePages(pageIndex, (err, data)=> {
if (err) {
console.log("mifareUltralight readMultiplePages AsyncCallback err: " + err);
} else {
console.log("mifareUltralight readMultiplePages AsyncCallback data: " + data);
}
});
} catch (busiError) {
console.log("mifareUltralight readMultiplePages AsyncCallback catch busiError: " + busiError);
}
```
### MifareUltralightTag.writeSinglePages<sup>9+</sup>
### MifareUltralightTag.writeSinglePage<sup>9+</sup>
writeSinglePages(pageIndex: number, data: number[]): Promise\<number>
writeSinglePage(pageIndex: number, data: number[]): Promise\<void>
写入一页数据,页面大小为4字节。使用promise方式作为异步方法。
写入一页数据,数据大小为4字节。使用Promise方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| pageIndex | number | 是 | 要写入页面的索引。 |
| data | number[] | 是 | 要写入页面的数据内容。 |
**返回值:**
| pageIndex | number | 是 | 要写入页面的索引,从0开始。 |
| data | number[] | 是 | 要写入页面的数据内容,必须是4个字节大小。 |
| **类型** | **说明** |
| ------------------ | --------------------------|
| Promise\<number> | 执行写入操作返回的错误代码。如果返回0,则表示成功。 |
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareUltralight' correctly.
// connect the tag at first if not connected.
if (!mifareUltralight.isTagConnected()) {
if (!mifareUltralight.connectTag()) {
console.log("mifareUltralight connectTag failed.");
return;
}
}
try {
let pageIndex = 1; // change it to be correct index.
let rawData = [0x01, 0x02, 0x03, 0x04]; // MUST be 4 bytes, change it to be correct raw data.
mifareUltralight.writeSinglePage(pageIndex, rawData).then(() => {
console.log("mifareUltralight writeSinglePage Promise success.");
}).catch((err)=> {
console.log("mifareUltralight writeSinglePages err: " + err);
console.log("mifareUltralight writeSinglePage Promise err: " + err);
});
} catch (busiError) {
console.log("mifareUltralight writeSinglePage Promise catch busiError: " + busiError);
}
```
### MifareUltralightTag.writeSinglePages<sup>9+</sup>
### MifareUltralightTag.writeSinglePage<sup>9+</sup>
writeSinglePages(pageIndex: number, data: number[], callback: AsyncCallback\<number>): void
writeSinglePage(pageIndex: number, data: number[], callback: AsyncCallback\<void>): void
写入一页数据,页面大小为4字节。使用callback方式作为异步方法。
写入一页数据,数据大小为4字节。使用AsyncCallback方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | ------------------------ |
| pageIndex | number | 是 | 要写入页面的索引。 |
| data | number[] | 是 | 要写入页面的数据内容。 |
| callback|AsyncCallback\<number> |是| 回调函数。 |
| pageIndex | number | 是 | 要写入页面的索引,从0开始。 |
| data | number[] | 是 | 要写入页面的数据内容,必须是4个字节大小。 |
| callback|AsyncCallback\<void> |是| 回调函数。 |
**示例:**
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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, (err, data)=> {
if (err) {
console.log("mifareUltralight writeSinglePages err: " + err);
} else {
console.log("mifareUltralight writeSinglePages data: " + data);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareUltralight' correctly.
// connect the tag at first if not connected.
if (!mifareUltralight.isTagConnected()) {
if (!mifareUltralight.connectTag()) {
console.log("mifareUltralight connectTag failed.");
return;
}
});
}
try {
let pageIndex = 1; // change it to be correct index.
let rawData = [0x01, 0x02, 0x03, 0x04]; // MUST be 4 bytes, change it to be correct raw data.
mifareUltralight.writeSinglePage(pageIndex, rawData, (err)=> {
if (err) {
console.log("mifareUltralight writeSinglePage AsyncCallback err: " + err);
} else {
console.log("mifareUltralight writeSinglePage AsyncCallback success.");
}
});
} catch (busiError) {
console.log("mifareUltralight writeSinglePage AsyncCallback catch busiError: " + busiError);
}
```
### MifareUltralightTag.getType<sup>9+</sup>
getType(): MifareUltralightType
获取MifareUltralight标签的类型,以字节形式返回,具体请参见 [MifareUltralightType](js-apis-nfcTag.md#mifareultralighttype9)
**需要权限**:ohos.permission.NFC_TAG
获取MIFARE Ultralight标签的类型,具体请参见 [MifareUltralightType](js-apis-nfcTag.md#mifareultralighttype9)
**系统能力**:SystemCapability.Communication.NFC
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| MifareUltralightType | MifareUltralight标签的类型, 具体请参见 [MifareUltralightType](js-apis-nfcTag.md#mifareultralighttype9)。|
| MifareUltralightType | MIFARE Ultralight标签的类型,具体请参见 [MifareUltralightType](js-apis-nfcTag.md#mifareultralighttype9)。|
**示例:**
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag', has obtained the 'mifareUltralight' correctly.
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareUltralight' correctly.
let getType = mifareClassic.getType();
console.log("mifareUltralight getType: " + getType);
```
## NdefFormatableTag<sup>9+</sup>
NdefFormatableTag为NDEF formattable的标签提供格式化操作,继承自TagSession。
NdefFormatableTag为NDEF Formattable的标签提供格式化操作,继承自TagSession。
TagSession是所有Nfc tag 技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)
TagSession是所有NFC Tag 技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)
以下是NdefFormatableTag的独有接口。
### NdefFormatableTag.format<sup>9+</sup>
format(message: [NdefMessage](#ndefmessage9)): Promise\<number>
format(message: [NdefMessage](#ndefmessage9)): Promise\<void>
将标签格式化为NDEF标签,然后将NDEF消息写入NDEF标签。使用promise方式作为异步方法。
将标签格式化为NDEF标签,将NDEF消息写入NDEF标签。使用Promise方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| message | [NdefMessage](#ndefmessage9) | 是 | 格式化成功时要写入的Ndef消息。可以为null,为null时仅格式化标签,不写入内容。 |
| message | [NdefMessage](#ndefmessage9) | 是 | 格式化成功时要写入的NDEF消息。可以为null,为null时仅格式化标签,不写入内容。 |
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| Promise\<number> | 执行操作后返回的错误代码。如果返回0,则表示成功。 |
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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.md', obtains the 'ndefFormatable' correctly.
// 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);
// connect the tag at first if not connected.
if (!ndefFormatable.isTagConnected()) {
if (!ndefFormatable.connectTag()) {
console.log("ndefFormatable connectTag failed.");
return;
}
});
}
try {
// ndefMessage created from raw data, such as:
let ndefMessage = ndefTag.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // change the raw data to be correct.
// or ndefMessage created from ndefTag.createNdefMessage(ndefRecords: NdefRecord[])
ndefFormatable.format(ndefMessage).then(() => {
console.log("ndefFormatable format Promise success.");
}).catch((err)=> {
console.log("ndefFormatable format Promise err: " + err);
});
} catch (busiError) {
console.log("ndefFormatable format Promise catch busiError: " + busiError);
}
```
### NdefFormatableTag.format<sup>9+</sup>
format(message: [NdefMessage](#ndefmessage9), callback: AsyncCallback\<number>): void
format(message: [NdefMessage](#ndefmessage9), callback: AsyncCallback\<void>): void
将标签格式化为NDEF标签,然后将NDEF消息写入NDEF标签。使用callback方式作为异步方法。
将标签格式化为NDEF标签,然后将NDEF消息写入NDEF标签。使用AsyncCallback方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| message | [NdefMessage](#ndefmessage9) | 是 | 格式化成功时要写入的Ndef消息。可以为null,为null时仅格式化标签,不写入内容。 |
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| callback: AsyncCallback\<number> | 回调函数。 |
| callback: AsyncCallback\<void> | 回调函数。 |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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.md', obtains the 'ndefFormatable' correctly.
// 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);
// connect the tag at first if not connected.
if (!ndefFormatable.isTagConnected()) {
if (!ndefFormatable.connectTag()) {
console.log("ndefFormatable connectTag failed.");
return;
}
});
}
try {
// ndefMessage created from raw data, such as:
let ndefMessage = ndefTag.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // change the raw data to be correct.
// or ndefMessage created from ndefTag.createNdefMessage(ndefRecords: NdefRecord[])
ndefFormatable.format(ndefMessage, (err)=> {
if (err) {
console.log("ndefFormatable format AsyncCallback err: " + err);
} else {
console.log("ndefFormatable format AsyncCallback success.");
}
});
} catch (busiError) {
console.log("ndefFormatable format AsyncCallback catch busiError: " + busiError);
}
```
### NdefFormatableTag.formatReadOnly<sup>9+</sup>
formatReadOnly(message: [NdefMessage](#ndefmessage9)): Promise\<number>
formatReadOnly(message: [NdefMessage](#ndefmessage9)): Promise\<void>
将标签格式化为NDEF标签,然后将NDEF消息写入NDEF标签,之后将标签设置为只读。使用promise方式作为异步方法。
将标签格式化为NDEF标签,将NDEF消息写入NDEF标签,之后将标签设置为只读。使用Promise方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| message | [NdefMessage](#ndefmessage9) | 是 | 格式化成功时要写入的Ndef消息。可以为null,为null时仅格式化标签,不写入内容。 |
**返回值:**
| message | [NdefMessage](#ndefmessage9) | 是 | 格式化成功时要写入的NDEF消息。可以为null,为null时仅格式化标签,不写入内容。 |
| **类型** | **说明** |
| ------------------ | --------------------------|
| Promise\<number> | 执行操作后返回的错误代码。如果返回0,则表示成功。 |
**错误码:**
以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)
| 错误码ID | 错误信息|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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.md', obtains the 'ndefFormatable' correctly.
// 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);
// connect the tag at first if not connected.
if (!ndefFormatable.isTagConnected()) {
if (!ndefFormatable.connectTag()) {
console.log("ndefFormatable connectTag failed.");
return;
}
});
}
try {
// ndefMessage created from raw data, such as:
let ndefMessage = ndefTag.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // change the raw data to be correct.
// or ndefMessage created from ndefTag.createNdefMessage(ndefRecords: NdefRecord[])
ndefFormatable.formatReadOnly(ndefMessage).then(() => {
console.log("ndefFormatable formatReadOnly Promise success.");
}).catch((err)=> {
console.log("ndefFormatable formatReadOnly Promise err: " + err);
});
} catch (busiError) {
console.log("ndefFormatable formatReadOnly Promise catch busiError: " + busiError);
}
```
### NdefFormatableTag.formatReadOnly<sup>9+</sup>
formatReadOnly(message: [NdefMessage](#ndefmessage9), callback: AsyncCallback\<number>): void
formatReadOnly(message: [NdefMessage](#ndefmessage9), callback: AsyncCallback\<void>): void
将标签格式化为NDEF标签,然后将NDEF消息写入NDEF标签,之后将标签设置为只读。使用callback方式作为异步方法。
......@@ -1892,33 +2424,43 @@ formatReadOnly(message: [NdefMessage](#ndefmessage9), callback: AsyncCallback\<n
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| message | [NdefMessage](#ndefmessage9) | 是 | 格式化成功时要写入的Ndef消息。可以为null,为null时仅格式化标签,不写入内容。|
| message | [NdefMessage](#ndefmessage9) | 是 | 格式化成功时要写入的NDEF消息。可以为null,为null时仅格式化标签,不写入内容。|
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| callback: AsyncCallback\<number> | 回调函数。 |
| callback: AsyncCallback\<void> | 回调函数。 |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// 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.md', obtains the 'ndefFormatable' correctly.
// 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);
// connect the tag at first if not connected.
if (!ndefFormatable.isTagConnected()) {
if (!ndefFormatable.connectTag()) {
console.log("ndefFormatable connectTag failed.");
return;
}
});
}
try {
// ndefMessage created from raw data, such as:
let ndefMessage = ndefTag.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // change the raw data to be correct.
// or ndefMessage created from ndefTag.createNdefMessage(ndefRecords: NdefRecord[])
ndefFormatable.formatReadOnly(ndefMessage, (err)=> {
if (err) {
console.log("ndefFormatable formatReadOnly AsyncCallback err: " + err);
} else {
console.log("ndefFormatable formatReadOnly AsyncCallback success.");
}
});
} catch (busiError) {
console.log("ndefFormatable formatReadOnly AsyncCallback catch busiError: " + busiError);
}
```
......@@ -13,64 +13,82 @@ import tag from '@ohos.nfc.tag';
## tagSession
tagSession是所有[Nfc tag 技术类型](js-apis-nfctech.md)的基类, 提供建立连接和发送数据等共同接口。
tagSession是所有[NFC Tag技术类型](js-apis-nfctech.md)的基类, 提供建立连接和发送数据等共同接口。
需要通过其子类来访问以下接口。在下面的示例中 统一用 getXXTag表示获取子类实例的方法。
需要通过其子类来访问以下接口。在下面的示例中 统一用 getXXX()表示获取子类实例的方法。
具体使用时,请根据实际采用的Nfc tag技术,选择对应的方法,具体请参见[nfcTag](js-apis-nfcTag.md)文档。
### tagSession.connectTag
connectTag(): boolean;
### tagSession.getTagInfo
和标签建立连接;
getTagInfo(): tag.TagInfo
在从标签读取数据或将数据写入标签之前,必须调用此方法
获取该Tag被分发时,NFC服务所提供的Tag数据对象
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC.Core
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| boolean | 连接建立成功返回 true,失败返回false。 |
| TagInfo | NFC服务所提供的Tag数据对象。 |
**示例:**
```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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
// the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
let tagInfo = tag.getXXX(tagInfo).getTagInfo();
console.log("tag tagInfo: " + tagInfo);
```
### tagSession.reset()
### tagSession.connectTag
reset(): void
connectTag(): boolean;
重置与标签的连接,并恢复将数据写入标签的默认超时时间
和标签建立连接。在从标签读取数据或将数据写入标签之前,必须调用此方法
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC.Core
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| boolean | 方法执行成功返回 true,失败返回false。 |
| boolean | 连接建立成功返回true,失败返回false。 |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
// the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
let connectStatus = tag.getXXX(tagInfo).connectTag();
console.log("connectStatus: " + connectStatus);
```
### tagSession.reset()
reset(): void
重置与标签的连接。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC.Core
**示例:**
```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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
// the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
tag.getXXX(tagInfo).reset();
```
### tagSession.isTagConnected
......@@ -84,19 +102,19 @@ isTagConnected(): boolean
**系统能力**: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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
// the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
let isTagConnected = tag.getXXX(tagInfo).isTagConnected();
console.log("isTagConnected: " + isTagConnected);
```
### tagSession.getMaxSendLength
......@@ -110,17 +128,160 @@ getMaxSendLength(): number
**系统能力**:SystemCapability.Communication.NFC.Core
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| number | 可以发送到标签的最大数据长度,非负数。 |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
// the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
let maxSendLen = tag.getXXX(tagInfo).getMaxSendLength();
console.log("tag maxSendLen: " + maxSendLen);
```
### tagSession.getSendDataTimeout
getSendDataTimeout(): number
查询发送数据到Tag的等待超时时间,单位是毫秒。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC.Core
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| number | 可以发送到标签的最大数据长度。 |
| number | 发送数据到Tag的等待超时时间,单位是毫秒,非负数。 |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
// the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
let sendDataTimeout = tag.getXXX(tagInfo).getSendDataTimeout();
console.log("tag sendDataTimeout: " + sendDataTimeout);
```
### tagSession.setSendDataTimeout
setSendDataTimeout(timeout: number): boolean
查询发送数据到Tag的等待超时时间,单位是毫秒。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| timeout | number | 是 | 超时时间,单位毫秒,非负值。 |
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| boolean | 设置超时时间成功返回true,设置失败返回false。 |
**示例:**
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
// the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
let timeoutMs = 700; // change it to be correct.
let setStatus = tag.getXXX(tagInfo).setSendDataTimeout(timeoutMs);
console.log("tag setSendDataTimeout setStatus: " + setStatus);
```
### tagSession.sendData
sendData(data: number[]): Promise<number[]>
发送指令到Tag上,使用Promise方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| data | number[] | 是 | 要发送的指令。每个number十六进制表示,范围是0x00~0xFF。 |
**返回值:**
| **类型** | **说明** |
| ------------------ | --------------------------|
| Promise<number[]> | 对端Tag对指令的响应数据。每个number十六进制表示,范围是0x00~0xFF。|
**示例:**
```js
import tag from '@ohos.nfc.tag';
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
// the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
// connect the tag at first if not connected.
if (!tag.getXXX(tagInfo).isTagConnected()) {
if (!tag.getXXX(tagInfo).connectTag()) {
console.log("tagSession connectTag failed.");
return;
}
}
let cmdData = [0x01, 0x02, ...]; // change the raw data to be correct.
tag.getXXX(tagInfo).sendData(cmdData).then((response) => {
console.log("tagSession sendData Promise response: " + response);
}).catch((err)=> {
console.log("tagSession sendData Promise err: " + err);
});
```
### tagSession.sendData
sendData(data: number[], callback: AsyncCallback<number[]>): void
发送指令到Tag上,使用AsyncCallback方式作为异步方法。
**需要权限**:ohos.permission.NFC_TAG
**系统能力**:SystemCapability.Communication.NFC
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ----------------------- | ---- | -------------------------------------- |
| data | number[] | 是 | 要发送的指令。每个number十六进制表示,范围是0x00~0xFF。 |
| callback | AsyncCallback<number[]> | 是 | 回调函数,返回响应数据。每个number十六进制表示,范围是0x00~0xFF。 |
**示例:**
```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);
// see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
// the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
// connect the tag at first if not connected.
if (!tag.getXXX(tagInfo).isTagConnected()) {
if (!tag.getXXX(tagInfo).connectTag()) {
console.log("tagSession connectTag failed.");
return;
}
}
let cmdData = [0x01, 0x02, ...]; // change the raw data to be correct.
tag.getXXX(tagInfo).sendData(cmdData, (err, response)=> {
if (err) {
console.log("tagSession sendData AsyncCallback err: " + err);
} else {
console.log("tagSession sendData AsyncCallback response: " + response);
}
});
```
# NFC错误码
## 3100101
**错误信息**
NFC opening or closing state is abnormal in service.
**错误描述**
NFC服务内部执行NFC打开或关闭异常。
**可能原因**
和NFC服务建立通信异常。
**处理步骤**
重新执行打开或关闭NFC。
## 3100201
**错误信息**
Tag running state is abnormal in service.
**错误描述**
NFC服务执行Tag业务逻辑遇到错误。
**可能原因**
1. Tag参数值和实际调用函数要求不匹配。
2. Tag操作时,NFC状态是关闭的。
3. Tag操作前,已经处在断开状态。
4. Tag芯片返回错误状态或响应超时。
5. 和NFC服务没有建立绑定关系,无法调用接口。
**处理步骤**
1. 检查NFC参数是否和所调用接口匹配。
2. 打开设备NFC。
3. 先调用连接,再执行读写操作。
4. 重新触碰读取卡片。
5. 退出应用后,重新读取卡片。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册