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