1# @ohos.nfc.tag (Standard NFC Tags) 2 3The **tag** module provides APIs for operating and managing NFC tags. The following tag read modes are available: 4<br>Background mode: The device reads the tag by using NFC without starting any application, and then searches for applications based on the tag type. If only one application is matched, the card reading page of that application will be started. If multiple applications are matched, an application selector will be started, asking the user to select an application. 5<br>Foreground mode: A foreground application has priority to read the NFC tag discovered. 6 7> **NOTE** 8> 9> 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. 10 11## Declaring the NFC Tag Background Mode 12 13To enable NFC tags to be read without starting an application, declare NFC-related attributes in the **module.json5** file. 14```json 15{ 16 "module": { 17 // Other declared attributes 18 19 "abilities": [ 20 { 21 "skills": [ 22 { 23 "actions": [ 24 // Other declared actions 25 26 // Add the action of the NFC tag. 27 "ohos.nfc.tag.action.TAG_FOUND" 28 ], 29 "uris": [ 30 { 31 "type":"tag-tech/NfcA" 32 }, 33 { 34 "type":"tag-tech/IsoDep" 35 } 36 // Add other technologies if necessary. 37 // Example: NfcB, NfcF, NfcV, Ndef, MifareClassic, MifareUL, and NdefFormatable 38 ] 39 } 40 ] 41 } 42 ], 43 "requestPermissions": [ 44 { 45 "name": "ohos.permission.NFC_TAG", 46 "reason": "$string:app_name", 47 } 48 ] 49 } 50} 51``` 52> **NOTE**<br> 53> 54>1. The **actions** field must contain **ohos.nfc.tag.action.TAG_FOUND** and cannot be changed. 55>2. 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. 56>3. The **name** field under **requestPermissions** is mandatory. It must be **ohos.permission.NFC_TAG** and cannot be changed. 57>4. When calling the APIs and constants of this module, use **canIUse("SystemCapability.Communication.NFC.Tag")** to check whether the device supports NFC. If the device does not support NFC, the application stability may be affected. For details, see [NFC Tag Read/Write Development](../../connectivity/nfc/nfc-tag-access-guide.md). 58 59## **Modules to Import** 60 61```js 62import { tag } from '@kit.ConnectivityKit'; 63``` 64 65## **tag.TagInfo** 66 67Before a card with tags is read or written, **[TagInfo](#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. 68```js 69import { tag } from '@kit.ConnectivityKit'; 70import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 71 72export default class EntryAbility extends UIAbility { 73 onCreate(want : Want, launchParam: AbilityConstant.LaunchParam) { 74 // Add other function code. 75 76 // Initialize the want that contains the found tag by using the NFC service. 77 let tagInfo : tag.TagInfo | null = null; 78 try { 79 tagInfo = tag.getTagInfo(want); 80 } catch (error) { 81 console.error("tag.getTagInfo catch error: " + error); 82 } 83 if (tagInfo == null || tagInfo == undefined) { 84 console.log("no TagInfo to be created, ignore it."); 85 return; 86 } 87 88 // Obtain the list of technologies that can be used for discovering tags. 89 let isNfcATag = false; 90 let isIsoDepTag = false; 91 for (let i = 0; i < tagInfo.technology.length; i++) { 92 if (tagInfo.technology[i] == tag.NFC_A) { 93 isNfcATag = true; 94 } 95 if (tagInfo.technology[i] == tag.ISO_DEP) { 96 isIsoDepTag = true; 97 } 98 // Check for other technologies, for example, NFC_B, NFC_F, NFC_V, NDEF, MIFARE_CLASSIC, MIFARE_ULTRALIGHT, and NDEF_FORMATABLE. 99 } 100 101 // Use NFC-A APIs to access the discovered tags. 102 if (isNfcATag) { 103 let nfcA : tag.NfcATag | null = null; 104 try { 105 nfcA = tag.getNfcA(tagInfo); 106 } catch (error) { 107 console.error("tag.getNfcA catch error: " + error); 108 } 109 // Code for reading or writing the discovered tag. 110 } 111 112 // Use IsoDep APIs to access the discovered tags. 113 if (isIsoDepTag) { 114 let isoDep : tag.IsoDepTag | null = null; 115 try { 116 isoDep = tag.getIsoDep(tagInfo); 117 } catch (error) { 118 console.error("tag.getIsoDep catch error: " + error); 119 } 120 // Code for reading or writing the discovered tag. 121 } 122 // Use the same code to process other NFC tags, including NfcA, NfcB, NfcF, NfcV, Ndef, MifareClassic, MifareUL, and NdefFormatable. 123 } 124} 125``` 126 127## tag.getNfcATag<sup>(deprecated)</sup> 128 129getNfcATag(tagInfo: [TagInfo](#taginfo)): [NfcATag](js-apis-nfctech.md#nfcatag) 130 131Obtains an **NfcATag** object, which allows access to the tags that use the NFC-A technology. 132 133> **NOTE** 134> 135> This API is supported since API version 7 and deprecated since API version 9. Use [tag.getNfcA](#taggetnfca9) instead. 136 137**System capability**: SystemCapability.Communication.NFC.Tag 138 139**Parameters** 140 141| Name | Type | Mandatory| Description | 142| ------- | ------------------- | ---- | ------------------------------------------------------------- | 143| tagInfo | [TagInfo](#taginfo) | Yes | Tag information, including the tag technology type and related parameters, obtained from [tag.getTagInfo(want: Want)](#taggettaginfo9).| 144 145**Return value** 146 147| **Type** | **Description** | 148| ------------------------------------- | ------------------ | 149| [NfcATag](js-apis-nfctech.md#nfcatag) | **NfcATag** object obtained.| 150 151## tag.getNfcA<sup>9+</sup> 152 153getNfcA(tagInfo: [TagInfo](#taginfo)): [NfcATag](js-apis-nfctech.md#nfcatag) 154 155Obtains an **NfcATag** object, which allows access to the tags that use the NFC-A technology. 156 157**System capability**: SystemCapability.Communication.NFC.Tag 158 159**Atomic service API**: This API can be used in atomic services since API version 12. 160 161**Parameters** 162 163| Name | Type | Mandatory| Description | 164| ------- | ------------------- | ---- | ------------------------------------------------------------- | 165| tagInfo | [TagInfo](#taginfo) | Yes | Tag information, including the tag technology type and related parameters, obtained from [tag.getTagInfo(want: Want)](#taggettaginfo9).| 166 167**Return value** 168 169| **Type** | **Description** | 170| ------------------------------------- | ------------------ | 171| [NfcATag](js-apis-nfctech.md#nfcatag) | **NfcATag** object obtained.| 172 173**Error codes** 174 175For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 176 177| ID| Error Message | 178| -------- | ----------------------------------------- | 179| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 180| 801 | Capability not supported. | 181| 3100201 | The tag running state is abnormal in the service. | 182 183## tag.getNfcBTag<sup>(deprecated)</sup> 184 185getNfcBTag(tagInfo: [TagInfo](#taginfo)): [NfcBTag](js-apis-nfctech.md#nfcbtag) 186 187Obtains an **NfcBTag** object, which allows access to the tags that use the NFC-B technology. 188 189> **NOTE** 190> 191> This API is supported since API version 7 and deprecated since API version 9. Use [tag.getNfcB](#taggetnfcb9) instead. 192 193**System capability**: SystemCapability.Communication.NFC.Tag 194 195**Parameters** 196 197| Name | Type | Mandatory| Description | 198| ------- | ------------------- | ---- | ------------------------------------------------------------- | 199| tagInfo | [TagInfo](#taginfo) | Yes | Tag information, including the tag technology type and related parameters, obtained from [tag.getTagInfo(want: Want)](#taggettaginfo9).| 200 201**Return value** 202 203| **Type** | **Description** | 204| ------------------------------------- | ------------------ | 205| [NfcBTag](js-apis-nfctech.md#nfcbtag) | **NfcBTag** object obtained.| 206 207## tag.getNfcB<sup>9+</sup> 208 209getNfcB(tagInfo: [TagInfo](#taginfo)): [NfcBTag](js-apis-nfctech.md#nfcbtag) 210 211Obtains an **NfcBTag** object, which allows access to the tags that use the NFC-B technology. 212 213**System capability**: SystemCapability.Communication.NFC.Tag 214 215**Atomic service API**: This API can be used in atomic services since API version 12. 216 217**Parameters** 218 219| Name | Type | Mandatory| Description | 220| ------- | ------------------- | ---- | ------------------------------------------------------------- | 221| tagInfo | [TagInfo](#taginfo) | Yes | Tag information, including the tag technology type and related parameters, obtained from [tag.getTagInfo(want: Want)](#taggettaginfo9).| 222 223**Return value** 224 225| **Type** | **Description** | 226| ------------------------------------- | ------------------ | 227| [NfcBTag](js-apis-nfctech.md#nfcbtag) | **NfcBTag** object obtained.| 228 229**Error codes** 230 231For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 232 233| ID| Error Message | 234| -------- | ----------------------------------------- | 235| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 236| 801 | Capability not supported. | 237| 3100201 | The tag running state is abnormal in the service. | 238 239## tag.getNfcFTag<sup>(deprecated)</sup> 240 241getNfcFTag(tagInfo: [TagInfo](#taginfo)): [NfcFTag](js-apis-nfctech.md#nfcftag) 242 243Obtains an **NfcFTag** object, which allows access to the tags that use the NFC-F technology. 244 245> **NOTE** 246> 247> This API is supported since API version 7 and deprecated since API version 9. Use [tag.getNfcF](#taggetnfcf9) instead. 248 249**System capability**: SystemCapability.Communication.NFC.Tag 250 251**Parameters** 252 253| Name | Type | Mandatory| Description | 254| ------- | ------------------- | ---- | ------------------------------------------------------------- | 255| tagInfo | [TagInfo](#taginfo) | Yes | Tag information, including the tag technology type and related parameters, obtained from [tag.getTagInfo(want: Want)](#taggettaginfo9).| 256 257**Return value** 258 259| **Type** | **Description** | 260| ------------------------------------- | ------------------ | 261| [NfcFTag](js-apis-nfctech.md#nfcftag) | **NfcFTag** object obtained.| 262 263## tag.getNfcF<sup>9+</sup> 264 265getNfcF(tagInfo: [TagInfo](#taginfo)): [NfcFTag](js-apis-nfctech.md#nfcftag) 266 267Obtains an **NfcFTag** object, which allows access to the tags that use the NFC-F 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**Parameters** 274 275| Name | Type | Mandatory| Description | 276| ------- | ------------------- | ---- | ------------------------------------------------------------- | 277| tagInfo | [TagInfo](#taginfo) | Yes | Tag information, including the tag technology type and related parameters, obtained from [tag.getTagInfo(want: Want)](#taggettaginfo9).| 278 279**Return value** 280 281| **Type** | **Description** | 282| ------------------------------------- | ------------------ | 283| [NfcFTag](js-apis-nfctech.md#nfcftag) | **NfcFTag** object obtained.| 284 285**Error codes** 286 287For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 288 289| ID| Error Message | 290| -------- | ----------------------------------------- | 291| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 292| 801 | Capability not supported. | 293| 3100201 | The tag running state is abnormal in the service. | 294 295## tag.getNfcVTag<sup>(deprecated)</sup> 296 297getNfcVTag(tagInfo: [TagInfo](#taginfo)): [NfcVTag](js-apis-nfctech.md#nfcvtag) 298 299Obtains an **NfcVTag** object, which allows access to the tags that use the NFC-V technology. 300 301> **NOTE** 302> 303> This API is supported since API version 7 and deprecated since API version 9. Use [tag.getNfcV](#taggetnfcv9) instead. 304 305**System capability**: SystemCapability.Communication.NFC.Tag 306 307**Parameters** 308 309| Name | Type | Mandatory| Description | 310| ------- | ------------------- | ---- | ------------------------------------------------------------- | 311| tagInfo | [TagInfo](#taginfo) | Yes | Tag information, including the tag technology type and related parameters, obtained from [tag.getTagInfo(want: Want)](#taggettaginfo9).| 312 313**Return value** 314 315| **Type** | **Description** | 316| ------------------------------------- | ------------------ | 317| [NfcVTag](js-apis-nfctech.md#nfcvtag) | **NfcVTag** object obtained.| 318 319## tag.getNfcV<sup>9+</sup> 320 321getNfcV(tagInfo: [TagInfo](#taginfo)): [NfcVTag](js-apis-nfctech.md#nfcvtag) 322 323Obtains an **NfcVTag** object, which allows access to the tags that use the NFC-V technology. 324 325**System capability**: SystemCapability.Communication.NFC.Tag 326 327**Atomic service API**: This API can be used in atomic services since API version 12. 328 329**Parameters** 330 331| Name | Type | Mandatory| Description | 332| ------- | ------------------- | ---- | ------------------------------------------------------------- | 333| tagInfo | [TagInfo](#taginfo) | Yes | Tag information, including the tag technology type and related parameters, obtained from [tag.getTagInfo(want: Want)](#taggettaginfo9).| 334 335**Return value** 336 337| **Type** | **Description** | 338| ------------------------------------- | ------------------ | 339| [NfcVTag](js-apis-nfctech.md#nfcvtag) | **NfcVTag** object obtained.| 340 341**Error codes** 342 343For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 344 345| ID| Error Message | 346| -------- | ----------------------------------------- | 347| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 348| 801 | Capability not supported. | 349| 3100201 | The tag running state is abnormal in the service. | 350 351## tag.getIsoDep<sup>9+</sup> 352 353getIsoDep(tagInfo: [TagInfo](#taginfo)): [IsoDepTag](js-apis-nfctech.md#isoDepTag9 ) 354 355Obtains an **IsoDepTag** object, which allows access to the tags that use the IsoDep technology. 356 357**System capability**: SystemCapability.Communication.NFC.Tag 358 359**Atomic service API**: This API can be used in atomic services since API version 12. 360 361**Parameters** 362 363| Name | Type | Mandatory| Description | 364| ------- | ------------------- | ---- | ------------------------------------------------------------- | 365| tagInfo | [TagInfo](#taginfo) | Yes | Tag information, including the tag technology type and related parameters, obtained from [tag.getTagInfo(want: Want)](#taggettaginfo9).| 366 367**Return value** 368 369| **Type** | **Description** | 370| ------------------------------------------ | ------------------------------------------------------- | 371| [IsoDepTag](js-apis-nfctech.md#isodeptag9) | **IsoDepTag** object obtained.| 372 373**Error codes** 374 375For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 376 377| ID| Error Message | 378| -------- | ----------------------------------------- | 379| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 380| 801 | Capability not supported. | 381| 3100201 | The tag running state is abnormal in the service. | 382 383## tag.getNdef<sup>9+</sup> 384 385getNdef(tagInfo: [TagInfo](#taginfo)): [NdefTag](js-apis-nfctech.md#ndeftag9) 386 387Obtains an **NdefTag** object, which allows access to NFC Data Exchange Format (NDEF) tags. 388 389**System capability**: SystemCapability.Communication.NFC.Tag 390 391**Atomic service API**: This API can be used in atomic services since API version 12. 392 393**Parameters** 394 395| Name | Type | Mandatory| Description | 396| ------- | ------------------- | ---- | ------------------------------------------------------------- | 397| tagInfo | [TagInfo](#taginfo) | Yes | Tag information, including the tag technology type and related parameters, obtained from [tag.getTagInfo(want: Want)](#taggettaginfo9).| 398 399**Return value** 400 401| **Type** | **Description** | 402| -------------------------------------- | --------------------------------------------------- | 403| [NdefTag](js-apis-nfctech.md#ndeftag9) | **NdefTag** object obtained.| 404 405**Error codes** 406 407For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 408 409| ID| Error Message | 410| -------- | ----------------------------------------- | 411| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 412| 801 | Capability not supported. | 413| 3100201 | The tag running state is abnormal in the service. | 414 415## tag.getMifareClassic<sup>9+</sup> 416 417getMifareClassic(tagInfo: [TagInfo](#taginfo)): [MifareClassicTag](js-apis-nfctech.md#mifareclassictag9) 418 419Obtains a **MifareClassicTag** object, which allows access to the tags that use MIFARE Classic. 420 421**System capability**: SystemCapability.Communication.NFC.Tag 422 423**Atomic service API**: This API can be used in atomic services since API version 12. 424 425**Parameters** 426 427| Name | Type | Mandatory| Description | 428| ------- | ------------------- | ---- | ------------------------------------------------------------- | 429| tagInfo | [TagInfo](#taginfo) | Yes | Tag information, including the tag technology type and related parameters, obtained from [tag.getTagInfo(want: Want)](#taggettaginfo9).| 430 431**Return value** 432 433| **Type** | **Description** | 434| --------------------------------------------------------- | ----------------------------------------------------------------------- | 435| [MifareClassicTag](js-apis-nfctech.md#mifareclassictag-9) | **MifareClassicTag** object obtained.| 436 437**Error codes** 438 439For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 440 441| ID| Error Message | 442| -------- | ----------------------------------------- | 443| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 444| 801 | Capability not supported. | 445| 3100201 | The tag running state is abnormal in the service. | 446 447## tag.getMifareUltralight<sup>9+</sup> 448 449getMifareUltralight(tagInfo: [TagInfo](#taginfo)): [MifareUltralightTag](js-apis-nfctech.md#mifareultralighttag9) 450 451Obtains a **MifareUltralightTag** object, which allows access to the tags that use MIFARE Ultralight. 452 453**System capability**: SystemCapability.Communication.NFC.Tag 454 455**Atomic service API**: This API can be used in atomic services since API version 12. 456 457**Parameters** 458| Name | Type | Mandatory| Description | 459| ------- | ------------------- | ---- | ------------------------------------------------------------- | 460| tagInfo | [TagInfo](#taginfo) | Yes | Tag information, including the tag technology type and related parameters, obtained from [tag.getTagInfo(want: Want)](#taggettaginfo9).| 461 462**Return value** 463 464| **Type** | **Description** | 465| -------------------------------------------------------------- | ----------------------------------------------------------------------------- | 466| [MifareUltralightTag](js-apis-nfctech.md#mifareultralighttag9) | **MifareUltralightTag** object obtained.| 467 468**Error codes** 469 470For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 471 472| ID| Error Message | 473| -------- | ----------------------------------------- | 474| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 475| 801 | Capability not supported. | 476| 3100201 | The tag running state is abnormal in the service. | 477 478## tag.getNdefFormatable<sup>9+</sup> 479 480getNdefFormatable(tagInfo: [TagInfo](#taginfo)): [NdefFormatableTag](js-apis-nfctech.md#ndefformatabletag9) 481 482Obtains an **NdefFormatableTag** object, which allows access to the tags that are NDEF formattable. 483 484**System capability**: SystemCapability.Communication.NFC.Tag 485 486**Atomic service API**: This API can be used in atomic services since API version 12. 487 488**Parameters** 489| Name | Type | Mandatory| Description | 490| ------- | ------------------- | ---- | ------------------------------------------------------------- | 491| tagInfo | [TagInfo](#taginfo) | Yes | Tag information, including the tag technology type and related parameters, obtained from [tag.getTagInfo(want: Want)](#taggettaginfo9).| 492 493**Return value** 494 495| **Type** | **Description** | 496| --------------------------------------------------------- | ------------------------------------------------------------------------- | 497| [NdefFormatableTag](js-apis-nfctech.md#ndefformatabletag) | **NdefFormatableTag** object obtained.| 498 499**Error codes** 500 501For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 502 503| ID| Error Message | 504| -------- | ----------------------------------------- | 505| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 506| 801 | Capability not supported. | 507| 3100201 | The tag running state is abnormal in the service. | 508 509## tag.getBarcodeTag<sup>18+</sup> 510 511getBarcodeTag(taginfo: [TagInfo](#taginfo)): [BarcodeTag](js-apis-nfctech.md#barcodetag18) 512 513Obtains a **BarcodeTag** object, which allows access to the tags in the BarcodeTag format. 514 515 516**System capability**: SystemCapability.Communication.NFC.Tag 517 518**Atomic service API**: This API can be used in atomic services since API version 18. 519 520**Parameters** 521| Name | Type | Mandatory| Description | 522| ------- | ------------------- | ---- | ------------------------------------------------------------- | 523| tagInfo | [TagInfo](#taginfo) | Yes | Tag information, including the tag technology type and related parameters, obtained from [tag.getTagInfo(want: Want)](#taggettaginfo9).| 524 525**Return value** 526 527| Type | Description | 528| ------------------------- | ------------------ | 529| [BartcodeTag](js-apis-nfctech.md#barcodetag18) | **BarcodeTag** object obtained.| 530 531**Error codes** 532 533For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 534 535| ID| Error Message| 536| ------- | -------| 537| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 538| 801 | Capability not supported. | 539| 3100201 | The tag running state is abnormal in the service. | 540 541 542## tag.getTagInfo<sup>9+</sup> 543 544getTagInfo(want: [Want](../apis-ability-kit/js-apis-app-ability-want.md#want)): [TagInfo](#taginfo) 545 546Obtains **TagInfo** from **Want**, which is initialized by the NFC service and contains the attributes required by **TagInfo**. 547 548**System capability**: SystemCapability.Communication.NFC.Tag 549 550**Atomic service API**: This API can be used in atomic services since API version 12. 551 552**Parameters** 553 554| Name| Type | Mandatory| Description | 555| ------ | ---------------------------------------- | ---- | --------------------------------------------------- | 556| want | [Want](../apis-ability-kit/js-apis-app-ability-want.md#want) | Yes | Data obtained from the parameters of the **onCreate** entry function when an ability is dispatched.| 557 558**Return value** 559 560| **Type** | **Description** | 561| ------------------- | -------------------------------------------- | 562| [TagInfo](#taginfo) | **TagInfo** object obtained.| 563 564**Error codes** 565 566For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 567 568| ID| Error Message | 569| -------- | ----------------------------------------- | 570| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 571| 801 | Capability not supported. | 572 573## tag.registerForegroundDispatch<sup>10+</sup> 574 575registerForegroundDispatch(elementName: [ElementName](../apis-ability-kit/js-apis-bundleManager-elementName.md), discTech: number[], callback: AsyncCallback<[TagInfo](#taginfo)>): void 576 577Registers a listener for the NFC tag read event so that the tag can be preferentially dispatched to a foreground application. You can set the supported NFC tag technologies in **discTech**. The callback returns [TagInfo](#taginfo) read. This API can be called only by an application running in the foreground. It must be used with [tag.unregisterForegroundDispatch](#tagunregisterforegrounddispatch10) in pairs. The registered callback must be unregistered before the tag reading page exits the foreground or is destroyed. 578 579**Required permissions**: ohos.permission.NFC_TAG 580 581**System capability**: SystemCapability.Communication.NFC.Tag 582 583**Atomic service API**: This API can be used in atomic services since API version 12. 584 585**Parameters** 586 587| Name | Type | Mandatory| Description | 588| ------------ | -------- | ---- | ------------------------------------------------------- | 589| elementName | [ElementName](../apis-ability-kit/js-apis-bundleManager-elementName.md) | Yes | Information about the tag reading page of the application. It cannot be empty and must contain at least **bundleName** and **abilityName**. | 590| discTech | number[] | Yes | NFC tag technologies supported by the foreground application. It cannot be empty. At least one NFC tag technology must be specified. Each number indicates the constant value of an NFC tag technology. The tag technologies are polled based on the specified value, which contains one or more of [NFC_A](#constants), [NFC_B](#constants), [NFC_F](#constants), and [NFC_V](#constants) only.| 591| callback | AsyncCallback<[TagInfo](#taginfo)> | Yes | Callback used to return the tag information read. It cannot be empty.| 592 593**Error codes** 594 595For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 596 597| ID| Error Message | 598| -------- | ----------------------------------------- | 599| 201 | Permission denied. | 600| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 601| 801 | Capability not supported. | 602| 3100201 | The tag running state is abnormal in the service. | 603| 3100202 | The element state is invalid. | 604 605**Example** 606 607See the example of [tag.unregisterForegroundDispatch](#tagunregisterforegrounddispatch10). 608 609## tag.unregisterForegroundDispatch<sup>10+</sup> 610 611unregisterForegroundDispatch(elementName: [ElementName](../apis-ability-kit/js-apis-bundleManager-elementName.md)): void 612 613Unregisters the listener for the NFC tag read event. If the listener is unregistered, the NFC tag discovered will not be dispatched to foreground applications. The registered callback must be unregistered before the tag reading page exits the foreground or is destroyed. 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| elementName | [ElementName](../apis-ability-kit/js-apis-bundleManager-elementName.md) | Yes | Information about the tag reading page of the application. It cannot be empty and must contain at least **bundleName** and **abilityName**. | 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| 801 | Capability not supported. | 636| 3100201 | The tag running state is abnormal in the service. | 637 638**Example** 639 640```js 641 642import { tag } from '@kit.ConnectivityKit'; 643import { BusinessError } from '@kit.BasicServicesKit'; 644import { AbilityConstant, UIAbility, Want, bundleManager } from '@kit.AbilityKit'; 645 646let discTech: number[] = [tag.NFC_A, tag.NFC_B]; // Specify the technology required for foreground ability. 647let elementName : bundleManager.ElementName; 648function foregroundCb(err : BusinessError, tagInfo : tag.TagInfo) { 649 if (!err) { 650 console.log("foreground callback: tag found tagInfo = ", JSON.stringify(tagInfo)); 651 } else { 652 console.log("foreground callback err: " + err.message); 653 return; 654 } 655 // Other operations on taginfo 656} 657 658export default class MainAbility extends UIAbility { 659 OnCreate(want : Want, launchParam : AbilityConstant.LaunchParam) { 660 console.log("OnCreate"); 661 elementName = { 662 bundleName: want.bundleName as string, 663 abilityName: want.abilityName as string, 664 moduleName: want.moduleName as string 665 } 666 } 667 668 onForeground() { 669 console.log("onForeground"); 670 try { 671 tag.registerForegroundDispatch(elementName, discTech, foregroundCb); 672 } catch (e) { 673 console.error("registerForegroundDispatch error: " + (e as BusinessError).message); 674 } 675 } 676 677 onBackground() { 678 console.log("onBackground"); 679 try { 680 tag.unregisterForegroundDispatch(elementName); 681 } catch (e) { 682 console.error("registerForegroundDispatch error: " + (e as BusinessError).message); 683 } 684 } 685 686 onWindowStageDestroy() { 687 console.log("onWindowStageDestroy"); 688 try { 689 tag.unregisterForegroundDispatch(elementName); 690 } catch (e) { 691 console.error("registerForegroundDispatch error: " + (e as BusinessError).message); 692 } 693 } 694 695 // Other functions in the ability lifecycle 696} 697``` 698 699## tag.on<sup>11+</sup> 700 701on(type: 'readerMode', elementName: [ElementName](../apis-ability-kit/js-apis-bundleManager-elementName.md), discTech: number[], callback: AsyncCallback<[TagInfo](#taginfo)>): void 702 703Subscribes to the NFC tag read event to implement dispatch of the tag to a foreground application preferentially. The device enters the reader mode and disables card simulation. You can set the supported NFC tag technologies in **discTech**. The callback returns [TagInfo](#taginfo) read. This API must be used with [tag.off](#tagoff11) in pairs. If the NFC reader mode is enabled by [tag.on](#tagon11), **tag.off** must be called when the application page exits the foreground or is destroyed. 704 705**Required permissions**: ohos.permission.NFC_TAG 706 707**System capability**: SystemCapability.Communication.NFC.Tag 708 709**Atomic service API**: This API can be used in atomic services since API version 12. 710 711**Parameters** 712 713| Name | Type | Mandatory| Description | 714| ------------ | -------- | ---- | ------------------------------------------------------- | 715| type | string | Yes | Event type, which has a fixed value of **readerMode**.| 716| elementName | [ElementName](../apis-ability-kit/js-apis-bundleManager-elementName.md) | Yes | Information about the tag reading page of the application. It cannot be empty and must contain at least **bundleName** and **abilityName**. | 717| discTech | number[] | Yes | NFC tag technologies supported by the foreground application. It cannot be empty. At least one NFC tag technology must be specified. Each number indicates the constant value of an NFC tag technology. The tag technologies are polled based on the specified value, which contains one or more of [NFC_A](#constants), [NFC_B](#constants), [NFC_F](#constants), and [NFC_V](#constants) only.| 718| callback | AsyncCallback<[TagInfo](#taginfo)> | Yes | Callback used to return the tag information read. It cannot be empty.| 719 720**Error codes** 721 722For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 723 724| ID| Error Message | 725| -------- | ----------------------------------------- | 726| 201 | Permission denied. | 727| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 728| 801 | Capability not supported. | 729| 3100201 | The tag running state is abnormal in the service. | 730| 3100202 | The element state is invalid. | 731 732**Example** 733 734See the example of [tag.off](#tagoff11). 735 736## tag.off<sup>11+</sup> 737 738off(type: 'readerMode', elementName: [ElementName](../apis-ability-kit/js-apis-bundleManager-elementName.md), callback?: AsyncCallback<[TagInfo](#taginfo)>): void 739 740Unsubscribes from the NFC tag card read event. The device exits the reader mode and resumes card simulation. If the NFC reader mode is enabled by [tag.on](#tagon11), this API must be used when the application page exits the foreground or is destroyed. 741 742**Required permissions**: ohos.permission.NFC_TAG 743 744**System capability**: SystemCapability.Communication.NFC.Tag 745 746**Atomic service API**: This API can be used in atomic services since API version 12. 747 748**Parameters** 749 750| Name | Type | Mandatory| Description | 751| ------------ | -------- | ---- | ------------------------------------------------------- | 752| type | string | Yes | Event type, which has a fixed value of **readerMode**.| 753| elementName | [ElementName](../apis-ability-kit/js-apis-bundleManager-elementName.md) | Yes | Information about the tag reading page of the application. It cannot be empty and must contain at least **bundleName** and **abilityName**. | 754| callback | AsyncCallback<[TagInfo](#taginfo)> | No | Callback to unregister.| 755 756**Error codes** 757 758For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 759 760| ID| Error Message | 761| -------- | ----------------------------------------- | 762| 201 | Permission denied. | 763| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 764| 801 | Capability not supported. | 765| 3100201 | The tag running state is abnormal in the service. | 766| 3100203 | The off() API can be called only when the on() has been called. | 767 768**Example** 769 770```js 771import { tag } from '@kit.ConnectivityKit'; 772import { BusinessError } from '@kit.BasicServicesKit'; 773import { AbilityConstant, UIAbility, Want, bundleManager } from '@kit.AbilityKit'; 774 775let discTech: number[] = [tag.NFC_A, tag.NFC_B]; // Specify the technology required for foreground ability. 776let elementName : bundleManager.ElementName; 777 778function readerModeCb(err : BusinessError, tagInfo : tag.TagInfo) { 779 if (!err) { 780 console.log("offCallback: tag found tagInfo = ", JSON.stringify(tagInfo)); 781 } else { 782 console.error("offCallback err: " + err.message); 783 return; 784 } 785 // Other operations on taginfo 786} 787 788export default class MainAbility extends UIAbility { 789 OnCreate(want : Want, launchParam : AbilityConstant.LaunchParam) { 790 console.log("OnCreate"); 791 elementName = { 792 bundleName: want.bundleName as string, 793 abilityName: want.abilityName as string, 794 moduleName: want.moduleName as string 795 } 796 } 797 798 onForeground() { 799 console.log("on start"); 800 try { 801 tag.on('readerMode', elementName, discTech, readerModeCb); 802 } catch (e) { 803 console.error("tag.on error: " + (e as BusinessError).message); 804 } 805 } 806 807 onBackground() { 808 console.log("onBackground"); 809 try { 810 tag.off('readerMode', elementName, readerModeCb); 811 } catch (e) { 812 console.error("tag.off error: " + (e as BusinessError).message); 813 } 814 } 815 816 onWindowStageDestroy() { 817 console.log("onWindowStageDestroy"); 818 try { 819 tag.off('readerMode', elementName, readerModeCb); 820 } catch (e) { 821 console.error("tag.off error: " + (e as BusinessError).message); 822 } 823 } 824 825 // Other functions in the ability lifecycle 826} 827``` 828 829## tag.ndef.makeUriRecord<sup>9+</sup> 830 831makeUriRecord(uri: string): NdefRecord 832 833Creates an NDEF record based on the specified URI. 834 835**System capability**: SystemCapability.Communication.NFC.Tag 836 837**Atomic service API**: This API can be used in atomic services since API version 12. 838 839**Parameters** 840 841| Name| Type | Mandatory| Description | 842| ------ | ------ | ---- | --------------------------------- | 843| uri | string | Yes | Data to write to the NDEF record.| 844 845**Return value** 846 847| **Type** | **Description** | 848| -------------------------- | ------------------------------------------------------------ | 849| [NdefRecord](#ndefrecord9) | NDEF record created. For details, see *NFCForum-TS-NDEF_1.0*.| 850 851**Error codes** 852 853For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 854 855| ID| Error Message | 856| -------- | ----------------------------------------- | 857| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 858 859**Example** 860 861```js 862import { tag } from '@kit.ConnectivityKit'; 863 864try { 865 let uri = "https://www.example.com"; // Set the correct URI. 866 let ndefRecord : tag.NdefRecord = tag.ndef.makeUriRecord(uri); 867 if (ndefRecord != undefined) { 868 console.log("ndefMessage makeUriRecord rtdType: " + ndefRecord.rtdType); 869 console.log("ndefMessage makeUriRecord payload: " + ndefRecord.payload); 870 } else { 871 console.log("ndefMessage makeUriRecord ndefRecord: " + ndefRecord); 872 } 873} catch (businessError) { 874 console.error("ndefMessage makeUriRecord catch businessError: " + businessError); 875} 876``` 877 878## tag.ndef.makeTextRecord<sup>9+</sup> 879 880makeTextRecord(text: string, locale: string): NdefRecord 881 882Creates an NDEF record based on the specified text data and encoding type. 883 884**System capability**: SystemCapability.Communication.NFC.Tag 885 886**Atomic service API**: This API can be used in atomic services since API version 12. 887 888**Parameters** 889 890| Name| Type | Mandatory| Description | 891| ------ | ------ | ---- | ------------------------------------- | 892| text | string | Yes | Text to write to the NDEF record.| 893| locale | string | Yes | Encoding mode of the text. | 894 895**Return value** 896 897| **Type** | **Description** | 898| -------------------------- | ------------------------------------------------------------ | 899| [NdefRecord](#ndefrecord9) | NDEF record created. For details, see *NFCForum-TS-NDEF_1.0*.| 900 901**Error codes** 902 903For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 904 905| ID| Error Message | 906| -------- | ----------------------------------------- | 907| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 908 909**Example** 910 911```js 912import { tag } from '@kit.ConnectivityKit'; 913 914try { 915 let text = "Hello World"; // Set the text as demanded. 916 let locale = "en"; // Set the expected encoding format. 917 let ndefRecord : tag.NdefRecord = tag.ndef.makeTextRecord(text, locale); 918 if (ndefRecord != undefined) { 919 console.log("ndefMessage makeTextRecord rtdType: " + ndefRecord.rtdType); 920 console.log("ndefMessage makeTextRecord payload: " + ndefRecord.payload); 921 } else { 922 console.log("ndefMessage makeTextRecord ndefRecord: " + ndefRecord); 923 } 924} catch (businessError) { 925 console.error("ndefMessage makeTextRecord catch businessError: " + businessError); 926} 927``` 928 929## tag.ndef.makeApplicationRecord<sup>18+</sup> 930 931makeApplicationRecord(bundleName: string): NdefRecord 932 933Creates an NDEF record based on the specified application bundle name. 934 935**System capability**: SystemCapability.Communication.NFC.Tag 936 937**Atomic service API**: This API can be used in atomic services since API version 18. 938 939**Parameters** 940 941| Name| Type | Mandatory| Description | 942| ------ | ------ | ---- | ------------------------------------- | 943| bundleName | string | Yes | Application bundle name.| 944 945**Return value** 946 947| **Type** | **Description** | 948| -------------------------- | ------------------------------------------------------------ | 949| [NdefRecord](#ndefrecord9) | NDEF record created. For details, see *NFCForum-TS-NDEF_1.0*.| 950 951**Error codes** 952 953For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 954 955| ID| Error Message | 956| -------- | ----------------------------------------- | 957| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 958 959**Example** 960 961```js 962import { tag } from '@kit.ConnectivityKit'; 963 964try { 965 let bundleName: string = 'com.demo.test'; 966 let ndefRecord : tag.NdefRecord = tag.ndef.makeApplicationRecord(bundleName); 967 if (ndefRecord != undefined) { 968 console.log("ndefMessage makeApplicationRecord rtdType: " + ndefRecord.rtdType); 969 console.log("ndefMessage makeApplicationRecord payload: " + ndefRecord.payload); 970 } else { 971 console.log("ndefMessage makeApplicationRecord ndefRecord: " + ndefRecord); 972 } 973} catch (businessError) { 974 console.error("ndefMessage makeApplicationRecord catch businessError: " + businessError); 975} 976``` 977 978## tag.ndef.makeMimeRecord<sup>9+</sup> 979 980makeMimeRecord(mimeType: string, mimeData: number[]): NdefRecord 981 982Creates an NDEF record based on the specified MIME data and type. 983 984**System capability**: SystemCapability.Communication.NFC.Tag 985 986**Atomic service API**: This API can be used in atomic services since API version 12. 987 988**Parameters** 989 990| Name | Type | Mandatory| Description | 991| -------- | -------- | ---- | ------------------------------------------------------- | 992| mimeType | string | Yes | MIME type that complies with RFC rules, for example, **text/plain** or **image/jpeg**.| 993| mimeData | number[] | Yes | MIME data, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.| 994 995**Return value** 996 997| **Type** | **Description** | 998| -------------------------- | ------------------------------------------------------------ | 999| [NdefRecord](#ndefrecord9) | NDEF record created. For details, see *NFCForum-TS-NDEF_1.0*.| 1000 1001**Error codes** 1002 1003For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 1004 1005| ID| Error Message | 1006| -------- | ----------------------------------------- | 1007| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1008 1009**Example** 1010 1011```js 1012import { tag } from '@kit.ConnectivityKit'; 1013 1014try { 1015 let mimeType = "text/plain"; // Set a correct MIME type. 1016 let mimeData = [0x01, 0x02, 0x03, 0x04]; // Set the correct MIME data. 1017 let ndefRecord : tag.NdefRecord = tag.ndef.makeMimeRecord(mimeType, mimeData); 1018 if (ndefRecord != undefined) { 1019 console.log("ndefMessage makeMimeRecord rtdType: " + ndefRecord.rtdType); 1020 console.log("ndefMessage makeMimeRecord payload: " + ndefRecord.payload); 1021 } else { 1022 console.log("ndefMessage makeMimeRecord ndefRecord: " + ndefRecord); 1023 } 1024} catch (businessError) { 1025 console.error("ndefMessage makeMimeRecord catch businessError: " + businessError); 1026} 1027``` 1028## tag.ndef.makeExternalRecord<sup>9+</sup> 1029 1030makeExternalRecord(domainName: string, type: string, externalData: number[]): NdefRecord 1031 1032Creates an NDEF record based on application-specific data. 1033 1034**System capability**: SystemCapability.Communication.NFC.Tag 1035 1036**Atomic service API**: This API can be used in atomic services since API version 12. 1037 1038**Parameters** 1039 1040| Name | Type | Mandatory| Description | 1041| ------------ | -------- | ---- | ------------------------------------------------------- | 1042| domainName | string | Yes | Bundle name of the application or domain name of the organization that releases the applications. | 1043| type | string | Yes | Type of the application data. | 1044| externalData | number[] | Yes | Application data, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.| 1045 1046**Return value** 1047 1048| **Type** | **Description** | 1049| -------------------------- | ------------------------------------------------------------ | 1050| [NdefRecord](#ndefrecord9) | NDEF record created. For details, see *NFCForum-TS-NDEF_1.0*.| 1051 1052**Error codes** 1053 1054For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 1055 1056| ID| Error Message | 1057| -------- | ----------------------------------------- | 1058| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1059 1060**Example** 1061 1062```js 1063import { tag } from '@kit.ConnectivityKit'; 1064 1065try { 1066 let domainName = "ohos.nfc.application"; // Set a correct bundle name. 1067 let type = "test"; // Set a correct data type. 1068 let externalData = [0x01, 0x02, 0x03, 0x04]; // Set the correct external data. 1069 let ndefRecord : tag.NdefRecord = tag.ndef.makeExternalRecord(domainName, type, externalData); 1070 if (ndefRecord != undefined) { 1071 console.log("ndefMessage makeExternalRecord rtdType: " + ndefRecord.rtdType); 1072 console.log("ndefMessage makeExternalRecord payload: " + ndefRecord.payload); 1073 } else { 1074 console.log("ndefMessage makeExternalRecord ndefRecord: " + ndefRecord); 1075 } 1076} catch (businessError) { 1077 console.error("ndefMessage makeExternalRecord catch businessError: " + businessError); 1078} 1079``` 1080 1081## tag.ndef.messageToBytes<sup>9+</sup> 1082 1083messageToBytes(ndefMessage: [NdefMessage](js-apis-nfctech.md#ndefmessage9)): number[] 1084 1085Converts an NDEF message to bytes. 1086 1087**System capability**: SystemCapability.Communication.NFC.Tag 1088 1089**Atomic service API**: This API can be used in atomic services since API version 12. 1090 1091**Parameters** 1092 1093| Name | Type | Mandatory| Description | 1094| ----------- | ---------------------------------------------- | ---- | ------------------ | 1095| ndefMessage | [NdefMessage](js-apis-nfctech.md#ndefmessage9) | Yes | NDEF message to convert.| 1096 1097**Return value** 1098 1099| **Type**| **Description** | 1100| -------- | ------------------------------------------------------------------------------------- | 1101| number[] | NDEF message in bytes, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.| 1102 1103**Error codes** 1104 1105For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 1106 1107| ID| Error Message | 1108| -------- | ----------------------------------------- | 1109| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1110 1111**Example** 1112 1113```js 1114import { tag } from '@kit.ConnectivityKit'; 1115 1116let rawData = [0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]; // Set the correct raw data, which is in the NDEF format. 1117try { 1118 let ndefMessage : tag.NdefMessage = tag.ndef.createNdefMessage(rawData); 1119 console.log("ndef createNdefMessage, ndefMessage: " + ndefMessage); 1120 let rawData2 = tag.ndef.messageToBytes(ndefMessage); 1121 console.log("ndefMessage messageToBytes rawData2: " + rawData2); 1122} catch (businessError) { 1123 console.error("ndef createNdefMessage businessError: " + businessError); 1124} 1125``` 1126## tag.ndef.createNdefMessage<sup>9+</sup> 1127 1128createNdefMessage(data: number[]): [NdefMessage](js-apis-nfctech.md#ndefmessage9) 1129 1130Creates 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. 1131 1132**System capability**: SystemCapability.Communication.NFC.Tag 1133 1134**Atomic service API**: This API can be used in atomic services since API version 12. 1135 1136**Parameters** 1137 1138| **Name**| **Type**| **Mandatory**| **Description** | 1139| ---------- | -------- | -------- | ---------------------------------------------------------------------------------- | 1140| 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.| 1141 1142**Return value** 1143 1144| **Type** | **Description** | 1145| ---------------------------------------------- | ------------------------------------------------------------- | 1146| [NdefMessage](js-apis-nfctech.md#ndefmessage9) | NDEF message created. For details, see *NFCForum-TS-NDEF_1.0*.| 1147 1148**Error codes** 1149 1150For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 1151 1152| ID| Error Message | 1153| -------- | ----------------------------------------- | 1154| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1155 1156**Example** 1157```js 1158import { tag } from '@kit.ConnectivityKit'; 1159 1160let rawData = [0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]; // The NDEF data must be resolvable. 1161try { 1162 let ndefMessage : tag.NdefMessage = tag.ndef.createNdefMessage(rawData); 1163 console.log("ndef createNdefMessage, ndefMessage: " + ndefMessage); 1164} catch (businessError) { 1165 console.error("ndef createNdefMessage businessError: " + businessError); 1166} 1167``` 1168 1169## tag.ndef.createNdefMessage<sup>9+</sup> 1170 1171createNdefMessage(ndefRecords: NdefRecord[]): [NdefMessage](js-apis-nfctech.md#ndefmessage9) 1172 1173Creates an NDEF message from the NDEF records list. 1174 1175**System capability**: SystemCapability.Communication.NFC.Tag 1176 1177**Atomic service API**: This API can be used in atomic services since API version 12. 1178 1179**Parameters** 1180 1181| **Name** | **Type** | **Mandatory**| **Description** | 1182| ----------- | --------------------------------------------- | -------- | ---------------------------------------------------------------- | 1183| 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*.| 1184 1185**Return value** 1186 1187| **Type** | **Description** | 1188| ---------------------------------------------- | ------------------------------------------------------------- | 1189| [NdefMessage](js-apis-nfctech.md#ndefmessage9) | NDEF message created. For details, see *NFCForum-TS-NDEF_1.0*.| 1190 1191**Error codes** 1192 1193For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). 1194 1195| ID| Error Message | 1196| -------- | ----------------------------------------- | 1197| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1198 1199**Example** 1200 1201```js 1202import { tag } from '@kit.ConnectivityKit'; 1203 1204let uriRecord : tag.NdefRecord = tag.ndef.makeUriRecord("https://www.example.com"); 1205let textRecord : tag.NdefRecord = tag.ndef.makeTextRecord("Hello World", "en"); 1206let ndefRecords : tag.NdefRecord[] = [uriRecord, textRecord]; 1207try { 1208 let ndefMessage : tag.NdefMessage = tag.ndef.createNdefMessage(ndefRecords); 1209 console.log("ndef createNdefMessage ndefMessage: " + ndefMessage); 1210} catch (businessError) { 1211 console.error("ndef createNdefMessage businessError: " + businessError); 1212} 1213``` 1214 1215## TagInfo 1216 1217Defines the **TagInfo** object, which provides information about the tag technologies supported by a card. 1218 1219**System capability**: SystemCapability.Communication.NFC.Tag 1220 1221**Required permissions**: ohos.permission.NFC_TAG 1222 1223| **Name** | **Type** | **Readable**| **Writable**| **Description** | 1224| ----------------------------- | ------------------------------------------------------------- | -------- | -------- | -------------------------------------------------------------------------------------------- | 1225| uid<sup>9+</sup> | number[] | Yes | No | Tag unique identifier (UID), which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 1226| technology<sup>9+</sup> | number[] | Yes | No | Supported tag technologies. Each number is a constant indicating the supported technology.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 1227| supportedProfiles | number[] | Yes | No | Supported profiles. This parameter is not supported since API version 9. Use [tag.TagInfo#technology](#taginfo) instead. | 1228 1229## NdefRecord<sup>9+</sup> 1230Defines an NDEF record. For details, see *NFCForum-TS-NDEF_1.0*. 1231 1232**System capability**: SystemCapability.Communication.NFC.Tag 1233 1234**Atomic service API**: This API can be used in atomic services since API version 12. 1235 1236| **Name**| **Type**| **Readable**| **Writable**| **Description** | 1237| -------- | -------- | -------- | -------- | ----------------------------------------------------------------------------------------- | 1238| tnf | number | Yes | No | Type name field (TNF) of the NDEF record. | 1239| rtdType | number[] | Yes | No | Record type definition (RTD) of the NDEF record. It consists of hexadecimal numbers ranging from **0x00** to **0xFF**.| 1240| id | number[] | Yes | No | NDEF record ID, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**. | 1241| payload | number[] | Yes | No | NDEF payload, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**. | 1242 1243## Constants 1244Enumerates the tag technology types. 1245 1246**System capability**: SystemCapability.Communication.NFC.Tag 1247 1248| **Name** |**Type**| **Value**| **Description** | 1249| ---------------------------- | ------ | ------ | --------------------------- | 1250| NFC_A<sup>12+</sup> | number | 1 | NFC-A (ISO 14443-3A). <br>**Atomic service API**: This API can be used in atomic services since API version 12.| 1251| NFC_B<sup>12+</sup> | number | 2 | NFC-B (ISO 14443-3B). <br>**Atomic service API**: This API can be used in atomic services since API version 12.| 1252| ISO_DEP<sup>12+</sup> | number | 3 | ISO-DEP (ISO 14443-4).<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 1253| NFC_F<sup>12+</sup> | number | 4 | NFC-F (JIS 6319-4).<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 1254| NFC_V<sup>12+</sup> | number | 5 | NFC-V (ISO 15693).<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 1255| NDEF<sup>12+</sup> | number | 6 | NDEF.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 1256| NDEF_FORMATABLE<sup>9+</sup> | number | 7 | NDEF formattable.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 1257| MIFARE_CLASSIC<sup>12+</sup> | number | 8 | MIFARE Classic.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 1258| MIFARE_ULTRALIGHT<sup>12+</sup> | number | 9 | MIFARE Ultralight.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 1259| NFC_BARCODE<sup>18+</sup> | number | 10 | BARCODE technology.<br>**Atomic service API**: This API can be used in atomic services since API version 18. | 1260 1261## TnfType<sup>9+</sup> 1262Enumerates the TNF types. For details, see *NFCForum-TS-NDEF_1.0*. 1263 1264**System capability**: SystemCapability.Communication.NFC.Tag 1265 1266**Atomic service API**: This API can be used in atomic services since API version 12. 1267 1268| **Name** | **Value**| **Description** | 1269| ---------------- | ------ | ------------------------------------------------ | 1270| TNF_EMPTY | 0x0 | Empty. | 1271| TNF_WELL_KNOWN | 0x1 | NFC Forum Well Known Type [NFC RTD]. | 1272| TNF_MEDIA | 0x2 | Media-type as defined in RFC 2046 [RFC 2046]. | 1273| TNF_ABSOLUTE_URI | 0x3 | Absolute URI as defined in RFC 3986 [RFC 3986].| 1274| TNF_EXT_APP | 0x4 | NFC Forum external type [NFC RTD]. | 1275| TNF_UNKNOWN | 0x5 | Unknown. | 1276| TNF_UNCHANGED | 0x6 | Unchanged (see section 2.3.3 in *NFCForum-TS-NDEF_1.0*). | 1277 1278## NDEF Record RTD 1279Enumerates the NDEF record types. For details about the RTD, see *NFCForum-TS-NDEF_1.0*. 1280 1281**System capability**: SystemCapability.Communication.NFC.Tag 1282 1283**Atomic service API**: This API can be used in atomic services since API version 12. 1284 1285| **Name** |**Type**| **Value**| **Description** | 1286| --------------------- | ------ | ------ | ----------------------- | 1287| RTD_TEXT<sup>9+</sup> |number[]| [0x54] | NDEF record of the text type.| 1288| RTD_URI<sup>9+</sup> |number[]| [0x55] | NDEF record of the URI type. | 1289 1290## NfcForumType<sup>9+</sup> 1291Enumerates the NFC Forum tag types. 1292 1293**System capability**: SystemCapability.Communication.NFC.Tag 1294 1295**Atomic service API**: This API can be used in atomic services since API version 12. 1296 1297| **Name** | **Value**| **Description** | 1298| ---------------- | ------ | -------------------- | 1299| NFC_FORUM_TYPE_1 | 1 | NFC Forum tag type 1. | 1300| NFC_FORUM_TYPE_2 | 2 | NFC Forum tag type 2. | 1301| NFC_FORUM_TYPE_3 | 3 | NFC Forum tag type 3. | 1302| NFC_FORUM_TYPE_4 | 4 | NFC Forum tag type 4. | 1303| MIFARE_CLASSIC | 101 | MIFARE Classic.| 1304 1305## MifareClassicType<sup>9+</sup> 1306Enumerates the MIFARE Classic tag types. 1307 1308**System capability**: SystemCapability.Communication.NFC.Tag 1309 1310**Atomic service API**: This API can be used in atomic services since API version 12. 1311 1312| **Name** | **Value**| **Description** | 1313| ------------ | ------ | -------------------- | 1314| TYPE_UNKNOWN | 0 | Unknown type. | 1315| TYPE_CLASSIC | 1 | MIFARE Classic.| 1316| TYPE_PLUS | 2 | MIFARE Plus. | 1317| TYPE_PRO | 3 | MIFARE Pro. | 1318 1319## MifareClassicSize<sup>9+</sup> 1320Enumerates the sizes of a MIFARE Classic tag. 1321 1322**System capability**: SystemCapability.Communication.NFC.Tag 1323 1324**Atomic service API**: This API can be used in atomic services since API version 12. 1325 1326| **Name** | **Value**| **Description** | 1327| ------------ | ------ | --------------------------------- | 1328| MC_SIZE_MINI | 320 | Each tag has 5 sectors, and each sector has 4 blocks. | 1329| MC_SIZE_1K | 1024 | Each tag has 16 sectors, and each sector has 4 blocks.| 1330| MC_SIZE_2K | 2048 | Each tag has 32 sectors, and each sector has 4 blocks.| 1331| MC_SIZE_4K | 4096 | Each tag has 40 sectors, and each sector has 4 blocks.| 1332 1333## MifareUltralightType<sup>9+</sup> 1334Enumerates the MIFARE Ultralight tag types. 1335 1336**System capability**: SystemCapability.Communication.NFC.Tag 1337 1338**Atomic service API**: This API can be used in atomic services since API version 12. 1339 1340| **Name** | **Value**| **Description** | 1341| ----------------- | ------ | ------------------------- | 1342| TYPE_UNKNOWN | 0 | Unknown type. | 1343| TYPE_ULTRALIGHT | 1 | MIFARE Ultralight. | 1344| TYPE_ULTRALIGHT_C | 2 | MIFARE Ultralight C.| 1345 1346## NfcATag<sup>7+</sup> 1347 1348type NfcATag = _NfcATag 1349 1350Defines an **NfcATag** object. 1351 1352**System capability**: SystemCapability.Communication.NFC.Tag 1353**Atomic service API**: This API can be used in atomic services since API version 12. 1354 1355| Type | Description | 1356| ------ | ------------------------------------------------------------ | 1357| [_NfcATag](./js-apis-nfctech.md#nfcatag) | Object that implements access to NFC-A (ISO 15693) properties and I/O operations on a tag. | 1358 1359## NfcBTag<sup>7+</sup> 1360 1361type NfcBTag = _NfcBTag 1362 1363Obtains an **NfcBTag** object. 1364 1365**System capability**: SystemCapability.Communication.NFC.Tag 1366**Atomic service API**: This API can be used in atomic services since API version 12. 1367 1368| Type | Description | 1369| ------ | ------------------------------------------------------------ | 1370| [_NfcBTag](./js-apis-nfctech.md#nfcbtag) | Object that implements access to NFC-B (ISO 14443-3B) properties and I/O operations on a tag. | 1371 1372## NfcFTag<sup>7+</sup> 1373 1374type NfcFTag = _NfcFTag 1375 1376Obtains an **NfcFTag** object. 1377 1378**System capability**: SystemCapability.Communication.NFC.Tag 1379**Atomic service API**: This API can be used in atomic services since API version 12. 1380 1381| Type | Description | 1382| ------ | ------------------------------------------------------------ | 1383| [_NfcFTag](./js-apis-nfctech.md#nfcftag) | Object that implements access to NFC-F (ISO 6319-4) properties and I/O operations on a tag. | 1384 1385## NfcVTag<sup>7+</sup> 1386 1387type NfcVTag = _NfcVTag 1388 1389Obtains an **NfcVTag** object. 1390 1391**System capability**: SystemCapability.Communication.NFC.Tag 1392**Atomic service API**: This API can be used in atomic services since API version 12. 1393 1394| Type | Description | 1395| ------ | ------------------------------------------------------------ | 1396| [_NfcVTag](./js-apis-nfctech.md#nfcvtag) | Object that implements access to NFC-V (ISO 15693) properties and I/O operations on a tag. | 1397 1398## IsoDepTag<sup>9+</sup> 1399 1400type IsoDepTag = _IsoDepTag 1401 1402Obtains an **IsoDepTag** object. 1403 1404**System capability**: SystemCapability.Communication.NFC.Tag 1405**Atomic service API**: This API can be used in atomic services since API version 12. 1406 1407| Type | Description | 1408| ------ | ------------------------------------------------------------ | 1409| [_IsoDepTag](./js-apis-nfctech.md#isodeptag9) | Object that implements access to ISO-DEP (ISO 14443-4) properties and I/O operations on a tag. | 1410 1411## NdefTag<sup>9+</sup> 1412 1413type NdefTag = _NdefTag 1414 1415Obtains an **NdefTag** object. 1416 1417**System capability**: SystemCapability.Communication.NFC.Tag 1418**Atomic service API**: This API can be used in atomic services since API version 12. 1419 1420| Type | Description | 1421| ------ | ------------------------------------------------------------ | 1422| [_NdefTag](./js-apis-nfctech.md#ndeftag9) | Object that implements access to NDEF tags. | 1423 1424## MifareClassicTag<sup>9+</sup> 1425 1426type MifareClassicTag = _MifareClassicTag 1427 1428Obtains a **MifareClassicTag** object. 1429 1430**System capability**: SystemCapability.Communication.NFC.Tag 1431**Atomic service API**: This API can be used in atomic services since API version 12. 1432 1433| Type | Description | 1434| ------ | ------------------------------------------------------------ | 1435| [_MifareClassicTag](./js-apis-nfctech.md#mifareclassictag9) | Object that implements access to MIFARE Classic properties and I/O operations on a tag.| 1436 1437## MifareUltralightTag<sup>9+</sup> 1438 1439type MifareUltralightTag = _MifareUltralightTag; 1440 1441Obtains a **MifareUltralightTag** object. 1442 1443**System capability**: SystemCapability.Communication.NFC.Tag 1444**Atomic service API**: This API can be used in atomic services since API version 12. 1445 1446| Type | Description | 1447| ------ | ------------------------------------------------------------ | 1448| [_MifareUltralightTag](./js-apis-nfctech.md#mifareultralighttag9) | Object that implements access to MIFARE Ultralight properties and I/O operations on a tag.| 1449 1450## NdefFormatableTag<sup>9+</sup> 1451 1452type NdefFormatableTag = _NdefFormatableTag 1453 1454Obtains a **NdefFormatableTag** object. 1455 1456**System capability**: SystemCapability.Communication.NFC.Tag 1457**Atomic service API**: This API can be used in atomic services since API version 12. 1458 1459| Type | Description | 1460| ------ | ------------------------------------------------------------ | 1461| [_NdefFormatableTag](./js-apis-nfctech.md#ndefformatabletag9) | Object that implements formatting of NDEF formattable tags. | 1462 1463## BarcodeTag<sup>18+</sup> 1464 1465type BarcodeTag = _BarcodeTag 1466 1467Obtains a **BarcodeTag** object. 1468 1469**System capability**: SystemCapability.Communication.NFC.Tag 1470**Atomic service API**: This API can be used in atomic services since API version 18. 1471 1472| Type | Description | 1473| ------ | ------------------------------------------------------------ | 1474| [_BarcodeTag](./js-apis-nfctech.md#barcodetag18) | Object that implements access to the barcode tag properties and I/O operations on a tag.| 1475 1476## NdefMessage<sup>9+</sup> 1477 1478type NdefMessage = _NdefMessage 1479 1480Obtains an **NdefMessage** object. 1481 1482**System capability**: SystemCapability.Communication.NFC.Tag 1483**Atomic service API**: This API can be used in atomic services since API version 12. 1484 1485| Type | Description | 1486| ------ | ------------------------------------------------------------ | 1487| [_NdefMessage](./js-apis-nfctech.md#ndefmessage9) | Obtains all NDEF records.| 1488 1489## TagSession<sup>7+</sup> 1490 1491type TagSession = _TagSession 1492 1493Obtains a **TagSession** object. 1494 1495**System capability**: SystemCapability.Communication.NFC.Tag 1496**Atomic service API**: This API can be used in atomic services since API version 12. 1497 1498| Type | Description | 1499| ------ | ------------------------------------------------------------ | 1500| [_TagSession](./js-apis-tagSession.md#tagsession) | Base class of all [NFC tag technologies](js-apis-nfctech.md). It provides common APIs for establishing connections and transferring data.| 1501<!--no_check--> 1502