1# nfctech (标准NFC-Tag Nfc 技术) 2 3本模块主要用于采用不同Nfc技术的Tag的读写操作。 4 5> **说明:** 6> 7> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9## 导入模块 10 11```js 12import tag from '@ohos.nfc.tag'; 13``` 14 15## NfcATag 16 17NfcATag 提供 NFC-A(ISO 14443-3A)技术的属性和I/O操作的访问,继承自[TagSession](js-apis-tagSession.md)。 18 19TagSession是所有NFC Tag技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)。 20 21以下是NfcATag的独有接口。 22 23### NfcATag.getSak 24 25getSak(): number 26 27获取NFC-A标签的SAK值。 28 29**系统能力:** SystemCapability.Communication.NFC.Tag 30 31**返回值:** 32 33| **类型** | **说明** | 34| ------------------ | --------------------------| 35| number | NfcA 标签的SAK值,十六进制表示,范围是0x00~0xFF。 | 36 37**示例:** 38 39```js 40import tag from '@ohos.nfc.tag'; 41 42// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'nfcA' correctly. 43let sak : number = nfcA.getSak(); 44console.log("nfcA sak: " + sak); 45``` 46 47### NfcATag.getAtqa 48 49getAtqa(): number[] 50 51获取NFC-A标签的Atqa值。 52 53**系统能力:** SystemCapability.Communication.NFC.Tag 54 55**返回值:** 56 57| **类型** | **说明** | 58| ------------------ | --------------------------| 59| number[] | NfcA 标签的Atqa值,每个number十六进制表示,范围是0x00~0xFF。 | 60 61**示例:** 62 63```js 64import tag from '@ohos.nfc.tag'; 65 66// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'nfcA' correctly. 67let atqa : number[] = nfcA.getAtqa(); 68console.log("nfcA atqa: " + atqa); 69``` 70 71## NfcBTag 72 73NfcBTag 提供对NFC-B(ISO 14443-3B)技术的属性和I/O操作的访问,继承自TagSession。 74 75TagSession是所有NFC Tag技术类型的基类,提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)。 76 77以下是NfcBTag的独有接口。 78 79### NfcBTag.getRespAppData 80 81getRespAppData(): number[] 82 83获取标签的应用程序数据。 84 85**系统能力:** SystemCapability.Communication.NFC.Tag 86 87**返回值:** 88 89| **类型** | **说明** | 90| ------------------ | --------------------------| 91| number[] | NfcB 标签的应用程序数据,每个number十六进制表示,范围是0x00~0xFF。 | 92 93**示例:** 94 95```js 96import tag from '@ohos.nfc.tag'; 97 98// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'nfcB' correctly. 99let respAppData : number[] = nfcB.getRespAppData(); 100console.log("nfcB respAppData: " + respAppData); 101``` 102 103### NfcBTag.getRespProtocol 104 105getRespProtocol(): number[] 106 107获取标签的协议信息。 108 109**系统能力:** SystemCapability.Communication.NFC.Tag 110 111**返回值:** 112 113| **类型** | **说明** | 114| ------------------ | --------------------------| 115| number[] | NfcB 标签的协议信息,每个number十六进制表示,范围是0x00~0xFF。| 116 117**示例:** 118 119```js 120import tag from '@ohos.nfc.tag'; 121 122// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'nfcB' correctly. 123let respProtocol : number[] = nfcB.getRespProtocol(); 124console.log("nfcB respProtocol: " + respProtocol); 125``` 126 127## NfcFTag 128 129NfcFTag 提供对NFC-F(JIS 6319-4)技术的属性和I/O操作的访问,继承自TagSession。 130 131TagSession是所有NFC Tag技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)。 132 133以下是NfcFTag的独有接口。 134 135### NfcFTag.getSystemCode 136 137getSystemCode(): number[] 138 139从标签实例获取系统代码。 140 141**系统能力:** SystemCapability.Communication.NFC.Tag 142 143**返回值:** 144 145| **类型** | **说明** | 146| ------------------ | --------------------------| 147| number[] | NfcF 标签的系统代码,每个number十六进制表示,范围是0x00~0xFF。| 148 149**示例:** 150 151```js 152import tag from '@ohos.nfc.tag'; 153 154// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'nfcF' correctly. 155let systemCode : number[] = nfcF.getSystemCode(); 156console.log("nfcF systemCode: " + systemCode); 157``` 158 159### NfcFTag.getPmm 160 161getPmm(): number[] 162 163从标签实例获取PMm(由IC代码和制造商参数组成)。 164 165**系统能力:** SystemCapability.Communication.NFC.Tag 166 167**返回值:** 168 169| **类型** | **说明** | 170| ------------------ | --------------------------| 171| number[] | NfcF 标签的PMm信息,每个number十六进制表示,范围是0x00~0xFF。| 172 173**示例:** 174 175```js 176import tag from '@ohos.nfc.tag'; 177 178// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'nfcF' correctly. 179let pmm : number[] = nfcF.getPmm(); 180console.log("nfcF pmm: " + pmm); 181``` 182 183## NfcVTag 184 185NfcVTag 提供对NFC-V(ISO 15693)技术的属性和I/O操作的访问,继承自TagSession。 186 187TagSession是所有NFC Tag技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)。 188 189以下是NfcVTag的独有接口。 190 191### NfcvTag.getResponseFlags 192 193getResponseFlags(): number 194 195从标签实例实例获取响应标志。 196 197**系统能力:** SystemCapability.Communication.NFC.Tag 198 199**返回值:** 200 201| **类型** | **说明** | 202| ------------------ | --------------------------| 203| number | NfcV 标签的响应标志,十六进制表示,范围是0x00~0xFF。| 204 205**示例:** 206 207```js 208import tag from '@ohos.nfc.tag'; 209 210// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'nfcV' correctly. 211let responseFlags : number = nfcV.getResponseFlags(); 212console.log("nfcV responseFlags: " + responseFlags); 213``` 214 215### NfcvTag.getDsfId 216 217getDsfId(): number 218 219从标签实例实例获取数据存储格式标识符(DSFID)。 220 221**系统能力:** SystemCapability.Communication.NFC.Tag 222 223**返回值:** 224 225| **类型** | **说明** | 226| ------------------ | --------------------------| 227| number | NfcV 标签的数据存储格式标识符,十六进制表示,范围是0x00~0xFF。| 228 229**示例:** 230 231```js 232import tag from '@ohos.nfc.tag'; 233 234// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'nfcV' correctly. 235let dsfId : number = nfcV.getDsfId(); 236console.log("nfcV dsfId: " + dsfId); 237``` 238 239## IsoDepTag<sup>9+</sup> 240 241IsoDepTag 提供对ISO-DEP(ISO 14443-4)技术的属性和I/O操作的访问,继承自TagSession。 242 243TagSession是所有NFC Tag技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)。 244 245以下是IsoDepTag的独有接口。 246 247### IsoDepTag.getHistoricalBytes<sup>9+</sup> 248 249getHistoricalBytes(): number[] 250 251获取标签的历史字节,针对基于NfcA通信技术的IsoDep卡片。 252 253**系统能力:** SystemCapability.Communication.NFC.Tag 254 255**返回值:** 256 257| **类型** | **说明** | 258| ------------------ | --------------------------| 259| number[] | IsoDepTag 标签的历史字节,每个number十六进制表示,范围是0x00~0xFF。如果该IsoDep类型Tag是基于NfcB技术的,则该返回值为空。| 260 261**示例:** 262 263```js 264import tag from '@ohos.nfc.tag'; 265 266// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'isoDep' correctly. 267let historicalBytes : number[] = isoDep.getHistoricalBytes(); 268console.log("isoDep historicalBytes: " + historicalBytes); 269``` 270 271### IsoDepTag.getHiLayerResponse<sup>9+</sup> 272 273getHiLayerResponse(): number[] 274 275获取标签的更高层响应字节,针对基于NfcB通信技术的IsoDep卡片。 276 277**系统能力:** SystemCapability.Communication.NFC.Tag 278 279**返回值:** 280 281| **类型** | **说明** | 282| ------------------ | --------------------------| 283| number[] | IsoDepTag 标签的更高层响应字节,每个number十六进制表示,范围是0x00~0xFF。如果该IsoDep类型Tag是基于NfcA技术的,则该返回值为空。| 284 285**示例:** 286 287```js 288import tag from '@ohos.nfc.tag'; 289 290// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'isoDep' correctly. 291let hiLayerResponse : number[] = isoDep.getHiLayerResponse(); 292console.log("isoDep hiLayerResponse: " + hiLayerResponse); 293``` 294 295### IsoDepTag.isExtendedApduSupported<sup>9+</sup> 296 297isExtendedApduSupported(): Promise<boolean> 298 299检查是否支持扩展的APDU,使用Promise方式作为异步方法。 300 301**需要权限:** ohos.permission.NFC_TAG 302 303**系统能力:** SystemCapability.Communication.NFC.Tag 304 305**返回值:** 306 307| **类型** | **说明** | 308| ------------------ | --------------------------| 309| Promise<boolean> | 以Promise形式返回检查结果,true: 支持, false: 不支持。| 310 311**错误码:** 312 313以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 314 315| 错误码ID | 错误信息| 316| ------- | -------| 317| 3100201 | Tag running state is abnormal in service. | 318 319**示例:** 320 321```js 322import tag from '@ohos.nfc.tag'; 323import {BusinessError} from '@ohos.base'; 324 325// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'isoDep' correctly. 326 327// connect the tag at first if not connected. 328if (!isoDep.isTagConnected()) { 329 if (!isoDep.connectTag()) { 330 console.log("isoDep connectTag failed."); 331 return; 332 } 333} 334 335try { 336 isoDep.isExtendedApduSupported().then((response: boolean) => { 337 console.log("isoDep isExtendedApduSupported Promise response: " + response); 338 }).catch((err: BusinessError) => { 339 console.error("isoDep isExtendedApduSupported Promise Code: ${err.code}, message: ${err.message}"); 340 }); 341} catch (busiError) { 342 console.error("isoDep isExtendedApduSupported Promise Code: ${(busiError as Businsess).code}, " + 343 "message: ${(busiError as Businsess).message}"); 344} 345``` 346 347### IsoDepTag.isExtendedApduSupported<sup>9+</sup> 348 349isExtendedApduSupported(callback: AsyncCallback\<boolean>): void 350 351检查是否支持扩展的APDU,使用AsyncCallback方式作为异步方法。 352 353**需要权限:** ohos.permission.NFC_TAG 354 355**系统能力:** SystemCapability.Communication.NFC.Tag 356 357**参数:** 358 359| 参数名 | 类型 | 必填 | 说明 | 360| -------- | ----------------------- | ---- | -------------------------------------- | 361| callback | AsyncCallback\<boolean> | 是 | 回调函数,true: 支持, false: 不支持。 | 362 363**错误码:** 364 365以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 366 367| 错误码ID | 错误信息| 368| ------- | -------| 369| 3100201 | Tag running state is abnormal in service. | 370 371**示例:** 372 373```js 374import tag from '@ohos.nfc.tag'; 375import {BusinessError} from '@ohos.base'; 376 377// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'isoDep' correctly. 378 379// connect the tag at first if not connected. 380if (!isoDep.isTagConnected()) { 381 if (!isoDep.connectTag()) { 382 console.log("isoDep connectTag failed."); 383 return; 384 } 385} 386 387try { 388 isoDep.isExtendedApduSupported((err: BusinessError, response: boolean) => { 389 if (err) { 390 console.log("isoDep isExtendedApduSupported AsyncCallback Code: ${err.code}, message: ${err.message}"); 391 } else { 392 console.log("isoDep isExtendedApduSupported AsyncCallback response: " + response); 393 } 394 }); 395} catch (busiErr) { 396 console.error("isoDep isExtendedApduSupported AsyncCallback Code: ${(busiError as Business).code}, " + 397 "message: ${(busiError as Business).message}"); 398} 399``` 400 401## NdefMessage<sup>9+</sup> 402 403### NdefMessage.getNdefRecords<sup>9+</sup> 404 405getNdefRecords(): [tag.NdefRecord](js-apis-nfcTag.md#ndefrecord9)[] 406 407获取NDEF消息中的所有记录。 408 409**系统能力:** SystemCapability.Communication.NFC.Tag 410 411**返回值:** 412 413| **类型** | **说明** | 414| ------------------ | --------------------------| 415| [tag.NdefRecord](js-apis-nfcTag.md#ndefrecord9)[] | NDEF标签的Record列表,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 | 416 417**示例:** 418 419```js 420import tag from '@ohos.nfc.tag'; 421 422// Obtains ndefMessage from tag.ndef.createNdefMessage or ndefTag.getNdefMessage. 423// let ndefMessage : tag.NdefMessage = tag.ndef.createNdefMessage(...); 424// let ndefMessage : tag.NdefMessage = ndefTag.getNdefMessage(); 425 426let ndefRecords : tag.NdefRecord[] = ndefMessage.getNdefRecords(); 427console.log("ndef ndefRecords number: " + ndefRecords.length); 428``` 429 430## NdefTag<sup>9+</sup> 431 432提供对已格式化为NDEF的NFC标签的数据和操作的访问,继承自TagSession。 433 434TagSession是所有NFC Tag技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)。 435 436以下是NdefTag的独有接口。 437 438### NdefTag.getNdefTagType<sup>9+</sup> 439 440getNdefTagType(): [tag.NfcForumType](js-apis-nfcTag.md#nfcforumtype9) 441 442获取NDEF标签的类型。 443 444**系统能力:** SystemCapability.Communication.NFC.Tag 445 446**返回值:** 447 448| **类型** | **说明** | 449| ------------------ | --------------------------| 450| [tag.NfcForumType](js-apis-nfcTag.md#nfcforumtype9) | NDEF标签类型,包括NFC FORUM TYPE 1/2/3/4等。| 451 452**示例:** 453 454```js 455import tag from '@ohos.nfc.tag'; 456 457// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly. 458let ndefTagType : tag.NfcForumType = ndefTag.getNdefTagType(); 459console.log("ndef ndefTagType: " + ndefTagType); 460``` 461 462### NdefTag.getNdefMessage<sup>9+</sup> 463 464getNdefMessage(): [NdefMessage](#ndefmessage9) 465 466获取发现NDEF标签时,从标签读取的Message。 467 468**系统能力:** SystemCapability.Communication.NFC.Tag 469 470**返回值:** 471 472| **类型** | **说明** | 473| ------------------ | --------------------------| 474| [NdefMessage](#ndefmessage9) | NDEF标签的Message,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。| 475 476**示例:** 477```js 478import tag from '@ohos.nfc.tag'; 479 480// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly. 481let ndefMessage : tag.NdefMessage = ndefTag.getNdefMessage(); 482console.log("ndef ndefMessage: " + ndefMessage); 483``` 484 485### NdefTag.isNdefWritable<sup>9+</sup> 486 487isNdefWritable(): boolean 488 489检查NDEF标签是否可写。在调用写数据接口前,需要先判断是否支持写操作。 490 491**系统能力:** SystemCapability.Communication.NFC.Tag 492 493**返回值:** 494 495| **类型** | **说明** | 496| ------------------ | --------------------------| 497| boolean | 检查结果,true: 可写, false: 不可写。| 498 499**示例:** 500 501```js 502import tag from '@ohos.nfc.tag'; 503 504// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly. 505let isWritable : boolean = ndefTag.isNdefWritable(); 506console.log("ndef isNdefWritable: " + isWritable); 507``` 508 509### NdefTag.readNdef<sup>9+</sup> 510 511readNdef(): Promise\<[NdefMessage](#ndefmessage9)> 512 513读取标签上的NDEF消息,使用Promise方式作为异步方法。 514 515**需要权限:** ohos.permission.NFC_TAG 516 517**系统能力:** SystemCapability.Communication.NFC.Tag 518 519**返回值:** 520 521| **类型** | **说明** | 522| ------------------ | --------------------------| 523| Promise\<[NdefMessage](#ndefmessage9)> | 以Promise形式返回从NDEF标签中读取到的Message数据对象。| 524 525**错误码:** 526 527以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 528 529| 错误码ID | 错误信息| 530| ------- | -------| 531| 3100201 | Tag running state is abnormal in service. | 532 533**示例:** 534 535```js 536import tag from '@ohos.nfc.tag'; 537import {BusinessError} from '@ohos.base'; 538 539// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly. 540 541// connect the tag at first if not connected. 542if (!ndefTag.isTagConnected()) { 543 if (!ndefTag.connectTag()) { 544 console.log("ndefTag connectTag failed."); 545 return; 546 } 547} 548 549try { 550 ndefTag.readNdef().then((ndefmessage : tag.NdefMessage) => { 551 console.log("ndef readNdef Promise ndefmessage: " + ndefmessage); 552 }).catch((err : BusinessError)=> { 553 console.error("ndef readNdef Promise err Code: ${err.code}, message: ${err.message}"); 554 }); 555} catch (busiError) { 556 console.error("ndef readNdef Promise catched busiError Code: ${(busiError as BusinessError).code}, " + 557 "message: ${(busiError as BusinessError).message}"); 558} 559``` 560 561### NdefTag.readNdef<sup>9+</sup> 562 563readNdef(callback: AsyncCallback\<[NdefMessage](#ndefmessage9)>): void 564 565读取标签上的NDEF消息,使用AsyncCallback方式作为异步方法。 566 567**需要权限:** ohos.permission.NFC_TAG 568 569**系统能力:** SystemCapability.Communication.NFC.Tag 570 571**参数:** 572 573| 参数名 | 类型 | 必填 | 说明 | 574| -------- | ----------------------- | ---- | -------------------------------------- | 575| callback | AsyncCallback\<[NdefMessage](#ndefmessage9)> | 是 | 回调函数,返回从NDEF标签中读取到的Message信息。| 576 577**错误码:** 578 579以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 580 581| 错误码ID | 错误信息| 582| ------- | -------| 583| 3100201 | Tag running state is abnormal in service. | 584 585**示例:** 586 587```js 588import tag from '@ohos.nfc.tag'; 589import {BusinessError} from '@ohos.base'; 590 591// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly. 592 593// connect the tag at first if not connected. 594if (!ndefTag.isTagConnected()) { 595 if (!ndefTag.connectTag()) { 596 console.log("ndefTag connectTag failed."); 597 return; 598 } 599} 600 601try { 602 ndefTag.readNdef((err : BusinessError, ndefmessage : tag.NdefMessage)=> { 603 if (err) { 604 console.log("ndef readNdef AsyncCallback err Code: ${err.code}, message: ${err.message}"); 605 } else { 606 console.log("ndef readNdef AsyncCallback ndefmessage: " + ndefmessage); 607 } 608 }); 609} catch (busiError) { 610 console.log("ndef readNdef AsyncCallback catched Code: ${(busiError : BusinessError).code}," + 611 " message: ${(busiError : BusinessError).message}"); 612} 613``` 614 615### NdefTag.writeNdef<sup>9+</sup> 616 617writeNdef(msg: [NdefMessage](#ndefmessage9)): Promise\<void> 618 619将NDEF Messsage数据对象写入标签,使用Promise方式作为异步方法。 620 621**需要权限:** ohos.permission.NFC_TAG 622 623**系统能力:** SystemCapability.Communication.NFC.Tag 624 625**参数:** 626 627| 参数名 | 类型 | 必填 | 说明 | 628| -------- | ----------------------- | ---- | -------------------------------------- | 629| msg | [NdefMessage](#ndefmessage9) | 是 | NDEF Message数据对象。| 630 631**错误码:** 632 633以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 634 635| 错误码ID | 错误信息| 636| ------- | -------| 637| 3100201 | Tag running state is abnormal in service. | 638 639**示例:** 640 641```js 642import tag from '@ohos.nfc.tag'; 643import {BusinessError} from '@ohos.base'; 644 645// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly. 646// ndefMessage created from raw data, such as: 647let ndefMessage : tag.NdefMessage = 648 tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // MUST can be parsed as NDEF Record. 649// or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[]) 650 651// connect the tag at first if not connected. 652if (!ndefTag.isTagConnected()) { 653 if (!ndefTag.connectTag()) { 654 console.log("ndefTag connectTag failed."); 655 return; 656 } 657} 658 659try { 660 ndefTag.writeNdef(ndefMessage).then(() => { 661 console.log("ndef writeNdef Promise success."); 662 }).catch((err : BusinessError)=> { 663 console.error("ndef writeNdef err Code: ${err.code}, message: ${err.message}"); 664 }); 665} catch (busiError) { 666 console.error("ndef writeNdef Promise catch busiError Code: ${(busiError as Businsess).code}, " + 667 "message: ${(busiError as Businsess).message}"); 668} 669``` 670 671### NdefTag.writeNdef<sup>9+</sup> 672 673writeNdef(msg: [NdefMessage](#ndefmessage9), callback: AsyncCallback\<void>): void 674 675将NDEF Message数据对象写入此标签,使用AsyncCallback方式作为异步方法。 676 677**需要权限:** ohos.permission.NFC_TAG 678 679**系统能力:** SystemCapability.Communication.NFC.Tag 680 681**参数:** 682 683| 参数名 | 类型 | 必填 | 说明 | 684| -------- | ----------------------- | ---- | -------------------------------------- | 685| msg | [NdefMessage](#ndefmessage9) | 是 | NDEF Message数据对象。 | 686| callback | AsyncCallback\<void> | 是 | 回调函数。 | 687 688**错误码:** 689 690以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 691 692| 错误码ID | 错误信息| 693| ------- | -------| 694| 3100201 | Tag running state is abnormal in service. | 695 696**示例:** 697 698```js 699import tag from '@ohos.nfc.tag'; 700import {BusinessError} from '@ohos.base'; 701 702// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly. 703// ndefMessage created from raw data, such as: 704let ndefMessage : tag.NdefMessage = 705 tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // MUST can be parsed as NDEF Record. 706// or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[]) 707 708// connect the tag at first if not connected. 709if (!ndefTag.isTagConnected()) { 710 if (!ndefTag.connectTag()) { 711 console.log("ndefTag connectTag failed."); 712 return; 713 } 714} 715 716try { 717 ndefTag.writeNdef(ndefMessage, (err : BusinessError)=> { 718 if (err) { 719 console.error("ndef writeNdef AsyncCallback Code: ${err.code}, message: ${err.message}"); 720 } else { 721 console.log("ndef writeNdef AsyncCallback success."); 722 } 723 }); 724} catch (busiError) { 725 console.error("ndef writeNdef AsyncCallback catch busiError Code: ${(busiError as Businsess).code}," + 726 " message: ${(busiError as Businsess).message}"); 727} 728``` 729 730### NdefTag.canSetReadOnly<sup>9+</sup> 731 732canSetReadOnly(): boolean 733 734检查NDEF标签是否可以设置为只读。 735 736**需要权限:** ohos.permission.NFC_TAG 737 738**系统能力:** SystemCapability.Communication.NFC.Tag 739 740**返回值:** 741 742| **类型** | **说明** | 743| ------------------ | --------------------------| 744| boolean| true: NDEF标签可设置为只读, false: NDEF标签不可设置为只读。 | 745 746**错误码:** 747 748以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 749 750| 错误码ID | 错误信息| 751| ------- | -------| 752| 3100201 | Tag running state is abnormal in service. | 753 754**示例:** 755 756```js 757import tag from '@ohos.nfc.tag'; 758 759// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly. 760let canSetReadOnly : boolean = ndefTag.canSetReadOnly(); 761console.log("ndef canSetReadOnly: " + canSetReadOnly); 762``` 763 764### NdefTag.setReadOnly<sup>9+</sup> 765 766setReadOnly(): Promise\<void> 767 768将NDEF标签设置为只读,使用Promise方式作为异步方法。 769 770**需要权限:** ohos.permission.NFC_TAG 771 772**系统能力:** SystemCapability.Communication.NFC.Tag 773 774**错误码:** 775 776以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 777 778| 错误码ID | 错误信息| 779| ------- | -------| 780| 3100201 | Tag running state is abnormal in service. | 781 782**示例:** 783 784```js 785import tag from '@ohos.nfc.tag'; 786import {BusinessError} from '@ohos.base'; 787 788// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly. 789 790// connect the tag at first if not connected. 791if (!ndefTag.isTagConnected()) { 792 if (!ndefTag.connectTag()) { 793 console.log("ndefTag connectTag failed."); 794 return; 795 } 796} 797 798try { 799 ndefTag.setReadOnly().then(() => { 800 console.log("ndef setReadOnly Promise success."); 801 }).catch((err : BusinessError)=> { 802 console.error("ndef setReadOnly Promise err Code: ${err.code}, message: ${err.message}"); 803 }); 804} catch (busiError) { 805 console.error("ndef setReadOnly Promise catch busiError Code: ${(busiError as Businsess).code}, " + 806 "message: ${(busiError as Businsess).message}"); 807} 808``` 809 810### NdefTag.setReadOnly<sup>9+</sup> 811 812setReadOnly(callback: AsyncCallback\<void>): void 813 814将NDEF标签设置为只读,使用AsyncCallback方式作为异步方法。 815 816**需要权限:** ohos.permission.NFC_TAG 817 818**系统能力:** SystemCapability.Communication.NFC.Tag 819 820**参数:** 821 822| 参数名 | 类型 | 必填 | 说明 | 823| -------- | ----------------------- | ---- | -------------------------------------- | 824| callback | AsyncCallback\<void> | 是 | 回调函数。 | 825 826**错误码:** 827 828以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 829 830| 错误码ID | 错误信息| 831| ------- | -------| 832| 3100201 | Tag running state is abnormal in service. | 833 834**示例:** 835 836```js 837import tag from '@ohos.nfc.tag'; 838import {BusinessError} from '@ohos.base'; 839 840// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly. 841 842// connect the tag at first if not connected. 843if (!ndefTag.isTagConnected()) { 844 if (!ndefTag.connectTag()) { 845 console.log("ndefTag connectTag failed."); 846 return; 847 } 848} 849 850try { 851 ndefTag.setReadOnly((err : BusinessError)=> { 852 if (err) { 853 console.log("ndef setReadOnly AsyncCallback err Code: ${err.code}, message: ${err.message}"); 854 } else { 855 console.log("ndef setReadOnly AsyncCallback success."); 856 } 857 }); 858} catch (busiError) { 859 console.error("ndef setReadOnly AsyncCallback catch busiError Code: ${(busiError as Businsess).code}, " + 860 "message: ${(busiError as Businsess).message}"); 861} 862``` 863 864### NdefTag.getNdefTagTypeString<sup>9+</sup> 865 866getNdefTagTypeString(type: [tag.NfcForumType](js-apis-nfcTag.md#nfcforumtype9)): string 867 868将NFC论坛类型,转换为NFC论坛中定义的字符串描述。 869 870**系统能力:** SystemCapability.Communication.NFC.Tag 871 872**参数:** 873 874| 参数名 | 类型 | 必填 | 说明 | 875| -------- | ----------------------- | ---- | -------------------------------------- | 876| type | [tag.NfcForumType](js-apis-nfcTag.md#nfcforumtype9) | 是 | NDEF标签类型,包括NFC FORUM TYPE 1/2/3/4等。 | 877 878**返回值:** 879 880| **类型** | **说明** | 881| ------------------ | --------------------------| 882| string | NFC论坛类型的字符串描述。| 883 884**示例:** 885 886```js 887import tag from '@ohos.nfc.tag'; 888import {BusinessError} from '@ohos.base'; 889 890// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly. 891 892try { 893 let ndefTypeString : tag.NfcForumType = ndefTag.getNdefTagTypeString(tag.NfcForumType.NFC_FORUM_TYPE_1); 894 console.log("ndef ndefTypeString: " + ndefTypeString); 895} catch (busiError) { 896 console.error("ndef getNdefTagTypeString catch busiError Code: ${(busiError as Businsess).code}, " + 897 "message: ${(busiError as Businsess).message}"); 898} 899``` 900 901## MifareClassicTag<sup>9+</sup> 902 903MifareClassicTag提供对MIFARE Classic属性和I/O操作的访问,继承自[TagSession](js-apis-tagSession.md)。 904 905TagSession是所有NFC Tag技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)。 906 907以下是MifareClassicTag的独有接口。 908 909### MifareClassicTag.authenticateSector<sup>9+</sup> 910 911authenticateSector(sectorIndex: number, key: number[], isKeyA: boolean): Promise\<void> 912 913使用密钥对扇区进行身份验证,只有身份验证成功的扇区可以进行操作。使用Promise方式作为异步方法。 914 915**需要权限:** ohos.permission.NFC_TAG 916 917**系统能力:** SystemCapability.Communication.NFC.Tag 918 919**参数:** 920 921| 参数名 | 类型 | 必填 | 说明 | 922| -------- | ----------------------- | ---- | -------------------------------------- | 923| sectorIndex | number | 是 | 待验证的扇区索引,从0开始。 | 924| key | number[]| 是 | 用于扇区验证的密钥(6字节)。 | 925| isKeyA | boolean | 是 | isKeyA标志。true 表示KeyA,false 表示KeyB。| 926 927**错误码:** 928 929以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 930 931| 错误码ID | 错误信息| 932| ------- | -------| 933| 3100201 | Tag running state is abnormal in service. | 934 935**示例:** 936 937```js 938import tag from '@ohos.nfc.tag'; 939import {BusinessError} from '@ohos.base'; 940 941// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly. 942 943// connect the tag at first if not connected. 944if (!mifareClassic.isTagConnected()) { 945 if (!mifareClassic.connectTag()) { 946 console.log("mifareClassic connectTag failed."); 947 return; 948 } 949} 950 951try { 952 let sectorIndex = 1; // change it to be correct index. 953 let key = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06] // MUST be 6 bytes, change it to be correct key. 954 mifareClassic.authenticateSector(sectorIndex, key, true).then(() => { 955 console.log("mifareClassic authenticateSector Promise success."); 956 }).catch((err : BusinessError)=> { 957 console.error("mifareClassic authenticateSector Promise errCode: ${err.code}, " + "message: ${err.message}"); 958 }); 959} catch (busiError) { 960 console.error("mifareClassic authenticateSector Promise catch busiError Code: ${(busiError as Businsess).code}, " + 961 "message: ${(busiError as Businsess).message}"); 962} 963``` 964 965### MifareClassicTag.authenticateSector<sup>9+</sup> 966 967authenticateSector(sectorIndex: number, key: number[], isKeyA: boolean, callback: AsyncCallback\<void>): void 968 969使用密钥对扇区进行身份验证,只有身份验证成功的扇区可以进行操作。使用AsyncCallback方式作为异步方法。 970 971**需要权限:** ohos.permission.NFC_TAG 972 973**系统能力:** SystemCapability.Communication.NFC.Tag 974 975**参数:** 976 977| 参数名 | 类型 | 必填 | 说明 | 978| -------- | ----------------------- | ---- | -------------------------------------- | 979| sectorIndex | number | 是 | 待验证的扇区索引,从0开始。 | 980| key | number[]| 是 | 用于扇区验证的密钥(6字节)。 | 981| isKeyA | boolean | 是 | isKeyA标志。true 表示KeyA,false 表示KeyB。| 982| callback | AsyncCallback\<void> | 是 | 以callback形式异步返回扇区验证结果。| 983 984**错误码:** 985 986以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 987 988| 错误码ID | 错误信息| 989| ------- | -------| 990| 3100201 | Tag running state is abnormal in service. | 991 992**示例:** 993```js 994import tag from '@ohos.nfc.tag'; 995import {BusinessError} from '@ohos.base'; 996 997// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly. 998 999// connect the tag at first if not connected. 1000if (!mifareClassic.isTagConnected()) { 1001 if (!mifareClassic.connectTag()) { 1002 console.log("mifareClassic connectTag failed."); 1003 return; 1004 } 1005} 1006 1007try { 1008 let sectorIndex = 1; // change it to be correct index. 1009 let key = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06] // MUST be 6 bytes, change it to be correct key. 1010 mifareClassic.authenticateSector(sectorIndex, key, true, (err : BusinessError)=> { 1011 if (err) { 1012 console.log("mifareClassic authenticateSector AsyncCallback errCode: ${err.code}, message: ${err.message}"); 1013 } else { 1014 console.log("mifareClassic authenticateSector AsyncCallback success."); 1015 } 1016 }); 1017} catch (busiError) { 1018 console.error("mifareClassic authenticateSector AsyncCallback catch Code: ${(busiError as Businsess).code}, " + 1019 "message: ${(busiError as Businsess).message}"); 1020} 1021``` 1022 1023### MifareClassicTag.readSingleBlock<sup>9+</sup> 1024 1025readSingleBlock(blockIndex: number): Promise\<number[]> 1026 1027读取标签中一个块存储的内容,一个块大小为16字节。使用Promise方式作为异步方法。 1028 1029**需要权限:** ohos.permission.NFC_TAG 1030 1031**系统能力:** SystemCapability.Communication.NFC.Tag 1032 1033**参数:** 1034 1035| 参数名 | 类型 | 必填 | 说明 | 1036| -------- | ----------------------- | ---- | -------------------------------------- | 1037| blockIndex | number | 是 | 要读取的块索引,从0开始。 | 1038 1039**返回值:** 1040 1041| **类型** | **说明** | 1042| ------------------ | --------------------------| 1043| Promise\<number[]> | 以Promise形式返回读取的块数据。 | 1044 1045**错误码:** 1046 1047以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 1048 1049| 错误码ID | 错误信息| 1050| ------- | -------| 1051| 3100201 | Tag running state is abnormal in service. | 1052 1053**示例:** 1054 1055```js 1056import tag from '@ohos.nfc.tag'; 1057import {BusinessError} from '@ohos.base'; 1058 1059// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly. 1060 1061// connect the tag at first if not connected. 1062if (!mifareClassic.isTagConnected()) { 1063 if (!mifareClassic.connectTag()) { 1064 console.log("mifareClassic connectTag failed."); 1065 return; 1066 } 1067} 1068 1069try { 1070 let blockIndex = 1; // change it to be correct index. 1071 mifareClassic.readSingleBlock(blockIndex).then((data : number[]) => { 1072 console.log("mifareClassic readSingleBlock Promise data: " + data); 1073 }).catch((err : BusinessError)=> { 1074 console.error("mifareClassic readSingleBlock Promise errCode: ${err.code}, message: ${err.message}"); 1075 }); 1076} catch (busiError) { 1077 console.error("mifareClassic readSingleBlock Promise catch busiError Code: ${(busiError as Businsess).code}, " + 1078 "message: ${(busiError as Businsess).message}"); 1079} 1080``` 1081 1082### MifareClassicTag.readSingleBlock<sup>9+</sup> 1083 1084readSingleBlock(blockIndex: number, callback: AsyncCallback\<number[]>): void 1085 1086读取标签中一个块存储的内容,一个块大小为16字节。使用AsyncCallback方式作为异步方法。 1087 1088**需要权限:** ohos.permission.NFC_TAG 1089 1090**系统能力:** SystemCapability.Communication.NFC.Tag 1091 1092**参数:** 1093 1094| 参数名 | 类型 | 必填 | 说明 | 1095| -------- | ----------------------- | ---- | -------------------------------------- | 1096| blockIndex | number | 是 | 要读取的块索引,从0开始。 | 1097| callback | AsyncCallback\<number[]> | 是 |以callback形式异步返回读取到的块数据。 | 1098 1099**错误码:** 1100 1101以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 1102 1103| 错误码ID | 错误信息| 1104| ------- | -------| 1105| 3100201 | Tag running state is abnormal in service. | 1106 1107**示例:** 1108 1109```js 1110import tag from '@ohos.nfc.tag'; 1111import {BusinessError} from '@ohos.base'; 1112 1113// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly. 1114 1115// connect the tag at first if not connected. 1116if (!mifareClassic.isTagConnected()) { 1117 if (!mifareClassic.connectTag()) { 1118 console.log("mifareClassic connectTag failed."); 1119 return; 1120 } 1121} 1122 1123try { 1124 let blockIndex = 1; // change it to be correct index. 1125 mifareClassic.readSingleBlock(blockIndex, (err : BusinessError, data : number[])=> { 1126 if (err) { 1127 console.log("mifareClassic readSingleBlock AsyncCallback err: " + err); 1128 } else { 1129 console.log("mifareClassic readSingleBlock AsyncCallback data: " + data); 1130 } 1131 }); 1132} catch (busiError) { 1133 console.error("mifareClassic readSingleBlock AsyncCallback catch busiError Code: ${(busiError as Businsess).code}, " + 1134 "message: ${(busiError as Businsess).message}"); 1135} 1136``` 1137 1138### MifareClassicTag.writeSingleBlock<sup>9+</sup> 1139 1140writeSingleBlock(blockIndex: number, data: number[]): Promise\<void> 1141 1142向标签中一个块存储写入内容,一个块大小为16字节。使用Promise方式作为异步方法。 1143 1144**需要权限:** ohos.permission.NFC_TAG 1145 1146**系统能力:** SystemCapability.Communication.NFC.Tag 1147 1148**参数:** 1149 1150| 参数名 | 类型 | 必填 | 说明 | 1151| -------- | ----------------------- | ---- | -------------------------------------- | 1152| blockIndex | number | 是 | 要写入的块索引,从0开始。 | 1153| data | number[] | 是 | 要写入的数据,大小必须是16个字节。 | 1154 1155**错误码:** 1156 1157以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 1158 1159| 错误码ID | 错误信息| 1160| ------- | -------| 1161| 3100201 | Tag running state is abnormal in service. | 1162 1163**示例:** 1164 1165```js 1166import tag from '@ohos.nfc.tag'; 1167import {BusinessError} from '@ohos.base'; 1168 1169// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly. 1170 1171// connect the tag at first if not connected. 1172if (!mifareClassic.isTagConnected()) { 1173 if (!mifareClassic.connectTag()) { 1174 console.log("mifareClassic connectTag failed."); 1175 return; 1176 } 1177} 1178 1179try { 1180 let blockIndex = 1; // change it to be correct index. 1181 let rawData = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 1182 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]; // MUST be 16 bytes, change it to be correct data. 1183 mifareClassic.writeSingleBlock(blockIndex, rawData).then(() => { 1184 console.log("mifareClassic writeSingleBlock Promise success."); 1185 }).catch((err : BusinessError)=> { 1186 console.error("mifareClassic writeSingleBlock Promise errCode: ${err.code}, message: ${err.message}"); 1187 }); 1188} catch (busiError) { 1189 console.error("mifareClassic writeSingleBlock Promise catch busiError Code: ${(busiError as Businsess).code}, " + 1190 "message: ${(busiError as Businsess).message}"); 1191} 1192``` 1193 1194### MifareClassicTag.writeSingleBlock<sup>9+</sup> 1195 1196writeSingleBlock(blockIndex: number, data: number[], callback: AsyncCallback\<void>): void 1197 1198向标签中一个块存储写入内容,一个块大小为16字节。使用AsyncCallback方式作为异步方法。 1199 1200**需要权限:** ohos.permission.NFC_TAG 1201 1202**系统能力:** SystemCapability.Communication.NFC.Tag 1203 1204**参数:** 1205 1206| 参数名 | 类型 | 必填 | 说明 | 1207| -------- | ----------------------- | ---- | -------------------------------------- | 1208| blockIndex | number | 是 | 要写入的块索引,从0开始。 | 1209| data | number[] | 是 | 要写入的数据,大小必须是16个字节。 | 1210| callback | AsyncCallback\<void> | 是 | 以callback形式异步返回块操作结果。 | 1211 1212**错误码:** 1213 1214以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 1215 1216| 错误码ID | 错误信息| 1217| ------- | -------| 1218| 3100201 | Tag running state is abnormal in service. | 1219 1220**示例:** 1221 1222```js 1223import tag from '@ohos.nfc.tag'; 1224import {BusinessError} from '@ohos.base'; 1225 1226// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly. 1227 1228// connect the tag at first if not connected. 1229if (!mifareClassic.isTagConnected()) { 1230 if (!mifareClassic.connectTag()) { 1231 console.log("mifareClassic connectTag failed."); 1232 return; 1233 } 1234} 1235 1236try { 1237 let blockIndex = 1; // change it to be correct index. 1238 let rawData = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 1239 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]; // MUST be 16 bytes, change it to be correct data. 1240 mifareClassic.writeSingleBlock(blockIndex, rawData, (err : BusinessError)=> { 1241 if (err) { 1242 console.log("mifareClassic writeSingleBlock AsyncCallback err Code: ${err.code}, message: ${err.message}"); 1243 } else { 1244 console.log("mifareClassic writeSingleBlock AsyncCallback success."); 1245 } 1246 }); 1247} catch (busiError) { 1248 console.error("mifareClassic writeSingleBlock AsyncCallback catch Code: ${(busiError as Businsess).code}, " + 1249 "message: ${(busiError as Businsess).message}"); 1250} 1251``` 1252 1253### MifareClassicTag.incrementBlock<sup>9+</sup> 1254 1255incrementBlock(blockIndex: number, value: number): Promise\<void> 1256 1257对指定块的内容,增加指定的数值,并将结果存储在内部传输缓冲器中。使用Promise方式作为异步方法。 1258 1259**需要权限:** ohos.permission.NFC_TAG 1260 1261**系统能力:** SystemCapability.Communication.NFC.Tag 1262 1263**参数:** 1264 1265| 参数名 | 类型 | 必填 | 说明 | 1266| -------- | ----------------------- | ---- | -------------------------------------- | 1267| blockIndex | number | 是 | 要指定增加的块索引,从0开始。 | 1268| value | number | 是 | 要指定增加的数据,非负数。 | 1269 1270**错误码:** 1271 1272以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 1273 1274| 错误码ID | 错误信息| 1275| ------- | -------| 1276| 3100201 | Tag running state is abnormal in service. | 1277 1278**示例:** 1279 1280```js 1281import tag from '@ohos.nfc.tag'; 1282import {BusinessError} from '@ohos.base'; 1283 1284// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly. 1285 1286// connect the tag at first if not connected. 1287if (!mifareClassic.isTagConnected()) { 1288 if (!mifareClassic.connectTag()) { 1289 console.log("mifareClassic connectTag failed."); 1290 return; 1291 } 1292} 1293 1294try { 1295 let blockIndex = 1; // change it to be correct index. 1296 let value = 0x20; // change it to be correct data. 1297 mifareClassic.incrementBlock(blockIndex, value).then(() => { 1298 console.log("mifareClassic incrementBlock Promise success."); 1299 }).catch((err : BusinessError)=> { 1300 console.error("mifareClassic incrementBlock Promise err Code: ${err.code}, message: ${err.message}"); 1301 }); 1302} catch (busiError) { 1303 console.error("mifareClassic incrementBlock Promise catch Code: ${(busiError as Businsess).code}, " + 1304 "message: ${(busiError as Businsess).message}"); 1305} 1306``` 1307 1308### MifareClassicTag.incrementBlock<sup>9+</sup> 1309 1310incrementBlock(blockIndex: number, value: number, callback: AsyncCallback\<void>): void 1311 1312对指定块的内容,增加指定的数值,并将结果存储在内部传输缓冲器中。使用AsyncCallback方式作为异步方法。 1313 1314**需要权限:** ohos.permission.NFC_TAG 1315 1316**系统能力:** SystemCapability.Communication.NFC.Tag 1317 1318**参数:** 1319 1320| 参数名 | 类型 | 必填 | 说明 | 1321| -------- | ----------------------- | ---- | -------------------------------------- | 1322| blockIndex | number | 是 | 要被运算的块索引,从0开始。 | 1323| value | number | 是 | 要增加的数值,非负数。 | 1324| callback | AsyncCallback\<void> | 是 | 以callback形式异步返回块操作结果。。 | 1325 1326**错误码:** 1327 1328以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 1329 1330| 错误码ID | 错误信息| 1331| ------- | -------| 1332| 3100201 | Tag running state is abnormal in service. | 1333 1334**示例:** 1335 1336```js 1337import tag from '@ohos.nfc.tag'; 1338import {BusinessError} from '@ohos.base'; 1339 1340// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly. 1341 1342// connect the tag at first if not connected. 1343if (!mifareClassic.isTagConnected()) { 1344 if (!mifareClassic.connectTag()) { 1345 console.log("mifareClassic connectTag failed."); 1346 return; 1347 } 1348} 1349 1350try { 1351 let blockIndex = 1; // change it to be correct index. 1352 let value = 0x20; // change it to be correct data. 1353 mifareClassic.incrementBlock(blockIndex, value, (err : BusinessError)=> { 1354 if (err) { 1355 console.log("mifareClassic incrementBlock AsyncCallback err Code: ${err.code}, message: ${err.message}"); 1356 } else { 1357 console.log("mifareClassic incrementBlock AsyncCallback success."); 1358 } 1359 }); 1360} catch (busiError) { 1361 console.error("mifareClassic incrementBlock AsyncCallback catch busiError Code: ${(busiError as Businsess).code}, " + 1362 "message: ${(busiError as Businsess).message}"); 1363} 1364``` 1365 1366### MifareClassicTag.decrementBlock<sup>9+</sup> 1367 1368decrementBlock(blockIndex: number, value: number): Promise\<void> 1369 1370对指定块的内容,减少指定的数值,并将结果存储在内部传输缓冲器中。使用Promise方式作为异步方法。 1371 1372**需要权限:** ohos.permission.NFC_TAG 1373 1374**系统能力:** SystemCapability.Communication.NFC.Tag 1375 1376**参数:** 1377 1378| 参数名 | 类型 | 必填 | 说明 | 1379| -------- | ----------------------- | ---- | -------------------------------------- | 1380| blockIndex | number | 是 | 要被运算的块索引,从0开始。 | 1381| value | number | 是 | 要减少的数值,非负数。 | 1382 1383**错误码:** 1384 1385以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 1386 1387| 错误码ID | 错误信息| 1388| ------- | -------| 1389| 3100201 | Tag running state is abnormal in service. | 1390 1391**示例:** 1392 1393```js 1394import tag from '@ohos.nfc.tag'; 1395import {BusinessError} from '@ohos.base'; 1396 1397// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly. 1398 1399// connect the tag at first if not connected. 1400if (!mifareClassic.isTagConnected()) { 1401 if (!mifareClassic.connectTag()) { 1402 console.log("mifareClassic connectTag failed."); 1403 return; 1404 } 1405} 1406 1407try { 1408 let blockIndex = 1; // change it to be correct index. 1409 let value = 0x20; // change it to be correct data. 1410 mifareClassic.decrementBlock(blockIndex, value).then(() => { 1411 console.log("mifareClassic decrementBlock Promise success."); 1412 }).catch((err : BusinessError)=> { 1413 console.error("mifareClassic decrementBlock Promise errCode: ${err.code}, message: ${err.message}"); 1414 }); 1415} catch (busiError) { 1416 console.error("mifareClassic decrementBlock Promise catch busiError: Code: ${(busiError as Businsess).code}, " + 1417 "message: ${(busiError as Businsess).message}"); 1418} 1419``` 1420 1421### MifareClassicTag.decrementBlock<sup>9+</sup> 1422 1423decrementBlock(blockIndex: number, value: number, callback: AsyncCallback\<void>): void 1424 1425对指定块的内容,减少指定的数值。使用AsyncCallback方式作为异步方法。 1426 1427**需要权限:** ohos.permission.NFC_TAG 1428 1429**系统能力:** SystemCapability.Communication.NFC.Tag 1430 1431**参数:** 1432 1433| 参数名 | 类型 | 必填 | 说明 | 1434| -------- | ----------------------- | ---- | -------------------------------------- | 1435| blockIndex | number | 是 | 要被运算的块索引,从0开始。 | 1436| value | number | 是 | 要减少的数值,非负数。 | 1437| callback | AsyncCallback\<void> | 是 | 以callback形式异步返回块操作结果。 | 1438 1439**错误码:** 1440 1441以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 1442 1443| 错误码ID | 错误信息| 1444| ------- | -------| 1445| 3100201 | Tag running state is abnormal in service. | 1446 1447**示例:** 1448 1449```js 1450import tag from '@ohos.nfc.tag'; 1451import {BusinessError} from '@ohos.base'; 1452 1453// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly. 1454 1455// connect the tag at first if not connected. 1456if (!mifareClassic.isTagConnected()) { 1457 if (!mifareClassic.connectTag()) { 1458 console.log("mifareClassic connectTag failed."); 1459 return; 1460 } 1461} 1462 1463try { 1464 let blockIndex = 1; // change it to be correct index. 1465 let value = 0x20; // change it to be correct data. 1466 mifareClassic.decrementBlock(blockIndex, value, (err : BusinessError)=> { 1467 if (err) { 1468 console.log("mifareClassic decrementBlock AsyncCallback errCode: ${err.code}, message: ${err.message}"); 1469 } else { 1470 console.log("mifareClassic decrementBlock AsyncCallback success."); 1471 } 1472 }); 1473} catch (busiError) { 1474 console.error("mifareClassic decrementBlock AsyncCallback catch Code: ${(busiError as Businsess).code}, " + 1475 "message: ${(busiError as Businsess).message}"); 1476} 1477``` 1478 1479### MifareClassicTag.transferToBlock<sup>9+</sup> 1480 1481transferToBlock(blockIndex: number): Promise\<void> 1482 1483将临时寄存器的值转移到指定的块。使用Promise方式作为异步方法。 1484 1485**需要权限:** ohos.permission.NFC_TAG 1486 1487**系统能力:** SystemCapability.Communication.NFC.Tag 1488 1489**参数:** 1490 1491| 参数名 | 类型 | 必填 | 说明 | 1492| -------- | ----------------------- | ---- | -------------------------------------- | 1493| blockIndex | number | 是 | 被操作的块的索引,从0开始。 | 1494 1495**错误码:** 1496 1497以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 1498 1499| 错误码ID | 错误信息| 1500| ------- | -------| 1501| 3100201 | Tag running state is abnormal in service. | 1502 1503**示例:** 1504 1505```js 1506import tag from '@ohos.nfc.tag'; 1507import {BusinessError} from '@ohos.base'; 1508 1509// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly. 1510 1511// connect the tag at first if not connected. 1512if (!mifareClassic.isTagConnected()) { 1513 if (!mifareClassic.connectTag()) { 1514 console.log("mifareClassic connectTag failed."); 1515 return; 1516 } 1517} 1518 1519try { 1520 let blockIndex = 1; // change it to be correct index. 1521 mifareClassic.transferToBlock(blockIndex).then(() => { 1522 console.log("mifareClassic transferToBlock Promise success."); 1523 }).catch((err : BusinessError)=> { 1524 console.error("mifareClassic transferToBlock Promise err Code: ${err.code}, message: ${err.message}"); 1525 }); 1526} catch (busiError) { 1527 console.error("mifareClassic transferToBlock Promise catch Code: ${(busiError as Businsess).code}, " + 1528 "message: ${(busiError as Businsess).message}"); 1529} 1530``` 1531 1532### MifareClassicTag.transferToBlock<sup>9+</sup> 1533 1534transferToBlock(blockIndex: number, callback: AsyncCallback\<void>): void 1535 1536将临时寄存器的值转移到指定的块。使用AsyncCallback方式作为异步方法。 1537 1538**需要权限:** ohos.permission.NFC_TAG 1539 1540**系统能力:** SystemCapability.Communication.NFC.Tag 1541 1542**参数:** 1543 1544| 参数名 | 类型 | 必填 | 说明 | 1545| -------- | ----------------------- | ---- | -------------------------------------- | 1546| blockIndex | number | 是 | 被操作的块的索引,从0开始。 | 1547| callback | AsyncCallback\<void> | 是 | 以callback形式异步返回块操作结果。 | 1548 1549**错误码:** 1550 1551以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 1552 1553| 错误码ID | 错误信息| 1554| ------- | -------| 1555| 3100201 | Tag running state is abnormal in service. | 1556 1557**示例:** 1558 1559```js 1560import tag from '@ohos.nfc.tag'; 1561import {BusinessError} from '@ohos.base'; 1562 1563// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly. 1564 1565// connect the tag at first if not connected. 1566if (!mifareClassic.isTagConnected()) { 1567 if (!mifareClassic.connectTag()) { 1568 console.log("mifareClassic connectTag failed."); 1569 return; 1570 } 1571} 1572 1573try { 1574 let blockIndex = 1; // change it to be correct index. 1575 mifareClassic.transferToBlock(blockIndex, (err : BusinessError)=> { 1576 if (err) { 1577 console.error("mifareClassic transferToBlock AsyncCallback errCode: ${err.code}, message: ${err.message}"); 1578 } else { 1579 console.log("mifareClassic transferToBlock AsyncCallback success."); 1580 } 1581 }); 1582} catch (busiError) { 1583 console.error("mifareClassic transferToBlock AsyncCallback catch Code: ${(busiError as Businsess).code}, " + 1584 "message: ${(busiError as Businsess).message}"); 1585} 1586``` 1587 1588### MifareClassicTag.restoreFromBlock<sup>9+</sup> 1589 1590restoreFromBlock(blockIndex: number): Promise\<void> 1591 1592将指定块的值复制到临时寄存器。使用Promise方式作为异步方法。 1593 1594**需要权限:** ohos.permission.NFC_TAG 1595 1596**系统能力:** SystemCapability.Communication.NFC.Tag 1597 1598**参数:** 1599 1600| 参数名 | 类型 | 必填 | 说明 | 1601| -------- | ----------------------- | ---- | -------------------------------------- | 1602| blockIndex | number | 是 | 被操作的块的索引,从0开始。| 1603 1604**错误码:** 1605 1606以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 1607 1608| 错误码ID | 错误信息| 1609| ------- | -------| 1610| 3100201 | Tag running state is abnormal in service. | 1611 1612**示例:** 1613 1614```js 1615import tag from '@ohos.nfc.tag'; 1616import {BusinessError} from '@ohos.base'; 1617 1618// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly. 1619 1620// connect the tag at first if not connected. 1621if (!mifareClassic.isTagConnected()) { 1622 if (!mifareClassic.connectTag()) { 1623 console.log("mifareClassic connectTag failed."); 1624 return; 1625 } 1626} 1627 1628try { 1629 let blockIndex = 1; // change it to be correct index. 1630 mifareClassic.restoreFromBlock(blockIndex).then(() => { 1631 console.log("mifareClassic restoreFromBlock Promise success."); 1632 }).catch((err : BusinessError)=> { 1633 console.error("mifareClassic restoreFromBlock Promise errCode: ${err.code}, message: ${err.message}"); 1634 }); 1635} catch (busiError) { 1636 console.error("mifareClassic restoreFromBlock Promise catch busiError Code: ${(busiError as Businsess).code}, " + 1637 "message: ${(busiError as Businsess).message}"); 1638} 1639``` 1640 1641### MifareClassicTag.restoreFromBlock<sup>9+</sup> 1642 1643restoreFromBlock(blockIndex: number, callback: AsyncCallback\<void>): void 1644 1645将指定块的值复制到临时寄存器。使用AsyncCallback方式作为异步方法。 1646 1647**需要权限:** ohos.permission.NFC_TAG 1648 1649**系统能力:** SystemCapability.Communication.NFC.Tag 1650 1651**参数:** 1652 1653| 参数名 | 类型 | 必填 | 说明 | 1654| -------- | ----------------------- | ---- | -------------------------------------- | 1655| blockIndex | number | 是 | 被操作的块的索引,从0开始。 | 1656| callback | AsyncCallback\<void> | 是 | 以callback形式异步返回块操作结果。| 1657 1658**错误码:** 1659 1660以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 1661 1662| 错误码ID | 错误信息| 1663| ------- | -------| 1664| 3100201 | Tag running state is abnormal in service. | 1665 1666**示例:** 1667 1668```js 1669import tag from '@ohos.nfc.tag'; 1670import {BusinessError} from '@ohos.base'; 1671 1672// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly. 1673 1674// connect the tag at first if not connected. 1675if (!mifareClassic.isTagConnected()) { 1676 if (!mifareClassic.connectTag()) { 1677 console.log("mifareClassic connectTag failed."); 1678 return; 1679 } 1680} 1681 1682try { 1683 let blockIndex = 1; // change it to be correct index. 1684 mifareClassic.restoreFromBlock(blockIndex, (err : BusinessError)=> { 1685 if (err) { 1686 console.log("mifareClassic restoreFromBlock AsyncCallback err Code: ${err.code}, message: ${err.message}"); 1687 } else { 1688 console.log("mifareClassic restoreFromBlock AsyncCallback success."); 1689 } 1690 }); 1691} catch (busiError) { 1692 console.error("mifareClassic restoreFromBlock AsyncCallback catch Code: ${(busiError as Businsess).code}, " + 1693 "message: ${(busiError as Businsess).message}"); 1694} 1695``` 1696 1697### MifareClassicTag.getSectorCount<sup>9+</sup> 1698 1699getSectorCount(): number 1700 1701获取MIFARE Classic标签中的扇区数。 1702 1703**系统能力:** SystemCapability.Communication.NFC.Tag 1704 1705**返回值:** 1706 1707| **类型** | **说明** | 1708| ------------------ | --------------------------| 1709| number | 标签中的扇区数量。| 1710 1711**示例:** 1712 1713```js 1714import tag from '@ohos.nfc.tag'; 1715import {BusinessError} from '@ohos.base'; 1716 1717// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly. 1718let sectorCount : number = mifareClassic.getSectorCount(); 1719console.log("mifareClassic sectorCount: " + sectorCount); 1720``` 1721 1722### MifareClassicTag.getBlockCountInSector<sup>9+</sup> 1723 1724getBlockCountInSector(sectorIndex: number): number 1725 1726获取指定扇区中的块数。 1727 1728**系统能力:** SystemCapability.Communication.NFC.Tag 1729 1730**参数:** 1731 1732| 参数名 | 类型 | 必填 | 说明 | 1733| -------- | ----------------------- | ---- | -------------------------------------- | 1734| sectorIndex | number | 是 | 扇区序号,从0开始。| 1735 1736**返回值:** 1737 1738| **类型** | **说明** | 1739| ------------------ | --------------------------| 1740| number | 该扇区内的块数量。| 1741 1742**示例:** 1743 1744```js 1745import tag from '@ohos.nfc.tag'; 1746import {BusinessError} from '@ohos.base'; 1747 1748// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly. 1749 1750try { 1751 let sectorIndex = 1; // change it to be correct index. 1752 let blockCnt : number = mifareClassic.getBlockCountInSector(sectorIndex); 1753 console.log("mifareClassic blockCnt: " + blockCnt); 1754} catch (busiError) { 1755 console.error("mifareClassic getBlockCountInSector catch busiError Code: ${(busiError as Businsess).code}, " + 1756 "message: ${(busiError as Businsess).message}"); 1757} 1758``` 1759 1760### MifareClassicTag.getType<sup>9+</sup> 1761 1762getType(): [tag.MifareClassicType](js-apis-nfcTag.md#mifareclassictype9) 1763 1764获取MIFARE Classic标签的类型。 1765 1766**系统能力:** SystemCapability.Communication.NFC.Tag 1767 1768**返回值:** 1769 1770| **类型** | **说明** | 1771| ------------------ | --------------------------| 1772| [tag.MifareClassicType](js-apis-nfcTag.md#mifareclassictype9) | MifareClassic标签的类型。| 1773 1774**示例:** 1775 1776```js 1777import tag from '@ohos.nfc.tag'; 1778 1779// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly. 1780let getType : tag.MifareClassicType = mifareClassic.getType(); 1781console.log("mifareClassic getType: " + getType); 1782``` 1783 1784### MifareClassicTag.getTagSize<sup>9+</sup> 1785 1786getTagSize(): number 1787 1788获取标签的存储空间大小,具体请参见[MifareClassicSize](js-apis-nfcTag.md#mifareclassicsize9)。 1789 1790**系统能力:** SystemCapability.Communication.NFC.Tag 1791 1792**返回值:** 1793 1794| **类型** | **说明** | 1795| ------------------ | --------------------------| 1796| number | 标签的大小,单位为字节,请参见[MifareClassicSize](js-apis-nfcTag.md#mifareclassicsize9)。| 1797 1798**示例:** 1799 1800```js 1801import tag from '@ohos.nfc.tag'; 1802import {BusinessError} from '@ohos.base'; 1803 1804// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly. 1805let tagSize : number = mifareClassic.getTagSize(); 1806console.log("mifareClassic tagSize: " + tagSize); 1807``` 1808 1809### MifareClassicTag.isEmulatedTag<sup>9+</sup> 1810 1811isEmulatedTag(): boolean 1812 1813检查标签是不是被模拟的。 1814 1815**系统能力:** SystemCapability.Communication.NFC.Tag 1816 1817**返回值:** 1818 1819| **类型** | **说明** | 1820| ------------------ | --------------------------| 1821| boolean |检查结果,true: 是;false:否。 | 1822 1823**示例:** 1824 1825```js 1826import tag from '@ohos.nfc.tag'; 1827import {BusinessError} from '@ohos.base'; 1828 1829// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly. 1830let isEmulatedTag : boolean = mifareClassic.isEmulatedTag(); 1831console.log("mifareClassic isEmulatedTag: " + isEmulatedTag); 1832``` 1833 1834### MifareClassicTag.getBlockIndex<sup>9+</sup> 1835 1836getBlockIndex(sectorIndex: number): number 1837 1838获取特定扇区的第一个块的序号。 1839 1840**系统能力:** SystemCapability.Communication.NFC.Tag 1841 1842**参数:** 1843 1844| 参数名 | 类型 | 必填 | 说明 | 1845| -------- | ----------------------- | ---- | -------------------------------------- | 1846| sectorIndex | number | 是 | 扇区序号,从0开始。 | 1847 1848**返回值:** 1849 1850| **类型** | **说明** | 1851| ------------------ | --------------------------| 1852| number | 该扇区内的第一个块的序号,从0开始。 | 1853 1854**示例:** 1855 1856```js 1857import tag from '@ohos.nfc.tag'; 1858import {BusinessError} from '@ohos.base'; 1859 1860// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly. 1861 1862try { 1863 let sectorIndex = 1; // change it to be correct index. 1864 let blockIndex : number = mifareClassic.getBlockIndex(sectorIndex); 1865 console.log("mifareClassic blockIndex: " + blockIndex); 1866} catch (busiError) { 1867 console.error("mifareClassic getBlockIndex catch busiError Code: ${(busiError as Businsess).code}, " + 1868 "message: ${(busiError as Businsess).message}"); 1869} 1870``` 1871 1872### MifareClassicTag.getSectorIndex<sup>9+</sup> 1873 1874getSectorIndex(blockIndex: number): number 1875 1876获取包含指定块号的扇区序号。 1877 1878**系统能力:** SystemCapability.Communication.NFC.Tag 1879 1880**参数:** 1881 1882| 参数名 | 类型 | 必填 | 说明 | 1883| -------- | ----------------------- | ---- | -------------------------------------- | 1884| blockIndex | number | 是 | 块序号,从0开始。 | 1885 1886**返回值:** 1887 1888| **类型** | **说明** | 1889| ------------------ | --------------------------| 1890| number | 扇区序号,从0开始。 | 1891 1892**示例:** 1893 1894```js 1895import tag from '@ohos.nfc.tag'; 1896import {BusinessError} from '@ohos.base'; 1897 1898// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly. 1899 1900try { 1901 let blockIndex = 1; // change it to be correct index. 1902 let sectorIndex : number = mifareClassic.getSectorIndex(blockIndex); 1903 console.log("mifareClassic sectorIndex: " + sectorIndex); 1904} catch (busiError) { 1905 console.error("mifareClassic getSectorIndex catch busiError Code: ${(busiError as Businsess).code}, " + 1906 "message: ${(busiError as Businsess).message}"); 1907} 1908``` 1909 1910## MifareUltralightTag<sup>9+</sup> 1911 1912MifareUltralightTag 提供对MIFARE Ultralight属性和I/O操作的访问,继承自TagSession。 1913 1914TagSession是所有NFC Tag技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)。 1915 1916以下是MifareUltralightTag的独有接口。 1917 1918### MifareUltralightTag.readMultiplePages<sup>9+</sup> 1919 1920readMultiplePages(pageIndex: number): Promise\<number[]> 1921 1922读取标签的4页数据,共16字节的数据。每个页面数据大小为4字节。使用Promise方式作为异步方法。 1923 1924**需要权限:** ohos.permission.NFC_TAG 1925 1926**系统能力:** SystemCapability.Communication.NFC.Tag 1927 1928**参数:** 1929 1930| 参数名 | 类型 | 必填 | 说明 | 1931| -------- | ----------------------- | ---- | ------------------------------ | 1932| pageIndex | number | 是 | 要读取页面的索引,从0开始。 | 1933 1934**返回值:** 1935 1936| **类型** | **说明** | 1937| ------------------ | --------------------------| 1938| Promise\<number[]> | 以Promise形式返回读取的4页的数据,共16字节。 | 1939 1940**错误码:** 1941 1942以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 1943 1944| 错误码ID | 错误信息| 1945| ------- | -------| 1946| 3100201 | Tag running state is abnormal in service. | 1947 1948**示例:** 1949 1950```js 1951 1952import tag from '@ohos.nfc.tag'; 1953import {BusinessError} from '@ohos.base'; 1954 1955// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareUltralight' correctly. 1956 1957// connect the tag at first if not connected. 1958if (!mifareUltralight.isTagConnected()) { 1959 if (!mifareUltralight.connectTag()) { 1960 console.log("mifareUltralight connectTag failed."); 1961 return; 1962 } 1963} 1964 1965try { 1966 let pageIndex = 1; // change it to be correct index. 1967 mifareUltralight.readMultiplePages(pageIndex).then((data : number[]) => { 1968 console.log("mifareUltralight readMultiplePages Promise data = " + data); 1969 }).catch((err : BusinessError)=> { 1970 console.error("mifareUltralight readMultiplePages Promise Code: ${err.code}, message: ${err.message}"); 1971 }); 1972} catch (busiError) { 1973 console.error("mifareUltralight readMultiplePages Promise catch busiError Code: ${(busiError as Businsess).code}, " + 1974 "message: ${(busiError as Businsess).message}"); 1975} 1976``` 1977 1978### MifareUltralightTag.readMultiplePages<sup>9+</sup> 1979 1980readMultiplePages(pageIndex: number, callback: AsyncCallback\<number[]>): void 1981 1982读取标签的4页数据,共16字节的数据。每个页面数据大小为4字节。使用AsyncCallback方式作为异步方法。 1983 1984**需要权限:** ohos.permission.NFC_TAG 1985 1986**系统能力:** SystemCapability.Communication.NFC.Tag 1987 1988**参数:** 1989 1990| 参数名 | 类型 | 必填 | 说明 | 1991| -------- | ----------------------- | ---- | -------------------------------------- | 1992| pageIndex | number | 是 | 要读取页面的索引,从0开始。 | 1993| callback | AsyncCallback\<number[]> | 是 | 以callback形式异步返回页操作结果。返回读取到的数据,共16字节。 | 1994 1995**错误码:** 1996 1997以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 1998 1999| 错误码ID | 错误信息| 2000| ------- | -------| 2001| 3100201 | Tag running state is abnormal in service. | 2002 2003**示例:** 2004 2005```js 2006import tag from '@ohos.nfc.tag'; 2007import {BusinessError} from '@ohos.base'; 2008 2009// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareUltralight' correctly. 2010 2011// connect the tag at first if not connected. 2012if (!mifareUltralight.isTagConnected()) { 2013 if (!mifareUltralight.connectTag()) { 2014 console.log("mifareUltralight connectTag failed."); 2015 return; 2016 } 2017} 2018 2019try { 2020 let pageIndex = 1; // change it to be correct index. 2021 mifareUltralight.readMultiplePages(pageIndex, (err : BusinessError, data : number[])=> { 2022 if (err) { 2023 console.log("mifareUltralight readMultiplePages AsyncCallback Code: ${err.code}, message: ${err.message}"); 2024 } else { 2025 console.log("mifareUltralight readMultiplePages AsyncCallback data: " + data); 2026 } 2027 }); 2028} catch (busiError) { 2029 console.error("mifareUltralight readMultiplePages AsyncCallback catch Code: ${(busiError as Businsess).code}, " + 2030 "message: ${(busiError as Businsess).message}"); 2031} 2032``` 2033 2034### MifareUltralightTag.writeSinglePage<sup>9+</sup> 2035 2036writeSinglePage(pageIndex: number, data: number[]): Promise\<void> 2037 2038写入一页数据,数据大小为4字节。使用Promise方式作为异步方法。 2039 2040**需要权限:** ohos.permission.NFC_TAG 2041 2042**系统能力:** SystemCapability.Communication.NFC.Tag 2043 2044**参数:** 2045 2046| 参数名 | 类型 | 必填 | 说明 | 2047| -------- | ----------------------- | ---- | -------------------------------------- | 2048| pageIndex | number | 是 | 要写入页面的索引,从0开始。 | 2049| data | number[] | 是 | 要写入页面的数据内容,必须是4个字节大小。 | 2050 2051**错误码:** 2052 2053以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 2054 2055| 错误码ID | 错误信息| 2056| ------- | -------| 2057| 3100201 | Tag running state is abnormal in service. | 2058 2059**示例:** 2060 2061```js 2062import tag from '@ohos.nfc.tag'; 2063import {BusinessError} from '@ohos.base'; 2064 2065// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareUltralight' correctly. 2066 2067// connect the tag at first if not connected. 2068if (!mifareUltralight.isTagConnected()) { 2069 if (!mifareUltralight.connectTag()) { 2070 console.log("mifareUltralight connectTag failed."); 2071 return; 2072 } 2073} 2074 2075try { 2076 let pageIndex = 1; // change it to be correct index. 2077 let rawData = [0x01, 0x02, 0x03, 0x04]; // MUST be 4 bytes, change it to be correct raw data. 2078 mifareUltralight.writeSinglePage(pageIndex, rawData).then(() => { 2079 console.log("mifareUltralight writeSinglePage Promise success."); 2080 }).catch((err : BusinessError)=> { 2081 console.error("mifareUltralight writeSinglePage Promise err Code: ${err.code}, message: ${err.message}"); 2082 }); 2083} catch (busiError) { 2084 console.error("mifareUltralight writeSinglePage Promise catch Code: ${(busiError as Businsess).code}, " + 2085 "message: ${(busiError as Businsess).message}"); 2086} 2087``` 2088 2089### MifareUltralightTag.writeSinglePage<sup>9+</sup> 2090 2091writeSinglePage(pageIndex: number, data: number[], callback: AsyncCallback\<void>): void 2092 2093写入一页数据,数据大小为4字节。使用AsyncCallback方式作为异步方法。 2094 2095**需要权限:** ohos.permission.NFC_TAG 2096 2097**系统能力:** SystemCapability.Communication.NFC.Tag 2098 2099**参数:** 2100 2101| 参数名 | 类型 | 必填 | 说明 | 2102| -------- | ----------------------- | ---- | ------------------------ | 2103| pageIndex | number | 是 | 要写入页面的索引,从0开始。 | 2104| data | number[] | 是 | 要写入页面的数据内容,必须是4个字节大小。 | 2105| callback|AsyncCallback\<void> |是| 以callback形式异步返回页操作结果。 | 2106 2107**错误码:** 2108 2109以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 2110 2111| 错误码ID | 错误信息| 2112| ------- | -------| 2113| 3100201 | Tag running state is abnormal in service. | 2114 2115**示例:** 2116 2117```js 2118import tag from '@ohos.nfc.tag'; 2119import {BusinessError} from '@ohos.base'; 2120 2121// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareUltralight' correctly. 2122 2123// connect the tag at first if not connected. 2124if (!mifareUltralight.isTagConnected()) { 2125 if (!mifareUltralight.connectTag()) { 2126 console.log("mifareUltralight connectTag failed."); 2127 return; 2128 } 2129} 2130 2131try { 2132 let pageIndex = 1; // change it to be correct index. 2133 let rawData = [0x01, 0x02, 0x03, 0x04]; // MUST be 4 bytes, change it to be correct raw data. 2134 mifareUltralight.writeSinglePage(pageIndex, rawData, (err : BusinessError)=> { 2135 if (err) { 2136 console.error("mifareUltralight writeSinglePage AsyncCallback Code: ${err.code}, message: ${err.message}"); 2137 } else { 2138 console.log("mifareUltralight writeSinglePage AsyncCallback success."); 2139 } 2140 }); 2141} catch (busiError) { 2142 console.error("mifareUltralight writeSinglePage AsyncCallback catch Code: ${(busiError as Businsess).code}, " + 2143 "message: ${(busiError as Businsess).message}"); 2144} 2145``` 2146 2147### MifareUltralightTag.getType<sup>9+</sup> 2148 2149getType(): [tag.MifareUltralightType](js-apis-nfcTag.md#mifareultralighttype9) 2150 2151获取MIFARE Ultralight标签的类型。 2152 2153**系统能力:** SystemCapability.Communication.NFC.Tag 2154 2155**返回值:** 2156 2157| **类型** | **说明** | 2158| ------------------ | --------------------------| 2159| [tag.MifareUltralightType](js-apis-nfcTag.md#mifareultralighttype9) | MIFARE Ultralight标签的类型。| 2160 2161**示例:** 2162 2163```js 2164import tag from '@ohos.nfc.tag'; 2165 2166// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareUltralight' correctly. 2167let getType : tag.MifareUltralightType = mifareClassic.getType(); 2168console.log("mifareUltralight getType: " + getType); 2169``` 2170 2171## NdefFormatableTag<sup>9+</sup> 2172 2173NdefFormatableTag为NDEF Formattable的标签提供格式化操作,继承自TagSession。 2174 2175TagSession是所有NFC Tag 技术类型的基类, 提供建立连接和发送数据等共同接口。具体请参见[TagSession](js-apis-tagSession.md)。 2176 2177以下是NdefFormatableTag的独有接口。 2178 2179### NdefFormatableTag.format<sup>9+</sup> 2180 2181format(message: [NdefMessage](#ndefmessage9)): Promise\<void> 2182 2183将标签格式化为NDEF标签,将NDEF消息写入NDEF标签。使用Promise方式作为异步方法。 2184 2185**需要权限:** ohos.permission.NFC_TAG 2186 2187**系统能力:** SystemCapability.Communication.NFC.Tag 2188 2189**参数:** 2190 2191| 参数名 | 类型 | 必填 | 说明 | 2192| -------- | ----------------------- | ---- | -------------------------------------- | 2193| message | [NdefMessage](#ndefmessage9) | 是 | 格式化成功时要写入的NDEF消息。可以为null,为null时仅格式化标签,不写入内容。 | 2194 2195**错误码:** 2196 2197以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 2198 2199| 错误码ID | 错误信息| 2200| ------- | -------| 2201| 3100201 | Tag running state is abnormal in service. | 2202 2203**示例:** 2204 2205```js 2206import tag from '@ohos.nfc.tag'; 2207import {BusinessError} from '@ohos.base'; 2208 2209// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefFormatable' correctly. 2210 2211// connect the tag at first if not connected. 2212if (!ndefFormatable.isTagConnected()) { 2213 if (!ndefFormatable.connectTag()) { 2214 console.log("ndefFormatable connectTag failed."); 2215 return; 2216 } 2217} 2218 2219try { 2220 // ndefMessage created from raw data, such as: 2221 let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // MUST can be parsed as NDEF Record. 2222 // or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[]) 2223 2224 ndefFormatable.format(ndefMessage).then(() => { 2225 console.log("ndefFormatable format Promise success."); 2226 }).catch((err : BusinessError)=> { 2227 console.error("ndefFormatable format Promise err Code: ${err.code}, message: ${err.message}"); 2228 }); 2229} catch (busiError) { 2230 console.error("ndefFormatable format Promise catch busiError Code: ${(busiError as Businsess).code}, " + 2231 "message: ${(busiError as Businsess).message}"); 2232} 2233``` 2234 2235### NdefFormatableTag.format<sup>9+</sup> 2236 2237format(message: [NdefMessage](#ndefmessage9), callback: AsyncCallback\<void>): void 2238 2239将标签格式化为NDEF标签,然后将NDEF消息写入NDEF标签。使用AsyncCallback方式作为异步方法。 2240 2241**需要权限:** ohos.permission.NFC_TAG 2242 2243**系统能力:** SystemCapability.Communication.NFC.Tag 2244 2245**参数:** 2246 2247| 参数名 | 类型 | 必填 | 说明 | 2248| -------- | ----------------------- | ---- | -------------------------------------- | 2249| message | [NdefMessage](#ndefmessage9) | 是 | 格式化成功时要写入的Ndef消息。可以为null,为null时仅格式化标签,不写入内容。 | 2250 2251**返回值:** 2252 2253| **类型** | **说明** | 2254| ------------------ | --------------------------| 2255| callback: AsyncCallback\<void> | 以callback形式异步返回格式化操作结果。 | 2256 2257**错误码:** 2258 2259以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 2260 2261| 错误码ID | 错误信息| 2262| ------- | -------| 2263| 3100201 | Tag running state is abnormal in service. | 2264 2265**示例:** 2266 2267```js 2268import tag from '@ohos.nfc.tag'; 2269import {BusinessError} from '@ohos.base'; 2270 2271// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefFormatable' correctly. 2272 2273// connect the tag at first if not connected. 2274if (!ndefFormatable.isTagConnected()) { 2275 if (!ndefFormatable.connectTag()) { 2276 console.log("ndefFormatable connectTag failed."); 2277 return; 2278 } 2279} 2280 2281try { 2282 // ndefMessage created from raw data, such as: 2283 let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // MUST can be parsed as NDEF Record. 2284 // or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[]) 2285 2286 ndefFormatable.format(ndefMessage, (err : BusinessError)=> { 2287 if (err) { 2288 console.log("ndefFormatable format AsyncCallback Code: ${err.code}, message: ${err.message}"); 2289 } else { 2290 console.log("ndefFormatable format AsyncCallback success."); 2291 } 2292 }); 2293} catch (busiError) { 2294 console.log("ndefFormatable format AsyncCallback catch Code: ${(busiError as Businsess).code}, " + 2295 "message: ${(busiError as Businsess).message}"); 2296} 2297``` 2298 2299### NdefFormatableTag.formatReadOnly<sup>9+</sup> 2300 2301formatReadOnly(message: [NdefMessage](#ndefmessage9)): Promise\<void> 2302 2303将标签格式化为NDEF标签,将NDEF消息写入NDEF标签,之后将标签设置为只读。使用Promise方式作为异步方法。 2304 2305**需要权限:** ohos.permission.NFC_TAG 2306 2307**系统能力:** SystemCapability.Communication.NFC.Tag 2308 2309**参数:** 2310 2311| 参数名 | 类型 | 必填 | 说明 | 2312| -------- | ----------------------- | ---- | -------------------------------------- | 2313| message | [NdefMessage](#ndefmessage9) | 是 | 格式化成功时要写入的NDEF消息。可以为null,为null时仅格式化标签,不写入内容。 | 2314 2315**错误码:** 2316 2317以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 2318 2319| 错误码ID | 错误信息| 2320| ------- | -------| 2321| 3100201 | Tag running state is abnormal in service. | 2322 2323**示例:** 2324 2325```js 2326import tag from '@ohos.nfc.tag'; 2327import {BusinessError} from '@ohos.base'; 2328 2329// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefFormatable' correctly. 2330 2331// connect the tag at first if not connected. 2332if (!ndefFormatable.isTagConnected()) { 2333 if (!ndefFormatable.connectTag()) { 2334 console.log("ndefFormatable connectTag failed."); 2335 return; 2336 } 2337} 2338 2339try { 2340 // ndefMessage created from raw data, such as: 2341 let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // MUST can be parsed as NDEF Record. 2342 // or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[]) 2343 2344 ndefFormatable.formatReadOnly(ndefMessage).then(() => { 2345 console.log("ndefFormatable formatReadOnly Promise success."); 2346 }).catch((err : BusinessError)=> { 2347 console.log("ndefFormatable formatReadOnly Promise Code: ${err.code}, message: ${err.message}"); 2348 }); 2349} catch (busiError) { 2350 console.error("ndefFormatable formatReadOnly Promise catch Code: ${(busiError as Businsess).code}, " + 2351 "message: ${(busiError as Businsess).message}"); 2352} 2353``` 2354 2355### NdefFormatableTag.formatReadOnly<sup>9+</sup> 2356 2357formatReadOnly(message: [NdefMessage](#ndefmessage9), callback: AsyncCallback\<void>): void 2358 2359将标签格式化为NDEF标签,然后将NDEF消息写入NDEF标签,之后将标签设置为只读。使用callback方式作为异步方法。 2360 2361**需要权限:** ohos.permission.NFC_TAG 2362 2363**系统能力:** SystemCapability.Communication.NFC.Tag 2364 2365**参数:** 2366 2367| 参数名 | 类型 | 必填 | 说明 | 2368| -------- | ----------------------- | ---- | -------------------------------------- | 2369| message | [NdefMessage](#ndefmessage9) | 是 | 格式化成功时要写入的NDEF消息。可以为null,为null时仅格式化标签,不写入内容。| 2370 2371**返回值:** 2372 2373| **类型** | **说明** | 2374| ------------------ | --------------------------| 2375| callback: AsyncCallback\<void> | 以callback形式异步返回格式化操作结果。 | 2376 2377**错误码:** 2378 2379以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 2380 2381| 错误码ID | 错误信息| 2382| ------- | -------| 2383| 3100201 | Tag running state is abnormal in service. | 2384 2385**示例:** 2386 2387```js 2388import tag from '@ohos.nfc.tag'; 2389import {BusinessError} from '@ohos.base'; 2390 2391// see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefFormatable' correctly. 2392 2393// connect the tag at first if not connected. 2394if (!ndefFormatable.isTagConnected()) { 2395 if (!ndefFormatable.connectTag()) { 2396 console.log("ndefFormatable connectTag failed."); 2397 return; 2398 } 2399} 2400 2401try { 2402 // ndefMessage created from raw data, such as: 2403 let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // MUST can be parsed as NDEF Record. 2404 // or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[]) 2405 2406 ndefFormatable.formatReadOnly(ndefMessage, (err : BusinessError)=> { 2407 if (err) { 2408 console.error("ndefFormatable formatReadOnly AsyncCallback err Code: ${err.code}, message: ${err.message}"); 2409 } else { 2410 console.log("ndefFormatable formatReadOnly AsyncCallback success."); 2411 } 2412 }); 2413} catch (busiError) { 2414 console.error("ndefFormatable formatReadOnly AsyncCallback catch Code: ${(busiError as Businsess).code}, " + 2415 "message: ${(busiError as Businsess).message}"); 2416} 2417``` 2418