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