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