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