1# nfctech (Standard NFC Technologies) 2 3The **nfctech** module provides APIs for reading and writing tags that use different Near-Field Communication (NFC) technologies. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8 9## Modules to Import 10 11```js 12import tag from '@ohos.nfc.tag'; 13``` 14 15## NfcATag 16 17Provides APIs to access NFC-A (ISO 14443-3A) properties and perform I/O operations on a tag. This class inherits from **[TagSession](js-apis-tagSession.md)**. 18 19**TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md). 20 21The following describes the unique APIs of **NfcATag**. 22 23### NfcATag.getSak 24 25getSak(): number 26 27Obtains the SAK value of this NFC-A tag. 28 29**System capability**: SystemCapability.Communication.NFC.Tag 30 31**Return value** 32 33| **Type**| **Description** | 34| ------------------ | --------------------------| 35| number | SAK value obtained. The SAK is a hexadecimal number ranging from **0x00** to **0xFF**.| 36 37**Example** 38 39```js 40import tag from '@ohos.nfc.tag'; 41 42// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcA' correctly. 43let sak : number = nfcA.getSak(); 44console.log("nfcA sak: " + sak); 45``` 46 47### NfcATag.getAtqa 48 49getAtqa(): number[] 50 51Obtains the ATQA value of this NFC-A tag. 52 53**System capability**: SystemCapability.Communication.NFC.Tag 54 55**Return value** 56 57| **Type**| **Description** | 58| ------------------ | --------------------------| 59| number[] | ATQA value obtained. Each number of the ATQA is a hexadecimal number ranging from **0x00** to **0xFF**.| 60 61**Example** 62 63```js 64import tag from '@ohos.nfc.tag'; 65 66// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcA' correctly. 67let atqa : number[] = nfcA.getAtqa(); 68console.log("nfcA atqa: " + atqa); 69``` 70 71## NfcBTag 72 73Provides APIs to access NFC-B (ISO 14443-3B) properties and perform I/O operations on a tag. This class inherits from **TagSession**. 74 75**TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md). 76 77The following describes the unique APIs of **NfcBTag**. 78 79### NfcBTag.getRespAppData 80 81getRespAppData(): number[] 82 83Obtains the application data of this NFC-B tag. 84 85**System capability**: SystemCapability.Communication.NFC.Tag 86 87**Return value** 88 89| **Type**| **Description** | 90| ------------------ | --------------------------| 91| number[] | Application data obtained, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.| 92 93**Example** 94 95```js 96import tag from '@ohos.nfc.tag'; 97 98// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcB' correctly. 99let respAppData : number[] = nfcB.getRespAppData(); 100console.log("nfcB respAppData: " + respAppData); 101``` 102 103### NfcBTag.getRespProtocol 104 105getRespProtocol(): number[] 106 107Obtains the protocol information of this NFC-B tag. 108 109**System capability**: SystemCapability.Communication.NFC.Tag 110 111**Return value** 112 113| **Type**| **Description** | 114| ------------------ | --------------------------| 115| number[] | Protocol information obtained, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.| 116 117**Example** 118 119```js 120import tag from '@ohos.nfc.tag'; 121 122// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcB' correctly. 123let respProtocol : number[] = nfcB.getRespProtocol(); 124console.log("nfcB respProtocol: " + respProtocol); 125``` 126 127## NfcFTag 128 129Provides APIs to access NFC-F (JIS 6319-4) properties and perform I/O operations on a tag. This class inherits from **TagSession**. 130 131**TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md). 132 133The following describes the unique APIs of **NfcFTag**. 134 135### NfcFTag.getSystemCode 136 137getSystemCode(): number[] 138 139Obtains the system code from this NFC-F tag. 140 141**System capability**: SystemCapability.Communication.NFC.Tag 142 143**Return value** 144 145| **Type**| **Description** | 146| ------------------ | --------------------------| 147| number[] | System code obtained, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.| 148 149**Example** 150 151```js 152import tag from '@ohos.nfc.tag'; 153 154// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcF' correctly. 155let systemCode : number[] = nfcF.getSystemCode(); 156console.log("nfcF systemCode: " + systemCode); 157``` 158 159### NfcFTag.getPmm 160 161getPmm(): number[] 162 163Obtains the PMm (consisting of the IC code and manufacturer parameters) information from this NFC-F tag. 164 165**System capability**: SystemCapability.Communication.NFC.Tag 166 167**Return value** 168 169| **Type**| **Description** | 170| ------------------ | --------------------------| 171| number[] | PMm information obtained, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.| 172 173**Example** 174 175```js 176import tag from '@ohos.nfc.tag'; 177 178// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcF' correctly. 179let pmm : number[] = nfcF.getPmm(); 180console.log("nfcF pmm: " + pmm); 181``` 182 183## NfcVTag 184 185Provides APIs to access NFC-V (ISO 15693) properties and perform I/O operations on a tag. This class inherits from **TagSession**. 186 187**TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md). 188 189The following describes the unique APIs of **NfcVTag**. 190 191### NfcvTag.getResponseFlags 192 193getResponseFlags(): number 194 195Obtains the response flags from this NFC-V tag. 196 197**System capability**: SystemCapability.Communication.NFC.Tag 198 199**Return value** 200 201| **Type**| **Description** | 202| ------------------ | --------------------------| 203| number | Response flags obtained, which consist of hexadecimal numbers ranging from **0x00** to **0xFF**.| 204 205**Example** 206 207```js 208import tag from '@ohos.nfc.tag'; 209 210// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcV' correctly. 211let responseFlags : number = nfcV.getResponseFlags(); 212console.log("nfcV responseFlags: " + responseFlags); 213``` 214 215### NfcvTag.getDsfId 216 217getDsfId(): number 218 219Obtains the data storage format identifier (DSFID) from this NFC-V tag. 220 221**System capability**: SystemCapability.Communication.NFC.Tag 222 223**Return value** 224 225| **Type**| **Description** | 226| ------------------ | --------------------------| 227| number | DSFID obtained, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.| 228 229**Example** 230 231```js 232import tag from '@ohos.nfc.tag'; 233 234// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcV' correctly. 235let dsfId : number = nfcV.getDsfId(); 236console.log("nfcV dsfId: " + dsfId); 237``` 238 239## IsoDepTag<sup>9+</sup> 240 241Provides APIs to access ISO-DEP (ISO 14443-4) properties and I/O operations on a tag. This class inherits from **TagSession**. 242 243**TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md). 244 245The following describes the unique APIs of **IsoDepTag**. 246 247### IsoDepTag.getHistoricalBytes<sup>9+</sup> 248 249getHistoricalBytes(): number[] 250 251Obtains the historical bytes for the given tag. This API applies only to the IsoDep cards that use the NFC-A technology. 252 253**System capability**: SystemCapability.Communication.NFC.Tag 254 255**Return value** 256 257| **Type**| **Description** | 258| ------------------ | --------------------------| 259| number[] | Historical bytes obtained, which consist of hexadecimal numbers ranging from **0x00** to **0xFF**. If the IsoDep tag uses the NFC-B technology, **null** will be returned.| 260 261**Example** 262 263```js 264import tag from '@ohos.nfc.tag'; 265 266// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'isoDep' correctly. 267let historicalBytes : number[] = isoDep.getHistoricalBytes(); 268console.log("isoDep historicalBytes: " + historicalBytes); 269``` 270 271### IsoDepTag.getHiLayerResponse<sup>9+</sup> 272 273getHiLayerResponse(): number[] 274 275Obtains the higher-layer response bytes for the given tag. This API applies only to the IsoDep cards that use the NFC-B technology. 276 277**System capability**: SystemCapability.Communication.NFC.Tag 278 279**Return value** 280 281| **Type**| **Description** | 282| ------------------ | --------------------------| 283| number[] | Higher-layer response bytes obtained, which consist of hexadecimal numbers ranging from **0x00** to **0xFF**. If the IsoDep tag uses the NFC-A technology, **null** will be returned.| 284 285**Example** 286 287```js 288import tag from '@ohos.nfc.tag'; 289 290// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained '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 299Checks whether an extended application protocol data unit (APDU) is supported. This API uses a promise to return the result. 300 301**Required permissions**: ohos.permission.NFC_TAG 302 303**System capability**: SystemCapability.Communication.NFC.Tag 304 305**Return value** 306 307| **Type**| **Description** | 308| ------------------ | --------------------------| 309| Promise<boolean> | Promise used to return the result. The value **true** means the extended APDU is supported; the value **false** means the opposite.| 310 311**Error codes** 312 313For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 314 315| ID| Error Message| 316| ------- | -------| 317| 3100201 | Tag running state is abnormal in service. | 318 319**Example** 320 321```js 322import tag from '@ohos.nfc.tag'; 323import {BusinessError} from '@ohos.base'; 324 325// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'isoDep' correctly. 326 327// Connect to the tag if it is 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 351Checks whether an extended APDU is supported. This API uses an asynchronous callback to return the result. 352 353**Required permissions**: ohos.permission.NFC_TAG 354 355**System capability**: SystemCapability.Communication.NFC.Tag 356 357**Parameters** 358 359| Name | Type | Mandatory| Description | 360| -------- | ----------------------- | ---- | -------------------------------------- | 361| callback | AsyncCallback\<boolean> | Yes | Callback invoked to return the result. If the extended APDU is supported, **true** is returned; otherwise, **false** is returned.| 362 363**Error codes** 364 365For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 366 367| ID| Error Message| 368| ------- | -------| 369| 3100201 | Tag running state is abnormal in service. | 370 371**Example** 372 373```js 374import tag from '@ohos.nfc.tag'; 375import {BusinessError} from '@ohos.base'; 376 377// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'isoDep' correctly. 378 379// Connect to the tag if it is 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 407Obtains all NDEF records. 408 409**System capability**: SystemCapability.Communication.NFC.Tag 410 411**Return value** 412 413| **Type**| **Description** | 414| ------------------ | --------------------------| 415| [tag.NdefRecord](js-apis-nfcTag.md#ndefrecord9)[] | List of NDEF records obtained. For details, see *NFCForum-TS-NDEF_1.0*.| 416 417**Example** 418 419```js 420import tag from '@ohos.nfc.tag'; 421 422// Obtain 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 432Provides APIs to access the tags in the NFC Data Exchange Format (NDEF). This class inherits from **TagSession**. 433 434**TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md). 435 436The following describes the unique APIs of **NdefTag**. 437 438### NdefTag.getNdefTagType<sup>9+</sup> 439 440getNdefTagType(): [tag.NfcForumType](js-apis-nfcTag.md#nfcforumtype9) 441 442Obtains the NDEF tag type. 443 444**System capability**: SystemCapability.Communication.NFC.Tag 445 446**Return value** 447 448| **Type**| **Description** | 449| ------------------ | --------------------------| 450| [tag.NfcForumType](js-apis-nfcTag.md#nfcforumtype9) | NDEF tag type obtained. It can be NFC FORUM TYPE 1, 2, 3, or 4.| 451 452**Example** 453 454```js 455import tag from '@ohos.nfc.tag'; 456 457// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained '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 466Obtains the NDEF message from this NDEF tag. 467 468**System capability**: SystemCapability.Communication.NFC.Tag 469 470**Return value** 471 472| **Type**| **Description** | 473| ------------------ | --------------------------| 474| [NdefMessage](#ndefmessage9) | NDEF message created. For details, see *NFCForum-TS-NDEF_1.0*.| 475 476**Example** 477```js 478import tag from '@ohos.nfc.tag'; 479 480// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained '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 489Check whether this NDEF tag is writable. Before calling the data write API, check whether the write operation is supported. 490 491**System capability**: SystemCapability.Communication.NFC.Tag 492 493**Return value** 494 495| **Type**| **Description** | 496| ------------------ | --------------------------| 497| boolean | Promise used to return the result. If the tag is writable, **true** is returned; otherwise, **false** is returned.| 498 499**Example** 500 501```js 502import tag from '@ohos.nfc.tag'; 503 504// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained '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 513Reads the NDEF message from this tag. This API uses a promise to return the result. 514 515**Required permissions**: ohos.permission.NFC_TAG 516 517**System capability**: SystemCapability.Communication.NFC.Tag 518 519**Return value** 520 521| **Type**| **Description** | 522| ------------------ | --------------------------| 523| Promise\<[NdefMessage](#ndefmessage9)> | Promise used to return the message object read.| 524 525**Error codes** 526 527For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 528 529| ID| Error Message| 530| ------- | -------| 531| 3100201 | Tag running state is abnormal in service. | 532 533**Example** 534 535```js 536import tag from '@ohos.nfc.tag'; 537import {BusinessError} from '@ohos.base'; 538 539// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly. 540 541// Connect to the tag if it is 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 565Reads the NDEF message from this tag. This API uses an asynchronous callback to return the result. 566 567**Required permissions**: ohos.permission.NFC_TAG 568 569**System capability**: SystemCapability.Communication.NFC.Tag 570 571**Parameters** 572 573| Name | Type | Mandatory| Description | 574| -------- | ----------------------- | ---- | -------------------------------------- | 575| callback | AsyncCallback\<[NdefMessage](#ndefmessage9)> | Yes | Callback invoked to return the NDEF message read.| 576 577**Error codes** 578 579For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 580 581| ID| Error Message| 582| ------- | -------| 583| 3100201 | Tag running state is abnormal in service. | 584 585**Example** 586 587```js 588import tag from '@ohos.nfc.tag'; 589import {BusinessError} from '@ohos.base'; 590 591// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly. 592 593// Connect to the tag if it is 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 619Writes an NDEF message to this tag. This API uses a promise to return the result. 620 621**Required permissions**: ohos.permission.NFC_TAG 622 623**System capability**: SystemCapability.Communication.NFC.Tag 624 625**Parameters** 626 627| Name | Type | Mandatory| Description | 628| -------- | ----------------------- | ---- | -------------------------------------- | 629| msg | [NdefMessage](#ndefmessage9) | Yes | NDEF message to write.| 630 631**Error codes** 632 633For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 634 635| ID| Error Message| 636| ------- | -------| 637| 3100201 | Tag running state is abnormal in service. | 638 639**Example** 640 641```js 642import tag from '@ohos.nfc.tag'; 643import {BusinessError} from '@ohos.base'; 644 645// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly. 646// NDEF message created from raw data, such as: 647let ndefMessage : tag.NdefMessage = 648 tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // It must be parsed as NDEF Record. 649// or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[]) 650 651// Connect to the tag if it is 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 675Writes an NDEF message to this tag. This API uses an asynchronous callback to return the result. 676 677**Required permissions**: ohos.permission.NFC_TAG 678 679**System capability**: SystemCapability.Communication.NFC.Tag 680 681**Parameters** 682 683| Name | Type | Mandatory| Description | 684| -------- | ----------------------- | ---- | -------------------------------------- | 685| msg | [NdefMessage](#ndefmessage9) | Yes | NDEF message to write.| 686| callback | AsyncCallback\<void> | Yes | Callback invoked to return the result.| 687 688**Error codes** 689 690For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 691 692| ID| Error Message| 693| ------- | -------| 694| 3100201 | Tag running state is abnormal in service. | 695 696**Example** 697 698```js 699import tag from '@ohos.nfc.tag'; 700import {BusinessError} from '@ohos.base'; 701 702// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly. 703// NDEF message created from raw data, such as: 704let ndefMessage : tag.NdefMessage = 705 tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // It must be parsed as NDEF Record. 706// or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[]) 707 708// Connect to the tag if it is 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 734Checks whether this NDEF tag can be set to read-only. 735 736**Required permissions**: ohos.permission.NFC_TAG 737 738**System capability**: SystemCapability.Communication.NFC.Tag 739 740**Return value** 741 742| **Type**| **Description** | 743| ------------------ | --------------------------| 744| boolean| Returns **true** if the tag can be set to read-only; returns **false** otherwise.| 745 746**Error codes** 747 748For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 749 750| ID| Error Message| 751| ------- | -------| 752| 3100201 | Tag running state is abnormal in service. | 753 754**Example** 755 756```js 757import tag from '@ohos.nfc.tag'; 758 759// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained '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 768Sets this NDEF tag to read-only. This API uses a promise to return the result. 769 770**Required permissions**: ohos.permission.NFC_TAG 771 772**System capability**: SystemCapability.Communication.NFC.Tag 773 774**Error codes** 775 776For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 777 778| ID| Error Message| 779| ------- | -------| 780| 3100201 | Tag running state is abnormal in service. | 781 782**Example** 783 784```js 785import tag from '@ohos.nfc.tag'; 786import {BusinessError} from '@ohos.base'; 787 788// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly. 789 790// Connect to the tag if it is 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 814Sets this NDEF tag to read-only. This API uses an asynchronous callback to return the result. 815 816**Required permissions**: ohos.permission.NFC_TAG 817 818**System capability**: SystemCapability.Communication.NFC.Tag 819 820**Parameters** 821 822| Name | Type | Mandatory| Description | 823| -------- | ----------------------- | ---- | -------------------------------------- | 824| callback | AsyncCallback\<void> | Yes | Callback invoked to return the result.| 825 826**Error codes** 827 828For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 829 830| ID| Error Message| 831| ------- | -------| 832| 3100201 | Tag running state is abnormal in service. | 833 834**Example** 835 836```js 837import tag from '@ohos.nfc.tag'; 838import {BusinessError} from '@ohos.base'; 839 840// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly. 841 842// Connect to the tag if it is 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 868Converts an NFC Forum Type tag to a string defined in the NFC Forum. 869 870**System capability**: SystemCapability.Communication.NFC.Tag 871 872**Parameters** 873 874| Name | Type | Mandatory| Description | 875| -------- | ----------------------- | ---- | -------------------------------------- | 876| type | [tag.NfcForumType](js-apis-nfcTag.md#nfcforumtype9) | Yes | NDEF tag type. It can be NFC FORUM type 1, 2, 3, or 4.| 877 878**Return value** 879 880| **Type**| **Description** | 881| ------------------ | --------------------------| 882| string | Byte array obtained.| 883 884**Example** 885 886```js 887import tag from '@ohos.nfc.tag'; 888import {BusinessError} from '@ohos.base'; 889 890// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained '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 903Provides APIs to access MIFARE Classic properties and perform I/O operations on a tag. This class inherits from [TagSession](js-apis-tagSession.md). 904 905**TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md). 906 907The following describes the unique APIs of **MifareClassicTag**. 908 909### MifareClassicTag.authenticateSector<sup>9+</sup> 910 911authenticateSector(sectorIndex: number, key: number[], isKeyA: boolean): Promise\<void> 912 913Authenticates a sector using a key. The sector can be accessed only after the authentication is successful. This API uses a promise to return the result. 914 915**Required permissions**: ohos.permission.NFC_TAG 916 917**System capability**: SystemCapability.Communication.NFC.Tag 918 919**Parameters** 920 921| Name | Type | Mandatory| Description | 922| -------- | ----------------------- | ---- | -------------------------------------- | 923| sectorIndex | number | Yes | Index of the sector to authenticate. The sector indexes start from **0**.| 924| key | number[]| Yes | Key (6 bytes) used for sector authentication.| 925| isKeyA | boolean | Yes | Whether the key is key A. The value **true** indicates key A, and **false** indicates key B.| 926 927**Error codes** 928 929For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 930 931| ID| Error Message| 932| ------- | -------| 933| 3100201 | Tag running state is abnormal in service. | 934 935**Example** 936 937```js 938import tag from '@ohos.nfc.tag'; 939import {BusinessError} from '@ohos.base'; 940 941// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. 942 943// Connect to the tag if it is 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 as required. 953 let key = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06] // The key must be of 6 bytes. 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 969Authenticates a sector using a key. The sector can be accessed only after the authentication is successful. This API uses an asynchronous callback to return the result. 970 971**Required permissions**: ohos.permission.NFC_TAG 972 973**System capability**: SystemCapability.Communication.NFC.Tag 974 975**Parameters** 976 977| Name | Type | Mandatory| Description | 978| -------- | ----------------------- | ---- | -------------------------------------- | 979| sectorIndex | number | Yes | Index of the sector to authenticate. The sector indexes start from **0**.| 980| key | number[]| Yes | Key (6 bytes) used for sector authentication.| 981| isKeyA | boolean | Yes | Whether the key is key A. The value **true** indicates key A, and **false** indicates key B.| 982| callback | AsyncCallback\<void> | Yes | Callback invoked to return the result.| 983 984**Error codes** 985 986For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 987 988| ID| Error Message| 989| ------- | -------| 990| 3100201 | Tag running state is abnormal in service. | 991 992**Example** 993```js 994import tag from '@ohos.nfc.tag'; 995import {BusinessError} from '@ohos.base'; 996 997// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. 998 999// Connect to the tag if it is 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 as required. 1009 let key = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06] // The key must be of 6 bytes. 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 1027Reads a block (16 bytes) on this tag. This API uses a promise to return the result. 1028 1029**Required permissions**: ohos.permission.NFC_TAG 1030 1031**System capability**: SystemCapability.Communication.NFC.Tag 1032 1033**Parameters** 1034 1035| Name | Type | Mandatory| Description | 1036| -------- | ----------------------- | ---- | -------------------------------------- | 1037| blockIndex | number | Yes | Index of the block to read. The block indexes start from **0**.| 1038 1039**Return value** 1040 1041| **Type**| **Description** | 1042| ------------------ | --------------------------| 1043| Promise\<number[]> | Promise used to return the block data read.| 1044 1045**Error codes** 1046 1047For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 1048 1049| ID| Error Message| 1050| ------- | -------| 1051| 3100201 | Tag running state is abnormal in service. | 1052 1053**Example** 1054 1055```js 1056import tag from '@ohos.nfc.tag'; 1057import {BusinessError} from '@ohos.base'; 1058 1059// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. 1060 1061// Connect to the tag if it is 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 as required. 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 1086Reads a block (16 bytes) on this tag. This API uses an asynchronous callback to return the result. 1087 1088**Required permissions**: ohos.permission.NFC_TAG 1089 1090**System capability**: SystemCapability.Communication.NFC.Tag 1091 1092**Parameters** 1093 1094| Name | Type | Mandatory| Description | 1095| -------- | ----------------------- | ---- | -------------------------------------- | 1096| blockIndex | number | Yes | Index of the block to read. The block indexes start from **0**.| 1097| callback | AsyncCallback\<number[]> | Yes | Callback invoked to return the data read.| 1098 1099**Error codes** 1100 1101For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 1102 1103| ID| Error Message| 1104| ------- | -------| 1105| 3100201 | Tag running state is abnormal in service. | 1106 1107**Example** 1108 1109```js 1110import tag from '@ohos.nfc.tag'; 1111import {BusinessError} from '@ohos.base'; 1112 1113// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. 1114 1115// Connect to the tag if it is 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 as required. 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 1142Writes data to a block on this tag. This API uses a promise to return the result. 1143 1144**Required permissions**: ohos.permission.NFC_TAG 1145 1146**System capability**: SystemCapability.Communication.NFC.Tag 1147 1148**Parameters** 1149 1150| Name | Type | Mandatory| Description | 1151| -------- | ----------------------- | ---- | -------------------------------------- | 1152| blockIndex | number | Yes | Index of the target block. The block indexes start from **0**.| 1153| data | number[] | Yes | 16-byte data to write.| 1154 1155**Error codes** 1156 1157For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 1158 1159| ID| Error Message| 1160| ------- | -------| 1161| 3100201 | Tag running state is abnormal in service. | 1162 1163**Example** 1164 1165```js 1166import tag from '@ohos.nfc.tag'; 1167import {BusinessError} from '@ohos.base'; 1168 1169// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. 1170 1171// Connect to the tag if it is 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 as required. 1181 let rawData = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 1182 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]; // It must be 16 bytes. 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 1198Writes data to a block on this tag. This API uses an asynchronous callback to return the result. 1199 1200**Required permissions**: ohos.permission.NFC_TAG 1201 1202**System capability**: SystemCapability.Communication.NFC.Tag 1203 1204**Parameters** 1205 1206| Name | Type | Mandatory| Description | 1207| -------- | ----------------------- | ---- | -------------------------------------- | 1208| blockIndex | number | Yes | Index of the target block. The block indexes start from **0**.| 1209| data | number[] | Yes | 16-byte data to write.| 1210| callback | AsyncCallback\<void> | Yes | Callback invoked to return the result.| 1211 1212**Error codes** 1213 1214For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 1215 1216| ID| Error Message| 1217| ------- | -------| 1218| 3100201 | Tag running state is abnormal in service. | 1219 1220**Example** 1221 1222```js 1223import tag from '@ohos.nfc.tag'; 1224import {BusinessError} from '@ohos.base'; 1225 1226// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. 1227 1228// Connect to the tag if it is 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 as required. 1238 let rawData = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 1239 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]; // It must be 16 bytes. 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 1257Increments a block with the specified value and saves the result in a buffer for internal transmission. This API uses a promise to return the result. This API uses a promise to return the result. 1258 1259**Required permissions**: ohos.permission.NFC_TAG 1260 1261**System capability**: SystemCapability.Communication.NFC.Tag 1262 1263**Parameters** 1264 1265| Name | Type | Mandatory| Description | 1266| -------- | ----------------------- | ---- | -------------------------------------- | 1267| blockIndex | number | Yes | Index of the block to increment. The block indexes start from **0**.| 1268| value | number | Yes | Block data to increment. The value cannot be a negative number.| 1269 1270**Error codes** 1271 1272For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 1273 1274| ID| Error Message| 1275| ------- | -------| 1276| 3100201 | Tag running state is abnormal in service. | 1277 1278**Example** 1279 1280```js 1281import tag from '@ohos.nfc.tag'; 1282import {BusinessError} from '@ohos.base'; 1283 1284// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. 1285 1286// Connect to the tag if it is 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 as required. 1296 let value = 0x20; // Change it as required. 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 1312Increments a block with the specified value and saves the result in a buffer for internal transmission. This API uses an asynchronous callback to return the result. This API uses an asynchronous callback to return the result. 1313 1314**Required permissions**: ohos.permission.NFC_TAG 1315 1316**System capability**: SystemCapability.Communication.NFC.Tag 1317 1318**Parameters** 1319 1320| Name | Type | Mandatory| Description | 1321| -------- | ----------------------- | ---- | -------------------------------------- | 1322| blockIndex | number | Yes | Index of the block to increment. The block indexes start from **0**.| 1323| value | number | Yes | Block data to increment. The value cannot be a negative number.| 1324| callback | AsyncCallback\<void> | Yes | Callback invoked to return the result.| 1325 1326**Error codes** 1327 1328For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 1329 1330| ID| Error Message| 1331| ------- | -------| 1332| 3100201 | Tag running state is abnormal in service. | 1333 1334**Example** 1335 1336```js 1337import tag from '@ohos.nfc.tag'; 1338import {BusinessError} from '@ohos.base'; 1339 1340// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. 1341 1342// Connect to the tag if it is 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 as required. 1352 let value = 0x20; // Change it as required. 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 1370Decrements a block with the specified value and saves the result in a buffer for internal transmission. This API uses a promise to return the result. This API uses a promise to return the result. 1371 1372**Required permissions**: ohos.permission.NFC_TAG 1373 1374**System capability**: SystemCapability.Communication.NFC.Tag 1375 1376**Parameters** 1377 1378| Name | Type | Mandatory| Description | 1379| -------- | ----------------------- | ---- | -------------------------------------- | 1380| blockIndex | number | Yes | Index of the block to decrement. The block indexes start from **0**.| 1381| value | number | Yes | Block data to decrement. The value cannot be a negative number.| 1382 1383**Error codes** 1384 1385For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 1386 1387| ID| Error Message| 1388| ------- | -------| 1389| 3100201 | Tag running state is abnormal in service. | 1390 1391**Example** 1392 1393```js 1394import tag from '@ohos.nfc.tag'; 1395import {BusinessError} from '@ohos.base'; 1396 1397// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. 1398 1399// Connect to the tag if it is 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 as required. 1409 let value = 0x20; // Change it as required. 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 1425Decrements a block with the specified value. This API uses an asynchronous callback to return the result. 1426 1427**Required permissions**: ohos.permission.NFC_TAG 1428 1429**System capability**: SystemCapability.Communication.NFC.Tag 1430 1431**Parameters** 1432 1433| Name | Type | Mandatory| Description | 1434| -------- | ----------------------- | ---- | -------------------------------------- | 1435| blockIndex | number | Yes | Index of the block to decrement. The block indexes start from **0**.| 1436| value | number | Yes | Block data to decrement. The value cannot be a negative number.| 1437| callback | AsyncCallback\<void> | Yes | Callback invoked to return the result.| 1438 1439**Error codes** 1440 1441For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 1442 1443| ID| Error Message| 1444| ------- | -------| 1445| 3100201 | Tag running state is abnormal in service. | 1446 1447**Example** 1448 1449```js 1450import tag from '@ohos.nfc.tag'; 1451import {BusinessError} from '@ohos.base'; 1452 1453// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. 1454 1455// Connect to the tag if it is 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 as required. 1465 let value = 0x20; // Change it as required. 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 1483Transfers data from the temporary register to a block. This API uses a promise to return the result. 1484 1485**Required permissions**: ohos.permission.NFC_TAG 1486 1487**System capability**: SystemCapability.Communication.NFC.Tag 1488 1489**Parameters** 1490 1491| Name | Type | Mandatory| Description | 1492| -------- | ----------------------- | ---- | -------------------------------------- | 1493| blockIndex | number | Yes | Index of the destination block. The value starts form **0**.| 1494 1495**Error codes** 1496 1497For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 1498 1499| ID| Error Message| 1500| ------- | -------| 1501| 3100201 | Tag running state is abnormal in service. | 1502 1503**Example** 1504 1505```js 1506import tag from '@ohos.nfc.tag'; 1507import {BusinessError} from '@ohos.base'; 1508 1509// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. 1510 1511// Connect to the tag if it is 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 as required. 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 1536Transfers data from the temporary register to a block. This API uses an asynchronous callback to return the result. 1537 1538**Required permissions**: ohos.permission.NFC_TAG 1539 1540**System capability**: SystemCapability.Communication.NFC.Tag 1541 1542**Parameters** 1543 1544| Name | Type | Mandatory| Description | 1545| -------- | ----------------------- | ---- | -------------------------------------- | 1546| blockIndex | number | Yes | Index of the destination block. The value starts form **0**.| 1547| callback | AsyncCallback\<void> | Yes | Callback invoked to return the result.| 1548 1549**Error codes** 1550 1551For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 1552 1553| ID| Error Message| 1554| ------- | -------| 1555| 3100201 | Tag running state is abnormal in service. | 1556 1557**Example** 1558 1559```js 1560import tag from '@ohos.nfc.tag'; 1561import {BusinessError} from '@ohos.base'; 1562 1563// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. 1564 1565// Connect to the tag if it is 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 as required. 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 1592Restores data in the temporary register from a block. This API uses a promise to return the result. 1593 1594**Required permissions**: ohos.permission.NFC_TAG 1595 1596**System capability**: SystemCapability.Communication.NFC.Tag 1597 1598**Parameters** 1599 1600| Name | Type | Mandatory| Description | 1601| -------- | ----------------------- | ---- | -------------------------------------- | 1602| blockIndex | number | Yes | Index of the target block. The value starts form **0**.| 1603 1604**Error codes** 1605 1606For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 1607 1608| ID| Error Message| 1609| ------- | -------| 1610| 3100201 | Tag running state is abnormal in service. | 1611 1612**Example** 1613 1614```js 1615import tag from '@ohos.nfc.tag'; 1616import {BusinessError} from '@ohos.base'; 1617 1618// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. 1619 1620// Connect to the tag if it is 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 as required. 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 1645Restores data in the temporary register from a block. This API uses an asynchronous callback to return the result. 1646 1647**Required permissions**: ohos.permission.NFC_TAG 1648 1649**System capability**: SystemCapability.Communication.NFC.Tag 1650 1651**Parameters** 1652 1653| Name | Type | Mandatory| Description | 1654| -------- | ----------------------- | ---- | -------------------------------------- | 1655| blockIndex | number | Yes | Index of the target block. The value starts form **0**.| 1656| callback | AsyncCallback\<void> | Yes | Callback invoked to return the result.| 1657 1658**Error codes** 1659 1660For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 1661 1662| ID| Error Message| 1663| ------- | -------| 1664| 3100201 | Tag running state is abnormal in service. | 1665 1666**Example** 1667 1668```js 1669import tag from '@ohos.nfc.tag'; 1670import {BusinessError} from '@ohos.base'; 1671 1672// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. 1673 1674// Connect to the tag if it is 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 as required. 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 1701Obtains the number of sectors in this MIFARE Classic tag. 1702 1703**System capability**: SystemCapability.Communication.NFC.Tag 1704 1705**Return value** 1706 1707| **Type**| **Description** | 1708| ------------------ | --------------------------| 1709| number | Number of sectors obtained.| 1710 1711**Example** 1712 1713```js 1714import tag from '@ohos.nfc.tag'; 1715import {BusinessError} from '@ohos.base'; 1716 1717// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained '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 1726Obtains the number of blocks in a sector. 1727 1728**System capability**: SystemCapability.Communication.NFC.Tag 1729 1730**Parameters** 1731 1732| Name | Type | Mandatory| Description | 1733| -------- | ----------------------- | ---- | -------------------------------------- | 1734| sectorIndex | number | Yes | Index of the target sector. The sector indexes start from **0**.| 1735 1736**Return value** 1737 1738| **Type**| **Description** | 1739| ------------------ | --------------------------| 1740| number | Number of blocks obtained.| 1741 1742**Example** 1743 1744```js 1745import tag from '@ohos.nfc.tag'; 1746import {BusinessError} from '@ohos.base'; 1747 1748// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. 1749 1750try { 1751 let sectorIndex = 1; // Change it as required. 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 1764Obtains the type of this MIFARE Classic tag. 1765 1766**System capability**: SystemCapability.Communication.NFC.Tag 1767 1768**Return value** 1769 1770| **Type**| **Description** | 1771| ------------------ | --------------------------| 1772| [tag.MifareClassicType](js-apis-nfcTag.md#mifareclassictype9) | Type of the MIFARE Classic tag obtained.| 1773 1774**Example** 1775 1776```js 1777import tag from '@ohos.nfc.tag'; 1778 1779// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained '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 1788Obtains the size of this tag. For details, see [MifareClassicSize](js-apis-nfcTag.md#mifareclassicsize9). 1789 1790**System capability**: SystemCapability.Communication.NFC.Tag 1791 1792**Return value** 1793 1794| **Type**| **Description** | 1795| ------------------ | --------------------------| 1796| number | Tag size obtained, in bytes. For details, see [MifareClassicSize](js-apis-nfcTag.md#mifareclassicsize9).| 1797 1798**Example** 1799 1800```js 1801import tag from '@ohos.nfc.tag'; 1802import {BusinessError} from '@ohos.base'; 1803 1804// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. 1805let tagSize : number = mifareClassic.getTagSize(); 1806console.log("mifareClassic tagSize: " + tagSize); 1807``` 1808 1809### MifareClassicTag.isEmulatedTag<sup>9+</sup> 1810 1811isEmulatedTag(): boolean 1812 1813Checks whether it is an emulated tag. 1814 1815**System capability**: SystemCapability.Communication.NFC.Tag 1816 1817**Return value** 1818 1819| **Type**| **Description** | 1820| ------------------ | --------------------------| 1821| boolean |Returns **true** if the tag is an emulated tag; returns **false** otherwise.| 1822 1823**Example** 1824 1825```js 1826import tag from '@ohos.nfc.tag'; 1827import {BusinessError} from '@ohos.base'; 1828 1829// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained '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 1838Obtains the index of the first block in a sector. 1839 1840**System capability**: SystemCapability.Communication.NFC.Tag 1841 1842**Parameters** 1843 1844| Name | Type | Mandatory| Description | 1845| -------- | ----------------------- | ---- | -------------------------------------- | 1846| sectorIndex | number | Yes | Index of the target sector. The sector indexes start from **0**.| 1847 1848**Return value** 1849 1850| **Type**| **Description** | 1851| ------------------ | --------------------------| 1852| number | Index of the first block obtained.| 1853 1854**Example** 1855 1856```js 1857import tag from '@ohos.nfc.tag'; 1858import {BusinessError} from '@ohos.base'; 1859 1860// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. 1861 1862try { 1863 let sectorIndex = 1; // Change it as required. 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 1876Obtains the index of the sector that holds the specified block. 1877 1878**System capability**: SystemCapability.Communication.NFC.Tag 1879 1880**Parameters** 1881 1882| Name | Type | Mandatory| Description | 1883| -------- | ----------------------- | ---- | -------------------------------------- | 1884| blockIndex | number | Yes| Index of the block. The block indexes start from **0**.| 1885 1886**Return value** 1887 1888| **Type**| **Description** | 1889| ------------------ | --------------------------| 1890| number | Index of the sector obtained.| 1891 1892**Example** 1893 1894```js 1895import tag from '@ohos.nfc.tag'; 1896import {BusinessError} from '@ohos.base'; 1897 1898// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. 1899 1900try { 1901 let blockIndex = 1; // Change it as required. 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 1912Provides APIs to access MIFARE Ultralight properties and perform I/O operations on a tag. This class inherits from **TagSession**. 1913 1914**TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md). 1915 1916The following describes the unique APIs of **MifareUltralightTag**. 1917 1918### MifareUltralightTag.readMultiplePages<sup>9+</sup> 1919 1920readMultiplePages(pageIndex: number): Promise\<number[]> 1921 1922Reads four pages of data (4 bytes per page) from this tag. This API uses a promise to return the result. 1923 1924**Required permissions**: ohos.permission.NFC_TAG 1925 1926**System capability**: SystemCapability.Communication.NFC.Tag 1927 1928**Parameters** 1929 1930| Name | Type | Mandatory| Description | 1931| -------- | ----------------------- | ---- | ------------------------------ | 1932| pageIndex | number | Yes | Index of the first page to read. The page indexes start from **0**.| 1933 1934**Return value** 1935 1936| **Type**| **Description** | 1937| ------------------ | --------------------------| 1938| Promise\<number[]> | Promise used to return the data read, which is 16 bytes in total.| 1939 1940**Error codes** 1941 1942For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 1943 1944| ID| Error Message| 1945| ------- | -------| 1946| 3100201 | Tag running state is abnormal in service. | 1947 1948**Example** 1949 1950```js 1951 1952import tag from '@ohos.nfc.tag'; 1953import {BusinessError} from '@ohos.base'; 1954 1955// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareUltralight' correctly. 1956 1957// Connect to the tag if it is 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 as required. 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 1982Reads four pages of data (4 bytes per page) from this tag. This API uses an asynchronous callback to return the result. 1983 1984**Required permissions**: ohos.permission.NFC_TAG 1985 1986**System capability**: SystemCapability.Communication.NFC.Tag 1987 1988**Parameters** 1989 1990| Name | Type | Mandatory| Description | 1991| -------- | ----------------------- | ---- | -------------------------------------- | 1992| pageIndex | number | Yes | Index of the first page to read. The page indexes start from **0**.| 1993| callback | AsyncCallback\<number[]> | Yes | Callback invoked to return the data read, which is 16 bytes in total.| 1994 1995**Error codes** 1996 1997For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 1998 1999| ID| Error Message| 2000| ------- | -------| 2001| 3100201 | Tag running state is abnormal in service. | 2002 2003**Example** 2004 2005```js 2006import tag from '@ohos.nfc.tag'; 2007import {BusinessError} from '@ohos.base'; 2008 2009// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareUltralight' correctly. 2010 2011// Connect to the tag if it is 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 as required. 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 2038Writes one page (4 bytes) of data to this tag. This API uses a promise to return the result. 2039 2040**Required permissions**: ohos.permission.NFC_TAG 2041 2042**System capability**: SystemCapability.Communication.NFC.Tag 2043 2044**Parameters** 2045 2046| Name | Type | Mandatory| Description | 2047| -------- | ----------------------- | ---- | -------------------------------------- | 2048| pageIndex | number | Yes | Index of the page to write. The page indexes start from **0**.| 2049| data | number[] | Yes | 4-byte data to write.| 2050 2051**Error codes** 2052 2053For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 2054 2055| ID| Error Message| 2056| ------- | -------| 2057| 3100201 | Tag running state is abnormal in service. | 2058 2059**Example** 2060 2061```js 2062import tag from '@ohos.nfc.tag'; 2063import {BusinessError} from '@ohos.base'; 2064 2065// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareUltralight' correctly. 2066 2067// Connect to the tag if it is 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 as required. 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 2093Writes one page (4 bytes) of data to this tag. This API uses an asynchronous callback to return the result. 2094 2095**Required permissions**: ohos.permission.NFC_TAG 2096 2097**System capability**: SystemCapability.Communication.NFC.Tag 2098 2099**Parameters** 2100 2101| Name | Type | Mandatory| Description | 2102| -------- | ----------------------- | ---- | ------------------------ | 2103| pageIndex | number | Yes | Index of the page to write. The page indexes start from **0**.| 2104| data | number[] | Yes | 4-byte data to write.| 2105| callback|AsyncCallback\<void> |Yes| Callback invoked to return the result.| 2106 2107**Error codes** 2108 2109For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 2110 2111| ID| Error Message| 2112| ------- | -------| 2113| 3100201 | Tag running state is abnormal in service. | 2114 2115**Example** 2116 2117```js 2118import tag from '@ohos.nfc.tag'; 2119import {BusinessError} from '@ohos.base'; 2120 2121// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareUltralight' correctly. 2122 2123// Connect to the tag if it is 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 as required. 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 2151Obtains the type of this MIFARE Ultralight tag. 2152 2153**System capability**: SystemCapability.Communication.NFC.Tag 2154 2155**Return value** 2156 2157| **Type**| **Description** | 2158| ------------------ | --------------------------| 2159| [tag.MifareUltralightType](js-apis-nfcTag.md#mifareultralighttype9) | Type of the MIFARE Ultralight tag obtained.| 2160 2161**Example** 2162 2163```js 2164import tag from '@ohos.nfc.tag'; 2165 2166// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareUltralight' correctly. 2167let getType : tag.MifareUltralightType = mifareClassic.getType(); 2168console.log("mifareUltralight getType: " + getType); 2169``` 2170 2171## NdefFormatableTag<sup>9+</sup> 2172 2173Provides APIs for formatting NDEF formattable tags. This class inherits from **TagSession**. 2174 2175**TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md). 2176 2177The following describes the unique APIs of **NdefFormatableTag**. 2178 2179### NdefFormatableTag.format<sup>9+</sup> 2180 2181format(message: [NdefMessage](#ndefmessage9)): Promise\<void> 2182 2183Formats this tag as an NDEF tag, and writes an NDEF message to it. This API uses a promise to return the result. 2184 2185**Required permissions**: ohos.permission.NFC_TAG 2186 2187**System capability**: SystemCapability.Communication.NFC.Tag 2188 2189**Parameters** 2190 2191| Name | Type | Mandatory| Description | 2192| -------- | ----------------------- | ---- | -------------------------------------- | 2193| message | [NdefMessage](#ndefmessage9) | Yes | NDEF message to write. If this parameter is **null**, the tag is formatted only (no data will be written).| 2194 2195**Error codes** 2196 2197For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 2198 2199| ID| Error Message| 2200| ------- | -------| 2201| 3100201 | Tag running state is abnormal in service. | 2202 2203**Example** 2204 2205```js 2206import tag from '@ohos.nfc.tag'; 2207import {BusinessError} from '@ohos.base'; 2208 2209// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefFormatable' correctly. 2210 2211// Connect to the tag if it is not connected. 2212if (!ndefFormatable.isTagConnected()) { 2213 if (!ndefFormatable.connectTag()) { 2214 console.log("ndefFormatable connectTag failed."); 2215 return; 2216 } 2217} 2218 2219try { 2220 // NDEF message created from raw data, such as: 2221 let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // It must 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 2239Formats this tag as an NDEF tag, and writes an NDEF message to it. This API uses an asynchronous callback to return the result. 2240 2241**Required permissions**: ohos.permission.NFC_TAG 2242 2243**System capability**: SystemCapability.Communication.NFC.Tag 2244 2245**Parameters** 2246 2247| Name | Type | Mandatory| Description | 2248| -------- | ----------------------- | ---- | -------------------------------------- | 2249| message | [NdefMessage](#ndefmessage9) | Yes | NDEF message to write when the formatting is successful. If this parameter is **null**, the tag is formatted only (no data will be written).| 2250 2251**Return value** 2252 2253| **Type**| **Description** | 2254| ------------------ | --------------------------| 2255| callback: AsyncCallback\<void> | Callback invoked to return the result.| 2256 2257**Error codes** 2258 2259For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 2260 2261| ID| Error Message| 2262| ------- | -------| 2263| 3100201 | Tag running state is abnormal in service. | 2264 2265**Example** 2266 2267```js 2268import tag from '@ohos.nfc.tag'; 2269import {BusinessError} from '@ohos.base'; 2270 2271// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefFormatable' correctly. 2272 2273// Connect to the tag if it is not connected. 2274if (!ndefFormatable.isTagConnected()) { 2275 if (!ndefFormatable.connectTag()) { 2276 console.log("ndefFormatable connectTag failed."); 2277 return; 2278 } 2279} 2280 2281try { 2282 // NDEF message created from raw data, such as: 2283 let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // It must 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 2303Formats this tag as an NDEF tag, writes an NDEF message to it, and then sets the tag to read-only. This API uses a promise to return the result. 2304 2305**Required permissions**: ohos.permission.NFC_TAG 2306 2307**System capability**: SystemCapability.Communication.NFC.Tag 2308 2309**Parameters** 2310 2311| Name | Type | Mandatory| Description | 2312| -------- | ----------------------- | ---- | -------------------------------------- | 2313| message | [NdefMessage](#ndefmessage9) | Yes | NDEF message to write. If this parameter is **null**, the tag is formatted only (no data will be written).| 2314 2315**Error codes** 2316 2317For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 2318 2319| ID| Error Message| 2320| ------- | -------| 2321| 3100201 | Tag running state is abnormal in service. | 2322 2323**Example** 2324 2325```js 2326import tag from '@ohos.nfc.tag'; 2327import {BusinessError} from '@ohos.base'; 2328 2329// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefFormatable' correctly. 2330 2331// Connect to the tag if it is not connected. 2332if (!ndefFormatable.isTagConnected()) { 2333 if (!ndefFormatable.connectTag()) { 2334 console.log("ndefFormatable connectTag failed."); 2335 return; 2336 } 2337} 2338 2339try { 2340 // NDEF message created from raw data, such as: 2341 let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // It must 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 2359Formats this tag as an NDEF tag, writes an NDEF message to the NDEF tag, and then sets the tag to read-only. This API uses an asynchronous callback to return the result. 2360 2361**Required permissions**: ohos.permission.NFC_TAG 2362 2363**System capability**: SystemCapability.Communication.NFC.Tag 2364 2365**Parameters** 2366 2367| Name | Type | Mandatory| Description | 2368| -------- | ----------------------- | ---- | -------------------------------------- | 2369| message | [NdefMessage](#ndefmessage9) | Yes | NDEF message to write. If this parameter is **null**, the tag is formatted only (no data will be written).| 2370 2371**Return value** 2372 2373| **Type**| **Description** | 2374| ------------------ | --------------------------| 2375| callback: AsyncCallback\<void> | Callback invoked to return the result.| 2376 2377**Error codes** 2378 2379For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 2380 2381| ID| Error Message| 2382| ------- | -------| 2383| 3100201 | Tag running state is abnormal in service. | 2384 2385**Example** 2386 2387```js 2388import tag from '@ohos.nfc.tag'; 2389import {BusinessError} from '@ohos.base'; 2390 2391// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefFormatable' correctly. 2392 2393// Connect to the tag if it is not connected. 2394if (!ndefFormatable.isTagConnected()) { 2395 if (!ndefFormatable.connectTag()) { 2396 console.log("ndefFormatable connectTag failed."); 2397 return; 2398 } 2399} 2400 2401try { 2402 // NDEF message created from raw data, such as: 2403 let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // It must 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