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> **NOTE** 10> 11> If an error is reported while importing the tag module editor, the capabilities of a specific device model may exceed the capability set defined for the default device. To use these capabilities, configure a custom SysCap by following instructions in [SystemCapability Development](https://developer.huawei.com/consumer/en/doc/harmonyos-references/syscap). 12 13## Modules to Import 14 15```js 16import { tag } from '@kit.ConnectivityKit'; 17``` 18 19## NfcATag 20 21Provides 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)**. 22 23**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). 24 25For details about how to obtain an **NfcATag** object, see [NFC Tag Read/Write Development](../../connectivity/nfc/nfc-tag-access-guide.md). 26 27The following describes the unique APIs of **NfcATag**. 28 29### NfcATag.getSak 30 31getSak(): number 32 33Obtains the SAK value of this NFC-A tag. 34 35**System capability**: SystemCapability.Communication.NFC.Tag 36 37**Atomic service API**: This API can be used in atomic services since API version 12. 38 39**Return value** 40 41| **Type**| **Description** | 42| ------------------ | --------------------------| 43| number | SAK value obtained. The SAK is a hexadecimal number ranging from **0x00** to **0xFF**.| 44 45**Example** 46 47```js 48import { tag } from '@kit.ConnectivityKit'; 49 50// Obtain the correct nfcA tag by using the tag.TagInfo API in @ohos.nfc.tag. 51let sak : number = nfcA.getSak(); 52console.info("nfcA sak: " + sak); 53``` 54 55### NfcATag.getAtqa 56 57getAtqa(): number[] 58 59Obtains the ATQA value of this NFC-A tag. 60 61**System capability**: SystemCapability.Communication.NFC.Tag 62 63**Atomic service API**: This API can be used in atomic services since API version 12. 64 65**Return value** 66 67| **Type**| **Description** | 68| ------------------ | --------------------------| 69| number[] | ATQA value obtained. Each number of the ATQA is a hexadecimal number ranging from **0x00** to **0xFF**.| 70 71**Example** 72 73```js 74import { tag } from '@kit.ConnectivityKit'; 75 76// Obtain the correct nfcA tag by using the tag.TagInfo API in @ohos.nfc.tag. 77let atqa : number[] = nfcA.getAtqa(); 78console.info("nfcA atqa: " + atqa); 79``` 80 81## NfcBTag 82 83Provides APIs to access NFC-B (ISO 14443-3B) properties and perform I/O operations on a tag. This class inherits from **TagSession**. 84 85**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). 86 87For details about how to obtain an **NfcBTag** object, see [NFC Tag Read/Write Development](../../connectivity/nfc/nfc-tag-access-guide.md). 88 89The following describes the unique APIs of **NfcBTag**. 90 91### NfcBTag.getRespAppData 92 93getRespAppData(): number[] 94 95Obtains the application data of this NFC-B tag. 96 97**System capability**: SystemCapability.Communication.NFC.Tag 98 99**Atomic service API**: This API can be used in atomic services since API version 12. 100 101**Return value** 102 103| **Type**| **Description** | 104| ------------------ | --------------------------| 105| number[] | Application data obtained, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.| 106 107**Example** 108 109```js 110import { tag } from '@kit.ConnectivityKit'; 111 112// Obtain the correct nfcB tag by using the tag.TagInfo API in @ohos.nfc.tag. 113let respAppData : number[] = nfcB.getRespAppData(); 114console.info("nfcB respAppData: " + respAppData); 115``` 116 117### NfcBTag.getRespProtocol 118 119getRespProtocol(): number[] 120 121Obtains the protocol information of this NFC-B tag. 122 123**System capability**: SystemCapability.Communication.NFC.Tag 124 125**Atomic service API**: This API can be used in atomic services since API version 12. 126 127**Return value** 128 129| **Type**| **Description** | 130| ------------------ | --------------------------| 131| number[] | Protocol information obtained, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.| 132 133**Example** 134 135```js 136import { tag } from '@kit.ConnectivityKit'; 137 138// Obtain the correct nfcB tag by using the tag.TagInfo API in @ohos.nfc.tag. 139let respProtocol : number[] = nfcB.getRespProtocol(); 140console.info("nfcB respProtocol: " + respProtocol); 141``` 142 143## NfcFTag 144 145Provides APIs to access NFC-F (JIS 6319-4) properties and perform I/O operations on a tag. This class inherits from **TagSession**. 146 147**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). 148 149For details about how to obtain an **NfcFTag** object, see [NFC Tag Read/Write Development](../../connectivity/nfc/nfc-tag-access-guide.md). 150 151The following describes the unique APIs of **NfcFTag**. 152 153### NfcFTag.getSystemCode 154 155getSystemCode(): number[] 156 157Obtains the system code from this NFC-F tag. 158 159**System capability**: SystemCapability.Communication.NFC.Tag 160 161**Atomic service API**: This API can be used in atomic services since API version 12. 162 163**Return value** 164 165| **Type**| **Description** | 166| ------------------ | --------------------------| 167| number[] | System code obtained, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.| 168 169**Example** 170 171```js 172import { tag } from '@kit.ConnectivityKit'; 173 174// Obtain the correct nfcF tag by using the tag.TagInfo API in @ohos.nfc.tag. 175let systemCode : number[] = nfcF.getSystemCode(); 176console.info("nfcF systemCode: " + systemCode); 177``` 178 179### NfcFTag.getPmm 180 181getPmm(): number[] 182 183Obtains the PMm (consisting of the IC code and manufacturer parameters) information from this NFC-F tag. 184 185**System capability**: SystemCapability.Communication.NFC.Tag 186 187**Atomic service API**: This API can be used in atomic services since API version 12. 188 189**Return value** 190 191| **Type**| **Description** | 192| ------------------ | --------------------------| 193| number[] | PMm information obtained, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.| 194 195**Example** 196 197```js 198import { tag } from '@kit.ConnectivityKit'; 199 200// Obtain the correct nfcF tag by using the tag.TagInfo API in @ohos.nfc.tag. 201let pmm : number[] = nfcF.getPmm(); 202console.info("nfcF pmm: " + pmm); 203``` 204 205## NfcVTag 206 207Provides APIs to access NFC-V (ISO 15693) properties and perform I/O operations on a tag. This class inherits from **TagSession**. 208 209**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). 210 211For details about how to obtain an **NfcVTag** object, see [NFC Tag Read/Write Development](../../connectivity/nfc/nfc-tag-access-guide.md). 212 213The following describes the unique APIs of **NfcVTag**. 214 215### NfcvTag.getResponseFlags 216 217getResponseFlags(): number 218 219Obtains the response flags from this NFC-V tag. 220 221**System capability**: SystemCapability.Communication.NFC.Tag 222 223**Atomic service API**: This API can be used in atomic services since API version 12. 224 225**Return value** 226 227| **Type**| **Description** | 228| ------------------ | --------------------------| 229| number | Response flags obtained, which consist of hexadecimal numbers ranging from **0x00** to **0xFF**.| 230 231**Example** 232 233```js 234import { tag } from '@kit.ConnectivityKit'; 235 236// Obtain the correct nfcV tag by using the tag.TagInfo API in @ohos.nfc.tag. 237let responseFlags : number = nfcV.getResponseFlags(); 238console.info("nfcV responseFlags: " + responseFlags); 239``` 240 241### NfcvTag.getDsfId 242 243getDsfId(): number 244 245Obtains the data storage format identifier (DSFID) from this NFC-V tag. 246 247**System capability**: SystemCapability.Communication.NFC.Tag 248 249**Atomic service API**: This API can be used in atomic services since API version 12. 250 251**Return value** 252 253| **Type**| **Description** | 254| ------------------ | --------------------------| 255| number | DSFID obtained, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.| 256 257**Example** 258 259```js 260import { tag } from '@kit.ConnectivityKit'; 261 262// Obtain the correct nfcV tag by using the tag.TagInfo API in @ohos.nfc.tag. 263let dsfId : number = nfcV.getDsfId(); 264console.info("nfcV dsfId: " + dsfId); 265``` 266 267## IsoDepTag<sup>9+</sup> 268 269Provides APIs to access ISO-DEP (ISO 14443-4) properties and I/O operations on a tag. This class inherits from **TagSession**. 270 271**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). 272 273For details about how to obtain an **IsoDepTag** object, see [NFC Tag Read/Write Development](../../connectivity/nfc/nfc-tag-access-guide.md). 274 275The following describes the unique APIs of **IsoDepTag**. 276 277### IsoDepTag.getHistoricalBytes<sup>9+</sup> 278 279getHistoricalBytes(): number[] 280 281Obtains the historical bytes for the given tag. This API applies only to the IsoDep tags that use the NFC-A technology. 282 283**System capability**: SystemCapability.Communication.NFC.Tag 284 285**Atomic service API**: This API can be used in atomic services since API version 12. 286 287**Return value** 288 289| **Type**| **Description** | 290| ------------------ | --------------------------| 291| 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.| 292 293**Example** 294 295```js 296import { tag } from '@kit.ConnectivityKit'; 297 298// Obtain the correct isoDep tag by using the tag.TagInfo API in @ohos.nfc.tag. 299let historicalBytes : number[] = isoDep.getHistoricalBytes(); 300console.info("isoDep historicalBytes: " + historicalBytes); 301``` 302 303### IsoDepTag.getHiLayerResponse<sup>9+</sup> 304 305getHiLayerResponse(): number[] 306 307Obtains the higher-layer response bytes for the given tag. This API applies only to the IsoDep tags that use the NFC-B technology. 308 309**System capability**: SystemCapability.Communication.NFC.Tag 310 311**Atomic service API**: This API can be used in atomic services since API version 12. 312 313**Return value** 314 315| **Type**| **Description** | 316| ------------------ | --------------------------| 317| 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.| 318 319**Example** 320 321```js 322import { tag } from '@kit.ConnectivityKit'; 323 324// Obtain the correct isoDep tag by using the tag.TagInfo API in @ohos.nfc.tag. 325let hiLayerResponse : number[] = isoDep.getHiLayerResponse(); 326console.info("isoDep hiLayerResponse: " + hiLayerResponse); 327``` 328 329### IsoDepTag.isExtendedApduSupported<sup>9+</sup> 330 331isExtendedApduSupported(): Promise<boolean> 332 333Checks whether an extended application protocol data unit (APDU) is supported. This API uses a promise to return the result. 334 335**Required permissions**: ohos.permission.NFC_TAG 336 337**System capability**: SystemCapability.Communication.NFC.Tag 338 339**Atomic service API**: This API can be used in atomic services since API version 12. 340 341**Return value** 342 343| **Type**| **Description** | 344| ------------------ | --------------------------| 345| Promise<boolean> | Promise used to return the result. The value **true** means the extended APDU is supported; the value **false** means the opposite.| 346 347**Error codes** 348 349For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 350 351| ID| Error Message| 352| ------- | -------| 353| 201 | Permission denied. | 354| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 355| 3100201 | The tag running state is abnormal in the service. | 356| 3100204 | The tag I/O operation failed. | 357 358**Example** 359 360```js 361import { tag } from '@kit.ConnectivityKit'; 362import { BusinessError } from '@kit.BasicServicesKit'; 363 364// Obtain the correct isoDep tag by using the tag.TagInfo API in @ohos.nfc.tag. 365function nfcTechDemo() { 366 // Connect the tag if it has not been connected. 367 if (!isoDep.isTagConnected()) { 368 if (!isoDep.connectTag()) { 369 console.error("isoDep connectTag failed."); 370 return; 371 } 372 } 373 374 try { 375 isoDep.isExtendedApduSupported().then((response: boolean) => { 376 console.info("isoDep isExtendedApduSupported Promise response: " + response); 377 }).catch((err: BusinessError) => { 378 console.error("isoDep isExtendedApduSupported Promise Code: ${err.code}, message: ${err.message}"); 379 }); 380 } catch (businessError) { 381 console.error("isoDep isExtendedApduSupported Promise Code: ${(businessError as Businsess).code}, " + 382 "message: ${(businessError as Businsess).message}"); 383 } 384} 385 386``` 387 388### IsoDepTag.isExtendedApduSupported<sup>9+</sup> 389 390isExtendedApduSupported(callback: AsyncCallback\<boolean>): void 391 392Checks whether an extended APDU is supported. This API uses an asynchronous callback to return the result. 393 394**Required permissions**: ohos.permission.NFC_TAG 395 396**System capability**: SystemCapability.Communication.NFC.Tag 397 398**Atomic service API**: This API can be used in atomic services since API version 12. 399 400**Parameters** 401 402| Name | Type | Mandatory| Description | 403| -------- | ----------------------- | ---- | -------------------------------------- | 404| callback | AsyncCallback\<boolean> | Yes | Callback used to return the operation result. If the extended APDU is supported, **true** is returned; otherwise, **false** is returned.| 405 406**Error codes** 407 408For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 409 410| ID| Error Message| 411| ------- | -------| 412| 201 | Permission denied. | 413| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 414| 3100201 | The tag running state is abnormal in the service. | 415| 3100204 | The Tag I/O operation failed. | 416 417**Example** 418 419```js 420import { tag } from '@kit.ConnectivityKit'; 421import { BusinessError } from '@kit.BasicServicesKit'; 422 423// Obtain the correct isoDep tag by using the tag.TagInfo API in @ohos.nfc.tag. 424function nfcTechDemo() { 425 // Connect the tag if it has not been connected. 426 if (!isoDep.isTagConnected()) { 427 if (!isoDep.connectTag()) { 428 console.error("isoDep connectTag failed."); 429 return; 430 } 431 } 432 433 try { 434 isoDep.isExtendedApduSupported((err: BusinessError, response: boolean) => { 435 if (err) { 436 console.error("isoDep isExtendedApduSupported AsyncCallback Code: ${err.code}, message: ${err. message}"); 437 } else { 438 console.info("isoDep isExtendedApduSupported AsyncCallback response: " + response); 439 } 440 }); 441 } catch (busiErr) { 442 console.error("isoDep isExtendedApduSupported AsyncCallback Code: ${(businessError as Business).code}, " + "message: ${(businessError as Business).message}"); 443 } 444} 445 446``` 447 448## NdefMessage<sup>9+</sup> 449 450### NdefMessage.getNdefRecords<sup>9+</sup> 451 452getNdefRecords(): [tag.NdefRecord](js-apis-nfcTag.md#ndefrecord9)[] 453 454Obtains all NDEF records. 455 456**System capability**: SystemCapability.Communication.NFC.Tag 457 458**Atomic service API**: This API can be used in atomic services since API version 12. 459 460**Return value** 461 462| **Type**| **Description** | 463| ------------------ | --------------------------| 464| [tag.NdefRecord](js-apis-nfcTag.md#ndefrecord9)[] | List of NDEF records obtained. For details, see *NFCForum-TS-NDEF_1.0*.| 465 466**Example** 467 468```js 469import { tag } from '@kit.ConnectivityKit'; 470 471// Obtain ndefMessage from tag.ndef.createNdefMessage or ndefTag.getNdefMessage. 472// let ndefMessage : tag.NdefMessage = tag.ndef.createNdefMessage(...); 473// let ndefMessage : tag.NdefMessage = ndefTag.getNdefMessage(); 474 475let ndefRecords : tag.NdefRecord[] = ndefMessage.getNdefRecords(); 476console.info("ndef ndefRecords number: " + ndefRecords.length); 477``` 478 479## NdefTag<sup>9+</sup> 480 481Provides APIs to access the tags in the NFC Data Exchange Format (NDEF). This class inherits from **TagSession**. 482 483**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). 484 485For details about how to obtain an **NdefTag** object, see [NFC Tag Read/Write Development](../../connectivity/nfc/nfc-tag-access-guide.md). 486 487The following describes the unique APIs of **NdefTag**. 488 489### NdefTag.getNdefTagType<sup>9+</sup> 490 491getNdefTagType(): [tag.NfcForumType](js-apis-nfcTag.md#nfcforumtype9) 492 493Obtains the NDEF tag type. 494 495**System capability**: SystemCapability.Communication.NFC.Tag 496 497**Atomic service API**: This API can be used in atomic services since API version 12. 498 499**Return value** 500 501| **Type**| **Description** | 502| ------------------ | --------------------------| 503| [tag.NfcForumType](js-apis-nfcTag.md#nfcforumtype9) | NDEF tag type obtained. It can be NFC FORUM TYPE 1, 2, 3, or 4.| 504 505**Example** 506 507```js 508import { tag } from '@kit.ConnectivityKit'; 509 510// Obtain the correct ndefTag tag by using the tag.TagInfo API in @ohos.nfc.tag. 511let ndefTagType : tag.NfcForumType = ndefTag.getNdefTagType(); 512console.info("ndef ndefTagType: " + ndefTagType); 513``` 514 515### NdefTag.getNdefMessage<sup>9+</sup> 516 517getNdefMessage(): [NdefMessage](#ndefmessage9) 518 519Obtains the NDEF message from this NDEF tag. 520 521**System capability**: SystemCapability.Communication.NFC.Tag 522 523**Atomic service API**: This API can be used in atomic services since API version 12. 524 525**Return value** 526 527| **Type**| **Description** | 528| ------------------ | --------------------------| 529| [NdefMessage](#ndefmessage9) | NDEF message created. For details, see *NFCForum-TS-NDEF_1.0*.| 530 531**Example** 532```js 533import { tag } from '@kit.ConnectivityKit'; 534 535// Obtain the correct ndefTag tag by using the tag.TagInfo API in @ohos.nfc.tag. 536let ndefMessage : tag.NdefMessage = ndefTag.getNdefMessage(); 537console.info("ndef ndefMessage: " + ndefMessage); 538``` 539 540### NdefTag.isNdefWritable<sup>9+</sup> 541 542isNdefWritable(): boolean 543 544Check whether this NDEF tag is writable. Before calling the data write API, check whether the write operation is supported. 545 546**System capability**: SystemCapability.Communication.NFC.Tag 547 548**Atomic service API**: This API can be used in atomic services since API version 12. 549 550**Return value** 551 552| **Type**| **Description** | 553| ------------------ | --------------------------| 554| boolean | Promise used to return the result. If the tag is writable, **true** is returned; otherwise, **false** is returned.| 555 556**Example** 557 558```js 559import { tag } from '@kit.ConnectivityKit'; 560 561// Obtain the correct ndefTag tag by using the tag.TagInfo API in @ohos.nfc.tag. 562let isWritable : boolean = ndefTag.isNdefWritable(); 563console.info("ndef isNdefWritable: " + isWritable); 564``` 565 566### NdefTag.readNdef<sup>9+</sup> 567 568readNdef(): Promise\<[NdefMessage](#ndefmessage9)> 569 570Reads the NDEF message from this tag. This API uses a promise to return the result. 571 572**Required permissions**: ohos.permission.NFC_TAG 573 574**System capability**: SystemCapability.Communication.NFC.Tag 575 576**Atomic service API**: This API can be used in atomic services since API version 12. 577 578**Return value** 579 580| **Type**| **Description** | 581| ------------------ | --------------------------| 582| Promise\<[NdefMessage](#ndefmessage9)> | Promise used to return the message object read.| 583 584**Error codes** 585 586For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 587 588| ID| Error Message| 589| ------- | -------| 590| 201 | Permission denied. | 591| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 592| 3100201 | The tag running state is abnormal in the service. | 593| 3100204 | The tag I/O operation failed. | 594 595**Example** 596 597```js 598import { tag } from '@kit.ConnectivityKit'; 599import { BusinessError } from '@kit.BasicServicesKit'; 600 601// Obtain the correct ndefTag tag by using the tag.TagInfo API in @ohos.nfc.tag. 602function nfcTechDemo(){ 603 // Connect the tag if it has not been connected. 604 if (!ndefTag.isTagConnected()) { 605 if (!ndefTag.connectTag()) { 606 console.error("ndefTag connectTag failed."); 607 return; 608 } 609 } 610 611 try { 612 ndefTag.readNdef().then((ndefmessage : tag.NdefMessage) => { 613 console.info("ndef readNdef Promise ndefmessage: " + ndefmessage); 614 }).catch((err : BusinessError)=> { 615 console.error("ndef readNdef Promise err Code: ${err.code}, message: ${err.message}"); 616 }); 617 } catch (businessError) { 618 console.error("ndef readNdef Promise catch businessError Code: ${(businessError as BusinessError).code}, " + 619 "message: ${(businessError as BusinessError).message}"); 620 } 621} 622 623``` 624 625### NdefTag.readNdef<sup>9+</sup> 626 627readNdef(callback: AsyncCallback\<[NdefMessage](#ndefmessage9)>): void 628 629Reads the NDEF message from this tag. This API uses an asynchronous callback to return the result. 630 631**Required permissions**: ohos.permission.NFC_TAG 632 633**System capability**: SystemCapability.Communication.NFC.Tag 634 635**Atomic service API**: This API can be used in atomic services since API version 12. 636 637**Parameters** 638 639| Name | Type | Mandatory| Description | 640| -------- | ----------------------- | ---- | -------------------------------------- | 641| callback | AsyncCallback\<[NdefMessage](#ndefmessage9)> | Yes | Callback used to return the NDEF message read.| 642 643**Error codes** 644 645For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 646 647| ID| Error Message| 648| ------- | -------| 649| 201 | Permission denied. | 650| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 651| 3100201 | The tag running state is abnormal in the service. | 652| 3100204 | The Tag I/O operation failed. | 653 654**Example** 655 656```js 657import { tag } from '@kit.ConnectivityKit'; 658import { BusinessError } from '@kit.BasicServicesKit'; 659 660// Obtain the correct ndefTag tag by using the tag.TagInfo API in @ohos.nfc.tag. 661function nfcTechDemo() { 662 // Connect the tag if it has not been connected. 663 if (!ndefTag.isTagConnected()) { 664 if (!ndefTag.connectTag()) { 665 console.error("ndefTag connectTag failed."); 666 return; 667 } 668 } 669 670 try { 671 ndefTag.readNdef((err : BusinessError, ndefmessage : tag.NdefMessage)=> { 672 if (err) { 673 console.error("ndef readNdef AsyncCallback err Code: ${err.code}, message: ${err.message}"); 674 } else { 675 console.info("ndef readNdef AsyncCallback ndefmessage: " + ndefmessage); 676 } 677 }); 678 } catch (businessError) { 679 console.error("ndef readNdef AsyncCallback catch Code: ${(businessError : BusinessError).code}," + 680 " message: ${(businessError : BusinessError).message}"); 681 } 682} 683 684``` 685 686### NdefTag.writeNdef<sup>9+</sup> 687 688writeNdef(msg: [NdefMessage](#ndefmessage9)): Promise\<void> 689 690Writes an NDEF message to this tag. This API uses a promise to return the result. 691 692**Required permissions**: ohos.permission.NFC_TAG 693 694**System capability**: SystemCapability.Communication.NFC.Tag 695 696**Atomic service API**: This API can be used in atomic services since API version 12. 697 698**Parameters** 699 700| Name | Type | Mandatory| Description | 701| -------- | ----------------------- | ---- | -------------------------------------- | 702| msg | [NdefMessage](#ndefmessage9) | Yes | NDEF message to write.| 703 704**Return value** 705 706| Type | Description | 707| ------------------------- | ------------------ | 708| Promise\<void> | Promise that returns no value.| 709 710**Error codes** 711 712For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 713 714| ID| Error Message| 715| ------- | -------| 716| 201 | Permission denied. | 717| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 718| 3100201 | The tag running state is abnormal in the service. | 719| 3100204 | The tag I/O operation failed. | 720 721**Example** 722 723```js 724import { tag } from '@kit.ConnectivityKit'; 725import { BusinessError } from '@kit.BasicServicesKit'; 726 727// Obtain the correct ndefTag tag by using the tag.TagInfo API in @ohos.nfc.tag. 728// ndefMessage created from the raw data. For example: 729let ndefMessage : tag.NdefMessage = 730 tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // The NDEF data must be resolvable. 731// Or create ndefMessage from tag.ndef.createNdefMessage (ndefRecords:NdefRecord[]). 732 733function nfcTechDemo() { 734 // Connect the tag if it has not been connected. 735 if (!ndefTag.isTagConnected()) { 736 if (!ndefTag.connectTag()) { 737 console.error("ndefTag connectTag failed."); 738 return; 739 } 740 } 741 742 try { 743 ndefTag.writeNdef(ndefMessage).then(() => { 744 console.info("ndef writeNdef Promise success."); 745 }).catch((err : BusinessError)=> { 746 console.error("ndef writeNdef err Code: ${err.code}, message: ${err.message}"); 747 }); 748 } catch (businessError) { 749 console.error("ndef writeNdef Promise catch businessError Code: ${(businessError as Businsess).code}, " + 750 "message: ${(businessError as Businsess).message}"); 751 } 752} 753``` 754 755### NdefTag.writeNdef<sup>9+</sup> 756 757writeNdef(msg: [NdefMessage](#ndefmessage9), callback: AsyncCallback\<void>): void 758 759Writes an NDEF message to this tag. This API uses an asynchronous callback to return the result. 760 761**Required permissions**: ohos.permission.NFC_TAG 762 763**System capability**: SystemCapability.Communication.NFC.Tag 764 765**Atomic service API**: This API can be used in atomic services since API version 12. 766 767**Parameters** 768 769| Name | Type | Mandatory| Description | 770| -------- | ----------------------- | ---- | -------------------------------------- | 771| msg | [NdefMessage](#ndefmessage9) | Yes | NDEF message to write.| 772| callback | AsyncCallback\<void> | Yes | Callback used to return the operation result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 773 774**Error codes** 775 776For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 777 778| ID| Error Message| 779| ------- | -------| 780| 201 | Permission denied. | 781| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 782| 3100201 | The tag running state is abnormal in the service. | 783| 3100204 | The Tag I/O operation failed. | 784 785**Example** 786 787```js 788import { tag } from '@kit.ConnectivityKit'; 789import { BusinessError } from '@kit.BasicServicesKit'; 790 791// Obtain the correct ndefTag tag by using the tag.TagInfo API in @ohos.nfc.tag. 792// ndefMessage created from the raw data. For example: 793let ndefMessage : tag.NdefMessage = 794 tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // The NDEF data must be resolvable. 795// Or create ndefMessage from tag.ndef.createNdefMessage (ndefRecords:NdefRecord[]). 796 797function nfcTechDemo() { 798 // Connect the tag if it has not been connected. 799 if (!ndefTag.isTagConnected()) { 800 if (!ndefTag.connectTag()) { 801 console.error("ndefTag connectTag failed."); 802 return; 803 } 804 } 805 806 try { 807 ndefTag.writeNdef(ndefMessage, (err : BusinessError)=> { 808 if (err) { 809 console.error("ndef writeNdef AsyncCallback Code: ${err.code}, message: ${err.message}"); 810 } else { 811 console.info("ndef writeNdef AsyncCallback success."); 812 } 813 }); 814 } catch (businessError) { 815 console.error("ndef writeNdef AsyncCallback catch businessError Code: ${(businessError as Businsess).code}," + 816 " message: ${(businessError as Businsess).message}"); 817 } 818} 819``` 820 821### NdefTag.canSetReadOnly<sup>9+</sup> 822 823canSetReadOnly(): boolean 824 825Checks whether this NDEF tag can be set to read-only. 826 827**Required permissions**: ohos.permission.NFC_TAG 828 829**System capability**: SystemCapability.Communication.NFC.Tag 830 831**Atomic service API**: This API can be used in atomic services since API version 12. 832 833**Return value** 834 835| **Type**| **Description** | 836| ------------------ | --------------------------| 837| boolean| Returns **true** if the tag can be set to read-only; returns **false** otherwise.| 838 839**Error codes** 840 841For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 842 843| ID| Error Message| 844| ------- | -------| 845| 201 | Permission denied. | 846| 3100201 | The tag running state is abnormal in the service. | 847 848**Example** 849 850```js 851import { tag } from '@kit.ConnectivityKit'; 852 853// Obtain the correct ndefTag tag by using the tag.TagInfo API in @ohos.nfc.tag. 854let canSetReadOnly : boolean = ndefTag.canSetReadOnly(); 855console.info("ndef canSetReadOnly: " + canSetReadOnly); 856``` 857 858### NdefTag.setReadOnly<sup>9+</sup> 859 860setReadOnly(): Promise\<void> 861 862Sets this NDEF tag to read-only. This API uses a promise to return the result. 863 864**Required permissions**: ohos.permission.NFC_TAG 865 866**System capability**: SystemCapability.Communication.NFC.Tag 867 868**Atomic service API**: This API can be used in atomic services since API version 12. 869 870**Return value** 871 872| Type | Description | 873| ------------------------- | ------------------ | 874| Promise\<void> | Promise that returns no value.| 875 876**Error codes** 877 878For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 879 880| ID| Error Message| 881| ------- | -------| 882| 201 | Permission denied. | 883| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 884| 3100201 | The tag running state is abnormal in the service. | 885| 3100204 | The tag I/O operation failed. | 886 887**Example** 888 889```js 890import { tag } from '@kit.ConnectivityKit'; 891import { BusinessError } from '@kit.BasicServicesKit'; 892 893// Obtain the correct ndefTag tag by using the tag.TagInfo API in @ohos.nfc.tag. 894 895function nfcTechDemo() { 896 // Connect the tag if it has not been connected. 897 if (!ndefTag.isTagConnected()) { 898 if (!ndefTag.connectTag()) { 899 console.error("ndefTag connectTag failed."); 900 return; 901 } 902 } 903 904 try { 905 ndefTag.setReadOnly().then(() => { 906 console.info("ndef setReadOnly Promise success."); 907 }).catch((err : BusinessError)=> { 908 console.error("ndef setReadOnly Promise err Code: ${err.code}, message: ${err.message}"); 909 }); 910 } catch (businessError) { 911 console.error("ndef setReadOnly Promise catch businessError Code: ${(businessError as Businsess).code}, " + 912 "message: ${(businessError as Businsess).message}"); 913 } 914} 915``` 916 917### NdefTag.setReadOnly<sup>9+</sup> 918 919setReadOnly(callback: AsyncCallback\<void>): void 920 921Sets this NDEF tag to read-only. This API uses an asynchronous callback to return the result. 922 923**Required permissions**: ohos.permission.NFC_TAG 924 925**System capability**: SystemCapability.Communication.NFC.Tag 926 927**Atomic service API**: This API can be used in atomic services since API version 12. 928 929**Parameters** 930 931| Name | Type | Mandatory| Description | 932| -------- | ----------------------- | ---- | -------------------------------------- | 933| callback | AsyncCallback\<void> | Yes | Callback used to return the operation result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 934 935**Error codes** 936 937For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 938 939| ID| Error Message| 940| ------- | -------| 941| 201 | Permission denied. | 942| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 943| 3100201 | The tag running state is abnormal in the service. | 944| 3100204 | The Tag I/O operation failed. | 945 946**Example** 947 948```js 949import { tag } from '@kit.ConnectivityKit'; 950import { BusinessError } from '@kit.BasicServicesKit'; 951 952// Obtain the correct ndefTag tag by using the tag.TagInfo API in @ohos.nfc.tag. 953 954function nfcTechDemo() { 955 // Connect the tag if it has not been connected. 956 if (!ndefTag.isTagConnected()) { 957 if (!ndefTag.connectTag()) { 958 console.error("ndefTag connectTag failed."); 959 return; 960 } 961 } 962 963 try { 964 ndefTag.setReadOnly((err : BusinessError)=> { 965 if (err) { 966 console.error("ndef setReadOnly AsyncCallback err Code: ${err.code}, message: ${err.message}"); 967 } else { 968 console.info("ndef setReadOnly AsyncCallback success."); 969 } 970 }); 971 } catch (businessError) { 972 console.error("ndef setReadOnly AsyncCallback catch businessError Code: ${(businessError as Businsess).code}, " + 973 "message: ${(businessError as Businsess).message}"); 974 } 975} 976``` 977 978### NdefTag.getNdefTagTypeString<sup>9+</sup> 979 980getNdefTagTypeString(type: [tag.NfcForumType](js-apis-nfcTag.md#nfcforumtype9)): string 981 982Converts an NFC Forum Type tag to a string defined in the NFC Forum. 983 984**System capability**: SystemCapability.Communication.NFC.Tag 985 986**Atomic service API**: This API can be used in atomic services since API version 12. 987 988**Parameters** 989 990| Name | Type | Mandatory| Description | 991| -------- | ----------------------- | ---- | -------------------------------------- | 992| type | [tag.NfcForumType](js-apis-nfcTag.md#nfcforumtype9) | Yes | NDEF tag type. It can be NFC FORUM type 1, 2, 3, or 4.| 993 994**Return value** 995 996| **Type**| **Description** | 997| ------------------ | --------------------------| 998| string | Byte array obtained.| 999 1000**Error codes** 1001 1002For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 1003 1004| ID| Error Message| 1005| ------- | -------| 1006| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1007 1008**Example** 1009 1010```js 1011import { tag } from '@kit.ConnectivityKit'; 1012import { BusinessError } from '@kit.BasicServicesKit'; 1013 1014// Obtain the correct ndefTag tag by using the tag.TagInfo API in @ohos.nfc.tag. 1015 1016try { 1017 let ndefTypeString : string = ndefTag.getNdefTagTypeString(tag.NfcForumType.NFC_FORUM_TYPE_1); 1018 console.info("ndef ndefTypeString: " + ndefTypeString); 1019} catch (businessError) { 1020 console.error("ndef getNdefTagTypeString catch businessError Code: ${(businessError as Businsess).code}, " + 1021 "message: ${(businessError as Businsess).message}"); 1022} 1023``` 1024 1025## MifareClassicTag<sup>9+</sup> 1026 1027Provides APIs to access MIFARE Classic properties and perform I/O operations on a tag. This class inherits from [TagSession](js-apis-tagSession.md). 1028 1029**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). 1030 1031For details about how to obtain a **MifareClassicTag** object, see [NFC Tag Read/Write Development](../../connectivity/nfc/nfc-tag-access-guide.md). 1032 1033The following describes the unique APIs of **MifareClassicTag**. 1034 1035### MifareClassicTag.authenticateSector<sup>9+</sup> 1036 1037authenticateSector(sectorIndex: number, key: number[], isKeyA: boolean): Promise\<void> 1038 1039Authenticates 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. 1040 1041**Required permissions**: ohos.permission.NFC_TAG 1042 1043**System capability**: SystemCapability.Communication.NFC.Tag 1044 1045**Atomic service API**: This API can be used in atomic services since API version 12. 1046 1047**Parameters** 1048 1049| Name | Type | Mandatory| Description | 1050| -------- | ----------------------- | ---- | -------------------------------------- | 1051| sectorIndex | number | Yes | Index of the sector to authenticate. The sector indexes start from **0**.| 1052| key | number[]| Yes | Key (6 bytes) used for sector authentication.| 1053| isKeyA | boolean | Yes | Whether the key is key A. The value **true** indicates key A, and **false** indicates key B.| 1054 1055**Return value** 1056 1057| Type | Description | 1058| ------------------------- | ------------------ | 1059| Promise\<void> | Promise that returns no value.| 1060 1061**Error codes** 1062 1063For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 1064 1065| ID| Error Message| 1066| ------- | -------| 1067| 201 | Permission denied. | 1068| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1069| 3100201 | The tag running state is abnormal in the service. | 1070| 3100204 | The tag I/O operation failed. | 1071 1072**Example** 1073 1074```js 1075import { tag } from '@kit.ConnectivityKit'; 1076import { BusinessError } from '@kit.BasicServicesKit'; 1077 1078// Obtain the correct MIFARE Classic tag by using the tag.TagInfo API in @ohos.nfc.tag. 1079 1080function nfcTechDemo() { 1081 // Connect the tag if it has not been connected. 1082 if (!mifareClassic.isTagConnected()) { 1083 if (!mifareClassic.connectTag()) { 1084 console.error("mifareClassic connectTag failed."); 1085 return; 1086 } 1087 } 1088 1089 try { 1090 let sectorIndex = 1; // Set a correct index. 1091 let key = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06] // Set a correct key. The value must contain six bytes. 1092 mifareClassic.authenticateSector(sectorIndex, key, true).then(() => { 1093 console.info("mifareClassic authenticateSector Promise success."); 1094 }).catch((err : BusinessError)=> { 1095 console.error("mifareClassic authenticateSector Promise errCode: ${err.code}, " + "message: ${err.message}"); 1096 }); 1097 } catch (businessError) { 1098 console.error("mifareClassic authenticateSector Promise catch businessError Code: ${(businessError as Businsess).code}, " + 1099 "message: ${(businessError as Businsess).message}"); 1100 } 1101} 1102``` 1103 1104### MifareClassicTag.authenticateSector<sup>9+</sup> 1105 1106authenticateSector(sectorIndex: number, key: number[], isKeyA: boolean, callback: AsyncCallback\<void>): void 1107 1108Authenticates 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. 1109 1110**Required permissions**: ohos.permission.NFC_TAG 1111 1112**System capability**: SystemCapability.Communication.NFC.Tag 1113 1114**Atomic service API**: This API can be used in atomic services since API version 12. 1115 1116**Parameters** 1117 1118| Name | Type | Mandatory| Description | 1119| -------- | ----------------------- | ---- | -------------------------------------- | 1120| sectorIndex | number | Yes | Index of the sector to authenticate. The sector indexes start from **0**.| 1121| key | number[]| Yes | Key (6 bytes) used for sector authentication.| 1122| isKeyA | boolean | Yes | Whether the key is key A. The value **true** indicates key A, and **false** indicates key B.| 1123| callback | AsyncCallback\<void> | Yes | Callback used to return the operation result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 1124 1125**Error codes** 1126 1127For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 1128 1129| ID| Error Message| 1130| ------- | -------| 1131| 201 | Permission denied. | 1132| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1133| 3100201 | The tag running state is abnormal in the service. | 1134| 3100204 | The Tag I/O operation failed. | 1135 1136**Example** 1137```js 1138import { tag } from '@kit.ConnectivityKit'; 1139import { BusinessError } from '@kit.BasicServicesKit'; 1140 1141// Obtain the correct MIFARE Classic tag by using the tag.TagInfo API in @ohos.nfc.tag. 1142 1143function nfcTechDemo() { 1144 // Connect the tag if it has not been connected. 1145 if (!mifareClassic.isTagConnected()) { 1146 if (!mifareClassic.connectTag()) { 1147 console.error("mifareClassic connectTag failed."); 1148 return; 1149 } 1150 } 1151 1152 try { 1153 let sectorIndex = 1; // Set a correct index. 1154 let key = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06] // Set a correct key. The value must contain six bytes. 1155 mifareClassic.authenticateSector(sectorIndex, key, true, (err : BusinessError)=> { 1156 if (err) { 1157 console.error("mifareClassic authenticateSector AsyncCallback errCode: ${err.code}, message: ${err.message}"); 1158 } else { 1159 console.info("mifareClassic authenticateSector AsyncCallback success."); 1160 } 1161 }); 1162 } catch (businessError) { 1163 console.error("mifareClassic authenticateSector AsyncCallback catch Code: ${(businessError as Businsess).code}, " + 1164 "message: ${(businessError as Businsess).message}"); 1165 } 1166} 1167``` 1168 1169### MifareClassicTag.readSingleBlock<sup>9+</sup> 1170 1171readSingleBlock(blockIndex: number): Promise\<number[]> 1172 1173Reads a block (16 bytes) on this tag. This API uses a promise to return the result. 1174 1175**Required permissions**: ohos.permission.NFC_TAG 1176 1177**System capability**: SystemCapability.Communication.NFC.Tag 1178 1179**Atomic service API**: This API can be used in atomic services since API version 12. 1180 1181**Parameters** 1182 1183| Name | Type | Mandatory| Description | 1184| -------- | ----------------------- | ---- | -------------------------------------- | 1185| blockIndex | number | Yes | Index of the block to read. The block indexes start from **0**.| 1186 1187**Return value** 1188 1189| **Type**| **Description** | 1190| ------------------ | --------------------------| 1191| Promise\<number[]> | Promise used to return the block read.| 1192 1193**Error codes** 1194 1195For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 1196 1197| ID| Error Message| 1198| ------- | -------| 1199| 201 | Permission denied. | 1200| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1201| 3100201 | The tag running state is abnormal in the service. | 1202| 3100204 | The tag I/O operation failed. | 1203 1204**Example** 1205 1206```js 1207import { tag } from '@kit.ConnectivityKit'; 1208import { BusinessError } from '@kit.BasicServicesKit'; 1209 1210// Obtain the correct MIFARE Classic tag by using the tag.TagInfo API in @ohos.nfc.tag. 1211 1212function nfcTechDemo() { 1213 // Connect the tag if it has not been connected. 1214 if (!mifareClassic.isTagConnected()) { 1215 if (!mifareClassic.connectTag()) { 1216 console.error("mifareClassic connectTag failed."); 1217 return; 1218 } 1219 } 1220 1221 try { 1222 let blockIndex = 1; // Set a correct index. 1223 mifareClassic.readSingleBlock(blockIndex).then((data : number[]) => { 1224 console.info("mifareClassic readSingleBlock Promise data: " + data); 1225 }).catch((err : BusinessError)=> { 1226 console.error("mifareClassic readSingleBlock Promise errCode: ${err.code}, message: ${err.message}"); 1227 }); 1228 } catch (businessError) { 1229 console.error("mifareClassic readSingleBlock Promise catch businessError Code: ${(businessError as Businsess).code}, " + 1230 "message: ${(businessError as Businsess).message}"); 1231 } 1232} 1233``` 1234 1235### MifareClassicTag.readSingleBlock<sup>9+</sup> 1236 1237readSingleBlock(blockIndex: number, callback: AsyncCallback\<number[]>): void 1238 1239Reads a block (16 bytes) on this tag. This API uses an asynchronous callback to return the result. 1240 1241**Required permissions**: ohos.permission.NFC_TAG 1242 1243**System capability**: SystemCapability.Communication.NFC.Tag 1244 1245**Atomic service API**: This API can be used in atomic services since API version 12. 1246 1247**Parameters** 1248 1249| Name | Type | Mandatory| Description | 1250| -------- | ----------------------- | ---- | -------------------------------------- | 1251| blockIndex | number | Yes | Index of the block to read. The block indexes start from **0**.| 1252| callback | AsyncCallback\<number[]> | Yes |Callback used to return the block data read.| 1253 1254**Error codes** 1255 1256For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 1257 1258| ID| Error Message| 1259| ------- | -------| 1260| 201 | Permission denied. | 1261| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1262| 3100201 | The tag running state is abnormal in the service. | 1263| 3100204 | The Tag I/O operation failed. | 1264 1265**Example** 1266 1267```js 1268import { tag } from '@kit.ConnectivityKit'; 1269import { BusinessError } from '@kit.BasicServicesKit'; 1270 1271// Obtain the correct MIFARE Classic tag by using the tag.TagInfo API in @ohos.nfc.tag. 1272 1273function nfcTechDemo() { 1274 // Connect the tag if it has not been connected. 1275 if (!mifareClassic.isTagConnected()) { 1276 if (!mifareClassic.connectTag()) { 1277 console.error("mifareClassic connectTag failed."); 1278 return; 1279 } 1280 } 1281 1282 try { 1283 let blockIndex = 1; // Set a correct index. 1284 mifareClassic.readSingleBlock(blockIndex, (err : BusinessError, data : number[])=> { 1285 if (err) { 1286 console.error("mifareClassic readSingleBlock AsyncCallback err: " + err); 1287 } else { 1288 console.info("mifareClassic readSingleBlock AsyncCallback data: " + data); 1289 } 1290 }); 1291 } catch (businessError) { 1292 console.error("mifareClassic readSingleBlock AsyncCallback catch businessError Code: " + 1293 " ${(businessError as Businsess).code}, message: ${(businessError as Businsess).message}"); 1294 } 1295} 1296``` 1297 1298### MifareClassicTag.writeSingleBlock<sup>9+</sup> 1299 1300writeSingleBlock(blockIndex: number, data: number[]): Promise\<void> 1301 1302Writes data to a block on this tag. This API uses a promise to return the result. 1303 1304**Required permissions**: ohos.permission.NFC_TAG 1305 1306**System capability**: SystemCapability.Communication.NFC.Tag 1307 1308**Atomic service API**: This API can be used in atomic services since API version 12. 1309 1310**Parameters** 1311 1312| Name | Type | Mandatory| Description | 1313| -------- | ----------------------- | ---- | -------------------------------------- | 1314| blockIndex | number | Yes | Index of the block to write. The block indexes start from **0**.| 1315| data | number[] | Yes | 16-byte data to write.| 1316 1317**Return value** 1318 1319| Type | Description | 1320| ------------------------- | ------------------ | 1321| Promise\<void> | Promise that returns no value.| 1322 1323**Error codes** 1324 1325For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 1326 1327| ID| Error Message| 1328| ------- | -------| 1329| 201 | Permission denied. | 1330| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1331| 3100201 | The tag running state is abnormal in the service. | 1332| 3100204 | The tag I/O operation failed. | 1333 1334**Example** 1335 1336```js 1337import { tag } from '@kit.ConnectivityKit'; 1338import { BusinessError } from '@kit.BasicServicesKit'; 1339 1340// Obtain the correct MIFARE Classic tag by using the tag.TagInfo API in @ohos.nfc.tag. 1341 1342function nfcTechDemo() { 1343 // Connect the tag if it has not been connected. 1344 if (!mifareClassic.isTagConnected()) { 1345 if (!mifareClassic.connectTag()) { 1346 console.error("mifareClassic connectTag failed."); 1347 return; 1348 } 1349 } 1350 1351 try { 1352 let blockIndex = 1; // Set a correct index. 1353 let rawData = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 1354 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]; // Set a correct key. The value must contain 16 bytes. 1355 mifareClassic.writeSingleBlock(blockIndex, rawData).then(() => { 1356 console.info("mifareClassic writeSingleBlock Promise success."); 1357 }).catch((err : BusinessError)=> { 1358 console.error("mifareClassic writeSingleBlock Promise errCode: ${err.code}, message: ${err.message}"); 1359 }); 1360 } catch (businessError) { 1361 console.error("mifareClassic writeSingleBlock Promise catch businessError Code: ${(businessError as Businsess).code}, " 1362 + "message: ${(businessError as Businsess).message}"); 1363 } 1364} 1365``` 1366 1367### MifareClassicTag.writeSingleBlock<sup>9+</sup> 1368 1369writeSingleBlock(blockIndex: number, data: number[], callback: AsyncCallback\<void>): void 1370 1371Writes data to a block on this tag. This API uses an asynchronous callback to return the result. 1372 1373**Required permissions**: ohos.permission.NFC_TAG 1374 1375**System capability**: SystemCapability.Communication.NFC.Tag 1376 1377**Atomic service API**: This API can be used in atomic services since API version 12. 1378 1379**Parameters** 1380 1381| Name | Type | Mandatory| Description | 1382| -------- | ----------------------- | ---- | -------------------------------------- | 1383| blockIndex | number | Yes | Index of the block to write. The block indexes start from **0**.| 1384| data | number[] | Yes | 16-byte data to write.| 1385| callback | AsyncCallback\<void> | Yes | Callback used to return the operation result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 1386 1387**Error codes** 1388 1389For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 1390 1391| ID| Error Message| 1392| ------- | -------| 1393| 201 | Permission denied. | 1394| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1395| 3100201 | The tag running state is abnormal in the service. | 1396| 3100204 | The Tag I/O operation failed. | 1397 1398**Example** 1399 1400```js 1401import { tag } from '@kit.ConnectivityKit'; 1402import { BusinessError } from '@kit.BasicServicesKit'; 1403 1404// Obtain the correct MIFARE Classic tag by using the tag.TagInfo API in @ohos.nfc.tag. 1405 1406function nfcTechDemo() { 1407 // Connect the tag if it has not been connected. 1408 if (!mifareClassic.isTagConnected()) { 1409 if (!mifareClassic.connectTag()) { 1410 console.error("mifareClassic connectTag failed."); 1411 return; 1412 } 1413 } 1414 1415 try { 1416 let blockIndex = 1; // Set a correct index. 1417 let rawData = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 1418 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]; // Set the correct data. The value must contain 16 bytes. 1419 mifareClassic.writeSingleBlock(blockIndex, rawData, (err : BusinessError)=> { 1420 if (err) { 1421 console.error("mifareClassic writeSingleBlock AsyncCallback err Code:" + 1422 "${err.code}, message: ${err.message}"); 1423 } else { 1424 console.info("mifareClassic writeSingleBlock AsyncCallback success."); 1425 } 1426 }); 1427 } catch (businessError) { 1428 console.error("mifareClassic writeSingleBlock AsyncCallback catch Code: ${(businessError as Businsess).code}, " + 1429 "message: ${(businessError as Businsess).message}"); 1430 } 1431} 1432``` 1433 1434### MifareClassicTag.incrementBlock<sup>9+</sup> 1435 1436incrementBlock(blockIndex: number, value: number): Promise\<void> 1437 1438Increments 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. 1439 1440**Required permissions**: ohos.permission.NFC_TAG 1441 1442**System capability**: SystemCapability.Communication.NFC.Tag 1443 1444**Atomic service API**: This API can be used in atomic services since API version 12. 1445 1446**Parameters** 1447 1448| Name | Type | Mandatory| Description | 1449| -------- | ----------------------- | ---- | -------------------------------------- | 1450| blockIndex | number | Yes | Index of the block to increment. The block indexes start from **0**.| 1451| value | number | Yes | Block data to increment. The value cannot be a negative number.| 1452 1453**Return value** 1454 1455| Type | Description | 1456| ------------------------- | ------------------ | 1457| Promise\<void> | Promise that returns no value.| 1458 1459**Error codes** 1460 1461For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 1462 1463| ID| Error Message| 1464| ------- | -------| 1465| 201 | Permission denied. | 1466| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1467| 3100201 | The tag running state is abnormal in the service. | 1468| 3100204 | The tag I/O operation failed. | 1469 1470**Example** 1471 1472```js 1473import { tag } from '@kit.ConnectivityKit'; 1474import { BusinessError } from '@kit.BasicServicesKit'; 1475 1476// Obtain the correct MIFARE Classic tag by using the tag.TagInfo API in @ohos.nfc.tag. 1477 1478function nfcTechDemo() { 1479 // Connect the tag if it has not been connected. 1480 if (!mifareClassic.isTagConnected()) { 1481 if (!mifareClassic.connectTag()) { 1482 console.error("mifareClassic connectTag failed."); 1483 return; 1484 } 1485 } 1486 1487 try { 1488 let blockIndex = 1; // Set a correct index. 1489 let value = 0x20; // Set the correct data. 1490 mifareClassic.incrementBlock(blockIndex, value).then(() => { 1491 console.info("mifareClassic incrementBlock Promise success."); 1492 }).catch((err : BusinessError)=> { 1493 console.error("mifareClassic incrementBlock Promise err Code: ${err.code}, message: ${err.message}"); 1494 }); 1495 } catch (businessError) { 1496 console.error("mifareClassic incrementBlock Promise catch Code: ${(businessError as Businsess).code}, " + 1497 "message: ${(businessError as Businsess).message}"); 1498 } 1499} 1500``` 1501 1502### MifareClassicTag.incrementBlock<sup>9+</sup> 1503 1504incrementBlock(blockIndex: number, value: number, callback: AsyncCallback\<void>): void 1505 1506Increments 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. 1507 1508**Required permissions**: ohos.permission.NFC_TAG 1509 1510**System capability**: SystemCapability.Communication.NFC.Tag 1511 1512**Atomic service API**: This API can be used in atomic services since API version 12. 1513 1514**Parameters** 1515 1516| Name | Type | Mandatory| Description | 1517| -------- | ----------------------- | ---- | -------------------------------------- | 1518| blockIndex | number | Yes | Index of the block to increment. The block indexes start from **0**.| 1519| value | number | Yes | Block data to increment. The value cannot be a negative number.| 1520| callback | AsyncCallback\<void> | Yes | Callback used to return the operation result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 1521 1522**Error codes** 1523 1524For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 1525 1526| ID| Error Message| 1527| ------- | -------| 1528| 201 | Permission denied. | 1529| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1530| 3100201 | The tag running state is abnormal in the service. | 1531| 3100204 | The Tag I/O operation failed. | 1532 1533**Example** 1534 1535```js 1536import { tag } from '@kit.ConnectivityKit'; 1537import { BusinessError } from '@kit.BasicServicesKit'; 1538 1539// Obtain the correct MIFARE Classic tag by using the tag.TagInfo API in @ohos.nfc.tag. 1540 1541function nfcTechDemo() { 1542 // Connect the tag if it has not been connected. 1543 if (!mifareClassic.isTagConnected()) { 1544 if (!mifareClassic.connectTag()) { 1545 console.error("mifareClassic connectTag failed."); 1546 return; 1547 } 1548 } 1549 1550 try { 1551 let blockIndex = 1; // Set a correct index. 1552 let value = 0x20; // Set the correct data. 1553 mifareClassic.incrementBlock(blockIndex, value, (err : BusinessError)=> { 1554 if (err) { 1555 console.error("mifareClassic incrementBlock AsyncCallback err Code: ${err.code}, message: ${err.message}"); 1556 } else { 1557 console.info("mifareClassic incrementBlock AsyncCallback success."); 1558 } 1559 }); 1560 } catch (businessError) { 1561 console.error("mifareClassic incrementBlock AsyncCallback catch businessError Code: ${(businessError as Businsess).code}, " + 1562 "message: ${(businessError as Businsess).message}"); 1563 } 1564} 1565``` 1566 1567### MifareClassicTag.decrementBlock<sup>9+</sup> 1568 1569decrementBlock(blockIndex: number, value: number): Promise\<void> 1570 1571Decrements 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. 1572 1573**Required permissions**: ohos.permission.NFC_TAG 1574 1575**System capability**: SystemCapability.Communication.NFC.Tag 1576 1577**Atomic service API**: This API can be used in atomic services since API version 12. 1578 1579**Parameters** 1580 1581| Name | Type | Mandatory| Description | 1582| -------- | ----------------------- | ---- | -------------------------------------- | 1583| blockIndex | number | Yes | Index of the block to increment. The block indexes start from **0**.| 1584| value | number | Yes | Block data to decrement. The value cannot be a negative number.| 1585 1586**Return value** 1587 1588| Type | Description | 1589| ------------------------- | ------------------ | 1590| Promise\<void> | Promise that returns no value.| 1591 1592**Error codes** 1593 1594For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 1595 1596| ID| Error Message| 1597| ------- | -------| 1598| 201 | Permission denied. | 1599| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1600| 3100201 | The tag running state is abnormal in the service. | 1601| 3100204 | The tag I/O operation failed. | 1602 1603**Example** 1604 1605```js 1606import { tag } from '@kit.ConnectivityKit'; 1607import { BusinessError } from '@kit.BasicServicesKit'; 1608 1609// Obtain the correct MIFARE Classic tag by using the tag.TagInfo API in @ohos.nfc.tag. 1610 1611function nfcTechDemo() { 1612 // Connect the tag if it has not been connected. 1613 if (!mifareClassic.isTagConnected()) { 1614 if (!mifareClassic.connectTag()) { 1615 console.error("mifareClassic connectTag failed."); 1616 return; 1617 } 1618 } 1619 1620 try { 1621 let blockIndex = 1; // Set a correct index. 1622 let value = 0x20; // Set the correct data. 1623 mifareClassic.decrementBlock(blockIndex, value).then(() => { 1624 console.info("mifareClassic decrementBlock Promise success."); 1625 }).catch((err : BusinessError)=> { 1626 console.error("mifareClassic decrementBlock Promise errCode: ${err.code}, message: ${err.message}"); 1627 }); 1628 } catch (businessError) { 1629 console.error("mifareClassic decrementBlock Promise catch businessError: Code: ${(businessError as Businsess).code}, " + 1630 "message: ${(businessError as Businsess).message}"); 1631 } 1632} 1633``` 1634 1635### MifareClassicTag.decrementBlock<sup>9+</sup> 1636 1637decrementBlock(blockIndex: number, value: number, callback: AsyncCallback\<void>): void 1638 1639Decrements a block with the specified value. This API uses an asynchronous callback to return the result. 1640 1641**Required permissions**: ohos.permission.NFC_TAG 1642 1643**System capability**: SystemCapability.Communication.NFC.Tag 1644 1645**Atomic service API**: This API can be used in atomic services since API version 12. 1646 1647**Parameters** 1648 1649| Name | Type | Mandatory| Description | 1650| -------- | ----------------------- | ---- | -------------------------------------- | 1651| blockIndex | number | Yes | Index of the block to increment. The block indexes start from **0**.| 1652| value | number | Yes | Block data to decrement. The value cannot be a negative number.| 1653| callback | AsyncCallback\<void> | Yes | Callback used to return the operation result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 1654 1655**Error codes** 1656 1657For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 1658 1659| ID| Error Message| 1660| ------- | -------| 1661| 201 | Permission denied. | 1662| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1663| 3100201 | The tag running state is abnormal in the service. | 1664| 3100204 | The Tag I/O operation failed. | 1665 1666**Example** 1667 1668```js 1669import { tag } from '@kit.ConnectivityKit'; 1670import { BusinessError } from '@kit.BasicServicesKit'; 1671 1672// Obtain the correct MIFARE Classic tag by using the tag.TagInfo API in @ohos.nfc.tag. 1673 1674function nfcTechDemo() { 1675 // Connect the tag if it has not been connected. 1676 if (!mifareClassic.isTagConnected()) { 1677 if (!mifareClassic.connectTag()) { 1678 console.error("mifareClassic connectTag failed."); 1679 return; 1680 } 1681 } 1682 1683 try { 1684 let blockIndex = 1; // Set a correct index. 1685 let value = 0x20; // Set the correct data. 1686 mifareClassic.decrementBlock(blockIndex, value, (err : BusinessError)=> { 1687 if (err) { 1688 console.error("mifareClassic decrementBlock AsyncCallback errCode:" + 1689 "${err.code}, message: ${err.message}"); 1690 } else { 1691 console.info("mifareClassic decrementBlock AsyncCallback success."); 1692 } 1693 }); 1694 } catch (businessError) { 1695 console.error("mifareClassic decrementBlock AsyncCallback catch Code: ${(businessError as Businsess).code}, " + 1696 "message: ${(businessError as Businsess).message}"); 1697 } 1698} 1699``` 1700 1701### MifareClassicTag.transferToBlock<sup>9+</sup> 1702 1703transferToBlock(blockIndex: number): Promise\<void> 1704 1705Transfers data from the temporary register to a block. This API uses a promise to return the result. 1706 1707**Required permissions**: ohos.permission.NFC_TAG 1708 1709**System capability**: SystemCapability.Communication.NFC.Tag 1710 1711**Atomic service API**: This API can be used in atomic services since API version 12. 1712 1713**Parameters** 1714 1715| Name | Type | Mandatory| Description | 1716| -------- | ----------------------- | ---- | -------------------------------------- | 1717| blockIndex | number | Yes | Index of the destination block. The value starts form **0**.| 1718 1719**Return value** 1720 1721| Type | Description | 1722| ------------------------- | ------------------ | 1723| Promise\<void> | Promise that returns no value.| 1724 1725**Error codes** 1726 1727For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 1728 1729| ID| Error Message| 1730| ------- | -------| 1731| 201 | Permission denied. | 1732| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1733| 3100201 | The tag running state is abnormal in the service. | 1734| 3100204 | The tag I/O operation failed. | 1735 1736**Example** 1737 1738```js 1739import { tag } from '@kit.ConnectivityKit'; 1740import { BusinessError } from '@kit.BasicServicesKit'; 1741 1742// Obtain the correct MIFARE Classic tag by using the tag.TagInfo API in @ohos.nfc.tag. 1743 1744function nfcTechDemo() { 1745 // Connect the tag if it has not been connected. 1746 if (!mifareClassic.isTagConnected()) { 1747 if (!mifareClassic.connectTag()) { 1748 console.error("mifareClassic connectTag failed."); 1749 return; 1750 } 1751 } 1752 1753 try { 1754 let blockIndex = 1; // Set a correct index. 1755 mifareClassic.transferToBlock(blockIndex).then(() => { 1756 console.info("mifareClassic transferToBlock Promise success."); 1757 }).catch((err : BusinessError)=> { 1758 console.error("mifareClassic transferToBlock Promise err Code: ${err.code}, message: ${err.message}"); 1759 }); 1760 } catch (businessError) { 1761 console.error("mifareClassic transferToBlock Promise catch Code: ${(businessError as Businsess).code}, " + 1762 "message: ${(businessError as Businsess).message}"); 1763 } 1764} 1765``` 1766 1767### MifareClassicTag.transferToBlock<sup>9+</sup> 1768 1769transferToBlock(blockIndex: number, callback: AsyncCallback\<void>): void 1770 1771Transfers data from the temporary register to a block. This API uses an asynchronous callback to return the result. 1772 1773**Required permissions**: ohos.permission.NFC_TAG 1774 1775**System capability**: SystemCapability.Communication.NFC.Tag 1776 1777**Atomic service API**: This API can be used in atomic services since API version 12. 1778 1779**Parameters** 1780 1781| Name | Type | Mandatory| Description | 1782| -------- | ----------------------- | ---- | -------------------------------------- | 1783| blockIndex | number | Yes | Index of the destination block. The value starts form **0**.| 1784| callback | AsyncCallback\<void> | Yes | Callback used to return the operation result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 1785 1786**Error codes** 1787 1788For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 1789 1790| ID| Error Message| 1791| ------- | -------| 1792| 201 | Permission denied. | 1793| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1794| 3100201 | The tag running state is abnormal in the service. | 1795| 3100204 | The Tag I/O operation failed. | 1796 1797**Example** 1798 1799```js 1800import { tag } from '@kit.ConnectivityKit'; 1801import { BusinessError } from '@kit.BasicServicesKit'; 1802 1803// Obtain the correct MIFARE Classic tag by using the tag.TagInfo API in @ohos.nfc.tag. 1804 1805function nfcTechDemo() { 1806 // Connect the tag if it has not been connected. 1807 if (!mifareClassic.isTagConnected()) { 1808 if (!mifareClassic.connectTag()) { 1809 console.error("mifareClassic connectTag failed."); 1810 return; 1811 } 1812 } 1813 1814 try { 1815 let blockIndex = 1; // Set a correct index. 1816 mifareClassic.transferToBlock(blockIndex, (err : BusinessError)=> { 1817 if (err) { 1818 console.error("mifareClassic transferToBlock AsyncCallback errCode: ${err.code}," + 1819 "message: ${err.message}"); 1820 } else { 1821 console.info("mifareClassic transferToBlock AsyncCallback success."); 1822 } 1823 }); 1824 } catch (businessError) { 1825 console.error("mifareClassic transferToBlock AsyncCallback catch Code: ${(businessError as Businsess).code}, " + 1826 "message: ${(businessError as Businsess).message}"); 1827 } 1828} 1829``` 1830 1831### MifareClassicTag.restoreFromBlock<sup>9+</sup> 1832 1833restoreFromBlock(blockIndex: number): Promise\<void> 1834 1835Restores data in the temporary register from a block. This API uses a promise to return the result. 1836 1837**Required permissions**: ohos.permission.NFC_TAG 1838 1839**System capability**: SystemCapability.Communication.NFC.Tag 1840 1841**Atomic service API**: This API can be used in atomic services since API version 12. 1842 1843**Parameters** 1844 1845| Name | Type | Mandatory| Description | 1846| -------- | ----------------------- | ---- | -------------------------------------- | 1847| blockIndex | number | Yes | Index of the destination block. The value starts form **0**.| 1848 1849**Return value** 1850 1851| Type | Description | 1852| ------------------------- | ------------------ | 1853| Promise\<void> | Promise that returns no value.| 1854 1855**Error codes** 1856 1857For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 1858 1859| ID| Error Message| 1860| ------- | -------| 1861| 201 | Permission denied. | 1862| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1863| 3100201 | The tag running state is abnormal in the service. | 1864| 3100204 | The tag I/O operation failed. | 1865 1866**Example** 1867 1868```js 1869import { tag } from '@kit.ConnectivityKit'; 1870import { BusinessError } from '@kit.BasicServicesKit'; 1871 1872// Obtain the correct MIFARE Classic tag by using the tag.TagInfo API in @ohos.nfc.tag. 1873 1874function nfcTechDemo() { 1875 // Connect the tag if it has not been connected. 1876 if (!mifareClassic.isTagConnected()) { 1877 if (!mifareClassic.connectTag()) { 1878 console.error("mifareClassic connectTag failed."); 1879 return; 1880 } 1881 } 1882 1883 try { 1884 let blockIndex = 1; // Set a correct index. 1885 mifareClassic.restoreFromBlock(blockIndex).then(() => { 1886 console.info("mifareClassic restoreFromBlock Promise success."); 1887 }).catch((err : BusinessError)=> { 1888 console.error("mifareClassic restoreFromBlock Promise errCode: ${err.code}, message: ${err.message}"); 1889 }); 1890 } catch (businessError) { 1891 console.error("mifareClassic restoreFromBlock Promise catch businessError Code: ${(businessError as Businsess).code}," + 1892 " message: ${(businessError as Businsess).message}"); 1893 } 1894} 1895``` 1896 1897### MifareClassicTag.restoreFromBlock<sup>9+</sup> 1898 1899restoreFromBlock(blockIndex: number, callback: AsyncCallback\<void>): void 1900 1901Restores data in the temporary register from a block. This API uses an asynchronous callback to return the result. 1902 1903**Required permissions**: ohos.permission.NFC_TAG 1904 1905**System capability**: SystemCapability.Communication.NFC.Tag 1906 1907**Atomic service API**: This API can be used in atomic services since API version 12. 1908 1909**Parameters** 1910 1911| Name | Type | Mandatory| Description | 1912| -------- | ----------------------- | ---- | -------------------------------------- | 1913| blockIndex | number | Yes | Index of the destination block. The value starts form **0**.| 1914| callback | AsyncCallback\<void> | Yes | Callback used to return the operation result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 1915 1916**Error codes** 1917 1918For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 1919 1920| ID| Error Message| 1921| ------- | -------| 1922| 201 | Permission denied. | 1923| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1924| 3100201 | The tag running state is abnormal in the service. | 1925| 3100204 | The Tag I/O operation failed. | 1926 1927**Example** 1928 1929```js 1930import { tag } from '@kit.ConnectivityKit'; 1931import { BusinessError } from '@kit.BasicServicesKit'; 1932 1933// Obtain the correct MIFARE Classic tag by using the tag.TagInfo API in @ohos.nfc.tag. 1934 1935function nfcTechDemo() { 1936 // Connect the tag if it has not been connected. 1937 if (!mifareClassic.isTagConnected()) { 1938 if (!mifareClassic.connectTag()) { 1939 console.error("mifareClassic connectTag failed."); 1940 return; 1941 } 1942 } 1943 1944 try { 1945 let blockIndex = 1; // Set a correct index. 1946 mifareClassic.restoreFromBlock(blockIndex, (err : BusinessError)=> { 1947 if (err) { 1948 console.error("mifareClassic restoreFromBlock AsyncCallback err Code: ${err.code}," + 1949 " message: ${err.message}"); 1950 } else { 1951 console.info("mifareClassic restoreFromBlock AsyncCallback success."); 1952 } 1953 }); 1954 } catch (businessError) { 1955 console.error("mifareClassic restoreFromBlock AsyncCallback catch Code: ${(businessError as Businsess).code}, " + 1956 "message: ${(businessError as Businsess).message}"); 1957 } 1958} 1959``` 1960 1961### MifareClassicTag.getSectorCount<sup>9+</sup> 1962 1963getSectorCount(): number 1964 1965Obtains the number of sectors in this MIFARE Classic tag. 1966 1967**System capability**: SystemCapability.Communication.NFC.Tag 1968 1969**Atomic service API**: This API can be used in atomic services since API version 12. 1970 1971**Return value** 1972 1973| **Type**| **Description** | 1974| ------------------ | --------------------------| 1975| number | Number of sectors obtained.| 1976 1977**Example** 1978 1979```js 1980import { tag } from '@kit.ConnectivityKit'; 1981import { BusinessError } from '@kit.BasicServicesKit'; 1982 1983// Obtain the correct MIFARE Classic tag by using the tag.TagInfo API in @ohos.nfc.tag. 1984let sectorCount : number = mifareClassic.getSectorCount(); 1985console.info("mifareClassic sectorCount: " + sectorCount); 1986``` 1987 1988### MifareClassicTag.getBlockCountInSector<sup>9+</sup> 1989 1990getBlockCountInSector(sectorIndex: number): number 1991 1992Obtains the number of blocks in a sector. 1993 1994**System capability**: SystemCapability.Communication.NFC.Tag 1995 1996**Atomic service API**: This API can be used in atomic services since API version 12. 1997 1998**Parameters** 1999 2000| Name | Type | Mandatory| Description | 2001| -------- | ----------------------- | ---- | -------------------------------------- | 2002| sectorIndex | number | Yes | Index of the target sector. The sector indexes start from **0**.| 2003 2004**Return value** 2005 2006| **Type**| **Description** | 2007| ------------------ | --------------------------| 2008| number | Number of blocks obtained.| 2009 2010**Error codes** 2011 2012For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 2013 2014| ID| Error Message| 2015| ------- | -------| 2016| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 2017 2018**Example** 2019 2020```js 2021import { tag } from '@kit.ConnectivityKit'; 2022import { BusinessError } from '@kit.BasicServicesKit'; 2023 2024// Obtain the correct MIFARE Classic tag by using the tag.TagInfo API in @ohos.nfc.tag. 2025 2026try { 2027 let sectorIndex = 1; // Set a correct index. 2028 let blockCnt : number = mifareClassic.getBlockCountInSector(sectorIndex); 2029 console.info("mifareClassic blockCnt: " + blockCnt); 2030} catch (businessError) { 2031 console.error("mifareClassic getBlockCountInSector catch businessError Code: ${(businessError as Businsess).code}, " + 2032 "message: ${(businessError as Businsess).message}"); 2033} 2034``` 2035 2036### MifareClassicTag.getType<sup>9+</sup> 2037 2038getType(): [tag.MifareClassicType](js-apis-nfcTag.md#mifareclassictype9) 2039 2040Obtains the type of this MIFARE Classic tag. 2041 2042**System capability**: SystemCapability.Communication.NFC.Tag 2043 2044**Atomic service API**: This API can be used in atomic services since API version 12. 2045 2046**Return value** 2047 2048| **Type**| **Description** | 2049| ------------------ | --------------------------| 2050| [tag.MifareClassicType](js-apis-nfcTag.md#mifareclassictype9) | Type of the MIFARE Classic tag obtained.| 2051 2052**Example** 2053 2054```js 2055import { tag } from '@kit.ConnectivityKit'; 2056 2057// Obtain the correct MIFARE Classic tag by using the tag.TagInfo API in @ohos.nfc.tag. 2058let getType : tag.MifareClassicType = mifareClassic.getType(); 2059console.info("mifareClassic getType: " + getType); 2060``` 2061 2062### MifareClassicTag.getTagSize<sup>9+</sup> 2063 2064getTagSize(): number 2065 2066Obtains the size of this tag. For details, see [MifareClassicSize](js-apis-nfcTag.md#mifareclassicsize9). 2067 2068**System capability**: SystemCapability.Communication.NFC.Tag 2069 2070**Atomic service API**: This API can be used in atomic services since API version 12. 2071 2072**Return value** 2073 2074| **Type**| **Description** | 2075| ------------------ | --------------------------| 2076| number | Tag size obtained, in bytes. For details, see [MifareClassicSize](js-apis-nfcTag.md#mifareclassicsize9).| 2077 2078**Example** 2079 2080```js 2081import { tag } from '@kit.ConnectivityKit'; 2082import { BusinessError } from '@kit.BasicServicesKit'; 2083 2084// Obtain the correct MIFARE Classic tag by using the tag.TagInfo API in @ohos.nfc.tag. 2085let tagSize : number = mifareClassic.getTagSize(); 2086console.info("mifareClassic tagSize: " + tagSize); 2087``` 2088 2089### MifareClassicTag.isEmulatedTag<sup>9+</sup> 2090 2091isEmulatedTag(): boolean 2092 2093Checks whether it is an emulated tag. 2094 2095**System capability**: SystemCapability.Communication.NFC.Tag 2096 2097**Atomic service API**: This API can be used in atomic services since API version 12. 2098 2099**Return value** 2100 2101| **Type**| **Description** | 2102| ------------------ | --------------------------| 2103| boolean |Returns **true** if the tag is an emulated tag; returns **false** otherwise.| 2104 2105**Example** 2106 2107```js 2108import { tag } from '@kit.ConnectivityKit'; 2109import { BusinessError } from '@kit.BasicServicesKit'; 2110 2111// Obtain the correct MIFARE Classic tag by using the tag.TagInfo API in @ohos.nfc.tag. 2112let isEmulatedTag : boolean = mifareClassic.isEmulatedTag(); 2113console.info("mifareClassic isEmulatedTag: " + isEmulatedTag); 2114``` 2115 2116### MifareClassicTag.getBlockIndex<sup>9+</sup> 2117 2118getBlockIndex(sectorIndex: number): number 2119 2120Obtains the index of the first block in a sector. 2121 2122**System capability**: SystemCapability.Communication.NFC.Tag 2123 2124**Atomic service API**: This API can be used in atomic services since API version 12. 2125 2126**Parameters** 2127 2128| Name | Type | Mandatory| Description | 2129| -------- | ----------------------- | ---- | -------------------------------------- | 2130| sectorIndex | number | Yes | Index of the target sector. The sector indexes start from **0**.| 2131 2132**Return value** 2133 2134| **Type**| **Description** | 2135| ------------------ | --------------------------| 2136| number | Index of the first block obtained.| 2137 2138**Error codes** 2139 2140For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 2141 2142| ID| Error Message| 2143| ------- | -------| 2144| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 2145 2146**Example** 2147 2148```js 2149import { tag } from '@kit.ConnectivityKit'; 2150import { BusinessError } from '@kit.BasicServicesKit'; 2151 2152// Obtain the correct MIFARE Classic tag by using the tag.TagInfo API in @ohos.nfc.tag. 2153 2154try { 2155 let sectorIndex = 1; // Set a correct index. 2156 let blockIndex : number = mifareClassic.getBlockIndex(sectorIndex); 2157 console.info("mifareClassic blockIndex: " + blockIndex); 2158} catch (businessError) { 2159 console.error("mifareClassic getBlockIndex catch businessError Code: ${(businessError as Businsess).code}, " + 2160 "message: ${(businessError as Businsess).message}"); 2161} 2162``` 2163 2164### MifareClassicTag.getSectorIndex<sup>9+</sup> 2165 2166getSectorIndex(blockIndex: number): number 2167 2168Obtains the index of the sector that holds the specified block. 2169 2170**System capability**: SystemCapability.Communication.NFC.Tag 2171 2172**Atomic service API**: This API can be used in atomic services since API version 12. 2173 2174**Parameters** 2175 2176| Name | Type | Mandatory| Description | 2177| -------- | ----------------------- | ---- | -------------------------------------- | 2178| blockIndex | number | Yes| Index of the block. The block indexes start from **0**.| 2179 2180**Return value** 2181 2182| **Type**| **Description** | 2183| ------------------ | --------------------------| 2184| number | Index of the sector obtained.| 2185 2186**Error codes** 2187 2188For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 2189 2190| ID| Error Message| 2191| ------- | -------| 2192| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 2193 2194**Example** 2195 2196```js 2197import { tag } from '@kit.ConnectivityKit'; 2198import { BusinessError } from '@kit.BasicServicesKit'; 2199 2200// Obtain the correct MIFARE Classic tag by using the tag.TagInfo API in @ohos.nfc.tag. 2201 2202try { 2203 let blockIndex = 1; // Set a correct index. 2204 let sectorIndex : number = mifareClassic.getSectorIndex(blockIndex); 2205 console.info("mifareClassic sectorIndex: " + sectorIndex); 2206} catch (businessError) { 2207 console.error("mifareClassic getSectorIndex catch businessError Code: ${(businessError as Businsess).code}, " + 2208 "message: ${(businessError as Businsess).message}"); 2209} 2210``` 2211 2212## MifareUltralightTag<sup>9+</sup> 2213 2214Provides APIs to access MIFARE Ultralight properties and perform I/O operations on a tag. This class inherits from **TagSession**. 2215 2216**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). 2217 2218For details about how to obtain a **MifareUltralightTag** object, see [NFC Tag Read/Write Development](../../connectivity/nfc/nfc-tag-access-guide.md). 2219 2220The following describes the unique APIs of **MifareUltralightTag**. 2221 2222### MifareUltralightTag.readMultiplePages<sup>9+</sup> 2223 2224readMultiplePages(pageIndex: number): Promise\<number[]> 2225 2226Reads four pages of data (16 bytes in total) from the tag. This API uses a promise to return the result. 2227 2228**Required permissions**: ohos.permission.NFC_TAG 2229 2230**System capability**: SystemCapability.Communication.NFC.Tag 2231 2232**Atomic service API**: This API can be used in atomic services since API version 12. 2233 2234**Parameters** 2235 2236| Name | Type | Mandatory| Description | 2237| -------- | ----------------------- | ---- | ------------------------------ | 2238| pageIndex | number | Yes | Index of the first page to read. The page indexes start from **0**.| 2239 2240**Return value** 2241 2242| **Type**| **Description** | 2243| ------------------ | --------------------------| 2244| Promise\<number[]> | Promise used to return the data read.| 2245 2246**Error codes** 2247 2248For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 2249 2250| ID| Error Message| 2251| ------- | -------| 2252| 201 | Permission denied. | 2253| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 2254| 3100201 | The tag running state is abnormal in the service. | 2255| 3100204 | The tag I/O operation failed. | 2256 2257**Example** 2258 2259```js 2260 2261import { tag } from '@kit.ConnectivityKit'; 2262import { BusinessError } from '@kit.BasicServicesKit'; 2263 2264// Obtain the correct MIFARE Ultralight tag by using the tag.TagInfo API in @ohos.nfc.tag. 2265 2266function nfcTechDemo() { 2267 // Connect the tag if it has not been connected. 2268 if (!mifareUltralight.isTagConnected()) { 2269 if (!mifareUltralight.connectTag()) { 2270 console.error("mifareUltralight connectTag failed."); 2271 return; 2272 } 2273 } 2274 2275 try { 2276 let pageIndex = 1; // Set a correct index. 2277 mifareUltralight.readMultiplePages(pageIndex).then((data : number[]) => { 2278 console.info("mifareUltralight readMultiplePages Promise data = " + data); 2279 }).catch((err : BusinessError)=> { 2280 console.error("mifareUltralight readMultiplePages Promise Code: ${err.code}, message: ${err.message}"); 2281 }); 2282 } catch (businessError) { 2283 console.error("mifareUltralight readMultiplePages Promise catch businessError" + 2284 " Code: ${(businessError as Businsess).code}, message: ${(businessError as Businsess).message}"); 2285 } 2286} 2287``` 2288 2289### MifareUltralightTag.readMultiplePages<sup>9+</sup> 2290 2291readMultiplePages(pageIndex: number, callback: AsyncCallback\<number[]>): void 2292 2293Reads four pages of data (16 bytes in total) from the tag. This API uses an asynchronous callback to return the result. 2294 2295**Required permissions**: ohos.permission.NFC_TAG 2296 2297**System capability**: SystemCapability.Communication.NFC.Tag 2298 2299**Atomic service API**: This API can be used in atomic services since API version 12. 2300 2301**Parameters** 2302 2303| Name | Type | Mandatory| Description | 2304| -------- | ----------------------- | ---- | -------------------------------------- | 2305| pageIndex | number | Yes | Index of the first page to read. The page indexes start from **0**.| 2306| callback | AsyncCallback\<number[]> | Yes | Callback used to return the data (16 bytes in size) read.| 2307 2308**Error codes** 2309 2310For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 2311 2312| ID| Error Message| 2313| ------- | -------| 2314| 201 | Permission denied. | 2315| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 2316| 3100201 | The tag running state is abnormal in the service. | 2317| 3100204 | The Tag I/O operation failed. | 2318 2319**Example** 2320 2321```js 2322import { tag } from '@kit.ConnectivityKit'; 2323import { BusinessError } from '@kit.BasicServicesKit'; 2324 2325// Obtain the correct MIFARE Ultralight tag by using the tag.TagInfo API in @ohos.nfc.tag. 2326 2327function nfcTechDemo() { 2328 // Connect the tag if it has not been connected. 2329 if (!mifareUltralight.isTagConnected()) { 2330 if (!mifareUltralight.connectTag()) { 2331 console.error("mifareUltralight connectTag failed."); 2332 return; 2333 } 2334 } 2335 2336 try { 2337 let pageIndex = 1; // Set a correct index. 2338 mifareUltralight.readMultiplePages(pageIndex, (err : BusinessError, data : number[])=> { 2339 if (err) { 2340 console.log("mifareUltralight readMultiplePages AsyncCallback Code: ${err.code}, message: ${err.message}"); 2341 } else { 2342 console.info("mifareUltralight readMultiplePages AsyncCallback data: " + data); 2343 } 2344 }); 2345 } catch (businessError) { 2346 console.error("mifareUltralight readMultiplePages AsyncCallback catch Code: ${(businessError as Businsess).code}, " + 2347 "message: ${(businessError as Businsess).message}"); 2348 } 2349} 2350``` 2351 2352### MifareUltralightTag.writeSinglePage<sup>9+</sup> 2353 2354writeSinglePage(pageIndex: number, data: number[]): Promise\<void> 2355 2356Writes one page (4 bytes) of data to this tag. This API uses a promise to return the result. 2357 2358**Required permissions**: ohos.permission.NFC_TAG 2359 2360**System capability**: SystemCapability.Communication.NFC.Tag 2361 2362**Atomic service API**: This API can be used in atomic services since API version 12. 2363 2364**Parameters** 2365 2366| Name | Type | Mandatory| Description | 2367| -------- | ----------------------- | ---- | -------------------------------------- | 2368| pageIndex | number | Yes | Index of the page to write. The page indexes start from **0**.| 2369| data | number[] | Yes | 4-byte data to write.| 2370 2371**Return value** 2372 2373| Type | Description | 2374| ------------------------- | ------------------ | 2375| Promise\<void> | Promise that returns no value.| 2376 2377**Error codes** 2378 2379For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 2380 2381| ID| Error Message| 2382| ------- | -------| 2383| 201 | Permission denied. | 2384| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 2385| 3100201 | The tag running state is abnormal in the service. | 2386| 3100204 | The tag I/O operation failed. | 2387 2388**Example** 2389 2390```js 2391import { tag } from '@kit.ConnectivityKit'; 2392import { BusinessError } from '@kit.BasicServicesKit'; 2393 2394// Obtain the correct MIFARE Ultralight tag by using the tag.TagInfo API in @ohos.nfc.tag. 2395 2396function nfcTechDemo() { 2397 // Connect the tag if it has not been connected. 2398 if (!mifareUltralight.isTagConnected()) { 2399 if (!mifareUltralight.connectTag()) { 2400 console.error("mifareUltralight connectTag failed."); 2401 return; 2402 } 2403 } 2404 2405 try { 2406 let pageIndex = 1; // Set a correct index. 2407 let rawData = [0x01, 0x02, 0x03, 0x04]; // Set the correct data. The value must contain 4 bytes. 2408 mifareUltralight.writeSinglePage(pageIndex, rawData).then(() => { 2409 console.info("mifareUltralight writeSinglePage Promise success."); 2410 }).catch((err : BusinessError)=> { 2411 console.error("mifareUltralight writeSinglePage Promise err Code: ${err.code}, message: ${err.message}"); 2412 }); 2413 } catch (businessError) { 2414 console.error("mifareUltralight writeSinglePage Promise catch Code: ${(businessError as Businsess).code}, " + 2415 "message: ${(businessError as Businsess).message}"); 2416 } 2417} 2418``` 2419 2420### MifareUltralightTag.writeSinglePage<sup>9+</sup> 2421 2422writeSinglePage(pageIndex: number, data: number[], callback: AsyncCallback\<void>): void 2423 2424Writes one page (4 bytes) of data to this tag. This API uses an asynchronous callback to return the result. 2425 2426**Required permissions**: ohos.permission.NFC_TAG 2427 2428**System capability**: SystemCapability.Communication.NFC.Tag 2429 2430**Atomic service API**: This API can be used in atomic services since API version 12. 2431 2432**Parameters** 2433 2434| Name | Type | Mandatory| Description | 2435| -------- | ----------------------- | ---- | ------------------------ | 2436| pageIndex | number | Yes | Index of the page to write. The page indexes start from **0**.| 2437| data | number[] | Yes | 4-byte data to write.| 2438| callback|AsyncCallback\<void> |Yes| Callback used to return the operation result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 2439 2440**Error codes** 2441 2442For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 2443 2444| ID| Error Message| 2445| ------- | -------| 2446| 201 | Permission denied. | 2447| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 2448| 3100201 | The tag running state is abnormal in the service. | 2449| 3100204 | The Tag I/O operation failed. | 2450 2451**Example** 2452 2453```js 2454import { tag } from '@kit.ConnectivityKit'; 2455import { BusinessError } from '@kit.BasicServicesKit'; 2456 2457// Obtain the correct MIFARE Ultralight tag by using the tag.TagInfo API in @ohos.nfc.tag. 2458 2459function nfcTechDemo() { 2460 // Connect the tag if it has not been connected. 2461 if (!mifareUltralight.isTagConnected()) { 2462 if (!mifareUltralight.connectTag()) { 2463 console.error("mifareUltralight connectTag failed."); 2464 return; 2465 } 2466 } 2467 2468 try { 2469 let pageIndex = 1; // Set a correct index. 2470 let rawData = [0x01, 0x02, 0x03, 0x04]; // Set the correct data. The value must contain 4 bytes. 2471 mifareUltralight.writeSinglePage(pageIndex, rawData, (err : BusinessError)=> { 2472 if (err) { 2473 console.error("mifareUltralight writeSinglePage AsyncCallback Code: ${err.code}," + 2474 "message: ${err.message}"); 2475 } else { 2476 console.info("mifareUltralight writeSinglePage AsyncCallback success."); 2477 } 2478 }); 2479 } catch (businessError) { 2480 console.error("mifareUltralight writeSinglePage AsyncCallback catch Code: ${(businessError as Businsess).code}, " + 2481 "message: ${(businessError as Businsess).message}"); 2482 } 2483} 2484``` 2485 2486### MifareUltralightTag.getType<sup>9+</sup> 2487 2488getType(): [tag.MifareUltralightType](js-apis-nfcTag.md#mifareultralighttype9) 2489 2490Obtains the type of this MIFARE Ultralight tag. 2491 2492**System capability**: SystemCapability.Communication.NFC.Tag 2493 2494**Atomic service API**: This API can be used in atomic services since API version 12. 2495 2496**Return value** 2497 2498| **Type**| **Description** | 2499| ------------------ | --------------------------| 2500| [tag.MifareUltralightType](js-apis-nfcTag.md#mifareultralighttype9) | Type of the MIFARE Ultralight tag obtained.| 2501 2502**Example** 2503 2504```js 2505import { tag } from '@kit.ConnectivityKit'; 2506 2507// Obtain the correct MIFARE Ultralight tag by using the tag.TagInfo API in @ohos.nfc.tag. 2508let getType : tag.MifareUltralightType = mifareClassic.getType(); 2509console.info("mifareUltralight getType: " + getType); 2510``` 2511 2512## NdefFormatableTag<sup>9+</sup> 2513 2514Provides APIs for formatting NDEF formattable tags. This class inherits from **TagSession**. 2515 2516**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). 2517 2518For details about how to obtain an **NdefFormatableTag** object, see [NFC Tag Read/Write Development](../../connectivity/nfc/nfc-tag-access-guide.md). 2519 2520The following describes the unique APIs of **NdefFormatableTag**. 2521 2522### NdefFormatableTag.format<sup>9+</sup> 2523 2524format(message: [NdefMessage](#ndefmessage9)): Promise\<void> 2525 2526Formats this tag as an NDEF tag, and writes an NDEF message to it. This API uses a promise to return the result. 2527 2528**Required permissions**: ohos.permission.NFC_TAG 2529 2530**System capability**: SystemCapability.Communication.NFC.Tag 2531 2532**Atomic service API**: This API can be used in atomic services since API version 12. 2533 2534**Parameters** 2535 2536| Name | Type | Mandatory| Description | 2537| -------- | ----------------------- | ---- | -------------------------------------- | 2538| message | [NdefMessage](#ndefmessage9) | Yes | NDEF message to write. If this parameter is **null**, the tag is formatted only (no data will be written).| 2539 2540**Return value** 2541 2542| Type | Description | 2543| ------------------------- | ------------------ | 2544| Promise\<void> | Promise that returns no value.| 2545 2546**Error codes** 2547 2548For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 2549 2550| ID| Error Message| 2551| ------- | -------| 2552| 201 | Permission denied. | 2553| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 2554| 3100201 | The tag running state is abnormal in the service. | 2555| 3100204 | The tag I/O operation failed. | 2556 2557**Example** 2558 2559```js 2560import { tag } from '@kit.ConnectivityKit'; 2561import { BusinessError } from '@kit.BasicServicesKit'; 2562 2563// Obtain the correct NDEF formattable tag by using the tag.TagInfo API in @ohos.nfc.tag. 2564 2565function nfcTechDemo() { 2566 // Connect the tag if it has not been connected. 2567 if (!ndefFormatable.isTagConnected()) { 2568 if (!ndefFormatable.connectTag()) { 2569 console.error("ndefFormatable connectTag failed."); 2570 return; 2571 } 2572 } 2573 2574 try { 2575 // ndefMessage created from the raw data. For example: 2576 let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); 2577 // The NDEF data must be resolvable. 2578 // Or create ndefMessage from tag.ndef.createNdefMessage (ndefRecords:NdefRecord[]). 2579 2580 ndefFormatable.format(ndefMessage).then(() => { 2581 console.info("ndefFormatable format Promise success."); 2582 }).catch((err : BusinessError)=> { 2583 console.error("ndefFormatable format Promise err Code: ${err.code}, message: ${err.message}"); 2584 }); 2585 } catch (businessError) { 2586 console.error("ndefFormatable format Promise catch businessError Code: ${(businessError as Businsess).code}, " + 2587 "message: ${(businessError as Businsess).message}"); 2588 } 2589} 2590``` 2591 2592### NdefFormatableTag.format<sup>9+</sup> 2593 2594format(message: [NdefMessage](#ndefmessage9), callback: AsyncCallback\<void>): void 2595 2596Formats this tag as an NDEF tag, and writes an NDEF message to it. This API uses an asynchronous callback to return the result. 2597 2598**Required permissions**: ohos.permission.NFC_TAG 2599 2600**System capability**: SystemCapability.Communication.NFC.Tag 2601 2602**Atomic service API**: This API can be used in atomic services since API version 12. 2603 2604**Parameters** 2605 2606| Name | Type | Mandatory| Description | 2607| -------- | ----------------------- | ---- | -------------------------------------- | 2608| 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).| 2609 2610**Return value** 2611 2612| **Type**| **Description** | 2613| ------------------ | --------------------------| 2614| callback: AsyncCallback\<void> | Callback used to return the operation result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 2615 2616**Error codes** 2617 2618For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 2619 2620| ID| Error Message| 2621| ------- | -------| 2622| 201 | Permission denied. | 2623| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 2624| 3100201 | The tag running state is abnormal in the service. | 2625| 3100204 | The Tag I/O operation failed. | 2626 2627**Example** 2628 2629```js 2630import { tag } from '@kit.ConnectivityKit'; 2631import { BusinessError } from '@kit.BasicServicesKit'; 2632 2633// Obtain the correct NDEF formattable tag by using the tag.TagInfo API in @ohos.nfc.tag. 2634 2635function nfcTechDemo() { 2636 // Connect the tag if it has not been connected. 2637 if (!ndefFormatable.isTagConnected()) { 2638 if (!ndefFormatable.connectTag()) { 2639 console.error("ndefFormatable connectTag failed."); 2640 return; 2641 } 2642 } 2643 2644 try { 2645 // ndefMessage created from the raw data. For example: 2646 let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // The NDEF data must be resolvable. 2647 // Or create ndefMessage from tag.ndef.createNdefMessage (ndefRecords:NdefRecord[]). 2648 2649 ndefFormatable.format(ndefMessage, (err : BusinessError)=> { 2650 if (err) { 2651 console.error("ndefFormatable format AsyncCallback Code: ${err.code}, message: ${err.message}"); 2652 } else { 2653 console.info("ndefFormatable format AsyncCallback success."); 2654 } 2655 }); 2656 } catch (businessError) { 2657 console.error("ndefFormatable format AsyncCallback catch Code: ${(businessError as Businsess).code}, " + 2658 "message: ${(businessError as Businsess).message}"); 2659 } 2660} 2661``` 2662 2663### NdefFormatableTag.formatReadOnly<sup>9+</sup> 2664 2665formatReadOnly(message: [NdefMessage](#ndefmessage9)): Promise\<void> 2666 2667Formats 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. 2668 2669**Required permissions**: ohos.permission.NFC_TAG 2670 2671**System capability**: SystemCapability.Communication.NFC.Tag 2672 2673**Atomic service API**: This API can be used in atomic services since API version 12. 2674 2675**Parameters** 2676 2677| Name | Type | Mandatory| Description | 2678| -------- | ----------------------- | ---- | -------------------------------------- | 2679| message | [NdefMessage](#ndefmessage9) | Yes | NDEF message to write. If this parameter is **null**, the tag is formatted only (no data will be written).| 2680 2681**Return value** 2682 2683| Type | Description | 2684| ------------------------- | ------------------ | 2685| Promise\<void> | Promise that returns no value.| 2686 2687**Error codes** 2688 2689For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 2690 2691| ID| Error Message| 2692| ------- | -------| 2693| 201 | Permission denied. | 2694| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 2695| 3100201 | The tag running state is abnormal in the service. | 2696| 3100204 | The tag I/O operation failed. | 2697 2698**Example** 2699 2700```js 2701import { tag } from '@kit.ConnectivityKit'; 2702import { BusinessError } from '@kit.BasicServicesKit'; 2703 2704// Obtain the correct NDEF formattable tag by using the tag.TagInfo API in @ohos.nfc.tag. 2705 2706function nfcTechDemo() { 2707 // Connect the tag if it has not been connected. 2708 if (!ndefFormatable.isTagConnected()) { 2709 if (!ndefFormatable.connectTag()) { 2710 console.error("ndefFormatable connectTag failed."); 2711 return; 2712 } 2713 } 2714 2715 try { 2716 // ndefMessage created from the raw data. For example: 2717 let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); 2718 // The NDEF data must be resolvable. 2719 // Or create ndefMessage from tag.ndef.createNdefMessage (ndefRecords:NdefRecord[]). 2720 2721 ndefFormatable.formatReadOnly(ndefMessage).then(() => { 2722 console.info("ndefFormatable formatReadOnly Promise success."); 2723 }).catch((err : BusinessError)=> { 2724 console.error("ndefFormatable formatReadOnly Promise Code: ${err.code}, message: ${err.message}"); 2725 }); 2726 } catch (businessError) { 2727 console.error("ndefFormatable formatReadOnly Promise catch Code: ${(businessError as Businsess).code}, " + 2728 "message: ${(businessError as Businsess).message}"); 2729 } 2730} 2731``` 2732 2733### NdefFormatableTag.formatReadOnly<sup>9+</sup> 2734 2735formatReadOnly(message: [NdefMessage](#ndefmessage9), callback: AsyncCallback\<void>): void 2736 2737Formats 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. 2738 2739**Required permissions**: ohos.permission.NFC_TAG 2740 2741**System capability**: SystemCapability.Communication.NFC.Tag 2742 2743**Atomic service API**: This API can be used in atomic services since API version 12. 2744 2745**Parameters** 2746 2747| Name | Type | Mandatory| Description | 2748| -------- | ----------------------- | ---- | -------------------------------------- | 2749| message | [NdefMessage](#ndefmessage9) | Yes | NDEF message to write. If this parameter is **null**, the tag is formatted only (no data will be written).| 2750 2751**Return value** 2752 2753| **Type**| **Description** | 2754| ------------------ | --------------------------| 2755| callback: AsyncCallback\<void> | Callback used to return the operation result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 2756 2757**Error codes** 2758 2759For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 2760 2761| ID| Error Message| 2762| ------- | -------| 2763| 201 | Permission denied. | 2764| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 2765| 3100201 | The tag running state is abnormal in the service. | 2766| 3100204 | The Tag I/O operation failed. | 2767 2768**Example** 2769 2770```js 2771import { tag } from '@kit.ConnectivityKit'; 2772import { BusinessError } from '@kit.BasicServicesKit'; 2773 2774// Obtain the correct NDEF formattable tag by using the tag.TagInfo API in @ohos.nfc.tag. 2775 2776function nfcTechDemo() { 2777 // Connect the tag if it has not been connected. 2778 if (!ndefFormatable.isTagConnected()) { 2779 if (!ndefFormatable.connectTag()) { 2780 console.error("ndefFormatable connectTag failed."); 2781 return; 2782 } 2783 } 2784 2785 try { 2786 // ndefMessage created from the raw data. For example: 2787 let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); 2788 // The NDEF data must be resolvable. 2789 // Or create ndefMessage from tag.ndef.createNdefMessage (ndefRecords:NdefRecord[]). 2790 2791 ndefFormatable.formatReadOnly(ndefMessage, (err : BusinessError)=> { 2792 if (err) { 2793 console.error("ndefFormatable formatReadOnly AsyncCallback err Code: ${err.code}, message: ${err.message}"); 2794 } else { 2795 console.info("ndefFormatable formatReadOnly AsyncCallback success."); 2796 } 2797 }); 2798 } catch (businessError) { 2799 console.error("ndefFormatable formatReadOnly AsyncCallback catch Code: ${(businessError as Businsess).code}, " + 2800 "message: ${(businessError as Businsess).message}"); 2801 } 2802} 2803``` 2804 2805## BarcodeTag<sup>18+</sup> 2806 2807Provides the capability of reading barcode label attributes and accessing I/O operations. It is inherited from **TagSession**. 2808 2809**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). 2810 2811For details about how to obtain a **BarcodeTag** object, see [NFC Tag Read/Write Development](../../connectivity/nfc/nfc-tag-access-guide.md). 2812 2813The following describes the unique APIs of **BarcodeTag**. 2814 2815### BarcodeTag.getBarcode<sup>18+</sup> 2816 2817getBarcode(): Promise\<ArrayBuffer> 2818 2819Obtains a complete barcode tag. This API uses a promise to return the result. 2820 2821**Required permissions**: ohos.permission.NFC_TAG 2822 2823**System capability**: SystemCapability.Communication.NFC.Tag 2824 2825**Atomic service API**: This API can be used in atomic services since API version 18. 2826 2827 2828**Return value** 2829 2830| Type | Description | 2831| ------------------------- | ------------------ | 2832| Promise\<ArrayBuffer> | Promise used to return the barcode tag read.| 2833 2834**Error codes** 2835 2836For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 2837 2838| ID| Error Message| 2839| ------- | -------| 2840| 201 | Permission denied. | 2841| 3100201 | The tag running state is abnormal in the service. | 2842| 3100204 | The tag I/O operation failed. | 2843