js-apis-nfcTag.md 27.0 KB
Newer Older
A
Annie_wang 已提交
1
# @ohos.nfc.tag
A
Annie_wang 已提交
2

A
Annie_wang 已提交
3
The **nfcTag** module provides APIs for managing Near-Field Communication (NFC) tags.
A
Annie_wang 已提交
4

A
Annie_wang 已提交
5 6
> **NOTE**
>
A
Annie_wang 已提交
7
> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
A
Annie_wang 已提交
8

A
Annie_wang 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
## **Declaration**

Before developing applications related to tag read and write, you must declare NFC-related attributes in the attribute configuration file of the applications. For example, declare the following attributes in the **module.json5** file:
```js
{
    "module": {
        // Attributes to declare.

        "abilities": [
            {
                "skills": [
                    {
                        "actions": [
                            // Actions to declare.

                            // Add the nfc tag action.
                            "ohos.nfc.tag.action.TAG_FOUND"
                        ]
                    }
                ],
                "metadata": [
                    {
                        "name": "tag-tech",
                        "value": "NfcA"
                    },
                    {
                        "name": "tag-tech",
                        "value": "IsoDep"
                    },
                    // Add other technologies,
                    // such as NfcB, NfcF, NfcV, Ndef, MifareClassic, MifareUL, and NdefFormatable.
                ]
            }
        ],
        "requestPermissions": [
            "name": "ohos.permission.NFC_TAG",
            "reason": "tag",
        ]
    }
}
```
> **CAUTION**<br>
A
Annie_wang 已提交
51 52 53 54
1. The **actions** field is mandatory. It must be **ohos.nfc.tag.action.TAG_FOUND** and cannot be changed.
2. The **name** field under **metadata** is mandatory. It must be **tag-tech** and cannot be changed.
3. The **value** field under **metadata** is mandatory. It can be **NfcA**, **NfcB**, **NfcF**, **NfcV**, **IsoDep**, **Ndef**, **MifareClassic**, **MifareUL**, **NdefFormatable** or any of their combinations. Incorrect settings of this field will cause a parsing failure.
4. The **name** field under **requestPermissions** is mandatory. It must be **ohos.permission.NFC_TAG** and cannot be changed.
A
Annie_wang 已提交
55

A
Annie_wang 已提交
56 57
## **Modules to Import**

A
Annie_wang 已提交
58
```js
A
Annie_wang 已提交
59 60 61
import tag from '@ohos.nfc.tag';
```

A
Annie_wang 已提交
62
## **tag.TagInfo**
A
Annie_wang 已提交
63 64

Before a card with tags is read or written, **TagInfo** must be obtained to determine the tag technologies supported by the card. In this way, the application can invoke the correct API to communicate with the card.
A
Annie_wang 已提交
65 66 67 68 69 70
```js
import tag from '@ohos.nfc.tag';

onCreate(want, launchParam) {
    // Add other code here.

A
Annie_wang 已提交
71 72 73 74 75 76 77 78
    // want is initialized by the NFC service and contains tagInfo.
    var tagInfo;
    try {
        tagInfo = tag.getTagInfo(want);
    } catch (error) {
        console.log("tag.getTagInfo catched error: " + error);
    }
    if (tagInfo == null || tagInfo == undefined) {
A
Annie_wang 已提交
79 80 81
        console.log("no TagInfo to be created, ignore it.");
        return;
    }
A
Annie_wang 已提交
82 83

    // get the supported technologies for this found tag.
A
Annie_wang 已提交
84
    var isNfcATag =  false;
A
Annie_wang 已提交
85
    var isIsoDepTag =  false;
A
Annie_wang 已提交
86 87 88
    for (var i = 0; i < tagInfo.technology.length; i++) {
        if (tagInfo.technology[i] == tag.NFC_A) {
            isNfcATag = true;
A
Annie_wang 已提交
89 90 91 92
        }

        if (tagInfo.technology[i] == tag.ISO_DEP) {
            isIsoDepTag = true;
A
Annie_wang 已提交
93 94 95
        }
        // Also check for technology tag.NFC_B, NFC_F, NFC_V, ISO_DEP, NDEF, MIFARE_CLASSIC, MIFARE_ULTRALIGHT, and NDEF_FORMATABLE.
    }
A
Annie_wang 已提交
96 97

    // use NfcA APIs to access the found tag.
A
Annie_wang 已提交
98
    if (isNfcATag) {
A
Annie_wang 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
        var nfcA;
        try {
            nfcA = tag.getNfcATag(taginfo);
        } catch (error) {
            console.log("tag.getNfcATag catched error: " + error);
        }
        // Other code to read or write this tag.
    }

    // use getIsoDep APIs to access the found tag.
    if (isIsoDepTag) {
        var isoDep;
        try {
            isoDep = tag.getIsoDep(taginfo);
        } catch (error) {
            console.log("tag.getIsoDep catched error: " + error);
        }
A
Annie_wang 已提交
116 117 118
        // Other code to read or write this tag.
    }

A
Annie_wang 已提交
119
    // Use the same code to handle "NfcA/NfcB/NfcF/NfcV/Ndef/MifareClassic/MifareUL/NdefFormatable".
A
Annie_wang 已提交
120 121 122
}
```

