• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# nfctech (Standard NFC Technologies)
2
3The **nfctech** module provides APIs for reading and writing tags that use different Near-Field Communication (NFC) technologies.
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## Modules to Import
10
11```js
12import tag from '@ohos.nfc.tag';
13```
14
15## NfcATag
16
17Provides APIs to access NFC-A (ISO 14443-3A) properties and perform I/O operations on a tag. This class inherits from **[TagSession](js-apis-tagSession.md)**.
18
19**TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md).
20
21The following describes the unique APIs of **NfcATag**.
22
23### NfcATag.getSak
24
25getSak(): number
26
27Obtains the SAK value of this NFC-A tag.
28
29**System capability**: SystemCapability.Communication.NFC.Tag
30
31**Return value**
32
33| **Type**| **Description**                            |
34| ------------------ | --------------------------|
35| number  | SAK value obtained. The SAK is a hexadecimal number ranging from **0x00** to **0xFF**.|
36
37**Example**
38
39```js
40import tag from '@ohos.nfc.tag';
41
42// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcA' correctly.
43let sak = nfcA.getSak();
44console.log("nfcA sak: " + sak);
45```
46
47### NfcATag.getAtqa
48
49getAtqa(): number[]
50
51Obtains the ATQA value of this NFC-A tag.
52
53**System capability**: SystemCapability.Communication.NFC.Tag
54
55**Return value**
56
57| **Type**| **Description**                            |
58| ------------------ | --------------------------|
59| number[]  | ATQA value obtained. Each number of the ATQA is a hexadecimal number ranging from **0x00** to **0xFF**.|
60
61**Example**
62
63```js
64import tag from '@ohos.nfc.tag';
65
66// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcA' correctly.
67let atqa = nfcA.getAtqa();
68console.log("nfcA atqa: " + atqa);
69```
70
71## NfcBTag
72
73Provides APIs to access NFC-B (ISO 14443-3B) properties and perform I/O operations on a tag. This class inherits from **TagSession**.
74
75**TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md).
76
77The following describes the unique APIs of **NfcBTag**.
78
79### NfcBTag.getRespAppData
80
81getRespAppData(): number[]
82
83Obtains the application data of this NFC-B tag.
84
85**System capability**: SystemCapability.Communication.NFC.Tag
86
87**Return value**
88
89| **Type**| **Description**                            |
90| ------------------ | --------------------------|
91| number[]  | Application data obtained, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.|
92
93**Example**
94
95```js
96import tag from '@ohos.nfc.tag';
97
98// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcB' correctly.
99let respAppData = nfcB.getRespAppData();
100console.log("nfcB respAppData: " + respAppData);
101```
102
103### NfcBTag.getRespProtocol
104
105getRespProtocol(): number[]
106
107Obtains the protocol information of this NFC-B tag.
108
109**System capability**: SystemCapability.Communication.NFC.Tag
110
111**Return value**
112
113| **Type**| **Description**                            |
114| ------------------ | --------------------------|
115| number[]  | Protocol information obtained, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.|
116
117**Example**
118
119```js
120import tag from '@ohos.nfc.tag';
121
122// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcB' correctly.
123let respProtocol = nfcB.getRespProtocol();
124console.log("nfcB respProtocol: " + respProtocol);
125```
126
127## NfcFTag
128
129Provides APIs to access NFC-F (JIS 6319-4) properties and perform I/O operations on a tag. This class inherits from **TagSession**.
130
131**TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md).
132
133The following describes the unique APIs of **NfcFTag**.
134
135### NfcFTag.getSystemCode
136
137getSystemCode(): number[]
138
139Obtains the system code from this NFC-F tag.
140
141**System capability**: SystemCapability.Communication.NFC.Tag
142
143**Return value**
144
145| **Type**| **Description**                           |
146| ------------------ | --------------------------|
147| number[]  | System code obtained, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.|
148
149**Example**
150
151```js
152import tag from '@ohos.nfc.tag';
153
154// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcF' correctly.
155let systemCode = nfcF.getSystemCode();
156console.log("nfcF systemCode: " + systemCode);
157```
158
159### NfcFTag.getPmm
160
161getPmm(): number[]
162
163Obtains the PMm (consisting of the IC code and manufacturer parameters) information from this NFC-F tag.
164
165**System capability**: SystemCapability.Communication.NFC.Tag
166
167**Return value**
168
169| **Type**| **Description**                            |
170| ------------------ | --------------------------|
171| number[]  | PMm information obtained, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.|
172
173**Example**
174
175```js
176import tag from '@ohos.nfc.tag';
177
178// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcF' correctly.
179let pmm = nfcF.getPmm();
180console.log("nfcF pmm: " + pmm);
181```
182
183## NfcVTag
184
185Provides APIs to access NFC-V (ISO 15693) properties and perform I/O operations on a tag. This class inherits from **TagSession**.
186
187**TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md).
188
189The following describes the unique APIs of **NfcVTag**.
190
191### NfcvTag.getResponseFlags
192
193getResponseFlags(): number
194
195Obtains the response flags from this NFC-V tag.
196
197**System capability**: SystemCapability.Communication.NFC.Tag
198
199**Return value**
200
201| **Type**| **Description**                            |
202| ------------------ | --------------------------|
203| number | Response flags obtained, which consist of hexadecimal numbers ranging from **0x00** to **0xFF**.|
204
205**Example**
206
207```js
208import tag from '@ohos.nfc.tag';
209
210// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcV' correctly.
211let responseFlags = nfcV.getResponseFlags();
212console.log("nfcV responseFlags: " + responseFlags);
213```
214
215### NfcvTag.getDsfId
216
217getDsfId(): number
218
219Obtains the data storage format identifier (DSFID) from this NFC-V tag.
220
221**System capability**: SystemCapability.Communication.NFC.Tag
222
223**Return value**
224
225| **Type**| **Description**                            |
226| ------------------ | --------------------------|
227| number | DSFID obtained, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.|
228
229**Example**
230
231```js
232import tag from '@ohos.nfc.tag';
233
234// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcV' correctly.
235let dsfId = nfcV.getDsfId();
236console.log("nfcV dsfId: " + dsfId);
237```
238
239## IsoDepTag<sup>9+</sup>
240
241Provides APIs to access ISO-DEP (ISO 14443-4) properties and I/O operations on a tag. This class inherits from **TagSession**.
242
243**TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md).
244
245The following describes the unique APIs of **IsoDepTag**.
246
247### IsoDepTag.getHistoricalBytes<sup>9+</sup>
248
249getHistoricalBytes(): number[]
250
251Obtains the historical bytes for the given tag. This API applies only to the IsoDep cards that use the NFC-A technology.
252
253**System capability**: SystemCapability.Communication.NFC.Tag
254
255**Return value**
256
257| **Type**| **Description**                            |
258| ------------------ | --------------------------|
259| number[] | Historical bytes obtained, which consist of hexadecimal numbers ranging from **0x00** to **0xFF**. If the IsoDep tag uses the NFC-B technology, **null** will be returned.|
260
261**Example**
262
263```js
264import tag from '@ohos.nfc.tag';
265
266// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'isoDep' correctly.
267let historicalBytes = isoDep.getHistoricalBytes();
268console.log("isoDep historicalBytes: " + historicalBytes);
269```
270
271### IsoDepTag.getHiLayerResponse<sup>9+</sup>
272
273getHiLayerResponse(): number[]
274
275Obtains the higher-layer response bytes for the given tag. This API applies only to the IsoDep cards that use the NFC-B technology.
276
277**System capability**: SystemCapability.Communication.NFC.Tag
278
279**Return value**
280
281| **Type**| **Description**                            |
282| ------------------ | --------------------------|
283| number[] | Higher-layer response bytes obtained, which consist of hexadecimal numbers ranging from **0x00** to **0xFF**. If the IsoDep tag uses the NFC-A technology, **null** will be returned.|
284
285**Example**
286
287```js
288import tag from '@ohos.nfc.tag';
289
290// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'isoDep' correctly.
291let hiLayerResponse = isoDep.getHiLayerResponse();
292console.log("isoDep hiLayerResponse: " + hiLayerResponse);
293```
294
295### IsoDepTag.isExtendedApduSupported<sup>9+</sup>
296
297isExtendedApduSupported(): Promise&lt;boolean&gt;
298
299Checks whether an extended application protocol data unit (APDU) is supported. This API uses a promise to return the result.
300
301**Required permissions**: ohos.permission.NFC_TAG
302
303**System capability**: SystemCapability.Communication.NFC.Tag
304
305**Return value**
306
307| **Type**| **Description**                            |
308| ------------------ | --------------------------|
309| Promise&lt;boolean&gt; | Promise used to return the result. If the extended APDU is supported, **true** is returned; otherwise, **false** is returned.|
310
311**Error codes**
312
313For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
314
315| ID| Error Message|
316| ------- | -------|
317| 3100201 | Tag running state is abnormal in service. |
318
319**Example**
320
321```js
322import tag from '@ohos.nfc.tag';
323
324// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'isoDep' correctly.
325
326// Connect to the tag if it is not connected.
327if (!isoDep.isTagConnected()) {
328    if (!isoDep.connectTag()) {
329        console.log("isoDep connectTag failed.");
330        return;
331    }
332}
333
334try {
335    isoDep.isExtendedApduSupported().then((response) => {
336        console.log("isoDep isExtendedApduSupported Promise response: " + response);
337    }).catch((err)=> {
338        console.log("isoDep isExtendedApduSupported Promise err: " + err);
339    });
340} catch (busiError) {
341    console.log("isoDep isExtendedApduSupported Promise busiError: " + busiError);
342}
343
344```
345
346### IsoDepTag.isExtendedApduSupported<sup>9+</sup>
347
348isExtendedApduSupported(callback: AsyncCallback\<boolean>): void
349
350Checks whether an extended APDU is supported. This API uses an asynchronous callback to return the result.
351
352**Required permissions**: ohos.permission.NFC_TAG
353
354**System capability**: SystemCapability.Communication.NFC.Tag
355
356**Parameters**
357
358| Name  | Type                   | Mandatory| Description                                  |
359| -------- | ----------------------- | ---- | -------------------------------------- |
360| callback | AsyncCallback\<boolean> | Yes  | Callback invoked to return the result. If the extended APDU is supported, **true** is returned; otherwise, **false** is returned.|
361
362**Error codes**
363
364For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
365
366| ID| Error Message|
367| ------- | -------|
368| 3100201 | Tag running state is abnormal in service. |
369
370**Example**
371
372```js
373import tag from '@ohos.nfc.tag';
374
375// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'isoDep' correctly.
376
377// Connect to the tag if it is not connected.
378if (!isoDep.isTagConnected()) {
379    if (!isoDep.connectTag()) {
380        console.log("isoDep connectTag failed.");
381        return;
382    }
383}
384
385try {
386    isoDep.isExtendedApduSupported((err, response)=> {
387        if (err) {
388            console.log("isoDep isExtendedApduSupported AsyncCallback err: " + err);
389        } else {
390            console.log("isoDep isExtendedApduSupported AsyncCallback response: " + response);
391        }
392    });
393} catch (busiError) {
394    console.log("isoDep isExtendedApduSupported AsyncCallback busiError: " + busiError);
395}
396```
397
398## NdefMessage<sup>9+</sup>
399
400### NdefMessage.getNdefRecords<sup>9+</sup>
401
402getNdefRecords(): [tag.NdefRecord](js-apis-nfcTag.md#ndefrecord9)[]
403
404Obtains all NDEF records.
405
406**System capability**: SystemCapability.Communication.NFC.Tag
407
408**Return value**
409
410| **Type**| **Description**                            |
411| ------------------ | --------------------------|
412| [tag.NdefRecord](js-apis-nfcTag.md#ndefrecord9)[] | List of NDEF records obtained. For details, see *NFCForum-TS-NDEF_1.0*.|
413
414**Example**
415
416```js
417import tag from '@ohos.nfc.tag';
418
419// Obtain ndefMessage from tag.ndef.createNdefMessage or ndefTag.getNdefMessage.
420// var ndefMessage = tag.ndef.createNdefMessage(...);
421// var ndefMessage = ndefTag.getNdefMessage();
422
423let ndefRecords = ndefMessage.getNdefRecords();
424console.log("ndef ndefRecords number: " + ndefRecords.length);
425```
426
427## NdefTag<sup>9+</sup>
428
429Provides APIs to access the tags in the NFC Data Exchange Format (NDEF). This class inherits from **TagSession**.
430
431**TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md).
432
433The following describes the unique APIs of **NdefTag**.
434
435### NdefTag.getNdefTagType<sup>9+</sup>
436
437getNdefTagType(): [tag.NfcForumType](js-apis-nfcTag.md#nfcforumtype9)
438
439Obtains the NDEF tag type.
440
441**System capability**: SystemCapability.Communication.NFC.Tag
442
443**Return value**
444
445| **Type**| **Description**                            |
446| ------------------ | --------------------------|
447| [tag.NfcForumType](js-apis-nfcTag.md#nfcforumtype9) | NDEF tag type obtained. It can be NFC FORUM TYPE 1, 2, 3, or 4.|
448
449**Example**
450
451```js
452import tag from '@ohos.nfc.tag';
453
454// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly.
455let ndefTagType = ndefTag.getNdefTagType();
456console.log("ndef ndefTagType: " + ndefTagType);
457```
458
459### NdefTag.getNdefMessage<sup>9+</sup>
460
461getNdefMessage(): [NdefMessage](#ndefmessage9)
462
463Obtains the NDEF message from this NDEF tag.
464
465**System capability**: SystemCapability.Communication.NFC.Tag
466
467**Return value**
468
469| **Type**| **Description**                            |
470| ------------------ | --------------------------|
471| [NdefMessage](#ndefmessage9)  | NDEF message created. For details, see *NFCForum-TS-NDEF_1.0*.|
472
473**Example**
474```js
475import tag from '@ohos.nfc.tag';
476
477// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly.
478let ndefMessage = ndefTag.getNdefMessage();
479console.log("ndef ndefMessage: " + ndefMessage);
480```
481
482### NdefTag.isNdefWritable<sup>9+</sup>
483
484isNdefWritable(): boolean;
485
486Check whether this NDEF tag is writable. Before calling the data write API, check whether the write operation is supported.
487
488**System capability**: SystemCapability.Communication.NFC.Tag
489
490**Return value**
491
492| **Type**| **Description**                            |
493| ------------------ | --------------------------|
494| boolean | Promise used to return the result. If the tag is writable, **true** is returned; otherwise, **false** is returned.|
495
496**Example**
497
498```js
499import tag from '@ohos.nfc.tag';
500
501// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly.
502var isWritable = ndefTag.isNdefWritable();
503console.log("ndef isNdefWritable: " + isWritable);
504```
505
506### NdefTag.readNdef<sup>9+</sup>
507
508readNdef(): Promise\<[NdefMessage](#ndefmessage9)>
509
510Reads the NDEF message from this tag. This API uses a promise to return the result.
511
512**Required permissions**: ohos.permission.NFC_TAG
513
514**System capability**: SystemCapability.Communication.NFC.Tag
515
516**Return value**
517
518| **Type**| **Description**                            |
519| ------------------ | --------------------------|
520| Promise\<[NdefMessage](#ndefmessage9)> | Promise used to return the message read.|
521
522**Error codes**
523
524For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
525
526| ID| Error Message|
527| ------- | -------|
528| 3100201 | Tag running state is abnormal in service. |
529
530**Example**
531
532```js
533import tag from '@ohos.nfc.tag';
534
535// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly.
536
537// Connect to the tag if it is not connected.
538if (!ndefTag.isTagConnected()) {
539    if (!ndefTag.connectTag()) {
540        console.log("ndefTag connectTag failed.");
541        return;
542    }
543}
544
545try {
546    ndefTag.readNdef().then((ndefmessage) => {
547        console.log("ndef readNdef Promise ndefmessage: " + ndefmessage);
548    }).catch((err)=> {
549        console.log("ndef readNdef Promise err: " + err);
550    });
551} catch (busiError) {
552    console.log("ndef readNdef Promise catched busiError: " + busiError);
553}
554```
555
556### NdefTag.readNdef<sup>9+</sup>
557
558readNdef(callback: AsyncCallback\<[NdefMessage](#ndefmessage9)>): void
559
560Reads the NDEF message from this tag. This API uses an asynchronous callback to return the result.
561
562**Required permissions**: ohos.permission.NFC_TAG
563
564**System capability**: SystemCapability.Communication.NFC.Tag
565
566**Parameters**
567
568| Name  | Type                   | Mandatory| Description                                  |
569| -------- | ----------------------- | ---- | -------------------------------------- |
570| callback | AsyncCallback\<[NdefMessage](#ndefmessage9)> | Yes  | Callback invoked to return the NDEF message read.|
571
572**Error codes**
573
574For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
575
576| ID| Error Message|
577| ------- | -------|
578| 3100201 | Tag running state is abnormal in service. |
579
580**Example**
581
582```js
583import tag from '@ohos.nfc.tag';
584
585// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly.
586
587// Connect to the tag if it is not connected.
588if (!ndefTag.isTagConnected()) {
589    if (!ndefTag.connectTag()) {
590        console.log("ndefTag connectTag failed.");
591        return;
592    }
593}
594
595try {
596    ndefTag.readNdef((err, ndefmessage)=> {
597        if (err) {
598            console.log("ndef readNdef AsyncCallback err: " + err);
599        } else {
600            console.log("ndef readNdef AsyncCallback ndefmessage: " + ndefmessage);
601        }
602    });
603} catch (busiError) {
604    console.log("ndef readNdef AsyncCallback catched busiError: " + busiError);
605}
606```
607
608### NdefTag.writeNdef<sup>9+</sup>
609
610writeNdef(msg: NdefMessage): Promise\<void>;
611
612Writes an NDEF message to this tag. This API uses a promise to return the result.
613
614**Required permissions**: ohos.permission.NFC_TAG
615
616**System capability**: SystemCapability.Communication.NFC.Tag
617
618**Parameters**
619
620| Name  | Type                   | Mandatory| Description                                  |
621| -------- | ----------------------- | ---- | -------------------------------------- |
622| msg | NdefMessage | Yes  | NDEF message to write.|
623
624**Error codes**
625
626For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
627
628| ID| Error Message|
629| ------- | -------|
630| 3100201 | Tag running state is abnormal in service. |
631
632**Example**
633
634```js
635import tag from '@ohos.nfc.tag';
636
637// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly.
638// NDEF message created from raw data, such as:
639let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]);  // It must be parsed as NDEF Record.
640// or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[])
641
642// Connect to the tag if it is not connected.
643if (!ndefTag.isTagConnected()) {
644    if (!ndefTag.connectTag()) {
645        console.log("ndefTag connectTag failed.");
646        return;
647    }
648}
649
650try {
651    ndefTag.writeNdef(ndefMessage).then(() => {
652        console.log("ndef writeNdef Promise success.");
653    }).catch((err)=> {
654        console.log("ndef writeNdef err: " + err);
655    });
656} catch (busiError) {
657    console.log("ndef writeNdef Promise catch busiError: " + busiError);
658}
659```
660
661### NdefTag.writeNdef<sup>9+</sup>
662
663writeNdef(msg: [NdefMessage](#ndefmessage9), callback: AsyncCallback\<void>): void
664
665Writes an NDEF message to this tag. This API uses an asynchronous callback to return the result.
666
667**Required permissions**: ohos.permission.NFC_TAG
668
669**System capability**: SystemCapability.Communication.NFC.Tag
670
671**Parameters**
672
673| Name  | Type                   | Mandatory| Description                                  |
674| -------- | ----------------------- | ---- | -------------------------------------- |
675| msg | [NdefMessage](#ndefmessage9) | Yes  | NDEF message to write.|
676| callback | AsyncCallback\<void> | Yes  | Callback invoked to return the result.|
677
678**Error codes**
679
680For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
681
682| ID| Error Message|
683| ------- | -------|
684| 3100201 | Tag running state is abnormal in service. |
685
686**Example**
687
688```js
689import tag from '@ohos.nfc.tag';
690
691// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly.
692// NDEF message created from raw data, such as:
693let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]);  // It must be parsed as NDEF Record.
694// or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[])
695
696// Connect to the tag if it is not connected.
697if (!ndefTag.isTagConnected()) {
698    if (!ndefTag.connectTag()) {
699        console.log("ndefTag connectTag failed.");
700        return;
701    }
702}
703
704try {
705    ndefTag.writeNdef(ndefMessage, (err)=> {
706        if (err) {
707            console.log("ndef writeNdef AsyncCallback err: " + err);
708        } else {
709            console.log("ndef writeNdef AsyncCallback success.");
710        }
711    });
712} catch (busiError) {
713    console.log("ndef writeNdef AsyncCallback catch busiError: " + busiError);
714}
715```
716
717### NdefTag.canSetReadOnly<sup>9+</sup>
718
719canSetReadOnly(): boolean
720
721Checks whether this NDEF tag can be set to read-only.
722
723**Required permissions**: ohos.permission.NFC_TAG
724
725**System capability**: SystemCapability.Communication.NFC.Tag
726
727**Return value**
728
729| **Type**| **Description**                            |
730| ------------------ | --------------------------|
731| boolean| Returns **true** if the tag can be set to read-only; returns **false** otherwise.|
732
733**Error codes**
734
735For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
736
737| ID| Error Message|
738| ------- | -------|
739| 3100201 | Tag running state is abnormal in service. |
740
741**Example**
742
743```js
744import tag from '@ohos.nfc.tag';
745
746// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly.
747var canSetReadOnly = ndefTag.canSetReadOnly();
748console.log("ndef canSetReadOnly: " + canSetReadOnly);
749```
750
751### NdefTag.setReadOnly<sup>9+</sup>
752
753setReadOnly(): Promise\<void>
754
755Sets this NDEF tag to read-only. This API uses a promise to return the result.
756
757**Required permissions**: ohos.permission.NFC_TAG
758
759**System capability**: SystemCapability.Communication.NFC.Tag
760
761**Error codes**
762
763For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
764
765| ID| Error Message|
766| ------- | -------|
767| 3100201 | Tag running state is abnormal in service. |
768
769**Example**
770
771```js
772import tag from '@ohos.nfc.tag';
773
774// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly.
775
776// Connect to the tag if it is not connected.
777if (!ndefTag.isTagConnected()) {
778    if (!ndefTag.connectTag()) {
779        console.log("ndefTag connectTag failed.");
780        return;
781    }
782}
783
784try {
785    ndefTag.setReadOnly().then(() => {
786        console.log("ndef setReadOnly Promise success.");
787    }).catch((err)=> {
788        console.log("ndef setReadOnly Promise err: " + err);
789    });
790} catch (busiError) {
791    console.log("ndef setReadOnly Promise catch busiError: " + busiError);
792}
793```
794
795### NdefTag.setReadOnly<sup>9+</sup>
796
797setReadOnly(callback: AsyncCallback\<void>): void
798
799Sets this NDEF tag to read-only. This API uses an asynchronous callback to return the result.
800
801**Required permissions**: ohos.permission.NFC_TAG
802
803**System capability**: SystemCapability.Communication.NFC.Tag
804
805**Parameters**
806
807| Name  | Type                   | Mandatory| Description                                  |
808| -------- | ----------------------- | ---- | -------------------------------------- |
809| callback | AsyncCallback\<void> | Yes  | Callback invoked to return the result.|
810
811**Error codes**
812
813For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
814
815| ID| Error Message|
816| ------- | -------|
817| 3100201 | Tag running state is abnormal in service. |
818
819**Example**
820
821```js
822import tag from '@ohos.nfc.tag';
823
824// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly.
825
826// Connect to the tag if it is not connected.
827if (!ndefTag.isTagConnected()) {
828    if (!ndefTag.connectTag()) {
829        console.log("ndefTag connectTag failed.");
830        return;
831    }
832}
833
834try {
835    ndefTag.setReadOnly((err)=> {
836        if (err) {
837            console.log("ndef setReadOnly AsyncCallback err: " + err);
838        } else {
839            console.log("ndef setReadOnly AsyncCallback success.");
840        }
841    });
842} catch (busiError) {
843    console.log("ndef setReadOnly AsyncCallback catch busiError: " + busiError);
844}
845```
846
847### NdefTag.getNdefTagTypeString<sup>9+</sup>
848
849getNdefTagTypeString(type: [tag.NfcForumType](js-apis-nfcTag.md#nfcforumtype9)): string
850
851Converts an NFC Forum Type tag to a string defined in the NFC Forum.
852
853**System capability**: SystemCapability.Communication.NFC.Tag
854
855**Parameters**
856
857| Name  | Type                   | Mandatory| Description                                  |
858| -------- | ----------------------- | ---- | -------------------------------------- |
859| type | [tag.NfcForumType](js-apis-nfcTag.md#nfcforumtype9) | Yes  | NDEF tag type. It can be NFC FORUM type 1, 2, 3, or 4.|
860
861**Return value**
862
863| **Type**| **Description**                            |
864| ------------------ | --------------------------|
865| string | Byte array obtained.|
866
867**Example**
868
869```js
870import tag from '@ohos.nfc.tag';
871
872// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly.
873
874try {
875    let ndefTypeString = ndefTag.getNdefTagTypeString(tag.NFC_FORUM_TYPE_1);
876    console.log("ndef ndefTypeString: " + ndefTypeString);
877} catch (busiError) {
878    console.log("ndef getNdefTagTypeString catch busiError: " + busiError);
879}
880```
881
882## MifareClassicTag<sup>9+</sup>
883
884Provides APIs to access MIFARE Classic properties and perform I/O operations on a tag. This class inherits from [TagSession](js-apis-tagSession.md).
885
886**TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md).
887
888The following describes the unique APIs of **MifareClassicTag**.
889
890### MifareClassicTag.authenticateSector<sup>9+</sup>
891
892authenticateSector(sectorIndex: number, key: number[], isKeyA: boolean): Promise\<void>
893
894Authenticates a sector using a key. The sector can be accessed only after the authentication is successful. This API uses a promise to return the result.
895
896**Required permissions**: ohos.permission.NFC_TAG
897
898**System capability**: SystemCapability.Communication.NFC.Tag
899
900**Parameters**
901
902| Name  | Type                   | Mandatory| Description                                  |
903| -------- | ----------------------- | ---- | -------------------------------------- |
904| sectorIndex | number | Yes  | Index of the sector to authenticate. The sector indexes start from **0**.|
905| key | number[]| Yes  | Key (6 bytes) used for sector authentication.|
906| isKeyA | boolean | Yes  | Whether the key is key A. The value **true** indicates key A, and **false** indicates key B.|
907
908**Error codes**
909
910For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
911
912| ID| Error Message|
913| ------- | -------|
914| 3100201 | Tag running state is abnormal in service. |
915
916**Example**
917
918```js
919import tag from '@ohos.nfc.tag';
920
921// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.
922
923// Connect to the tag if it is not connected.
924if (!mifareClassic.isTagConnected()) {
925    if (!mifareClassic.connectTag()) {
926        console.log("mifareClassic connectTag failed.");
927        return;
928    }
929}
930
931try {
932    let sectorIndex = 1; // Change it as required.
933    let key = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06]  // The key must be of 6 bytes.
934    mifareClassic.authenticateSector(sectorIndex, key, true).then(() => {
935        console.log("mifareClassic authenticateSector Promise success.");
936    }).catch((err)=> {
937        console.log("mifareClassic authenticateSector Promise err: " + err);
938    });
939} catch (busiError) {
940    console.log("mifareClassic authenticateSector Promise catch busiError: " + busiError);
941}
942```
943
944### MifareClassicTag.authenticateSector<sup>9+</sup>
945
946authenticateSector(sectorIndex: number, key: number[], isKeyA: boolean, callback: AsyncCallback\<void>): void
947
948Authenticates a sector using a key. The sector can be accessed only after the authentication is successful. This API uses an asynchronous callback to return the result.
949
950**Required permissions**: ohos.permission.NFC_TAG
951
952**System capability**: SystemCapability.Communication.NFC.Tag
953
954**Parameters**
955
956| Name  | Type                   | Mandatory| Description                                  |
957| -------- | ----------------------- | ---- | -------------------------------------- |
958| sectorIndex | number | Yes  | Index of the sector to authenticate. The sector indexes start from **0**.|
959| key | number[]| Yes  | Key (6 bytes) used for sector authentication.|
960| isKeyA | boolean | Yes  | Whether the key is key A. The value **true** indicates key A, and **false** indicates key B.|
961| callback | AsyncCallback\<void> | Yes  | Callback invoked to return the result.|
962
963**Error codes**
964
965For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
966
967| ID| Error Message|
968| ------- | -------|
969| 3100201 | Tag running state is abnormal in service. |
970
971**Example**
972```js
973import tag from '@ohos.nfc.tag';
974
975// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.
976
977// Connect to the tag if it is not connected.
978if (!mifareClassic.isTagConnected()) {
979    if (!mifareClassic.connectTag()) {
980        console.log("mifareClassic connectTag failed.");
981        return;
982    }
983}
984
985try {
986    let sectorIndex = 1; // Change it as required.
987    let key = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06]  // The key must be of 6 bytes.
988    mifareClassic.authenticateSector(sectorIndex, key, true, (err)=> {
989        if (err) {
990            console.log("mifareClassic authenticateSector AsyncCallback err: " + err);
991        } else {
992            console.log("mifareClassic authenticateSector AsyncCallback success.");
993        }
994    });
995} catch (busiError) {
996    console.log("mifareClassic authenticateSector AsyncCallback catch busiError: " + busiError);
997}
998```
999
1000### MifareClassicTag.readSingleBlock<sup>9+</sup>
1001
1002readSingleBlock(blockIndex: number): Promise\<number[]>
1003
1004Reads a block (16 bytes) on this tag. This API uses a promise to return the result.
1005
1006**Required permissions**: ohos.permission.NFC_TAG
1007
1008**System capability**: SystemCapability.Communication.NFC.Tag
1009
1010**Parameters**
1011
1012| Name  | Type                   | Mandatory| Description                                  |
1013| -------- | ----------------------- | ---- | -------------------------------------- |
1014| blockIndex | number | Yes  | Index of the block to read. The block indexes start from **0**.|
1015
1016**Return value**
1017
1018| **Type**| **Description**                            |
1019| ------------------ | --------------------------|
1020| Promise\<number[]> | Promise used to return the block data read.|
1021
1022**Error codes**
1023
1024For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
1025
1026| ID| Error Message|
1027| ------- | -------|
1028| 3100201 | Tag running state is abnormal in service. |
1029
1030**Example**
1031
1032```js
1033import tag from '@ohos.nfc.tag';
1034
1035// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.
1036
1037// Connect to the tag if it is not connected.
1038if (!mifareClassic.isTagConnected()) {
1039    if (!mifareClassic.connectTag()) {
1040        console.log("mifareClassic connectTag failed.");
1041        return;
1042    }
1043}
1044
1045try {
1046    let blockIndex = 1; // Change it as required.
1047    mifareClassic.readSingleBlock(blockIndex).then((data) => {
1048        console.log("mifareClassic readSingleBlock Promise data: " + data);
1049    }).catch((err)=> {
1050        console.log("mifareClassic readSingleBlock Promise err: " + err);
1051    });
1052} catch (busiError) {
1053    console.log("mifareClassic readSingleBlock Promise catch busiError: " + busiError);
1054}
1055```
1056
1057### MifareClassicTag.readSingleBlock<sup>9+</sup>
1058
1059readSingleBlock(blockIndex: number, callback: AsyncCallback\<number[]>): void
1060
1061Reads a block (16 bytes) on this tag. This API uses an asynchronous callback to return the result.
1062
1063**Required permissions**: ohos.permission.NFC_TAG
1064
1065**System capability**: SystemCapability.Communication.NFC.Tag
1066
1067**Parameters**
1068
1069| Name  | Type                   | Mandatory| Description                                  |
1070| -------- | ----------------------- | ---- | -------------------------------------- |
1071| blockIndex | number | Yes  | Index of the block to read. The block indexes start from **0**.|
1072| callback | AsyncCallback\<number[]> | Yes  | Callback invoked to return the block read.|
1073
1074**Error codes**
1075
1076For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
1077
1078| ID| Error Message|
1079| ------- | -------|
1080| 3100201 | Tag running state is abnormal in service. |
1081
1082**Example**
1083
1084```js
1085import tag from '@ohos.nfc.tag';
1086
1087// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.
1088
1089// Connect to the tag if it is not connected.
1090if (!mifareClassic.isTagConnected()) {
1091    if (!mifareClassic.connectTag()) {
1092        console.log("mifareClassic connectTag failed.");
1093        return;
1094    }
1095}
1096
1097try {
1098    let blockIndex = 1; // Change it as required.
1099    mifareClassic.readSingleBlock(blockIndex, (err, data)=> {
1100        if (err) {
1101            console.log("mifareClassic readSingleBlock AsyncCallback err: " + err);
1102        } else {
1103            console.log("mifareClassic readSingleBlock AsyncCallback data: " + data);
1104        }
1105    });
1106} catch (busiError) {
1107    console.log("mifareClassic readSingleBlock AsyncCallback catch busiError: " + busiError);
1108}
1109```
1110
1111### MifareClassicTag.writeSingleBlock<sup>9+</sup>
1112
1113writeSingleBlock(blockIndex: number, data: number[]): Promise\<void>
1114
1115Writes data to a block on this tag. This API uses a promise to return the result.
1116
1117**Required permissions**: ohos.permission.NFC_TAG
1118
1119**System capability**: SystemCapability.Communication.NFC.Tag
1120
1121**Parameters**
1122
1123| Name  | Type                   | Mandatory| Description                                  |
1124| -------- | ----------------------- | ---- | -------------------------------------- |
1125| blockIndex | number | Yes  | Index of the target block. The block indexes start from **0**.|
1126| data | number[] | Yes  | 16-byte data to write.|
1127
1128**Error codes**
1129
1130For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
1131
1132| ID| Error Message|
1133| ------- | -------|
1134| 3100201 | Tag running state is abnormal in service. |
1135
1136**Example**
1137
1138```js
1139import tag from '@ohos.nfc.tag';
1140
1141// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.
1142
1143// Connect to the tag if it is not connected.
1144if (!mifareClassic.isTagConnected()) {
1145    if (!mifareClassic.connectTag()) {
1146        console.log("mifareClassic connectTag failed.");
1147        return;
1148    }
1149}
1150
1151try {
1152    let blockIndex = 1; // Change it as required.
1153    let rawData = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
1154        0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]; // MUST be 16 bytes, change it to be correct data.
1155    mifareClassic.writeSingleBlock(blockIndex, rawData).then(() => {
1156        console.log("mifareClassic writeSingleBlock Promise success.");
1157    }).catch((err)=> {
1158        console.log("mifareClassic writeSingleBlock Promise err: " + err);
1159    });
1160} catch (busiError) {
1161    console.log("mifareClassic writeSingleBlock Promise catch busiError: " + busiError);
1162}
1163```
1164
1165### MifareClassicTag.writeSingleBlock<sup>9+</sup>
1166
1167writeSingleBlock(blockIndex: number, data: number[], callback: AsyncCallback\<void>): void
1168
1169Writes data to a block on this tag. This API uses an asynchronous callback to return the result.
1170
1171**Required permissions**: ohos.permission.NFC_TAG
1172
1173**System capability**: SystemCapability.Communication.NFC.Tag
1174
1175**Parameters**
1176
1177| Name  | Type                   | Mandatory| Description                                  |
1178| -------- | ----------------------- | ---- | -------------------------------------- |
1179| blockIndex | number | Yes  | Index of the target block. The block indexes start from **0**.|
1180| data | number[] | Yes  | 16-byte data to write.|
1181| callback | AsyncCallback\<void> | Yes  | Callback invoked to return the result.|
1182
1183**Error codes**
1184
1185For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
1186
1187| ID| Error Message|
1188| ------- | -------|
1189| 3100201 | Tag running state is abnormal in service. |
1190
1191**Example**
1192
1193```js
1194import tag from '@ohos.nfc.tag';
1195
1196// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.
1197
1198// Connect to the tag if it is not connected.
1199if (!mifareClassic.isTagConnected()) {
1200    if (!mifareClassic.connectTag()) {
1201        console.log("mifareClassic connectTag failed.");
1202        return;
1203    }
1204}
1205
1206try {
1207    let blockIndex = 1; // Change it as required.
1208    let rawData = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
1209        0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]; // MUST be 16 bytes, change it to be correct data.
1210    mifareClassic.writeSingleBlock(blockIndex, rawData, (err)=> {
1211        if (err) {
1212            console.log("mifareClassic writeSingleBlock AsyncCallback err: " + err);
1213        } else {
1214            console.log("mifareClassic writeSingleBlock AsyncCallback success.");
1215        }
1216    });
1217} catch (busiError) {
1218    console.log("mifareClassic writeSingleBlock AsyncCallback catch busiError: " + busiError);
1219}
1220```
1221
1222### MifareClassicTag.incrementBlock<sup>9+</sup>
1223
1224incrementBlock(blockIndex: number, value: number): Promise\<void>
1225
1226Increments a block with data. This API uses a promise to return the result.
1227
1228**Required permissions**: ohos.permission.NFC_TAG
1229
1230**System capability**: SystemCapability.Communication.NFC.Tag
1231
1232**Parameters**
1233
1234| Name  | Type                   | Mandatory| Description                                  |
1235| -------- | ----------------------- | ---- | -------------------------------------- |
1236| blockIndex | number | Yes  | Index of the block to increment. The block indexes start from **0**.|
1237| value | number | Yes  | Block data to increment. The value cannot be a negative number.|
1238
1239**Error codes**
1240
1241For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
1242
1243| ID| Error Message|
1244| ------- | -------|
1245| 3100201 | Tag running state is abnormal in service. |
1246
1247**Example**
1248
1249```js
1250import tag from '@ohos.nfc.tag';
1251
1252// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.
1253
1254// Connect to the tag if it is not connected.
1255if (!mifareClassic.isTagConnected()) {
1256    if (!mifareClassic.connectTag()) {
1257        console.log("mifareClassic connectTag failed.");
1258        return;
1259    }
1260}
1261
1262try {
1263    let blockIndex = 1; // Change it as required.
1264    let value = 0x20; // Change it as required.
1265    mifareClassic.incrementBlock(blockIndex, value).then(() => {
1266        console.log("mifareClassic incrementBlock Promise success.");
1267    }).catch((err)=> {
1268        console.log("mifareClassic incrementBlock Promise err: " + err);
1269    });
1270} catch (busiError) {
1271    console.log("mifareClassic incrementBlock Promise catch busiError: " + busiError);
1272}
1273```
1274
1275### MifareClassicTag.incrementBlock<sup>9+</sup>
1276
1277incrementBlock(blockIndex: number, value: number, callback: AsyncCallback\<void>): void
1278
1279Increments a block with data. This API uses an asynchronous callback to return the result.
1280
1281**Required permissions**: ohos.permission.NFC_TAG
1282
1283**System capability**: SystemCapability.Communication.NFC.Tag
1284
1285**Parameters**
1286
1287| Name  | Type                   | Mandatory| Description                                  |
1288| -------- | ----------------------- | ---- | -------------------------------------- |
1289| blockIndex | number | Yes  | Index of the block to increment. The block indexes start from **0**.|
1290| value | number | Yes  | Block data to increment. The value cannot be a negative number.|
1291| callback | AsyncCallback\<void> | Yes  | Callback invoked to return the result.|
1292
1293**Error codes**
1294
1295For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
1296
1297| ID| Error Message|
1298| ------- | -------|
1299| 3100201 | Tag running state is abnormal in service. |
1300
1301**Example**
1302
1303```js
1304import tag from '@ohos.nfc.tag';
1305
1306// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.
1307
1308// Connect to the tag if it is not connected.
1309if (!mifareClassic.isTagConnected()) {
1310    if (!mifareClassic.connectTag()) {
1311        console.log("mifareClassic connectTag failed.");
1312        return;
1313    }
1314}
1315
1316try {
1317    let blockIndex = 1; // Change it as required.
1318    let value = 0x20; // Change it as required.
1319    mifareClassic.incrementBlock(blockIndex, value, (err)=> {
1320        if (err) {
1321            console.log("mifareClassic incrementBlock AsyncCallback err: " + err);
1322        } else {
1323            console.log("mifareClassic incrementBlock AsyncCallback success.");
1324        }
1325    });
1326} catch (busiError) {
1327    console.log("mifareClassic incrementBlock AsyncCallback catch busiError: " + busiError);
1328}
1329```
1330
1331### MifareClassicTag.decrementBlock<sup>9+</sup>
1332
1333decrementBlock(blockIndex: number, value: number): Promise\<void>
1334
1335Decrements a block. This API uses a promise to return the result.
1336
1337**Required permissions**: ohos.permission.NFC_TAG
1338
1339**System capability**: SystemCapability.Communication.NFC.Tag
1340
1341**Parameters**
1342
1343| Name  | Type                   | Mandatory| Description                                  |
1344| -------- | ----------------------- | ---- | -------------------------------------- |
1345| blockIndex | number | Yes  | Index of the block to decrement. The block indexes start from **0**.|
1346| value | number | Yes  | Block data to decrement. The value cannot be a negative number.|
1347
1348**Error codes**
1349
1350For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
1351
1352| ID| Error Message|
1353| ------- | -------|
1354| 3100201 | Tag running state is abnormal in service. |
1355
1356**Example**
1357
1358```js
1359import tag from '@ohos.nfc.tag';
1360
1361// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.
1362
1363// Connect to the tag if it is not connected.
1364if (!mifareClassic.isTagConnected()) {
1365    if (!mifareClassic.connectTag()) {
1366        console.log("mifareClassic connectTag failed.");
1367        return;
1368    }
1369}
1370
1371try {
1372    let blockIndex = 1; // Change it as required.
1373    let value = 0x20; // Change it as required.
1374    mifareClassic.decrementBlock(blockIndex, value).then(() => {
1375        console.log("mifareClassic decrementBlock Promise success.");
1376    }).catch((err)=> {
1377        console.log("mifareClassic decrementBlock Promise err: " + err);
1378    });
1379} catch (busiError) {
1380    console.log("mifareClassic decrementBlock Promise catch busiError: " + busiError);
1381}
1382```
1383
1384### MifareClassicTag.decrementBlock<sup>9+</sup>
1385
1386decrementBlock(blockIndex: number, value: number, callback: AsyncCallback\<void>): void
1387
1388Decrements a block. This API uses an asynchronous callback to return the result.
1389
1390**Required permissions**: ohos.permission.NFC_TAG
1391
1392**System capability**: SystemCapability.Communication.NFC.Tag
1393
1394**Parameters**
1395
1396| Name  | Type                   | Mandatory| Description                                  |
1397| -------- | ----------------------- | ---- | -------------------------------------- |
1398| blockIndex | number | Yes  | Index of the block to decrement. The block indexes start from **0**.|
1399| value | number | Yes  | Block data to decrement. The value cannot be a negative number.|
1400| callback | AsyncCallback\<void> | Yes  | Callback invoked to return the result.|
1401
1402**Error codes**
1403
1404For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
1405
1406| ID| Error Message|
1407| ------- | -------|
1408| 3100201 | Tag running state is abnormal in service. |
1409
1410**Example**
1411
1412```js
1413import tag from '@ohos.nfc.tag';
1414
1415// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.
1416
1417// Connect to the tag if it is not connected.
1418if (!mifareClassic.isTagConnected()) {
1419    if (!mifareClassic.connectTag()) {
1420        console.log("mifareClassic connectTag failed.");
1421        return;
1422    }
1423}
1424
1425try {
1426    let blockIndex = 1; // Change it as required.
1427    let value = 0x20; // Change it as required.
1428    mifareClassic.decrementBlock(blockIndex, value, (err)=> {
1429        if (err) {
1430            console.log("mifareClassic decrementBlock AsyncCallback err: " + err);
1431        } else {
1432            console.log("mifareClassic decrementBlock AsyncCallback success.");
1433        }
1434    });
1435} catch (busiError) {
1436    console.log("mifareClassic decrementBlock AsyncCallback catch busiError: " + busiError);
1437}
1438```
1439
1440### MifareClassicTag.transferToBlock<sup>9+</sup>
1441
1442transferToBlock(blockIndex: number): Promise\<void>
1443
1444Transfers data from the temporary register to a block. This API uses a promise to return the result.
1445
1446**Required permissions**: ohos.permission.NFC_TAG
1447
1448**System capability**: SystemCapability.Communication.NFC.Tag
1449
1450**Parameters**
1451
1452| Name  | Type                   | Mandatory| Description                                  |
1453| -------- | ----------------------- | ---- | -------------------------------------- |
1454| blockIndex | number | Yes | Index of the destination block. The value starts form **0**.|
1455
1456**Error codes**
1457
1458For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
1459
1460| ID| Error Message|
1461| ------- | -------|
1462| 3100201 | Tag running state is abnormal in service. |
1463
1464**Example**
1465
1466```js
1467import tag from '@ohos.nfc.tag';
1468
1469// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.
1470
1471// Connect to the tag if it is not connected.
1472if (!mifareClassic.isTagConnected()) {
1473    if (!mifareClassic.connectTag()) {
1474        console.log("mifareClassic connectTag failed.");
1475        return;
1476    }
1477}
1478
1479try {
1480    let blockIndex = 1; // Change it as required.
1481    mifareClassic.transferToBlock(blockIndex).then(() => {
1482        console.log("mifareClassic transferToBlock Promise success.");
1483    }).catch((err)=> {
1484        console.log("mifareClassic transferToBlock Promise err: " + err);
1485    });
1486} catch (busiError) {
1487    console.log("mifareClassic transferToBlock Promise catch busiError: " + busiError);
1488}
1489```
1490
1491### MifareClassicTag.transferToBlock<sup>9+</sup>
1492
1493transferToBlock(blockIndex: number, callback: AsyncCallback\<void>): void
1494
1495Transfers data from the temporary register to a block. This API uses an asynchronous callback to return the result.
1496
1497**Required permissions**: ohos.permission.NFC_TAG
1498
1499**System capability**: SystemCapability.Communication.NFC.Tag
1500
1501**Parameters**
1502
1503| Name  | Type                   | Mandatory| Description                                  |
1504| -------- | ----------------------- | ---- | -------------------------------------- |
1505| blockIndex | number | Yes  | Index of the destination block. The value starts form **0**.|
1506| callback | AsyncCallback\<void> | Yes  | Callback invoked to return the result.|
1507
1508**Error codes**
1509
1510For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
1511
1512| ID| Error Message|
1513| ------- | -------|
1514| 3100201 | Tag running state is abnormal in service. |
1515
1516**Example**
1517
1518```js
1519import tag from '@ohos.nfc.tag';
1520
1521// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.
1522
1523// Connect to the tag if it is not connected.
1524if (!mifareClassic.isTagConnected()) {
1525    if (!mifareClassic.connectTag()) {
1526        console.log("mifareClassic connectTag failed.");
1527        return;
1528    }
1529}
1530
1531try {
1532    let blockIndex = 1; // Change it as required.
1533    mifareClassic.transferToBlock(blockIndex, (err)=> {
1534        if (err) {
1535            console.log("mifareClassic transferToBlock AsyncCallback err: " + err);
1536        } else {
1537            console.log("mifareClassic transferToBlock AsyncCallback success.");
1538        }
1539    });
1540} catch (busiError) {
1541    console.log("mifareClassic transferToBlock AsyncCallback catch busiError: " + busiError);
1542}
1543```
1544
1545### MifareClassicTag.restoreFromBlock<sup>9+</sup>
1546
1547restoreFromBlock(blockIndex: number): Promise\<void>
1548
1549Restores data in the temporary register from a block. This API uses a promise to return the result.
1550
1551**Required permissions**: ohos.permission.NFC_TAG
1552
1553**System capability**: SystemCapability.Communication.NFC.Tag
1554
1555**Parameters**
1556
1557| Name  | Type                   | Mandatory| Description                                  |
1558| -------- | ----------------------- | ---- | -------------------------------------- |
1559| blockIndex | number | Yes  | Index of the target block. The value starts form **0**.|
1560
1561**Error codes**
1562
1563For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
1564
1565| ID| Error Message|
1566| ------- | -------|
1567| 3100201 | Tag running state is abnormal in service. |
1568
1569**Example**
1570
1571```js
1572import tag from '@ohos.nfc.tag';
1573
1574// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.
1575
1576// Connect to the tag if it is not connected.
1577if (!mifareClassic.isTagConnected()) {
1578    if (!mifareClassic.connectTag()) {
1579        console.log("mifareClassic connectTag failed.");
1580        return;
1581    }
1582}
1583
1584try {
1585    let blockIndex = 1; // Change it as required.
1586    mifareClassic.restoreFromBlock(blockIndex).then(() => {
1587        console.log("mifareClassic restoreFromBlock Promise success.");
1588    }).catch((err)=> {
1589        console.log("mifareClassic restoreFromBlock Promise err: " + err);
1590    });
1591} catch (busiError) {
1592    console.log("mifareClassic restoreFromBlock Promise catch busiError: " + busiError);
1593}
1594```
1595
1596### MifareClassicTag.restoreFromBlock<sup>9+</sup>
1597
1598restoreFromBlock(blockIndex: number, callback: AsyncCallback\<void>): void
1599
1600Restores data in the temporary register from a block. This API uses an asynchronous callback to return the result.
1601
1602**Required permissions**: ohos.permission.NFC_TAG
1603
1604**System capability**: SystemCapability.Communication.NFC.Tag
1605
1606**Parameters**
1607
1608| Name  | Type                   | Mandatory| Description                                  |
1609| -------- | ----------------------- | ---- | -------------------------------------- |
1610| blockIndex | number | Yes  | Index of the target block. The value starts form **0**.|
1611| callback | AsyncCallback\<void> | Yes  | Callback invoked to return the result.|
1612
1613**Error codes**
1614
1615For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
1616
1617| ID| Error Message|
1618| ------- | -------|
1619| 3100201 | Tag running state is abnormal in service. |
1620
1621**Example**
1622
1623```js
1624import tag from '@ohos.nfc.tag';
1625
1626// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.
1627
1628// Connect to the tag if it is not connected.
1629if (!mifareClassic.isTagConnected()) {
1630    if (!mifareClassic.connectTag()) {
1631        console.log("mifareClassic connectTag failed.");
1632        return;
1633    }
1634}
1635
1636try {
1637    let blockIndex = 1; // Change it as required.
1638    mifareClassic.restoreFromBlock(blockIndex, (err)=> {
1639        if (err) {
1640            console.log("mifareClassic restoreFromBlock AsyncCallback err: " + err);
1641        } else {
1642            console.log("mifareClassic restoreFromBlock AsyncCallback success.");
1643        }
1644    });
1645} catch (busiError) {
1646    console.log("mifareClassic restoreFromBlock AsyncCallback catch busiError: " + busiError);
1647}
1648```
1649
1650### MifareClassicTag.getSectorCount<sup>9+</sup>
1651
1652getSectorCount(): number
1653
1654Obtains the number of sectors in this MIFARE Classic tag.
1655
1656**System capability**: SystemCapability.Communication.NFC.Tag
1657
1658**Return value**
1659
1660| **Type**| **Description**                            |
1661| ------------------ | --------------------------|
1662| number | Number of sectors obtained.|
1663
1664**Example**
1665
1666```js
1667import tag from '@ohos.nfc.tag';
1668
1669// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.
1670let sectorCount = mifareClassic.getSectorCount();
1671console.log("mifareClassic sectorCount: " + sectorCount);
1672```
1673
1674### MifareClassicTag.getBlockCountInSector<sup>9+</sup>
1675
1676getBlockCountInSector(sectorIndex: number): number
1677
1678Obtains the number of blocks in a sector.
1679
1680**System capability**: SystemCapability.Communication.NFC.Tag
1681
1682**Parameters**
1683
1684| Name  | Type                   | Mandatory| Description                                  |
1685| -------- | ----------------------- | ---- | -------------------------------------- |
1686| sectorIndex | number | Yes  | Index of the target sector. The sector indexes start from **0**.|
1687
1688**Return value**
1689
1690| **Type**| **Description**                            |
1691| ------------------ | --------------------------|
1692| number | Number of blocks obtained.|
1693
1694**Example**
1695
1696```js
1697import tag from '@ohos.nfc.tag';
1698
1699// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.
1700
1701try {
1702    let sectorIndex = 1; // Change it as required.
1703    let blockCnt = mifareClassic.getBlockCountInSector(sectorIndex);
1704    console.log("mifareClassic blockCnt: " + blockCnt);
1705} catch (busiError) {
1706    console.log("mifareClassic getBlockCountInSector catch busiError: " + busiError);
1707}
1708```
1709
1710### MifareClassicTag.getType<sup>9+</sup>
1711
1712getType(): [tag.MifareClassicType](js-apis-nfcTag.md#mifareclassictype9)
1713
1714Obtains the type of this MIFARE Classic tag.
1715
1716**System capability**: SystemCapability.Communication.NFC.Tag
1717
1718**Return value**
1719
1720| **Type**| **Description**                            |
1721| ------------------ | --------------------------|
1722| [tag.MifareClassicType](js-apis-nfcTag.md#mifareclassictype9) | Type of the MIFARE Classic tag obtained.|
1723
1724**Example**
1725
1726```js
1727import tag from '@ohos.nfc.tag';
1728
1729// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.
1730let getType = mifareClassic.getType();
1731console.log("mifareClassic getType: " + getType);
1732```
1733
1734### MifareClassicTag.getTagSize<sup>9+</sup>
1735
1736getTagSize(): number
1737
1738Obtains the size of this tag. For details, see [MifareClassicSize](js-apis-nfcTag.md#mifareclassicsize9).
1739
1740**System capability**: SystemCapability.Communication.NFC.Tag
1741
1742**Return value**
1743
1744| **Type**| **Description**                            |
1745| ------------------ | --------------------------|
1746| number | Tag size obtained, in bytes. For details, see [MifareClassicSize](js-apis-nfcTag.md#mifareclassicsize9).|
1747
1748**Example**
1749
1750```js
1751import tag from '@ohos.nfc.tag';
1752
1753// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.
1754let tagSize = mifareClassic.getTagSize();
1755console.log("mifareClassic tagSize: " + tagSize);
1756```
1757
1758### MifareClassicTag.isEmulatedTag<sup>9+</sup>
1759
1760isEmulatedTag(): boolean
1761
1762Checks whether it is an emulated tag.
1763
1764**System capability**: SystemCapability.Communication.NFC.Tag
1765
1766**Return value**
1767
1768| **Type**| **Description**                            |
1769| ------------------ | --------------------------|
1770| boolean |Returns **true** if the tag is an emulated tag; returns **false** otherwise.|
1771
1772**Example**
1773
1774```js
1775import tag from '@ohos.nfc.tag';
1776
1777// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.
1778let isEmulatedTag = mifareClassic.isEmulatedTag();
1779console.log("mifareClassic isEmulatedTag: " + isEmulatedTag);
1780```
1781
1782### MifareClassicTag.getBlockIndex<sup>9+</sup>
1783
1784getBlockIndex(sectorIndex: number): number
1785
1786Obtains the index of the first block in a sector.
1787
1788**System capability**: SystemCapability.Communication.NFC.Tag
1789
1790**Parameters**
1791
1792| Name  | Type                   | Mandatory| Description                                  |
1793| -------- | ----------------------- | ---- | -------------------------------------- |
1794| sectorIndex | number | Yes  | Index of the target sector. The sector indexes start from **0**.|
1795
1796**Return value**
1797
1798| **Type**| **Description**                            |
1799| ------------------ | --------------------------|
1800| number | Index of the first block obtained.|
1801
1802**Example**
1803
1804```js
1805import tag from '@ohos.nfc.tag';
1806
1807// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.
1808
1809try {
1810    let sectorIndex = 1; // Change it as required.
1811    let blockIndex = mifareClassic.getBlockIndex(sectorIndex);
1812    console.log("mifareClassic blockIndex: " + blockIndex);
1813} catch (busiError) {
1814    console.log("mifareClassic getBlockIndex catch busiError: " + busiError);
1815}
1816```
1817
1818### MifareClassicTag.getSectorIndex<sup>9+</sup>
1819
1820getSectorIndex(blockIndex: number): number
1821
1822Obtains the index of the sector that holds the specified block.
1823
1824**System capability**: SystemCapability.Communication.NFC.Tag
1825
1826**Parameters**
1827
1828| Name  | Type                   | Mandatory| Description                                  |
1829| -------- | ----------------------- | ---- | -------------------------------------- |
1830| blockIndex | number | Yes| Index of the block. The block indexes start from **0**.|
1831
1832**Return value**
1833
1834| **Type**| **Description**                            |
1835| ------------------ | --------------------------|
1836| number | Index of the sector obtained.|
1837
1838**Example**
1839
1840```js
1841import tag from '@ohos.nfc.tag';
1842
1843// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.
1844
1845try {
1846    let blockIndex = 1; // Change it as required.
1847    let sectorIndex = mifareClassic.getSectorIndex(blockIndex);
1848    console.log("mifareClassic sectorIndex: " + sectorIndex);
1849} catch (busiError) {
1850    console.log("mifareClassic getSectorIndex catch busiError: " + busiError);
1851}
1852```
1853
1854## MifareUltralightTag<sup>9+</sup>
1855
1856Provides APIs to access MIFARE Ultralight properties and perform I/O operations on a tag. This class inherits from **TagSession**.
1857
1858**TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md).
1859
1860The following describes the unique APIs of **MifareUltralightTag**.
1861
1862### MifareUltralightTag.readMultiplePages<sup>9+</sup>
1863
1864readMultiplePages(pageIndex: number): Promise\<number[]>
1865
1866Reads four pages (4 bytes per page) from this tag. This API uses a promise to return the result.
1867
1868**Required permissions**: ohos.permission.NFC_TAG
1869
1870**System capability**: SystemCapability.Communication.NFC.Tag
1871
1872**Parameters**
1873
1874| Name  | Type                   | Mandatory| Description                                  |
1875| -------- | ----------------------- | ---- | ------------------------------ |
1876| pageIndex | number | Yes  | Index of the first page to read. The page indexes start from **0**.|
1877
1878**Return value**
1879
1880| **Type**| **Description**                            |
1881| ------------------ | --------------------------|
1882| Promise\<number[]> | Promise used to return the data read, which is 16 bytes in total.|
1883
1884**Error codes**
1885
1886For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
1887
1888| ID| Error Message|
1889| ------- | -------|
1890| 3100201 | Tag running state is abnormal in service. |
1891
1892**Example**
1893
1894```js
1895
1896import tag from '@ohos.nfc.tag';
1897
1898// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareUltralight' correctly.
1899
1900// Connect to the tag if it is not connected.
1901if (!mifareUltralight.isTagConnected()) {
1902    if (!mifareUltralight.connectTag()) {
1903        console.log("mifareUltralight connectTag failed.");
1904        return;
1905    }
1906}
1907
1908try {
1909    let pageIndex = 1; // Change it as required.
1910    mifareUltralight.readMultiplePages(pageIndex).then((data) => {
1911        console.log("mifareUltralight readMultiplePages Promise data = " + data);
1912    }).catch((err)=> {
1913        console.log("mifareUltralight readMultiplePages Promise err: " + err);
1914    });
1915} catch (busiError) {
1916    console.log("mifareUltralight readMultiplePages Promise catch busiError: " + busiError);
1917}
1918```
1919
1920### MifareUltralightTag.readMultiplePages<sup>9+</sup>
1921
1922readMultiplePages(pageIndex: number, callback: AsyncCallback\<number[]>): void
1923
1924Reads four pages (4 bytes per page) from this tag. This API uses an asynchronous callback to return the result.
1925
1926**Required permissions**: ohos.permission.NFC_TAG
1927
1928**System capability**: SystemCapability.Communication.NFC.Tag
1929
1930**Parameters**
1931
1932| Name  | Type                   | Mandatory| Description                                  |
1933| -------- | ----------------------- | ---- | -------------------------------------- |
1934| pageIndex | number | Yes  | Index of the first page to read. The page indexes start from **0**.|
1935| callback | AsyncCallback\<number[]> | Yes  | Callback invoked to return the data read, which is 16 bytes in total.|
1936
1937**Error codes**
1938
1939For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
1940
1941| ID| Error Message|
1942| ------- | -------|
1943| 3100201 | Tag running state is abnormal in service. |
1944
1945**Example**
1946
1947```js
1948import tag from '@ohos.nfc.tag';
1949
1950// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareUltralight' correctly.
1951
1952// Connect to the tag if it is not connected.
1953if (!mifareUltralight.isTagConnected()) {
1954    if (!mifareUltralight.connectTag()) {
1955        console.log("mifareUltralight connectTag failed.");
1956        return;
1957    }
1958}
1959
1960try {
1961    let pageIndex = 1; // Change it as required.
1962    mifareUltralight.readMultiplePages(pageIndex, (err, data)=> {
1963        if (err) {
1964            console.log("mifareUltralight readMultiplePages AsyncCallback err: " + err);
1965        } else {
1966            console.log("mifareUltralight readMultiplePages AsyncCallback data: " + data);
1967        }
1968    });
1969} catch (busiError) {
1970    console.log("mifareUltralight readMultiplePages AsyncCallback catch busiError: " + busiError);
1971}
1972```
1973
1974### MifareUltralightTag.writeSinglePage<sup>9+</sup>
1975
1976writeSinglePage(pageIndex: number, data: number[]): Promise\<void>
1977
1978Writes one page (4 bytes) of data to this tag. This API uses a promise to return the result.
1979
1980**Required permissions**: ohos.permission.NFC_TAG
1981
1982**System capability**: SystemCapability.Communication.NFC.Tag
1983
1984**Parameters**
1985
1986| Name  | Type                   | Mandatory| Description                                  |
1987| -------- | ----------------------- | ---- | -------------------------------------- |
1988| pageIndex | number | Yes  | Index of the page to write. The page indexes start from **0**.|
1989| data | number[] | Yes  | 4-byte data to write.|
1990
1991**Error codes**
1992
1993For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
1994
1995| ID| Error Message|
1996| ------- | -------|
1997| 3100201 | Tag running state is abnormal in service. |
1998
1999**Example**
2000
2001```js
2002import tag from '@ohos.nfc.tag';
2003
2004// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareUltralight' correctly.
2005
2006// Connect to the tag if it is not connected.
2007if (!mifareUltralight.isTagConnected()) {
2008    if (!mifareUltralight.connectTag()) {
2009        console.log("mifareUltralight connectTag failed.");
2010        return;
2011    }
2012}
2013
2014try {
2015    let pageIndex = 1; // Change it as required.
2016    let rawData = [0x01, 0x02, 0x03, 0x04]; // MUST be 4 bytes, change it to be correct raw data.
2017    mifareUltralight.writeSinglePage(pageIndex, rawData).then(() => {
2018        console.log("mifareUltralight writeSinglePage Promise success.");
2019    }).catch((err)=> {
2020        console.log("mifareUltralight writeSinglePage Promise err: " + err);
2021    });
2022} catch (busiError) {
2023    console.log("mifareUltralight writeSinglePage Promise catch busiError: " + busiError);
2024}
2025```
2026
2027### MifareUltralightTag.writeSinglePage<sup>9+</sup>
2028
2029writeSinglePage(pageIndex: number, data: number[], callback: AsyncCallback\<void>): void
2030
2031Writes one page (4 bytes) of data to this tag. This API uses an asynchronous callback to return the result.
2032
2033**Required permissions**: ohos.permission.NFC_TAG
2034
2035**System capability**: SystemCapability.Communication.NFC.Tag
2036
2037**Parameters**
2038
2039| Name  | Type                   | Mandatory| Description                    |
2040| -------- | ----------------------- | ---- | ------------------------ |
2041| pageIndex | number | Yes  | Index of the page to write. The page indexes start from **0**.|
2042| data | number[] | Yes  | 4-byte data to write.|
2043| callback|AsyncCallback\<void> |Yes| Callback invoked to return the result.|
2044
2045**Error codes**
2046
2047For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
2048
2049| ID| Error Message|
2050| ------- | -------|
2051| 3100201 | Tag running state is abnormal in service. |
2052
2053**Example**
2054
2055```js
2056import tag from '@ohos.nfc.tag';
2057
2058// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareUltralight' correctly.
2059
2060// Connect to the tag if it is not connected.
2061if (!mifareUltralight.isTagConnected()) {
2062    if (!mifareUltralight.connectTag()) {
2063        console.log("mifareUltralight connectTag failed.");
2064        return;
2065    }
2066}
2067
2068try {
2069    let pageIndex = 1; // Change it as required.
2070    let rawData = [0x01, 0x02, 0x03, 0x04]; // MUST be 4 bytes, change it to be correct raw data.
2071    mifareUltralight.writeSinglePage(pageIndex, rawData, (err)=> {
2072        if (err) {
2073            console.log("mifareUltralight writeSinglePage AsyncCallback err: " + err);
2074        } else {
2075            console.log("mifareUltralight writeSinglePage AsyncCallback success.");
2076        }
2077    });
2078} catch (busiError) {
2079    console.log("mifareUltralight writeSinglePage AsyncCallback catch busiError: " + busiError);
2080}
2081```
2082
2083### MifareUltralightTag.getType<sup>9+</sup>
2084
2085getType(): [tag.MifareUltralightType](js-apis-nfcTag.md#mifareultralighttype9)
2086
2087Obtains the type of this MIFARE Ultralight tag.
2088
2089**System capability**: SystemCapability.Communication.NFC.Tag
2090
2091**Return value**
2092
2093| **Type**| **Description**                            |
2094| ------------------ | --------------------------|
2095| [tag.MifareUltralightType](js-apis-nfcTag.md#mifareultralighttype9) | Type of the MIFARE Ultralight tag obtained.|
2096
2097**Example**
2098
2099```js
2100import tag from '@ohos.nfc.tag';
2101
2102// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareUltralight' correctly.
2103let getType = mifareClassic.getType();
2104console.log("mifareUltralight getType: " + getType);
2105```
2106
2107## NdefFormatableTag<sup>9+</sup>
2108
2109Provides APIs for formatting NDEF formattable tags. This class inherits from **TagSession**.
2110
2111**TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md).
2112
2113The following describes the unique APIs of **NdefFormatableTag**.
2114
2115### NdefFormatableTag.format<sup>9+</sup>
2116
2117format(message: [NdefMessage](#ndefmessage9)): Promise\<void>
2118
2119Formats this tag as an NDEF tag, and writes an NDEF message to it. This API uses a promise to return the result.
2120
2121**Required permissions**: ohos.permission.NFC_TAG
2122
2123**System capability**: SystemCapability.Communication.NFC.Tag
2124
2125**Parameters**
2126
2127| Name  | Type                   | Mandatory| Description                                  |
2128| -------- | ----------------------- | ---- | -------------------------------------- |
2129| message | [NdefMessage](#ndefmessage9) | Yes  | NDEF message to write. If this parameter is **null**, the tag is formatted only (no data will be written).|
2130
2131**Error codes**
2132
2133For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
2134
2135| ID| Error Message|
2136| ------- | -------|
2137| 3100201 | Tag running state is abnormal in service. |
2138
2139**Example**
2140
2141```js
2142import tag from '@ohos.nfc.tag';
2143
2144// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefFormatable' correctly.
2145
2146// Connect to the tag if it is not connected.
2147if (!ndefFormatable.isTagConnected()) {
2148    if (!ndefFormatable.connectTag()) {
2149        console.log("ndefFormatable connectTag failed.");
2150        return;
2151    }
2152}
2153
2154try {
2155    // NDEF message created from raw data, such as:
2156    let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]);  // It must be parsed as NDEF Record.
2157    // or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[])
2158
2159    ndefFormatable.format(ndefMessage).then(() => {
2160        console.log("ndefFormatable format Promise success.");
2161    }).catch((err)=> {
2162        console.log("ndefFormatable format Promise err: " + err);
2163    });
2164} catch (busiError) {
2165    console.log("ndefFormatable format Promise catch busiError: " + busiError);
2166}
2167```
2168
2169### NdefFormatableTag.format<sup>9+</sup>
2170
2171format(message: [NdefMessage](#ndefmessage9), callback: AsyncCallback\<void>): void
2172
2173Formats this tag as an NDEF tag, and writes an NDEF message to it. This API uses an asynchronous callback to return the result.
2174
2175**Required permissions**: ohos.permission.NFC_TAG
2176
2177**System capability**: SystemCapability.Communication.NFC.Tag
2178
2179**Parameters**
2180
2181| Name  | Type                   | Mandatory| Description                                  |
2182| -------- | ----------------------- | ---- | -------------------------------------- |
2183| message | [NdefMessage](#ndefmessage9) | Yes  | NDEF message to write when the formatting is successful. If this parameter is **null**, the tag is formatted only (no data will be written).|
2184
2185**Return value**
2186
2187| **Type**| **Description**                            |
2188| ------------------ | --------------------------|
2189| callback: AsyncCallback\<void> | Callback invoked to return the result.|
2190
2191
2192**Example**
2193
2194```js
2195import tag from '@ohos.nfc.tag';
2196
2197// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefFormatable' correctly.
2198
2199// Connect to the tag if it is not connected.
2200if (!ndefFormatable.isTagConnected()) {
2201    if (!ndefFormatable.connectTag()) {
2202        console.log("ndefFormatable connectTag failed.");
2203        return;
2204    }
2205}
2206
2207try {
2208    // NDEF message created from raw data, such as:
2209    let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]);  // It must be parsed as NDEF Record.
2210    // or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[])
2211
2212    ndefFormatable.format(ndefMessage, (err)=> {
2213        if (err) {
2214            console.log("ndefFormatable format AsyncCallback err: " + err);
2215        } else {
2216            console.log("ndefFormatable format AsyncCallback success.");
2217        }
2218    });
2219} catch (busiError) {
2220    console.log("ndefFormatable format AsyncCallback catch busiError: " + busiError);
2221}
2222```
2223
2224### NdefFormatableTag.formatReadOnly<sup>9+</sup>
2225
2226formatReadOnly(message: [NdefMessage](#ndefmessage9)): Promise\<void>
2227
2228Formats this tag as an NDEF tag, writes an NDEF message to it, and then sets the tag to read-only. This API uses a promise to return the result.
2229
2230**Required permissions**: ohos.permission.NFC_TAG
2231
2232**System capability**: SystemCapability.Communication.NFC.Tag
2233
2234**Parameters**
2235
2236| Name  | Type                   | Mandatory| Description                                  |
2237| -------- | ----------------------- | ---- | -------------------------------------- |
2238| message | [NdefMessage](#ndefmessage9) | Yes  | NDEF message to write. If this parameter is **null**, the tag is formatted only (no data will be written).|
2239
2240**Error codes**
2241
2242For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).
2243
2244| ID| Error Message|
2245| ------- | -------|
2246| 3100201 | Tag running state is abnormal in service. |
2247
2248**Example**
2249
2250```js
2251import tag from '@ohos.nfc.tag';
2252
2253// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefFormatable' correctly.
2254
2255// Connect to the tag if it is not connected.
2256if (!ndefFormatable.isTagConnected()) {
2257    if (!ndefFormatable.connectTag()) {
2258        console.log("ndefFormatable connectTag failed.");
2259        return;
2260    }
2261}
2262
2263try {
2264    // NDEF message created from raw data, such as:
2265    let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]);  // It must be parsed as NDEF Record.
2266    // or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[])
2267
2268    ndefFormatable.formatReadOnly(ndefMessage).then(() => {
2269        console.log("ndefFormatable formatReadOnly Promise success.");
2270    }).catch((err)=> {
2271        console.log("ndefFormatable formatReadOnly Promise err: " + err);
2272    });
2273} catch (busiError) {
2274    console.log("ndefFormatable formatReadOnly Promise catch busiError: " + busiError);
2275}
2276```
2277
2278### NdefFormatableTag.formatReadOnly<sup>9+</sup>
2279
2280formatReadOnly(message: [NdefMessage](#ndefmessage9), callback: AsyncCallback\<void>): void
2281
2282Formats this tag as an NDEF tag, writes an NDEF message to the NDEF tag, and then sets the tag to read-only. This API uses an asynchronous callback to return the result.
2283
2284**Required permissions**: ohos.permission.NFC_TAG
2285
2286**System capability**: SystemCapability.Communication.NFC.Tag
2287
2288**Parameters**
2289
2290| Name  | Type                   | Mandatory| Description                                  |
2291| -------- | ----------------------- | ---- | -------------------------------------- |
2292| message | [NdefMessage](#ndefmessage9) | Yes  | NDEF message to write. If this parameter is **null**, the tag is formatted only (no data will be written).|
2293
2294**Return value**
2295
2296| **Type**| **Description**                            |
2297| ------------------ | --------------------------|
2298| callback: AsyncCallback\<void> | Callback invoked to return the result.|
2299
2300
2301**Example**
2302
2303```js
2304import tag from '@ohos.nfc.tag';
2305
2306// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefFormatable' correctly.
2307
2308// Connect to the tag if it is not connected.
2309if (!ndefFormatable.isTagConnected()) {
2310    if (!ndefFormatable.connectTag()) {
2311        console.log("ndefFormatable connectTag failed.");
2312        return;
2313    }
2314}
2315
2316try {
2317    // NDEF message created from raw data, such as:
2318    let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]);  // It must be parsed as NDEF Record.
2319    // or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[])
2320
2321    ndefFormatable.formatReadOnly(ndefMessage, (err)=> {
2322        if (err) {
2323            console.log("ndefFormatable formatReadOnly AsyncCallback err: " + err);
2324        } else {
2325            console.log("ndefFormatable formatReadOnly AsyncCallback success.");
2326        }
2327    });
2328} catch (busiError) {
2329    console.log("ndefFormatable formatReadOnly AsyncCallback catch busiError: " + busiError);
2330}
2331```
2332