1# @ohos.nfc.tag (Standard NFC Tags) 2 3The **nfcTag** module provides APIs for managing Near-Field Communication (NFC) tags. 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## **Declaration** 10 11Before developing applications related to tag read and write, you must declare NFC-related attributes in the attribute configuration file of the applications. For example, declare the following attributes in the **module.json5** file: 12```json 13{ 14 "module": { 15 // Attributes to declare. 16 17 "abilities": [ 18 { 19 "skills": [ 20 { 21 "actions": [ 22 // Actions to declare. 23 24 // Add the nfc tag action. 25 "ohos.nfc.tag.action.TAG_FOUND" 26 ], 27 "uris": [ 28 { 29 "type":"tag-tech/NfcA" 30 }, 31 { 32 "type":"tag-tech/IsoDep" 33 } 34 // Add other technology if neccessary, 35 // such as NfcB, NfcF, NfcV, Ndef, MifareClassic, MifareUL, and NdefFormatable. 36 ] 37 } 38 ] 39 } 40 ], 41 "requestPermissions": [ 42 { 43 "name": "ohos.permission.NFC_TAG", 44 "reason": "tag", 45 } 46 ] 47 } 48} 49``` 50> **CAUTION** 51> 52> - The **actions** field is mandatory. It must be **ohos.nfc.tag.action.TAG_FOUND** and cannot be changed. 53> - The **type** field under **uris** must start with **tag-tech/**, followed by NfcA, NfcB, NfcF, NfcV, IsoDep, Ndef, MifareClassic, MifareUL, or NdefFormatable. If there are multiple types, enter them in different lines. Incorrect settings of this field will cause a parsing failure. 54> - The **name** field under **requestPermissions** is mandatory. It must be **ohos.permission.NFC_TAG** and cannot be changed. 55## **Modules to Import** 56 57```js 58import tag from '@ohos.nfc.tag'; 59``` 60 61## **tag.TagInfo** 62 63Before a card with tags is read or written, **TagInfo** must be obtained to determine the tag technologies supported by the card. In this way, the application can invoke the correct API to communicate with the card. 64```js 65import tag from '@ohos.nfc.tag'; 66import UIAbility from '@ohos.app.ability.UIAbility'; 67import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 68import Want from '@ohos.app.ability.Want' 69 70export default class EntryAbility extends UIAbility { 71 onCreate(want : Want, launchParam: AbilityConstant.LaunchParam) { 72 // Add other code here. 73 74 // want is initialized by the NFC service and contains tagInfo. 75 let tagInfo : tag.TagInfo | null = null; 76 try { 77 tagInfo = tag.getTagInfo(want); 78 } catch (error) { 79 console.log("tag.getTagInfo caught error: " + error); 80 } 81 if (tagInfo == null || tagInfo == undefined) { 82 console.log("no TagInfo to be created, ignore it."); 83 return; 84 } 85 86 // Get the supported technologies for this found tag. 87 let isNfcATag = false; 88 let isIsoDepTag = false; 89 for (let i = 0; i < tagInfo.technology.length; i++) { 90 if (tagInfo.technology[i] == tag.NFC_A) { 91 isNfcATag = true; 92 } 93 94 if (tagInfo.technology[i] == tag.ISO_DEP) { 95 isIsoDepTag = true; 96 } 97 // Also check for technology tag.NFC_B, NFC_F, NFC_V, ISO_DEP, NDEF, MIFARE_CLASSIC, MIFARE_ULTRALIGHT, and NDEF_FORMATABLE. 98 } 99 100 // Use NfcA APIs to access the found tag. 101 if (isNfcATag) { 102 let nfcA : tag.NfcATag | null = null; 103 try { 104 nfcA = tag.getNfcATag(tagInfo); 105 } catch (error) { 106 console.log("tag.getNfcATag caught error: " + error); 107 } 108 // Other code to read or write this tag. 109 } 110 111 // Use getIsoDep APIs to access the found tag. 112 if (isIsoDepTag) { 113 let isoDep : tag.IsoDepTag | null = null; 114 try { 115 isoDep = tag.getIsoDep(tagInfo); 116 } catch (error) { 117 console.log("tag.getIsoDep caught error: " + error); 118 } 119 // Other code to read or write this tag. 120 } 121 // Use the same code to handle "NfcA/NfcB/NfcF/NfcV/Ndef/MifareClassic/MifareUL/NdefFormatable". 122 } 123} 124``` 125 126## tag.getNfcATag 127 128getNfcATag(tagInfo: [TagInfo](#taginfo)): [NfcATag](js-apis-nfctech.md#nfcatag) 129 130Obtains an **NfcATag** object, which allows access to the tags that use the NFC-A technology. 131 132> **NOTE** 133> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [tag.getNfcA](#taggetnfca9). 134 135**System capability**: SystemCapability.Communication.NFC.Tag 136 137**Parameters** 138 139| Name | Type | Mandatory| Description | 140| ------- | ------------------- | ---- | ------------------------------------------------------------- | 141| taginfo | [TagInfo](#taginfo) | Yes | Tag information including the technology type and related parameters, which are obtained from **tag.getTagInfo(want: Want)**.| 142 143**Return value** 144 145| **Type** | **Description** | 146| ------------------------------------- | ------------------ | 147| [NfcATag](js-apis-nfctech.md#nfcatag) | **NfcATag** object obtained.| 148 149## tag.getNfcA<sup>9+</sup> 150 151getNfcA(tagInfo: [TagInfo](#taginfo)): [NfcATag](js-apis-nfctech.md#nfcatag) 152 153Obtains an **NfcATag** object, which allows access to the tags that use the NFC-A technology. 154 155**System capability**: SystemCapability.Communication.NFC.Tag 156 157**Parameters** 158 159| Name | Type | Mandatory| Description | 160| ------- | ------------------- | ---- | ------------------------------------------------------------- | 161| taginfo | [TagInfo](#taginfo) | Yes | Tag information including the technology type and related parameters, which are obtained from **tag.getTagInfo(want: Want)**.| 162 163**Return value** 164 165| **Type** | **Description** | 166| ------------------------------------- | ------------------ | 167| [NfcATag](js-apis-nfctech.md#nfcatag) | **NfcATag** object obtained.| 168 169**Error codes** 170 171For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 172 173| ID| Error Message | 174| -------- | ----------------------------------------- | 175| 3100201 | Tag running state is abnormal in service. | 176 177## tag.getNfcBTag 178 179getNfcBTag(tagInfo: [TagInfo](#taginfo)): [NfcBTag](js-apis-nfctech.md#nfcbtag) 180 181Obtains an **NfcBTag** object, which allows access to the tags that use the NFC-B technology. 182 183> **NOTE** 184> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [tag.getNfcB](#taggetnfcb9). 185 186**System capability**: SystemCapability.Communication.NFC.Tag 187 188**Parameters** 189 190| Name | Type | Mandatory| Description | 191| ------- | ------------------- | ---- | ------------------------------------------------------------- | 192| taginfo | [TagInfo](#taginfo) | Yes | Tag information including the technology type and related parameters, which are obtained from **tag.getTagInfo(want: Want)**.| 193 194**Return value** 195 196| **Type** | **Description** | 197| ------------------------------------- | ------------------ | 198| [NfcBTag](js-apis-nfctech.md#nfcbtag) | **NfcBTag** object obtained.| 199 200## tag.getNfcB<sup>9+</sup> 201 202getNfcB(tagInfo: [TagInfo](#taginfo)): [NfcBTag](js-apis-nfctech.md#nfcbtag) 203 204Obtains an **NfcBTag** object, which allows access to the tags that use the NFC-B technology. 205 206**System capability**: SystemCapability.Communication.NFC.Tag 207 208**Parameters** 209 210| Name | Type | Mandatory| Description | 211| ------- | ------------------- | ---- | ------------------------------------------------------------- | 212| taginfo | [TagInfo](#taginfo) | Yes | Tag information including the technology type and related parameters, which are obtained from **tag.getTagInfo(want: Want)**.| 213 214**Return value** 215 216| **Type** | **Description** | 217| ------------------------------------- | ------------------ | 218| [NfcBTag](js-apis-nfctech.md#nfcbtag) | **NfcBTag** object obtained.| 219 220**Error codes** 221 222For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 223 224| ID| Error Message | 225| -------- | ----------------------------------------- | 226| 3100201 | Tag running state is abnormal in service. | 227 228## tag.getNfcFTag 229 230getNfcFTag(tagInfo: [TagInfo](#taginfo)): [NfcFTag](js-apis-nfctech.md#nfcftag) 231 232Obtains an **NfcFTag** object, which allows access to the tags that use the NFC-F technology. 233 234> **NOTE** 235> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [tag.getNfcF](#taggetnfcf9). 236 237**System capability**: SystemCapability.Communication.NFC.Tag 238 239**Parameters** 240 241| Name | Type | Mandatory| Description | 242| ------- | ------------------- | ---- | ------------------------------------------------------------- | 243| taginfo | [TagInfo](#taginfo) | Yes | Tag information including the technology type and related parameters, which are obtained from **tag.getTagInfo(want: Want)**.| 244 245**Return value** 246 247| **Type** | **Description** | 248| ------------------------------------- | ------------------ | 249| [NfcFTag](js-apis-nfctech.md#nfcftag) | **NfcFTag** object obtained.| 250 251## tag.getNfcF<sup>9+</sup> 252 253getNfcF(tagInfo: [TagInfo](#taginfo)): [NfcFTag](js-apis-nfctech.md#nfcftag) 254 255Obtains an **NfcFTag** object, which allows access to the tags that use the NFC-F technology. 256 257**System capability**: SystemCapability.Communication.NFC.Tag 258 259**Parameters** 260 261| Name | Type | Mandatory| Description | 262| ------- | ------------------- | ---- | ------------------------------------------------------------- | 263| taginfo | [TagInfo](#taginfo) | Yes | Tag information including the technology type and related parameters, which are obtained from **tag.getTagInfo(want: Want)**.| 264 265**Return value** 266 267| **Type** | **Description** | 268| ------------------------------------- | ------------------ | 269| [NfcFTag](js-apis-nfctech.md#nfcftag) | **NfcFTag** object obtained.| 270 271**Error codes** 272 273For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 274 275| ID| Error Message | 276| -------- | ----------------------------------------- | 277| 3100201 | Tag running state is abnormal in service. | 278 279## tag.getNfcVTag 280 281getNfcVTag(tagInfo: [TagInfo](#taginfo)): [NfcVTag](js-apis-nfctech.md#nfcvtag) 282 283Obtains an **NfcVTag** object, which allows access to the tags that use the NFC-V technology. 284 285> **NOTE** 286> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [tag.getNfcV](#taggetnfcv9). 287 288**System capability**: SystemCapability.Communication.NFC.Tag 289 290**Parameters** 291 292| Name | Type | Mandatory| Description | 293| ------- | ------------------- | ---- | ------------------------------------------------------------- | 294| taginfo | [TagInfo](#taginfo) | Yes | Tag information including the technology type and related parameters, which are obtained from **tag.getTagInfo(want: Want)**.| 295 296**Return value** 297 298| **Type** | **Description** | 299| ------------------------------------- | ------------------ | 300| [NfcVTag](js-apis-nfctech.md#nfcvtag) | **NfcVTag** object obtained.| 301 302## tag.getNfcV<sup>9+</sup> 303 304getNfcV(tagInfo: [TagInfo](#taginfo)): [NfcVTag](js-apis-nfctech.md#nfcvtag) 305 306Obtains an **NfcVTag** object, which allows access to the tags that use the NFC-V technology. 307 308**System capability**: SystemCapability.Communication.NFC.Tag 309 310**Parameters** 311 312| Name | Type | Mandatory| Description | 313| ------- | ------------------- | ---- | ------------------------------------------------------------- | 314| taginfo | [TagInfo](#taginfo) | Yes | Tag information including the technology type and related parameters, which are obtained from **tag.getTagInfo(want: Want)**.| 315 316**Return value** 317 318| **Type** | **Description** | 319| ------------------------------------- | ------------------ | 320| [NfcVTag](js-apis-nfctech.md#nfcvtag) | **NfcVTag** object obtained.| 321 322**Error codes** 323 324For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 325 326| ID| Error Message | 327| -------- | ----------------------------------------- | 328| 3100201 | Tag running state is abnormal in service. | 329 330## tag.getIsoDep<sup>9+</sup> 331 332getIsoDep(tagInfo: [TagInfo](#taginfo)): [IsoDepTag](js-apis-nfctech.md#isoDepTag9 ) 333 334Obtains an **IsoDepTag** object, which allows access to the tags that use the ISO-DEP technology. 335 336**System capability**: SystemCapability.Communication.NFC.Tag 337 338**Parameters** 339 340| Name | Type | Mandatory| Description | 341| ------- | ------------------- | ---- | ------------------------------------------------------------- | 342| taginfo | [TagInfo](#taginfo) | Yes | Tag information including the technology type and related parameters, which are obtained from **tag.getTagInfo(want: Want)**.| 343 344**Return value** 345 346| **Type** | **Description** | 347| ------------------------------------------ | ------------------------------------------------------- | 348| [IsoDepTag](js-apis-nfctech.md#isodeptag9) | **IsoDepTag** object obtained.| 349 350**Error codes** 351 352For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 353 354| ID| Error Message | 355| -------- | ----------------------------------------- | 356| 3100201 | Tag running state is abnormal in service. | 357 358## tag.getNdef<sup>9+</sup> 359 360getNdef(tagInfo: [TagInfo](#taginfo)): [NdefTag](js-apis-nfctech.md#ndeftag9) 361 362Obtains an **NdefTag** object, which allows access to the tags in the NFC Data Exchange Format (NDEF). 363 364**System capability**: SystemCapability.Communication.NFC.Tag 365 366**Parameters** 367 368| Name | Type | Mandatory| Description | 369| ------- | ------------------- | ---- | ------------------------------------------------------------- | 370| taginfo | [TagInfo](#taginfo) | Yes | Tag information including the technology type and related parameters, which are obtained from **tag.getTagInfo(want: Want)**.| 371 372**Return value** 373 374| **Type** | **Description** | 375| -------------------------------------- | --------------------------------------------------- | 376| [NdefTag](js-apis-nfctech.md#ndeftag9) | **NdefTag** object obtained.| 377 378**Error codes** 379 380For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 381 382| ID| Error Message | 383| -------- | ----------------------------------------- | 384| 3100201 | Tag running state is abnormal in service. | 385 386## tag.getMifareClassic<sup>9+</sup> 387 388getMifareClassic(tagInfo: [TagInfo](#taginfo)): [MifareClassicTag](js-apis-nfctech.md#mifareclassictag9) 389 390Obtains a **MifareClassicTag** object, which allows access to the tags that use MIFARE Classic. 391 392**System capability**: SystemCapability.Communication.NFC.Tag 393 394**Parameters** 395 396| Name | Type | Mandatory| Description | 397| ------- | ------------------- | ---- | ------------------------------------------------------------- | 398| taginfo | [TagInfo](#taginfo) | Yes | Tag information including the technology type and related parameters, which are obtained from **tag.getTagInfo(want: Want)**.| 399 400**Return value** 401 402| **Type** | **Description** | 403| --------------------------------------------------------- | ----------------------------------------------------------------------- | 404| [MifareClassicTag](js-apis-nfctech.md#mifareclassictag-9) | **MifareClassicTag** object obtained.| 405 406**Error codes** 407 408For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 409 410| ID| Error Message | 411| -------- | ----------------------------------------- | 412| 3100201 | Tag running state is abnormal in service. | 413 414## tag.getMifareUltralight<sup>9+</sup> 415 416getMifareUltralight(tagInfo: [TagInfo](#taginfo)): [MifareUltralightTag](js-apis-nfctech.md#mifareultralighttag9) 417 418Obtains a **MifareUltralightTag** object, which allows access to the tags that use MIFARE Ultralight. 419 420**System capability**: SystemCapability.Communication.NFC.Tag 421 422**Parameters** 423| Name | Type | Mandatory| Description | 424| ------- | ------------------- | ---- | ------------------------------------------------------------- | 425| taginfo | [TagInfo](#taginfo) | Yes | Tag information including the technology type and related parameters, which are obtained from **tag.getTagInfo(want: Want)**.| 426 427**Return value** 428 429| **Type** | **Description** | 430| -------------------------------------------------------------- | ----------------------------------------------------------------------------- | 431| [MifareUltralightTag](js-apis-nfctech.md#mifareultralighttag9) | **MifareUltralightTag** object obtained.| 432 433**Error codes** 434 435For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 436 437| ID| Error Message | 438| -------- | ----------------------------------------- | 439| 3100201 | Tag running state is abnormal in service. | 440 441## tag.getNdefFormatable<sup>9+</sup> 442 443getNdefFormatable(tagInfo: [TagInfo](#taginfo)): [NdefFormatableTag](js-apis-nfctech.md#ndefformatabletag9) 444 445Obtains an **NdefFormatableTag** object, which allows access to the tags that are NDEF formattable. 446 447**System capability**: SystemCapability.Communication.NFC.Tag 448 449**Return value** 450 451| **Type** | **Description** | 452| --------------------------------------------------------- | ------------------------------------------------------------------------- | 453| [NdefFormatableTag](js-apis-nfctech.md#ndefformatabletag) | **NdefFormatableTag** object obtained.| 454 455**Error codes** 456 457For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 458 459| ID| Error Message | 460| -------- | ----------------------------------------- | 461| 3100201 | Tag running state is abnormal in service. | 462 463## tag.getTagInfo<sup>9+</sup> 464 465getTagInfo(want: [Want](js-apis-app-ability-want.md#Want)): [TagInfo](#taginfo) 466 467Obtains **TagInfo** from **Want**, which is initialized by the NFC service and contains the attributes required by **TagInfo**. 468 469**System capability**: SystemCapability.Communication.NFC.Tag 470 471**Parameters** 472 473| Name| Type | Mandatory| Description | 474| ------ | ---------------------------------------- | ---- | --------------------------------------------------- | 475| want | [Want](js-apis-app-ability-want.md#Want) | Yes | Data obtained from the parameters of the **onCreate** entry function when an ability is dispatched.| 476 477**Return value** 478 479| **Type** | **Description** | 480| ------------------- | -------------------------------------------- | 481| [TagInfo](#taginfo) | **TagInfo** object obtained.| 482 483## tag.registerForegroundDispatch<sup>10+</sup> 484 485registerForegroundDispatch(elementName: [ElementName](js-apis-bundleManager-elementName.md), discTech: number[], callback: AsyncCallback<[TagInfo](#taginfo)>): void; 486 487Registers listening for the card reading events of an NFC tag foreground application. You can set the supported tag technologies in **discTech**, and obtain the [TagInfo](#taginfo) read in a callback. <br>This API must be used with [tag.unregisterForegroundDispatch](#tagunregisterforegrounddispatch10) in pairs. The registered event listening must be unregistered before the page exits the foreground or the page is destroyed. 488 489**Required permissions**: ohos.permission.NFC_TAG 490 491**System capability**: SystemCapability.Communication.NFC.Tag 492 493**Parameters** 494 495| Name | Type | Mandatory| Description | 496| ------------ | -------- | ---- | ------------------------------------------------------- | 497| elementName | [ElementName](js-apis-bundleManager-elementName.md) | Yes | Information about the application page. It must contain at least the **bundleName**, **abilityName**, and **moduleName**. | 498| discTech | number[] | Yes | Technologies supported by the foreground dispatch system. Each number indicates the constant value of the supported technology. Based on the value of **number**, the system sets tag technologies ([NFC_A](#technology-type-definition), [NFC_B](#technology-type-definition), and [NFC_F](#technology-type-definition), and [NFC_V](#technology-type-definition)) for NFC card read polling and disable card emulation. If the **number** length is 0, both card read polling and card emulation will be disabled.| 499| callback | AsyncCallback<[TagInfo](#taginfo)> | Yes | Callback invoked to return the card read event in the foreground.| 500 501**Example** 502 503See the example of [tag.unregisterForegroundDispatch](#tagunregisterforegrounddispatch10). 504 505## tag.unregisterForegroundDispatch<sup>10+</sup> 506 507unregisterForegroundDispatch(elementName: [ElementName](js-apis-bundleManager-elementName.md)): void; 508 509Unregisters the listening for card reading events of an NFC tag foreground application. The registered event listening must be unregistered before the page exits the foreground or the page is destroyed. 510 511**Required permissions**: ohos.permission.NFC_TAG 512 513**System capability**: SystemCapability.Communication.NFC.Tag 514 515**Parameters** 516 517| Name | Type | Mandatory| Description | 518| ------------ | -------- | ---- | ------------------------------------------------------- | 519| elementName | [ElementName](js-apis-bundleManager-elementName.md) | Yes | Information about the application page. It must contain at least the **bundleName**, **abilityName**, and **moduleName**. | 520 521**Example** 522 523```js 524import Want from '@ohos.app.ability.Want' 525import UIAbility from '@ohos.app.ability.UIAbility' 526import tag from '@ohos.nfc.tag'; 527import { BusinessError } from '@ohos.base'; 528import bundleManager from '@ohos.bundle.bundleManager'; 529import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 530 531let discTech : number[] = [tag.NFC_A, tag.NFC_B]; // replace with the tech(s) that is needed by foreground ability 532let elementName : bundleManager.ElementName; 533function foregroundCb(err : BusinessError, tagInfo : tag.TagInfo) { 534 if (err as BusinessError) { 535 if (!err) { 536 console.log("foreground callback: tag found tagInfo = ", JSON.stringify(tagInfo)); 537 } else { 538 console.log("foreground callback err: " + (err as BusinessError).message); 539 return; 540 } 541 } 542 // Other operations of taginfo. 543} 544 545export default class MainAbility extends UIAbility { 546 OnCreate(want : Want, launchParam : AbilityConstant.LaunchParam) { 547 console.log("OnCreate"); 548 elementName = { 549 bundleName: want.bundleName as string, 550 abilityName: want.abilityName as string, 551 moduleName: want.moduleName as string 552 } 553 } 554 555 onForeground() { 556 console.log("onForeground"); 557 try { 558 tag.registerForegroundDispatch(elementName, discTech, foregroundCb); 559 } catch (e) { 560 console.log("registerForegroundDispatch error: " + (e as BusinessError).message); 561 } 562 } 563 564 onBackground() { 565 console.log("onBackground"); 566 try { 567 tag.unregisterForegroundDispatch(elementName); 568 } catch (e) { 569 console.log("registerForegroundDispatch error: " + (e as BusinessError).message); 570 } 571 } 572 573 onWindowStageDestroy() { 574 console.log("onWindowStageDestroy"); 575 try { 576 tag.unregisterForegroundDispatch(elementName); 577 } catch (e) { 578 console.log("registerForegroundDispatch error: " + (e as BusinessError).message); 579 } 580 } 581 582 // Override other lifecycle functions. 583} 584``` 585 586## tag.ndef.makeUriRecord<sup>9+</sup> 587 588makeUriRecord(uri: string): [NdefRecord](#ndefrecord9); 589 590Creates an NDEF record based on the specified URI. 591 592**System capability**: SystemCapability.Communication.NFC.Tag 593 594**Parameters** 595 596| Name| Type | Mandatory| Description | 597| ------ | ------ | ---- | --------------------------------- | 598| uri | string | Yes | Data to write to the NDEF record.| 599 600**Return value** 601 602| **Type** | **Description** | 603| -------------------------- | ------------------------------------------------------------ | 604| [NdefRecord](#ndefrecord9) | NDEF record created. For details, see *NFCForum-TS-NDEF_1.0*.| 605 606**Example** 607 608```js 609import tag from '@ohos.nfc.tag'; 610 611try { 612 let uri = "https://gitee.com/openharmony"; // change it to be correct. 613 let ndefRecord = tag.ndef.makeUriRecord(uri); 614 if (ndefRecord != undefined) { 615 console.log("ndefMessage makeUriRecord rtdType: " + ndefRecord.rtdType); 616 console.log("ndefMessage makeUriRecord payload: " + ndefRecord.payload); 617 } else { 618 console.log("ndefMessage makeUriRecord ndefRecord: " + ndefRecord); 619 } 620} catch (busiError) { 621 console.log("ndefMessage makeUriRecord caught busiError: " + busiError); 622} 623``` 624 625## tag.ndef.makeTextRecord<sup>9+</sup> 626 627makeTextRecord(text: string, locale: string): [NdefRecord](#ndefrecord9); 628 629Creates an NDEF record based on the specified text data and encoding type. 630 631**System capability**: SystemCapability.Communication.NFC.Tag 632 633**Parameters** 634 635| Name| Type | Mandatory| Description | 636| ------ | ------ | ---- | ------------------------------------- | 637| text | string | Yes | Text to write to the NDEF record.| 638| locale | string | Yes | Encoding mode of the text. | 639 640**Return value** 641 642| **Type** | **Description** | 643| -------------------------- | ------------------------------------------------------------ | 644| [NdefRecord](#ndefrecord9) | NDEF record created. For details, see *NFCForum-TS-NDEF_1.0*.| 645 646**Example** 647 648```js 649import tag from '@ohos.nfc.tag'; 650 651try { 652 let text = "Hello World"; // change it to be correct. 653 let locale = "en"; // change it to be correct. 654 let ndefRecord = tag.ndef.makeTextRecord(text, locale); 655 if (ndefRecord != undefined) { 656 console.log("ndefMessage makeTextRecord rtdType: " + ndefRecord.rtdType); 657 console.log("ndefMessage makeTextRecord payload: " + ndefRecord.payload); 658 } else { 659 console.log("ndefMessage makeTextRecord ndefRecord: " + ndefRecord); 660 } 661} catch (busiError) { 662 console.log("ndefMessage makeTextRecord caught busiError: " + busiError); 663} 664``` 665 666 667## tag.ndef.makeMimeRecord<sup>9+</sup> 668 669makeMimeRecord(mimeType: string, mimeData: number[]): [NdefRecord](#ndefrecord9); 670 671Creates an NDEF record based on the specified MIME data and type. 672 673**System capability**: SystemCapability.Communication.NFC.Tag 674 675**Parameters** 676 677| Name | Type | Mandatory| Description | 678| -------- | -------- | ---- | ------------------------------------------------------- | 679| mimeType | string | Yes | MIME type that complies with RFC rules, for example, **text/plain** or **image/jpeg**.| 680| mimeData | number[] | Yes | MIME data, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.| 681 682**Return value** 683 684| **Type** | **Description** | 685| -------------------------- | ------------------------------------------------------------ | 686| [NdefRecord](#ndefrecord9) | NDEF record created. For details, see *NFCForum-TS-NDEF_1.0*.| 687 688**Example** 689 690```js 691import tag from '@ohos.nfc.tag'; 692 693try { 694 let mimeType = "text/plain"; // change it to be correct. 695 let mimeData = [0x01, 0x02, 0x03, 0x04]; // change it to be correct. 696 let ndefRecord = tag.ndef.makeMimeRecord(mimeType, mimeData); 697 if (ndefRecord != undefined) { 698 console.log("ndefMessage makeMimeRecord rtdType: " + ndefRecord.rtdType); 699 console.log("ndefMessage makeMimeRecord payload: " + ndefRecord.payload); 700 } else { 701 console.log("ndefMessage makeMimeRecord ndefRecord: " + ndefRecord); 702 } 703} catch (busiError) { 704 console.log("ndefMessage makeMimeRecord caught busiError: " + busiError); 705} 706``` 707## tag.ndef.makeExternalRecord<sup>9+</sup> 708 709makeExternalRecord(domainName: string, type: string, externalData: number[]): [NdefRecord](#ndefrecord9); 710 711Creates an NDEF record based on application-specific data. 712 713**System capability**: SystemCapability.Communication.NFC.Tag 714 715**Parameters** 716 717| Name | Type | Mandatory| Description | 718| ------------ | -------- | ---- | ------------------------------------------------------- | 719| domainName | string | Yes | Bundle name of the application or domain name of the organization that releases the applications. | 720| type | string | Yes | Type of the application data. | 721| externalData | number[] | Yes | Application data, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.| 722 723**Return value** 724 725| **Type** | **Description** | 726| -------------------------- | ------------------------------------------------------------ | 727| [NdefRecord](#ndefrecord9) | NDEF record created. For details, see *NFCForum-TS-NDEF_1.0*.| 728 729**Example** 730 731```js 732import tag from '@ohos.nfc.tag'; 733 734try { 735 let domainName = "ohos.nfc.application"; // change it to be correct. 736 let type = "test"; // change it to be correct. 737 let externalData = [0x01, 0x02, 0x03, 0x04]; // change it to be correct. 738 let ndefRecord = tag.ndef.makeExternalRecord(domainName, type, externalData); 739 if (ndefRecord != undefined) { 740 console.log("ndefMessage makeExternalRecord rtdType: " + ndefRecord.rtdType); 741 console.log("ndefMessage makeExternalRecord payload: " + ndefRecord.payload); 742 } else { 743 console.log("ndefMessage makeExternalRecord ndefRecord: " + ndefRecord); 744 } 745} catch (busiError) { 746 console.log("ndefMessage makeExternalRecord caught busiError: " + busiError); 747} 748``` 749 750## tag.ndef.messageToBytes<sup>9+</sup> 751 752messageToBytes(ndefMessage: [NdefMessage](js-apis-nfctech.md#ndefmessage9)): number[]; 753 754Converts an NDEF message to bytes. 755 756**System capability**: SystemCapability.Communication.NFC.Tag 757 758**Parameters** 759 760| Name | Type | Mandatory| Description | 761| ----------- | ---------------------------------------------- | ---- | ------------------ | 762| ndefMessage | [NdefMessage](js-apis-nfctech.md#ndefmessage9) | Yes | NDEF message to convert.| 763 764**Return value** 765 766| **Type**| **Description** | 767| -------- | ------------------------------------------------------------------------------------- | 768| number[] | NDEF message in bytes, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.| 769 770**Example** 771 772```js 773import tag from '@ohos.nfc.tag'; 774 775let rawData = [0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]; // MUST can be parsed as NDEF Record. 776try { 777 let ndefMessage = tag.ndef.createNdefMessage(rawData); 778 console.log("ndef createNdefMessage, ndefMessage: " + ndefMessage); 779 let rawData2 = tag.ndef.messageToBytes(ndefMessage); 780 console.log("ndefMessage messageToBytes rawData2: " + rawData2); 781} catch (busiError) { 782 console.log("ndef createNdefMessage busiError: " + busiError); 783} 784``` 785## tag.ndef.createNdefMessage<sup>9+</sup> 786 787createNdefMessage(data: number[]): [NdefMessage](js-apis-nfctech.md#ndefmessage9) 788 789Creates an NDEF message from raw byte data. The data must comply with the NDEF record format. Otherwise, the NDE record list contained in the **NdefMessage** object will be empty. 790 791**System capability**: SystemCapability.Communication.NFC.Tag 792 793**Parameters** 794 795| **Name**| **Type**| **Mandatory**| **Description** | 796| ---------- | -------- | -------- | ---------------------------------------------------------------------------------- | 797| data | number[] | Yes | Raw byte data, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**. The data must comply with the NDEF record format.| 798 799**Return value** 800 801| **Type** | **Description** | 802| ---------------------------------------------- | ------------------------------------------------------------- | 803| [NdefMessage](js-apis-nfctech.md#ndefmessage9) | NDEF message created. For details, see *NFCForum-TS-NDEF_1.0*.| 804 805**Example** 806```js 807import tag from '@ohos.nfc.tag'; 808 809let rawData = [0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]; // MUST can be parsed as NDEF Record. 810try { 811 let ndefMessage = tag.ndef.createNdefMessage(rawData); 812 console.log("ndef createNdefMessage, ndefMessage: " + ndefMessage); 813} catch (busiError) { 814 console.log("ndef createNdefMessage busiError: " + busiError); 815} 816``` 817 818## tag.ndef.createNdefMessage<sup>9+</sup> 819 820createNdefMessage(ndefRecords: NdefRecord[]): [NdefMessage](js-apis-nfctech.md#ndefmessage9) 821 822Creates an NDEF message from the NDEF records list. 823 824**System capability**: SystemCapability.Communication.NFC.Tag 825 826**Parameters** 827 828| **Name** | **Type** | **Mandatory**| **Description** | 829| ----------- | --------------------------------------------- | -------- | ---------------------------------------------------------------- | 830| ndefRecords | [NdefRecord](js-apis-nfcTag.md#ndefrecord9)[] | Yes | NDEF record list used to create the NDEF message. For details, see *NFCForum-TS-NDEF_1.0*.| 831 832**Return value** 833 834| **Type** | **Description** | 835| ---------------------------------------------- | ------------------------------------------------------------- | 836| [NdefMessage](js-apis-nfctech.md#ndefmessage9) | NDEF message created. For details, see *NFCForum-TS-NDEF_1.0*.| 837 838**Example** 839 840```js 841import tag from '@ohos.nfc.tag'; 842 843let uriRecord = tag.ndef.makeUriRecord("https://gitee.com/openharmony"); 844let textRecord = tag.ndef.makeTextRecord("Hello World", "en"); 845let ndefRecords = [uriRecord, textRecord]; 846try { 847 let ndefMessage = tag.ndef.createNdefMessage(ndefRecords); 848 console.log("ndef createNdefMessage ndefMessage: " + ndefMessage); 849} catch (busiError) { 850 console.log("ndef createNdefMessage busiError: " + busiError); 851} 852``` 853 854## TagInfo 855 856Defines the **TagInfo** object, which provides information about the tag technologies supported by a card. 857 858**System capability**: SystemCapability.Communication.NFC.Tag 859 860**Required permissions**: ohos.permission.NFC_TAG 861 862| **Name** | **Type** | **Readable**| **Writable**| **Description** | 863| ----------------------------- | ------------------------------------------------------------- | -------- | -------- | -------------------------------------------------------------------------------------------- | 864| uid<sup>9+</sup> | number[] | Yes | No | Tag unique identifier (UID), which consists of hexadecimal numbers ranging from **0x00** to **0xFF**. | 865| technology<sup>9+</sup> | number[] | Yes | No | Supported technologies. Each number is a constant indicating the supported technology. | 866| supportedProfiles | number[] | Yes | No | Supported profiles. This parameter is not supported since API version 9. Use [tag.TagInfo#technology](#tagtaginfo) instead. | 867| extrasData<sup>9+</sup> | [PacMap](js-apis-inner-ability-dataAbilityHelper.md#pacmap)[] | Yes | No | Extended attribute value of the tag technology.<br>**System API**: This is a system API. | 868| tagRfDiscId<sup>9+</sup> | number | Yes | No | ID allocated when the tag is discovered.<br>**System API**: This is a system API. | 869| remoteTagService<sup>9+</sup> | [rpc.RemoteObject](js-apis-rpc.md#remoteobject) | Yes | No | Remote object of the NFC service process used for interface communication between the client and the service.<br>**System API**: This is a system API.| 870## NdefRecord<sup>9+</sup> 871Defines an NDEF record. For details, see *NFCForum-TS-NDEF_1.0*. 872 873**System capability**: SystemCapability.Communication.NFC.Tag 874 875| **Name**| **Type**| **Readable**| **Writable**| **Description** | 876| -------- | -------- | -------- | -------- | ----------------------------------------------------------------------------------------- | 877| tnf | number | Yes | No | Type name field (TNF) of the NDEF record. | 878| rtdType | number[] | Yes | No | Record type definition (RTD) of the NDEF record. It consists of hexadecimal numbers ranging from **0x00** to **0xFF**.| 879| id | number[] | Yes | No | NDEF record ID, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**. | 880| payload | number[] | Yes | No | NDEF payload, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**. | 881 882## Technology Type Definition 883Enumerates the tag technology types. 884 885**System capability**: SystemCapability.Communication.NFC.Tag 886 887| **Name** | **Value**| **Description** | 888| ---------------------------- | ------ | --------------------------- | 889| NFC_A | 1 | NFC-A (ISO 14443-3A). | 890| NFC_B | 2 | NFC-B (ISO 14443-3B). | 891| ISO_DEP | 3 | ISO-DEP (ISO 14443-4).| 892| NFC_F | 4 | NFC-F (JIS 6319-4). | 893| NFC_V | 5 | NFC-V (ISO 15693). | 894| NDEF | 6 | NDEF. | 895| NDEF_FORMATABLE<sup>9+</sup> | 7 | NDEF formattable. | 896| MIFARE_CLASSIC | 8 | MIFARE Classic. | 897| MIFARE_ULTRALIGHT | 9 | MIFARE Ultralight. | 898 899## TnfType<sup>9+</sup> 900Enumerates the TNF types. For details, see *NFCForum-TS-NDEF_1.0*. 901 902**System capability**: SystemCapability.Communication.NFC.Tag 903 904| **Name** | **Value**| **Description** | 905| ---------------- | ------ | ------------------------------------------------ | 906| TNF_EMPTY | 0x0 | Empty. | 907| TNF_WELL_KNOWN | 0x1 | NFC Forum Well Known Type [NFC RTD]. | 908| TNF_MEDIA | 0x2 | Media-type as defined in RFC 2046 [RFC 2046]. | 909| TNF_ABSOLUTE_URI | 0x3 | Absolute URI as defined in RFC 3986 [RFC 3986].| 910| TNF_EXT_APP | 0x4 | NFC Forum external type [NFC RTD]. | 911| TNF_UNKNOWN | 0x5 | Unknown. | 912| TNF_UNCHANGED | 0x6 | Unchanged (see section 2.3.3 in *NFCForum-TS-NDEF_1.0*). | 913 914## NDEF Record RTD 915Enumerates the NDEF record types. For details about the RTD, see *NFCForum-TS-NDEF_1.0*. 916 917**System capability**: SystemCapability.Communication.NFC.Tag 918 919| **Name** | **Value**| **Description** | 920| --------------------- | ------ | ----------------------- | 921| RTD_TEXT<sup>9+</sup> | [0x54] | NDEF record of the text type.| 922| RTD_URI<sup>9+</sup> | [0x55] | NDEF record of the URI type. | 923 924## NfcForumType<sup>9+</sup> 925Enumerates the NFC Forum tag types. 926 927**System capability**: SystemCapability.Communication.NFC.Tag 928 929| **Name** | **Value**| **Description** | 930| ---------------- | ------ | -------------------- | 931| NFC_FORUM_TYPE_1 | 1 | NFC Forum tag type 1. | 932| NFC_FORUM_TYPE_2 | 2 | NFC Forum tag type 2. | 933| NFC_FORUM_TYPE_3 | 3 | NFC Forum tag type 3. | 934| NFC_FORUM_TYPE_4 | 4 | NFC Forum tag type 4. | 935| MIFARE_CLASSIC | 101 | MIFARE Classic.| 936 937## MifareClassicType<sup>9+</sup> 938Enumerates the MIFARE Classic tag types. 939 940**System capability**: SystemCapability.Communication.NFC.Tag 941 942| **Name** | **Value**| **Description** | 943| ------------ | ------ | -------------------- | 944| TYPE_UNKNOWN | 0 | Unknown type. | 945| TYPE_CLASSIC | 1 | MIFARE Classic.| 946| TYPE_PLUS | 2 | MIFARE Plus. | 947| TYPE_PRO | 3 | MIFARE Pro. | 948 949## MifareClassicSize<sup>9+</sup> 950Enumerates the sizes of a MIFARE Classic tag. 951 952**System capability**: SystemCapability.Communication.NFC.Tag 953 954| **Name** | **Value**| **Description** | 955| ------------ | ------ | --------------------------------- | 956| MC_SIZE_MINI | 320 | Each tag has 5 sectors, and each sector has 4 blocks. | 957| MC_SIZE_1K | 1024 | Each tag has 16 sectors, and each sector has 4 blocks.| 958| MC_SIZE_2K | 2048 | Each tag has 32 sectors, and each sector has 4 blocks.| 959| MC_SIZE_4K | 4096 | Each tag has 40 sectors, and each sector has 4 blocks.| 960 961## MifareUltralightType<sup>9+</sup> 962Enumerates the MIFARE Ultralight tag types. 963 964**System capability**: SystemCapability.Communication.NFC.Tag 965 966| **Name** | **Value**| **Description** | 967| ----------------- | ------ | ------------------------- | 968| TYPE_UNKNOWN | 0 | Unknown type. | 969| TYPE_ULTRALIGHT | 1 | MIFARE Ultralight. | 970| TYPE_ULTRALIGHT_C | 2 | MIFARE Ultralight C.| 971<!--no_check--> 972