A
Annie_wang 已提交
123 124
## tag.getNfcATag

A
Annie_wang 已提交
125
getNfcATag(tagInfo: [TagInfo](#taginfo)): [NfcATag](js-apis-nfctech.md#nfcatag)
A
Annie_wang 已提交
126

A
Annie_wang 已提交
127
Obtains an **NfcATag** object, which allows access to the tags that use the NFC-A technology.
A
Annie_wang 已提交
128 129 130

**Required permissions**: ohos.permission.NFC_TAG

A
Annie_wang 已提交
131
**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
132 133 134 135 136

**Return value**

| **Type**| **Description**|
| -------- | -------- |
A
Annie_wang 已提交
137
| [NfcATag](js-apis-nfctech.md#nfcatag) | **NfcATag** object obtained.|
A
Annie_wang 已提交
138 139 140

## tag.getNfcBTag

A
Annie_wang 已提交
141
getNfcBTag(tagInfo: [TagInfo](#taginfo)): [NfcBTag](js-apis-nfctech.md#nfcbtag)
A
Annie_wang 已提交
142

A
Annie_wang 已提交
143
Obtains an **NfcBTag** object, which allows access to the tags that use the NFC-B technology.
A
Annie_wang 已提交
144 145 146

**Required permissions**: ohos.permission.NFC_TAG

A
Annie_wang 已提交
147
**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
148 149 150 151 152

**Return value**

| **Type**| **Description**        |
| -------- | ---------------- |
A
Annie_wang 已提交
153
| [NfcBTag](js-apis-nfctech.md#nfcbtag)  | **NfcBTag** object obtained.|
A
Annie_wang 已提交
154 155 156

## tag.getNfcFTag

A
Annie_wang 已提交
157
getNfcFTag(tagInfo: [TagInfo](#taginfo)): [NfcFTag](js-apis-nfctech.md#nfcftag)
A
Annie_wang 已提交
158

A
Annie_wang 已提交
159
Obtains an **NfcFTag** object, which allows access to the tags that use the NFC-F technology.
A
Annie_wang 已提交
160 161 162

**Required permissions**: ohos.permission.NFC_TAG

A
Annie_wang 已提交
163
**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
164 165 166 167 168

**Return value**

| **Type**| **Description**        |
| -------- | ---------------- |
A
Annie_wang 已提交
169
| [NfcFTag](js-apis-nfctech.md#nfcftag)  | **NfcFTag** object obtained.|
A
Annie_wang 已提交
170 171 172

## tag.getNfcVTag

A
Annie_wang 已提交
173
getNfcVTag(tagInfo: [TagInfo](#taginfo)): [NfcVTag](js-apis-nfctech.md#nfcvtag)
A
Annie_wang 已提交
174

A
Annie_wang 已提交
175
Obtains an **NfcVTag** object, which allows access to the tags that use the NFC-V technology.
A
Annie_wang 已提交
176 177 178

**Required permissions**: ohos.permission.NFC_TAG

A
Annie_wang 已提交
179
**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
180 181 182 183 184

**Return value**

| **Type**| **Description**        |
| -------- | ---------------- |
A
Annie_wang 已提交
185 186
| [NfcVTag](js-apis-nfctech.md#nfcvtag)  | **NfcVTag** object obtained.|

A
Annie_wang 已提交
187
## tag.getIsoDep<sup>9+</sup>
A
Annie_wang 已提交
188

A
Annie_wang 已提交
189
getIsoDep(tagInfo: [TagInfo](#taginfo)): [IsoDepTag](js-apis-nfctech.md#isoDepTag9 )
A
Annie_wang 已提交
190 191 192 193 194

Obtains an **IsoDepTag** object, which allows access to the tags that use the ISO-DEP technology.

**System capability**: SystemCapability.Communication.NFC.Core

A
Annie_wang 已提交
195 196 197 198 199 200
**Parameters**

| Name      | Type                       | Mandatory  | Description                                      |
| --------- | ------------------------- | ---- | ---------------------------------------- |
| taginfo      | [TagInfo](#taginfo)                   | Yes| Tag information including the technology type and related parameters, which are obtained from **tag.getTagInfo(want: Want)**.                             |

A
Annie_wang 已提交
201 202 203 204 205 206
**Return value**

| **Type**| **Description**           |
| ---------- | ------------------|
| [IsoDepTag](js-apis-nfctech.md#isodeptag9)  | **IsoDepTag** object obtained.|

A
Annie_wang 已提交
207
**Error codes**
A
Annie_wang 已提交
208

A
Annie_wang 已提交
209
For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
A
Annie_wang 已提交
210

A
Annie_wang 已提交
211 212 213
| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
214

A
Annie_wang 已提交
215
## tag.getNdef<sup>9+</sup>
A
Annie_wang 已提交
216

A
Annie_wang 已提交
217 218 219
getNdef(tagInfo: [TagInfo](#taginfo)): [NdefTag](js-apis-nfctech.md#ndeftag9)

Obtains an **NdefTag** object, which allows access to the tags in the NFC Data Exchange Format (NDEF).
A
Annie_wang 已提交
220 221 222

**System capability**: SystemCapability.Communication.NFC.Core

A
Annie_wang 已提交
223 224 225 226 227 228
**Parameters**

| Name      | Type                       | Mandatory  | Description                                      |
| --------- | ------------------------- | ---- | ---------------------------------------- |
| taginfo      | [TagInfo](#taginfo)                   | Yes   | Tag information including the technology type and related parameters, which are obtained from **tag.getTagInfo(want: Want)**.                             |

A
Annie_wang 已提交
229 230 231 232 233 234
**Return value**

| **Type**| **Description**       |
| ---------| -------------- |
| [NdefTag](js-apis-nfctech.md#ndeftag9)  | **NdefTag** object obtained.|

A
Annie_wang 已提交
235
**Error codes**
A
Annie_wang 已提交
236

A
Annie_wang 已提交
237
For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
A
Annie_wang 已提交
238

A
Annie_wang 已提交
239 240 241
| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
242

A
Annie_wang 已提交
243 244 245 246 247
## tag.getMifareClassic<sup>9+</sup>

getMifareClassic(tagInfo: [TagInfo](#taginfo)): [MifareClassicTag](js-apis-nfctech.md#mifareclassictag-9)

Obtains a **MifareClassicTag** object, which allows access to the tags that use MIFARE Classic.
A
Annie_wang 已提交
248 249 250

**System capability**: SystemCapability.Communication.NFC.Core

A
Annie_wang 已提交
251 252 253 254 255 256
**Parameters**

| Name      | Type                       | Mandatory  | Description                                      |
| --------- | ------------------------- | ---- | ---------------------------------------- |
| taginfo      | [TagInfo](#taginfo)                   | Yes   | Tag information including the technology type and related parameters, which are obtained from **tag.getTagInfo(want: Want)**.                             |

A
Annie_wang 已提交
257 258 259 260 261 262
**Return value**

| **Type**| **Description**                         |
| ----------------- | ------------------------|
| [MifareClassicTag](js-apis-nfctech.md#mifareclassictag-9)  | **MifareClassicTag** object obtained.|

A
Annie_wang 已提交
263
**Error codes**
A
Annie_wang 已提交
264

A
Annie_wang 已提交
265
For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
A
Annie_wang 已提交
266

A
Annie_wang 已提交
267 268 269
| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
270

A
Annie_wang 已提交
271 272 273 274 275
## tag.getMifareUltralight<sup>9+</sup>

getMifareUltralight(tagInfo: [TagInfo](#taginfo)): [MifareUltralightTag](js-apis-nfctech.md#mifareultralighttag9)

Obtains a **MifareUltralightTag** object, which allows access to the tags that use MIFARE Ultralight.
A
Annie_wang 已提交
276 277 278

**System capability**: SystemCapability.Communication.NFC.Core

A
Annie_wang 已提交
279 280 281 282 283
**Parameters**
| Name      | Type                       | Mandatory  | Description                                      |
| --------- | ------------------------- | ---- | ---------------------------------------- |
| taginfo      | [TagInfo](#taginfo)                   | Yes   | Tag information including the technology type and related parameters, which are obtained from **tag.getTagInfo(want: Want)**.                             |

A
Annie_wang 已提交
284 285 286 287 288 289
**Return value**

| **Type**| **Description**                               |
| -------------------- | ---------------------------|
| [MifareUltralightTag](js-apis-nfctech.md#mifareultralighttag9)  | **MifareUltralightTag** object obtained.|

A
Annie_wang 已提交
290
**Error codes**
A
Annie_wang 已提交
291

A
Annie_wang 已提交
292
For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
A
Annie_wang 已提交
293

A
Annie_wang 已提交
294 295 296
| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
297

A
Annie_wang 已提交
298 299 300 301 302
## tag.getNdefFormatable<sup>9+</sup>

getNdefFormatable(tagInfo: [TagInfo](#taginfo)): [NdefFormatableTag](js-apis-nfctech.md#ndefformatabletag9)

Obtains an **NdefFormatableTag** object, which allows access to the tags that are NDEF formattable.
A
Annie_wang 已提交
303 304 305 306 307 308 309 310 311

**System capability**: SystemCapability.Communication.NFC.Core

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
| [NdefFormatableTag](js-apis-nfctech.md#ndefformatabletag)  | **NdefFormatableTag** object obtained.|

A
Annie_wang 已提交
312 313 314 315 316 317 318 319
**Error codes**

For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).

| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |

A
Annie_wang 已提交
320 321
## tag.getTagInfo<sup>9+</sup>

A
Annie_wang 已提交
322
getTagInfo(want: [Want](js-apis-app-ability-want.md#Want)): [TagInfo](#taginfo)
A
Annie_wang 已提交
323 324 325 326 327

Obtains **TagInfo** from **Want**, which is initialized by the NFC service and contains the attributes required by **TagInfo**.

**System capability**: SystemCapability.Communication.NFC.Core

A
Annie_wang 已提交
328 329 330 331 332 333
**Parameters**

| Name      | Type                       | Mandatory  | Description                                      |
| --------- | ------------------------- | ---- | ---------------------------------------- |
| want      | [Want](js-apis-app-ability-want.md#Want)                   | Yes   | Data obtained from the parameters of the **onCreate** entry function when an ability is dispatched.                             |

A
Annie_wang 已提交
334 335 336 337 338 339 340
**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
| [TagInfo](#taginfo) | **TagInfo** object obtained.|


A
Annie_wang 已提交
341
## tag.ndef.makeUriRecord<sup>9+</sup>
A
Annie_wang 已提交
342

A
Annie_wang 已提交
343 344 345
makeUriRecord(uri: string): [NdefRecord](#ndefrecord9);

Creates an NDEF record based on the specified URI.
A
Annie_wang 已提交
346 347

**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
348

A
Annie_wang 已提交
349
**Parameters**
A
Annie_wang 已提交
350

A
Annie_wang 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
| uri | string | Yes| Data to write to the NDEF record.|

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
| [NdefRecord](#ndefrecord9) | NDEF record created. For details, see *NFCForum-TS-NDEF_1.0*.|

**Example**

```js
import tag from '@ohos.nfc.tag';

try {
    let uri = "https://gitee.com/openharmony"; // change it to be correct.
    let ndefRecord = tag.ndef.makeUriRecord(uri);
    if (ndefRecord != undefined) {
        console.log("ndefMessage makeUriRecord rtdType: " + ndefRecord.rtdType);
        console.log("ndefMessage makeUriRecord payload: " + ndefRecord.payload);
    } else {
        console.log("ndefMessage makeUriRecord ndefRecord: " + ndefRecord);
    }
} catch (busiError) {
    console.log("ndefMessage makeUriRecord catched busiError: " + busiError);
}
```

## tag.ndef.makeTextRecord<sup>9+</sup>

makeTextRecord(text: string, locale: string): [NdefRecord](#ndefrecord9);

Creates an NDEF record based on the specified text data and encoding type.

**System capability**: SystemCapability.Communication.NFC.Core

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
| text | string | Yes  | Text to write to the NDEF record.|
| locale | string | Yes  | Encoding mode of the text.|

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
| [NdefRecord](#ndefrecord9) | NDEF record created. For details, see *NFCForum-TS-NDEF_1.0*.|

**Example**

```js
import tag from '@ohos.nfc.tag';

try {
    let text = "Hello World";   // change it to be correct.
    let locale = "en"; // change it to be correct.
    let ndefRecord = tag.ndef.makeTextRecord(text, locale);
    if (ndefRecord != undefined) {
        console.log("ndefMessage makeTextRecord rtdType: " + ndefRecord.rtdType);
        console.log("ndefMessage makeTextRecord payload: " + ndefRecord.payload);
    } else {
        console.log("ndefMessage makeTextRecord ndefRecord: " + ndefRecord);
    }
} catch (busiError) {
    console.log("ndefMessage makeTextRecord catched busiError: " + busiError);
}
```


## tag.ndef.makeMimeRecord<sup>9+</sup>

makeMimeRecord(mimeType: string, mimeData: number[]): [NdefRecord](#ndefrecord9);

Creates an NDEF record based on the specified MIME data and type.

**System capability**: SystemCapability.Communication.NFC.Core

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
| mimeType | string | Yes  | MIME type that complies with RFC rules, for example, **text/plain** or **image/jpeg**.|
| mimeData | number[] | Yes  | MIME data, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.|

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
| [NdefRecord](#ndefrecord9) | NDEF record created. For details, see *NFCForum-TS-NDEF_1.0*.|

**Example**

```js
import tag from '@ohos.nfc.tag';

try {
    let mimeType = "text/plain";   // change it to be correct.
    let mimeData = [0x01, 0x02, 0x03, 0x04]; // change it to be correct.
    let ndefRecord = tag.ndef.makeMimeRecord(mimeType, mimeData);
    if (ndefRecord != undefined) {
        console.log("ndefMessage makeMimeRecord rtdType: " + ndefRecord.rtdType);
        console.log("ndefMessage makeMimeRecord payload: " + ndefRecord.payload);
    } else {
        console.log("ndefMessage makeMimeRecord ndefRecord: " + ndefRecord);
    }
} catch (busiError) {
    console.log("ndefMessage makeMimeRecord catched busiError: " + busiError);
}
```
## tag.ndef.makeExternalRecord<sup>9+</sup>

makeExternalRecord(domainName: string, type: string, externalData: number[]): [NdefRecord](#ndefrecord9);

Creates an NDEF record based on application-specific data.

**System capability**: SystemCapability.Communication.NFC.Core

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
| domainName | string | Yes  | Bundle name of the application or domain name of the organization that releases the applications.|
| type | string | Yes  | Type of the application data.|
| externalData | number[] | Yes  | Application data, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.|

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
| [NdefRecord](#ndefrecord9) | NDEF record created. For details, see *NFCForum-TS-NDEF_1.0*.|

**Example**

```js
import tag from '@ohos.nfc.tag';

try {
    let domainName = "ohos.nfc.application"; // change it to be correct.
    let type = "test"; // change it to be correct.
    let externalData = [0x01, 0x02, 0x03, 0x04]; // change it to be correct.
    let ndefRecord = tag.ndef.makeExternalRecord(domainName, type, externalData);
    if (ndefRecord != undefined) {
        console.log("ndefMessage makeExternalRecord rtdType: " + ndefRecord.rtdType);
        console.log("ndefMessage makeExternalRecord payload: " + ndefRecord.payload);
    } else {
        console.log("ndefMessage makeExternalRecord ndefRecord: " + ndefRecord);
    }
} catch (busiError) {
    console.log("ndefMessage makeExternalRecord catched busiError: " + busiError);
}
```

## tag.ndef.messageToBytes<sup>9+</sup>

messageToBytes(ndefMessage: [NdefMessage](js-apis-nfctech.md#ndefmessage9)): number[];

Converts an NDEF message to bytes.

**System capability**: SystemCapability.Communication.NFC.Core

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
| ndefMessage | [NdefMessage](js-apis-nfctech.md#ndefmessage9) | Yes  | NDEF message to convert.|

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
| number[] | NDEF message in bytes, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.|

**Example**

```js
import tag from '@ohos.nfc.tag';

let rawData = [0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]; // MUST can be parsed as NDEF Record.
let ndefMessage;
try {
    ndefMessage = tag.ndef.createNdefMessage(rawData);
    console.log("ndef createNdefMessage, ndefMessage: " + ndefMessage);
} catch (busiError) {
    console.log("ndef createNdefMessage busiError: " + busiError);
}

try {
    let rawData2 = tag.ndef.messageToBytes(ndefMessage);
    console.log("ndefMessage messageToBytes rawData2: " + rawData2);
} catch (busiError) {
    console.log("ndefMessage messageToBytes catched busiError: " + busiError);
}
```
## tag.ndef.createNdefMessage<sup>9+</sup>

createNdefMessage(data: number[]): [NdefMessage](js-apis-nfctech.md#ndefmessage9)

Creates 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.

**System capability**: SystemCapability.Communication.NFC.Core

**Parameters**

| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| 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.|

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
| [NdefMessage](js-apis-nfctech.md#ndefmessage9) | NDEF message created. For details, see *NFCForum-TS-NDEF_1.0*.|

**Example**
```js
import tag from '@ohos.nfc.tag';

let rawData = [0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43];  // MUST can be parsed as NDEF Record.
let ndefMessage;
try {
    ndefMessage = tag.ndef.createNdefMessage(rawData);
    console.log("ndef createNdefMessage, ndefMessage: " + ndefMessage);
} catch (busiError) {
    console.log("ndef createNdefMessage busiError: " + busiError);
}
```

## tag.ndef.createNdefMessage<sup>9+</sup>

createNdefMessage(ndefRecords: NdefRecord[]): [NdefMessage](js-apis-nfctech.md#ndefmessage9)

Creates an NDEF message from the NDEF records list.

**System capability**: SystemCapability.Communication.NFC.Core

**Parameters**

| **Name**| **Type**| **Mandatory**| **Description**|
| -------- | -------- | -------- | -------- |
| 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*.|

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
| [NdefMessage](js-apis-nfctech.md#ndefmessage9) | NDEF message created. For details, see *NFCForum-TS-NDEF_1.0*.|

**Example**

```js
import tag from '@ohos.nfc.tag';

let uriRecord = tag.ndef.makeUriRecord("https://gitee.com/openharmony");
let textRecord = tag.ndef.makeTextRecord("Hello World", "en");
let ndefRecords = [uriRecord, textRecord];
let ndefMessage;
try {
    ndefMessage = tag.ndef.createNdefMessage(ndefRecords);
    console.log("ndef createNdefMessage ndefMessage: " + ndefMessage);
} catch (busiError) {
    console.log("ndef createNdefMessage busiError: " + busiError);
}
```

## TagInfo

Defines the **TagInfo** object, which provides information about the tag technologies supported by a card.

**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
622 623 624

**Required permissions**: ohos.permission.NFC_TAG

A
Annie_wang 已提交
625 626 627 628 629 630 631 632 633 634 635
| **Name**| **Type**| **Readable**| **Writable**| **Description**|
| -------- | -------- | -------- | -------- | -------- |
| uid<sup>9+</sup> | number[] | Yes| No| Tag unique identifier (UID), which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.|
| technology<sup>9+</sup> | number[] | Yes| No| Supported technologies. Each number is a constant indicating the supported technology.|
| supportedProfiles | number[] | Yes| No|  Supported profiles. This parameter is not supported since API version 9. Use [tag.TagInfo#technology](#taginfo) instead.|
| extrasData<sup>9+</sup> | [PacMap](js-apis-inner-ability-dataAbilityHelper.md#pacmap)[] | Yes| No|  Extended attribute value of the tag technology.<br>**System API**: This is a system API.|
| tagRfDiscId<sup>9+</sup> | number | Yes| No|  ID allocated when the tag is discovered.<br>**System API**: This is a system API.|
| remoteTagService<sup>9+</sup> | [rpc.RemoteObject](js-apis-rpc.md#remoteobject) | Yes| No| Remote object of the NFC service process used for interface communication between the client and the service.<br>**System API**: This is a system API.|
## NdefRecord<sup>9+</sup>
Defines an NDEF record. For details, see *NFCForum-TS-NDEF_1.0*.

A
Annie_wang 已提交
636
**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
637 638 639 640 641 642 643

| **Name**| **Type**| **Readable**| **Writable**| **Description**|
| -------- | -------- | -------- | -------- | -------- |
| tnf | number | Yes| No| Type name field (TNF) of the NDEF record.|
| rtdType| number[] | Yes| No| Record type definition (RTD) of the NDEF record. It consists of hexadecimal numbers ranging from **0x00** to **0xFF**.|
| id | number[] | Yes| No| NDEF record ID, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.|
| payload | number[] | Yes| No| NDEF payload, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.|
A
Annie_wang 已提交
644 645 646 647 648

## Technology Type Definition
Enumerates the tag technology types.

**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
649

A
Annie_wang 已提交
650 651 652
| **Name**| **Value**|  **Description**|
| -------- | -------- | -------- |
| NFC_A | 1 | NFC-A (ISO 14443-3A).|
A
Annie_wang 已提交
653
| NFC_B  | 2 | NFC-B (ISO 14443-3B).|
A
Annie_wang 已提交
654 655 656 657
| ISO_DEP | 3 | ISO-DEP (ISO 14443-4).|
| NFC_F  | 4 | NFC-F (JIS 6319-4).|
| NFC_V | 5 | NFC-V (ISO 15693).|
| NDEF  | 6 | NDEF.|
A
Annie_wang 已提交
658
| NDEF_FORMATABLE<sup>9+</sup> | 7 | NDEF formattable.|
A
Annie_wang 已提交
659 660 661 662 663 664 665
| MIFARE_CLASSIC | 8 | MIFARE Classic.|
| MIFARE_ULTRALIGHT | 9 | MIFARE Ultralight.|

## TnfType<sup>9+</sup>
Enumerates the TNF types. For details, see *NFCForum-TS-NDEF_1.0*.

**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
666

A
Annie_wang 已提交
667 668 669
| **Name**| **Value**|  **Description**|
| -------- | -------- | -------- |
| TNF_EMPTY | 0x0 | Empty.|
A
Annie_wang 已提交
670 671 672 673 674 675
| TNF_WELL_KNOWN  | 0x1 | NFC Forum Well Known Type [NFC RTD].|
| TNF_MEDIA | 0x2 | Media-type as defined in RFC 2046 [RFC 2046].|
| TNF_ABSOLUTE_URI  | 0x3 | Absolute URI as defined in RFC 3986 [RFC 3986].|
| TNF_EXT_APP | 0x4 | NFC Forum external type [NFC RTD].|
| TNF_UNKNOWN  | 0x5 | Unknown.|
| TNF_UNCHANGED | 0x6 | Unchanged (see section 2.3.3 in *NFCForum-TS-NDEF_1.0*).|
A
Annie_wang 已提交
676 677 678 679 680

## NDEF Record RTD
Enumerates the NDEF record types. For details about the RTD, see *NFCForum-TS-NDEF_1.0*.

**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
681

A
Annie_wang 已提交
682 683 684 685 686 687 688 689 690
| **Name**| **Value**|  **Description**|
| -------- | -------- | -------- |
| RTD_TEXT<sup>9+</sup> | [0x54] | NDEF record of the text type.|
| RTD_URI<sup>9+</sup>  | [0x55] | NDEF record of the URI type.|

## NfcForumType<sup>9+</sup>
Enumerates the NFC Forum tag types.

**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
691

A
Annie_wang 已提交
692 693 694 695 696 697
| **Name**| **Value**|  **Description**|
| -------- | -------- | -------- |
| NFC_FORUM_TYPE_1 | 1 |  NFC Forum tag type 1.|
| NFC_FORUM_TYPE_2 | 2 |  NFC Forum tag type 2.|
| NFC_FORUM_TYPE_3  | 3 |  NFC Forum tag type 3.|
| NFC_FORUM_TYPE_4  | 4 |  NFC Forum tag type 4.|
A
Annie_wang 已提交
698
| MIFARE_CLASSIC  | 101 |   MIFARE Classic.|
A
Annie_wang 已提交
699 700 701 702 703

## MifareClassicType<sup>9+</sup>
Enumerates the MIFARE Classic tag types.

**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
704

A
Annie_wang 已提交
705 706
| **Name**| **Value**|  **Description**|
| -------- | -------- | -------- |
A
Annie_wang 已提交
707 708 709 710
| TYPE_UNKNOWN | 0 |  Unknown type.|
| TYPE_CLASSIC | 1 |  MIFARE Classic.|
| TYPE_PLUS   | 2 |  MIFARE Plus.|
| TYPE_PRO  | 3 |  MIFARE Pro.|
A
Annie_wang 已提交
711 712

## MifareClassicSize<sup>9+</sup>
A
Annie_wang 已提交
713
Enumerates the sizes of MIFARE Classic tags.
A
Annie_wang 已提交
714 715

**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
716

A
Annie_wang 已提交
717 718 719 720 721 722 723
| **Name**| **Value**|  **Description**|
| -------- | -------- | -------- |
| MC_SIZE_MINI | 320 |  Each tag has 5 sectors, and each sector has 4 blocks.|
| MC_SIZE_1K  | 1024 |  Each tag has 16 sectors, and each sector has 4 blocks.|
| MC_SIZE_2K   | 2048 |  Each tag has 32 sectors, and each sector has 4 blocks.|
| MC_SIZE_4K   | 4096 |  Each tag has 40 sectors, and each sector has 4 blocks.|

A
Annie_wang 已提交
724
## MifareUltralightType<sup>9+</sup>
A
Annie_wang 已提交
725 726 727
Enumerates the MIFARE Ultralight tag types.

**System capability**: SystemCapability.Communication.NFC.Core
A
Annie_wang 已提交
728

A
Annie_wang 已提交
729 730
| **Name**| **Value**|  **Description**|
| -------- | -------- | -------- |
A
Annie_wang 已提交
731
| TYPE_UNKNOWN  | 0 |  Unknown type.|
A
Annie_wang 已提交
732 733 734
| TYPE_ULTRALIGHT   | 1 |  MIFARE Ultralight.|
| TYPE_ULTRALIGHT_C    | 2 |  MIFARE Ultralight C.|
<!--no_check-->