js-apis-nfctech.md 73.8 KB
Newer Older
A
Annie_wang 已提交
1
# nfctech (Standard NFC Technologies)
A
Annie_wang 已提交
2

A
Annie_wang 已提交
3
The **nfctech** module provides APIs for reading and writing tags that use different Near-Field Communication (NFC) technologies.
A
Annie_wang 已提交
4 5

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

A
Annie_wang 已提交
9
## Modules to Import
A
Annie_wang 已提交
10 11 12 13 14 15 16

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

## NfcATag

A
Annie_wang 已提交
17
Provides 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)**.
A
Annie_wang 已提交
18 19 20

**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).

A
Annie_wang 已提交
21
The following describes the unique APIs of **NfcATag**.
A
Annie_wang 已提交
22 23 24 25 26 27 28

### NfcATag.getSak

getSak(): number

Obtains the SAK value of this NFC-A tag.

A
Annie_wang 已提交
29
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
30 31 32 33 34

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
A
Annie_wang 已提交
35
| number  | SAK value obtained. The SAK is a hexadecimal number ranging from **0x00** to **0xFF**.|
A
Annie_wang 已提交
36 37 38 39 40 41

**Example**

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

A
Annie_wang 已提交
42
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcA' correctly.
A
Annie_wang 已提交
43 44
let sak = nfcA.getSak();
console.log("nfcA sak: " + sak);
A
Annie_wang 已提交
45 46 47 48 49 50 51 52
```

### NfcATag.getAtqa

getAtqa(): number[]

Obtains the ATQA value of this NFC-A tag.

A
Annie_wang 已提交
53
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
54 55 56 57 58

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
A
Annie_wang 已提交
59
| number[]  | ATQA value obtained. Each number of the ATQA is a hexadecimal number ranging from **0x00** to **0xFF**.|
A
Annie_wang 已提交
60 61 62 63 64 65

**Example**

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

A
Annie_wang 已提交
66
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcA' correctly.
A
Annie_wang 已提交
67 68
let atqa = nfcA.getAtqa();
console.log("nfcA atqa: " + atqa);
A
Annie_wang 已提交
69 70 71 72
```

## NfcBTag

A
Annie_wang 已提交
73
Provides APIs to access NFC-B (ISO 14443-3B) properties and perform I/O operations on a tag. This class inherits from **TagSession**.
A
Annie_wang 已提交
74 75 76

**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).

A
Annie_wang 已提交
77
The following describes the unique APIs of **NfcBTag**.
A
Annie_wang 已提交
78 79 80 81 82 83 84

### NfcBTag.getRespAppData

getRespAppData(): number[]

Obtains the application data of this NFC-B tag.

A
Annie_wang 已提交
85
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
86 87 88 89 90

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
A
Annie_wang 已提交
91
| number[]  | Application data obtained, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.|
A
Annie_wang 已提交
92 93 94 95 96 97

**Example**

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

A
Annie_wang 已提交
98
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcB' correctly.
A
Annie_wang 已提交
99 100
let respAppData = nfcB.getRespAppData();
console.log("nfcB respAppData: " + respAppData);
A
Annie_wang 已提交
101 102 103 104 105 106
```

### NfcBTag.getRespProtocol

getRespProtocol(): number[]

A
Annie_wang 已提交
107
Obtains the protocol information of this NFC-B tag.
A
Annie_wang 已提交
108

A
Annie_wang 已提交
109
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
110 111 112 113 114

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
A
Annie_wang 已提交
115
| number[]  | Protocol information obtained, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.|
A
Annie_wang 已提交
116 117 118 119 120 121

**Example**

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

A
Annie_wang 已提交
122
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcB' correctly.
A
Annie_wang 已提交
123 124
let respProtocol = nfcB.getRespProtocol();
console.log("nfcB respProtocol: " + respProtocol);
A
Annie_wang 已提交
125 126 127 128
```

## NfcFTag

A
Annie_wang 已提交
129
Provides APIs to access NFC-F (JIS 6319-4) properties and perform I/O operations on a tag. This class inherits from **TagSession**.
A
Annie_wang 已提交
130 131 132

**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).

A
Annie_wang 已提交
133
The following describes the unique APIs of **NfcFTag**.
A
Annie_wang 已提交
134 135 136 137 138

### NfcFTag.getSystemCode

getSystemCode(): number[]

A
Annie_wang 已提交
139
Obtains the system code from this NFC-F tag.
A
Annie_wang 已提交
140

A
Annie_wang 已提交
141
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
142 143 144 145 146

**Return value**

| **Type**| **Description**                           |
| ------------------ | --------------------------|
A
Annie_wang 已提交
147
| number[]  | System code obtained, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.|
A
Annie_wang 已提交
148 149 150 151 152 153

**Example**

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

A
Annie_wang 已提交
154
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcF' correctly.
A
Annie_wang 已提交
155 156
let systemCode = nfcF.getSystemCode();
console.log("nfcF systemCode: " + systemCode);
A
Annie_wang 已提交
157 158 159 160 161 162
```

### NfcFTag.getPmm

getPmm(): number[]

A
Annie_wang 已提交
163
Obtains the PMm (consisting of the IC code and manufacturer parameters) information from this NFC-F tag.
A
Annie_wang 已提交
164

A
Annie_wang 已提交
165
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
166 167 168 169 170

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
A
Annie_wang 已提交
171
| number[]  | PMm information obtained, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.|
A
Annie_wang 已提交
172 173 174 175 176 177

**Example**

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

A
Annie_wang 已提交
178
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcF' correctly.
A
Annie_wang 已提交
179 180
let pmm = nfcF.getPmm();
console.log("nfcF pmm: " + pmm);
A
Annie_wang 已提交
181 182 183 184
```

## NfcVTag

A
Annie_wang 已提交
185
Provides APIs to access NFC-V (ISO 15693) properties and perform I/O operations on a tag. This class inherits from **TagSession**.
A
Annie_wang 已提交
186 187 188

**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).

A
Annie_wang 已提交
189
The following describes the unique APIs of **NfcVTag**.
A
Annie_wang 已提交
190 191 192 193 194

### NfcvTag.getResponseFlags

getResponseFlags(): number

A
Annie_wang 已提交
195
Obtains the response flags from this NFC-V tag.
A
Annie_wang 已提交
196

A
Annie_wang 已提交
197
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
198 199 200 201 202

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
A
Annie_wang 已提交
203
| number | Response flags obtained, which consist of hexadecimal numbers ranging from **0x00** to **0xFF**.|
A
Annie_wang 已提交
204 205 206 207 208 209

**Example**

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

A
Annie_wang 已提交
210
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcV' correctly.
A
Annie_wang 已提交
211 212
let responseFlags = nfcV.getResponseFlags();
console.log("nfcV responseFlags: " + responseFlags);
A
Annie_wang 已提交
213 214 215 216 217 218
```

### NfcvTag.getDsfId

getDsfId(): number

A
Annie_wang 已提交
219
Obtains the data storage format identifier (DSFID) from this NFC-V tag.
A
Annie_wang 已提交
220

A
Annie_wang 已提交
221
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
222 223 224 225 226

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
A
Annie_wang 已提交
227
| number | DSFID obtained, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.|
A
Annie_wang 已提交
228 229 230 231 232 233

**Example**

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

A
Annie_wang 已提交
234
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcV' correctly.
A
Annie_wang 已提交
235 236
let dsfId = nfcV.getDsfId();
console.log("nfcV dsfId: " + dsfId);
A
Annie_wang 已提交
237 238 239 240
```

## IsoDepTag<sup>9+</sup> 

A
Annie_wang 已提交
241
Provides APIs to access ISO-DEP (ISO 14443-4) properties and I/O operations on a tag. This class inherits from **TagSession**.
A
Annie_wang 已提交
242 243 244

**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).

A
Annie_wang 已提交
245
The following describes the unique APIs of **IsoDepTag**.
A
Annie_wang 已提交
246 247 248

### IsoDepTag.getHistoricalBytes<sup>9+</sup>

A
Annie_wang 已提交
249
getHistoricalBytes(): number[]
A
Annie_wang 已提交
250

A
Annie_wang 已提交
251
Obtains the historical bytes for the given tag. This API applies only to the IsoDep cards that use the NFC-A technology.
A
Annie_wang 已提交
252

A
Annie_wang 已提交
253
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
254 255 256 257 258

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
A
Annie_wang 已提交
259
| number[] | Historical bytes obtained, which consist of hexadecimal numbers ranging from **0x00** to **0xFF**. If the IsoDep tag uses the NFC-B technology, **null** will be returned.|
A
Annie_wang 已提交
260 261 262 263 264 265

**Example**

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

A
Annie_wang 已提交
266
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'isoDep' correctly.
A
Annie_wang 已提交
267 268
let historicalBytes = isoDep.getHistoricalBytes();
console.log("isoDep historicalBytes: " + historicalBytes);
A
Annie_wang 已提交
269 270 271 272
```

### IsoDepTag.getHiLayerResponse<sup>9+</sup>

A
Annie_wang 已提交
273
getHiLayerResponse(): number[]
A
Annie_wang 已提交
274

A
Annie_wang 已提交
275
Obtains the higher-layer response bytes for the given tag. This API applies only to the IsoDep cards that use the NFC-B technology.
A
Annie_wang 已提交
276

A
Annie_wang 已提交
277
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
278 279 280 281 282

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
A
Annie_wang 已提交
283
| number[] | Higher-layer response bytes obtained, which consist of hexadecimal numbers ranging from **0x00** to **0xFF**. If the IsoDep tag uses the NFC-A technology, **null** will be returned.|
A
Annie_wang 已提交
284 285 286 287 288 289

**Example**

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

A
Annie_wang 已提交
290
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'isoDep' correctly.
A
Annie_wang 已提交
291 292
let hiLayerResponse = isoDep.getHiLayerResponse();
console.log("isoDep hiLayerResponse: " + hiLayerResponse);
A
Annie_wang 已提交
293 294 295 296 297 298 299 300 301 302
```

### IsoDepTag.isExtendedApduSupported<sup>9+</sup>

isExtendedApduSupported(): Promise&lt;boolean&gt;

Checks whether an extended application protocol data unit (APDU) is supported. This API uses a promise to return the result.

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

A
Annie_wang 已提交
303
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
304 305 306 307 308 309 310

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
| Promise&lt;boolean&gt; | Promise used to return the result. If the extended APDU is supported, **true** is returned; otherwise, **false** is returned.|

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

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

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

A
Annie_wang 已提交
319 320 321 322 323
**Example**

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

A
Annie_wang 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'isoDep' correctly.

// Connect to the tag if it is not connected.
if (!isoDep.isTagConnected()) {
    if (!isoDep.connectTag()) {
        console.log("isoDep connectTag failed.");
        return;
    }
}

try {
    isoDep.isExtendedApduSupported().then((response) => {
        console.log("isoDep isExtendedApduSupported Promise response: " + response);
A
Annie_wang 已提交
337
    }).catch((err)=> {
A
Annie_wang 已提交
338
        console.log("isoDep isExtendedApduSupported Promise err: " + err);
A
Annie_wang 已提交
339
    });
A
Annie_wang 已提交
340 341 342 343
} catch (busiError) {
    console.log("isoDep isExtendedApduSupported Promise busiError: " + busiError);
}

A
Annie_wang 已提交
344 345 346 347 348 349
```

### IsoDepTag.isExtendedApduSupported<sup>9+</sup>

isExtendedApduSupported(callback: AsyncCallback\<boolean>): void

A
Annie_wang 已提交
350
Checks whether an extended APDU is supported. This API uses an asynchronous callback to return the result.
A
Annie_wang 已提交
351 352 353

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

A
Annie_wang 已提交
354
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
355 356 357 358 359 360 361

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
| callback | AsyncCallback\<boolean> | Yes  | Callback invoked to return the result. If the extended APDU is supported, **true** is returned; otherwise, **false** is returned.|

A
Annie_wang 已提交
362
**Error codes**
A
Annie_wang 已提交
363

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

A
Annie_wang 已提交
366 367 368
| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
369 370 371 372 373 374

**Example**

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

A
Annie_wang 已提交
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'isoDep' correctly.

// Connect to the tag if it is not connected.
if (!isoDep.isTagConnected()) {
    if (!isoDep.connectTag()) {
        console.log("isoDep connectTag failed.");
        return;
    }
}

try {
    isoDep.isExtendedApduSupported((err, response)=> {
        if (err) {
            console.log("isoDep isExtendedApduSupported AsyncCallback err: " + err);
        } else {
            console.log("isoDep isExtendedApduSupported AsyncCallback response: " + response);
        }
    });
} catch (busiError) {
    console.log("isoDep isExtendedApduSupported AsyncCallback busiError: " + busiError);
}
A
Annie_wang 已提交
396 397 398 399 400 401
```

## NdefMessage<sup>9+</sup>

### NdefMessage.getNdefRecords<sup>9+</sup>

A
Annie_wang 已提交
402
getNdefRecords(): [tag.NdefRecord](js-apis-nfcTag.md#ndefrecord9)[]
A
Annie_wang 已提交
403 404 405

Obtains all NDEF records.

A
Annie_wang 已提交
406
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
407 408 409 410 411

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
A
Annie_wang 已提交
412
| [tag.NdefRecord](js-apis-nfcTag.md#ndefrecord9)[] | List of NDEF records obtained. For details, see *NFCForum-TS-NDEF_1.0*.|
A
Annie_wang 已提交
413 414 415 416 417 418

**Example**

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

A
Annie_wang 已提交
419 420 421 422 423
// Obtain ndefMessage from tag.ndef.createNdefMessage or ndefTag.getNdefMessage.
// var ndefMessage = tag.ndef.createNdefMessage(...);
// var ndefMessage = ndefTag.getNdefMessage();

let ndefRecords = ndefMessage.getNdefRecords();
A
Annie_wang 已提交
424
console.log("ndef ndefRecords number: " + ndefRecords.length);
A
Annie_wang 已提交
425 426
```

A
Annie_wang 已提交
427
## NdefTag<sup>9+</sup>
A
Annie_wang 已提交
428

A
Annie_wang 已提交
429
Provides APIs to access the tags in the NFC Data Exchange Format (NDEF). This class inherits from **TagSession**.
A
Annie_wang 已提交
430

A
Annie_wang 已提交
431
**TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md).
A
Annie_wang 已提交
432

A
Annie_wang 已提交
433
The following describes the unique APIs of **NdefTag**.
A
Annie_wang 已提交
434 435 436

### NdefTag.getNdefTagType<sup>9+</sup>

A
Annie_wang 已提交
437
getNdefTagType(): [tag.NfcForumType](js-apis-nfcTag.md#nfcforumtype9)
A
Annie_wang 已提交
438

A
Annie_wang 已提交
439
Obtains the NDEF tag type.
A
Annie_wang 已提交
440

A
Annie_wang 已提交
441
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
442 443 444 445 446

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
A
Annie_wang 已提交
447
| [tag.NfcForumType](js-apis-nfcTag.md#nfcforumtype9) | NDEF tag type obtained. It can be NFC FORUM TYPE 1, 2, 3, or 4.|
A
Annie_wang 已提交
448 449 450 451 452 453

**Example**

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

A
Annie_wang 已提交
454 455
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly.
let ndefTagType = ndefTag.getNdefTagType();
A
Annie_wang 已提交
456
console.log("ndef ndefTagType: " + ndefTagType);
A
Annie_wang 已提交
457 458 459 460
```

### NdefTag.getNdefMessage<sup>9+</sup>

A
Annie_wang 已提交
461
getNdefMessage(): [NdefMessage](#ndefmessage9)
A
Annie_wang 已提交
462

A
Annie_wang 已提交
463
Obtains the NDEF message from this NDEF tag.
A
Annie_wang 已提交
464

A
Annie_wang 已提交
465
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
466 467 468 469 470

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
A
Annie_wang 已提交
471
| [NdefMessage](#ndefmessage9)  | NDEF message created. For details, see *NFCForum-TS-NDEF_1.0*.|
A
Annie_wang 已提交
472 473 474 475 476

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

A
Annie_wang 已提交
477 478
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly.
let ndefMessage = ndefTag.getNdefMessage();
A
Annie_wang 已提交
479
console.log("ndef ndefMessage: " + ndefMessage);
A
Annie_wang 已提交
480 481 482 483
```

### NdefTag.isNdefWritable<sup>9+</sup>

A
Annie_wang 已提交
484
isNdefWritable(): boolean;
A
Annie_wang 已提交
485

A
Annie_wang 已提交
486
Check whether this NDEF tag is writable. Before calling the data write API, check whether the write operation is supported.
A
Annie_wang 已提交
487

A
Annie_wang 已提交
488
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
489 490 491 492 493

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
A
Annie_wang 已提交
494
| boolean | Promise used to return the result. If the tag is writable, **true** is returned; otherwise, **false** is returned.|
A
Annie_wang 已提交
495 496 497 498 499 500

**Example**

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

A
Annie_wang 已提交
501 502 503
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly.
var isWritable = ndefTag.isNdefWritable();
console.log("ndef isNdefWritable: " + isWritable);
A
Annie_wang 已提交
504 505 506 507
```

### NdefTag.readNdef<sup>9+</sup>

A
Annie_wang 已提交
508
readNdef(): Promise\<[NdefMessage](#ndefmessage9)>
A
Annie_wang 已提交
509 510 511 512 513

Reads the NDEF message from this tag. This API uses a promise to return the result.

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

A
Annie_wang 已提交
514
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
515 516 517 518 519

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
A
Annie_wang 已提交
520
| Promise\<[NdefMessage](#ndefmessage9)> | Promise used to return the message read.|
A
Annie_wang 已提交
521

A
Annie_wang 已提交
522 523 524 525 526 527 528 529
**Error codes**

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

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

A
Annie_wang 已提交
530 531 532 533 534
**Example**

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

A
Annie_wang 已提交
535 536 537 538 539 540 541 542 543 544 545 546 547
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly.

// Connect to the tag if it is not connected.
if (!ndefTag.isTagConnected()) {
    if (!ndefTag.connectTag()) {
        console.log("ndefTag connectTag failed.");
        return;
    }
}

try {
    ndefTag.readNdef().then((ndefmessage) => {
        console.log("ndef readNdef Promise ndefmessage: " + ndefmessage);
A
Annie_wang 已提交
548
    }).catch((err)=> {
A
Annie_wang 已提交
549
        console.log("ndef readNdef Promise err: " + err);
A
Annie_wang 已提交
550
    });
A
Annie_wang 已提交
551 552 553
} catch (busiError) {
    console.log("ndef readNdef Promise catched busiError: " + busiError);
}
A
Annie_wang 已提交
554 555 556 557 558 559 560 561 562 563
```

### NdefTag.readNdef<sup>9+</sup>

readNdef(callback: AsyncCallback\<[NdefMessage](#ndefmessage9)>): void

Reads the NDEF message from this tag. This API uses an asynchronous callback to return the result.

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

A
Annie_wang 已提交
564
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
565 566 567 568 569

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
570 571 572 573 574 575 576 577 578
| callback | AsyncCallback\<[NdefMessage](#ndefmessage9)> | Yes  | Callback invoked to return the NDEF message read.|

**Error codes**

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

| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
579 580 581 582 583 584

**Example**

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

A
Annie_wang 已提交
585 586 587 588 589 590 591
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly.

// Connect to the tag if it is not connected.
if (!ndefTag.isTagConnected()) {
    if (!ndefTag.connectTag()) {
        console.log("ndefTag connectTag failed.");
        return;
A
Annie_wang 已提交
592
    }
A
Annie_wang 已提交
593 594 595 596 597 598 599 600 601 602 603 604 605
}

try {
    ndefTag.readNdef((err, ndefmessage)=> {
        if (err) {
            console.log("ndef readNdef AsyncCallback err: " + err);
        } else {
            console.log("ndef readNdef AsyncCallback ndefmessage: " + ndefmessage);
        }
    });
} catch (busiError) {
    console.log("ndef readNdef AsyncCallback catched busiError: " + busiError);
}
A
Annie_wang 已提交
606 607 608 609
```

### NdefTag.writeNdef<sup>9+</sup>

A
Annie_wang 已提交
610
writeNdef(msg: NdefMessage): Promise\<void>;
A
Annie_wang 已提交
611

A
Annie_wang 已提交
612
Writes an NDEF message to this tag. This API uses a promise to return the result.
A
Annie_wang 已提交
613 614 615

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

A
Annie_wang 已提交
616
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
617 618 619 620 621 622 623

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
| msg | NdefMessage | Yes  | NDEF message to write.|

A
Annie_wang 已提交
624
**Error codes**
A
Annie_wang 已提交
625

A
Annie_wang 已提交
626 627 628 629 630
For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).

| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
631 632 633 634 635 636

**Example**

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

A
Annie_wang 已提交
637 638 639 640
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly.
// NDEF message created from raw data, such as:
let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]);  // It must be parsed as NDEF Record.
// or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[])
A
Annie_wang 已提交
641

A
Annie_wang 已提交
642 643 644 645 646 647 648 649 650 651 652
// Connect to the tag if it is not connected.
if (!ndefTag.isTagConnected()) {
    if (!ndefTag.connectTag()) {
        console.log("ndefTag connectTag failed.");
        return;
    }
}

try {
    ndefTag.writeNdef(ndefMessage).then(() => {
        console.log("ndef writeNdef Promise success.");
A
Annie_wang 已提交
653 654 655
    }).catch((err)=> {
        console.log("ndef writeNdef err: " + err);
    });
A
Annie_wang 已提交
656 657 658
} catch (busiError) {
    console.log("ndef writeNdef Promise catch busiError: " + busiError);
}
A
Annie_wang 已提交
659 660 661 662
```

### NdefTag.writeNdef<sup>9+</sup>

A
Annie_wang 已提交
663
writeNdef(msg: [NdefMessage](#ndefmessage9), callback: AsyncCallback\<void>): void
A
Annie_wang 已提交
664

A
Annie_wang 已提交
665
Writes an NDEF message to this tag. This API uses an asynchronous callback to return the result.
A
Annie_wang 已提交
666 667 668

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

A
Annie_wang 已提交
669
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
670 671 672 673 674

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
675 676 677 678 679 680 681 682 683 684
| msg | [NdefMessage](#ndefmessage9) | Yes  | NDEF message to write.|
| callback | AsyncCallback\<void> | Yes  | Callback invoked to return the result.|

**Error codes**

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

| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
685 686 687 688 689 690

**Example**

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

A
Annie_wang 已提交
691 692 693 694 695 696 697 698 699 700
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly.
// NDEF message created from raw data, such as:
let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]);  // It must be parsed as NDEF Record.
// or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[])

// Connect to the tag if it is not connected.
if (!ndefTag.isTagConnected()) {
    if (!ndefTag.connectTag()) {
        console.log("ndefTag connectTag failed.");
        return;
A
Annie_wang 已提交
701
    }
A
Annie_wang 已提交
702 703 704 705 706 707 708 709 710 711 712 713 714
}

try {
    ndefTag.writeNdef(ndefMessage, (err)=> {
        if (err) {
            console.log("ndef writeNdef AsyncCallback err: " + err);
        } else {
            console.log("ndef writeNdef AsyncCallback success.");
        }
    });
} catch (busiError) {
    console.log("ndef writeNdef AsyncCallback catch busiError: " + busiError);
}
A
Annie_wang 已提交
715 716 717 718
```

### NdefTag.canSetReadOnly<sup>9+</sup>

A
Annie_wang 已提交
719
canSetReadOnly(): boolean
A
Annie_wang 已提交
720

A
Annie_wang 已提交
721
Checks whether this NDEF tag can be set to read-only.
A
Annie_wang 已提交
722 723 724

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

A
Annie_wang 已提交
725
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
726 727 728 729 730

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
A
Annie_wang 已提交
731
| boolean| Returns **true** if the tag can be set to read-only; returns **false** otherwise.|
A
Annie_wang 已提交
732

A
Annie_wang 已提交
733 734 735 736 737 738 739 740
**Error codes**

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

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

A
Annie_wang 已提交
741 742 743 744 745
**Example**

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

A
Annie_wang 已提交
746 747
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly.
var canSetReadOnly = ndefTag.canSetReadOnly();
A
Annie_wang 已提交
748
console.log("ndef canSetReadOnly: " + canSetReadOnly);
A
Annie_wang 已提交
749 750 751 752
```

### NdefTag.setReadOnly<sup>9+</sup>

A
Annie_wang 已提交
753
setReadOnly(): Promise\<void>
A
Annie_wang 已提交
754 755 756 757 758

Sets this NDEF tag to read-only. This API uses a promise to return the result.

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

A
Annie_wang 已提交
759
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
760

A
Annie_wang 已提交
761
**Error codes**
A
Annie_wang 已提交
762

A
Annie_wang 已提交
763 764 765 766 767
For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).

| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
768 769 770 771 772 773

**Example**

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

A
Annie_wang 已提交
774 775 776 777 778 779 780 781 782 783 784 785 786
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly.

// Connect to the tag if it is not connected.
if (!ndefTag.isTagConnected()) {
    if (!ndefTag.connectTag()) {
        console.log("ndefTag connectTag failed.");
        return;
    }
}

try {
    ndefTag.setReadOnly().then(() => {
        console.log("ndef setReadOnly Promise success.");
A
Annie_wang 已提交
787
    }).catch((err)=> {
A
Annie_wang 已提交
788
        console.log("ndef setReadOnly Promise err: " + err);
A
Annie_wang 已提交
789
    });
A
Annie_wang 已提交
790 791 792
} catch (busiError) {
    console.log("ndef setReadOnly Promise catch busiError: " + busiError);
}
A
Annie_wang 已提交
793 794 795 796
```

### NdefTag.setReadOnly<sup>9+</sup>

A
Annie_wang 已提交
797
setReadOnly(callback: AsyncCallback\<void>): void
A
Annie_wang 已提交
798 799 800 801 802

Sets this NDEF tag to read-only. This API uses an asynchronous callback to return the result.

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

A
Annie_wang 已提交
803
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
804 805 806 807 808

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
809 810 811 812 813 814 815 816 817
| callback | AsyncCallback\<void> | Yes  | Callback invoked to return the result.|

**Error codes**

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

| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
818 819 820 821 822 823

**Example**

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

A
Annie_wang 已提交
824 825 826 827 828 829 830
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly.

// Connect to the tag if it is not connected.
if (!ndefTag.isTagConnected()) {
    if (!ndefTag.connectTag()) {
        console.log("ndefTag connectTag failed.");
        return;
A
Annie_wang 已提交
831
    }
A
Annie_wang 已提交
832 833 834 835 836 837 838 839 840 841 842 843 844
}

try {
    ndefTag.setReadOnly((err)=> {
        if (err) {
            console.log("ndef setReadOnly AsyncCallback err: " + err);
        } else {
            console.log("ndef setReadOnly AsyncCallback success.");
        }
    });
} catch (busiError) {
    console.log("ndef setReadOnly AsyncCallback catch busiError: " + busiError);
}
A
Annie_wang 已提交
845 846 847 848
```

### NdefTag.getNdefTagTypeString<sup>9+</sup>

A
Annie_wang 已提交
849
getNdefTagTypeString(type: [tag.NfcForumType](js-apis-nfcTag.md#nfcforumtype9)): string
A
Annie_wang 已提交
850

A
Annie_wang 已提交
851
Converts an NFC Forum Type tag to a string defined in the NFC Forum.
A
Annie_wang 已提交
852

A
Annie_wang 已提交
853
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
854 855 856 857 858

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
859
| type | [tag.NfcForumType](js-apis-nfcTag.md#nfcforumtype9) | Yes  | NDEF tag type. It can be NFC FORUM type 1, 2, 3, or 4.|
A
Annie_wang 已提交
860 861 862 863 864 865 866 867 868 869 870 871

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
| string | Byte array obtained.|

**Example**

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

A
Annie_wang 已提交
872 873 874 875 876 877 878 879
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly.

try {
    let ndefTypeString = ndefTag.getNdefTagTypeString(tag.NFC_FORUM_TYPE_1);
    console.log("ndef ndefTypeString: " + ndefTypeString);
} catch (busiError) {
    console.log("ndef getNdefTagTypeString catch busiError: " + busiError);
}
A
Annie_wang 已提交
880 881
```

A
Annie_wang 已提交
882
## MifareClassicTag<sup>9+</sup>
A
Annie_wang 已提交
883

A
Annie_wang 已提交
884
Provides APIs to access MIFARE Classic properties and perform I/O operations on a tag. This class inherits from [TagSession](js-apis-tagSession.md).
A
Annie_wang 已提交
885 886 887

**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).

A
Annie_wang 已提交
888
The following describes the unique APIs of **MifareClassicTag**.
A
Annie_wang 已提交
889 890 891

### MifareClassicTag.authenticateSector<sup>9+</sup>

A
Annie_wang 已提交
892
authenticateSector(sectorIndex: number, key: number[], isKeyA: boolean): Promise\<void>
A
Annie_wang 已提交
893

A
Annie_wang 已提交
894
Authenticates 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.
A
Annie_wang 已提交
895 896 897

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

A
Annie_wang 已提交
898
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
899 900 901 902 903

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
904 905
| sectorIndex | number | Yes  | Index of the sector to authenticate. The sector indexes start from **0**.|
| key | number[]| Yes  | Key (6 bytes) used for sector authentication.|
A
Annie_wang 已提交
906 907
| isKeyA | boolean | Yes  | Whether the key is key A. The value **true** indicates key A, and **false** indicates key B.|

A
Annie_wang 已提交
908
**Error codes**
A
Annie_wang 已提交
909

A
Annie_wang 已提交
910 911 912 913 914
For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).

| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
915 916 917 918 919 920

**Example**

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

A
Annie_wang 已提交
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.

// Connect to the tag if it is not connected.
if (!mifareClassic.isTagConnected()) {
    if (!mifareClassic.connectTag()) {
        console.log("mifareClassic connectTag failed.");
        return;
    }
}

try {
    let sectorIndex = 1; // Change it as required.
    let key = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06]  // The key must be of 6 bytes.
    mifareClassic.authenticateSector(sectorIndex, key, true).then(() => {
        console.log("mifareClassic authenticateSector Promise success.");
A
Annie_wang 已提交
936
    }).catch((err)=> {
A
Annie_wang 已提交
937
        console.log("mifareClassic authenticateSector Promise err: " + err);
A
Annie_wang 已提交
938
    });
A
Annie_wang 已提交
939 940 941
} catch (busiError) {
    console.log("mifareClassic authenticateSector Promise catch busiError: " + busiError);
}
A
Annie_wang 已提交
942 943 944 945
```

### MifareClassicTag.authenticateSector<sup>9+</sup>

A
Annie_wang 已提交
946
authenticateSector(sectorIndex: number, key: number[], isKeyA: boolean, callback: AsyncCallback\<void>): void
A
Annie_wang 已提交
947

A
Annie_wang 已提交
948
Authenticates 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.
A
Annie_wang 已提交
949 950 951

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

A
Annie_wang 已提交
952
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
953 954 955 956 957

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
958 959
| sectorIndex | number | Yes  | Index of the sector to authenticate. The sector indexes start from **0**.|
| key | number[]| Yes  | Key (6 bytes) used for sector authentication.|
A
Annie_wang 已提交
960
| isKeyA | boolean | Yes  | Whether the key is key A. The value **true** indicates key A, and **false** indicates key B.|
A
Annie_wang 已提交
961
| callback | AsyncCallback\<void> | Yes  | Callback invoked to return the result.|
A
Annie_wang 已提交
962

A
Annie_wang 已提交
963
**Error codes**
A
Annie_wang 已提交
964

A
Annie_wang 已提交
965 966 967 968 969 970 971
For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).

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

**Example**
A
Annie_wang 已提交
972 973 974
```js
import tag from '@ohos.nfc.tag';

A
Annie_wang 已提交
975 976 977 978 979 980 981
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.

// Connect to the tag if it is not connected.
if (!mifareClassic.isTagConnected()) {
    if (!mifareClassic.connectTag()) {
        console.log("mifareClassic connectTag failed.");
        return;
A
Annie_wang 已提交
982
    }
A
Annie_wang 已提交
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
}

try {
    let sectorIndex = 1; // Change it as required.
    let key = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06]  // The key must be of 6 bytes.
    mifareClassic.authenticateSector(sectorIndex, key, true, (err)=> {
        if (err) {
            console.log("mifareClassic authenticateSector AsyncCallback err: " + err);
        } else {
            console.log("mifareClassic authenticateSector AsyncCallback success.");
        }
    });
} catch (busiError) {
    console.log("mifareClassic authenticateSector AsyncCallback catch busiError: " + busiError);
}
A
Annie_wang 已提交
998 999 1000 1001
```

### MifareClassicTag.readSingleBlock<sup>9+</sup>

A
Annie_wang 已提交
1002
readSingleBlock(blockIndex: number): Promise\<number[]>
A
Annie_wang 已提交
1003

A
Annie_wang 已提交
1004
Reads a block (16 bytes) on this tag. This API uses a promise to return the result.
A
Annie_wang 已提交
1005 1006 1007

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

A
Annie_wang 已提交
1008
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
1009 1010 1011 1012 1013

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
1014
| blockIndex | number | Yes  | Index of the block to read. The block indexes start from **0**.|
A
Annie_wang 已提交
1015 1016 1017 1018 1019

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
A
Annie_wang 已提交
1020
| Promise\<number[]> | Promise used to return the block data read.|
A
Annie_wang 已提交
1021

A
Annie_wang 已提交
1022 1023 1024 1025 1026 1027 1028 1029
**Error codes**

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

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

A
Annie_wang 已提交
1030 1031 1032 1033 1034
**Example**

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

A
Annie_wang 已提交
1035 1036 1037 1038 1039 1040 1041
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.

// Connect to the tag if it is not connected.
if (!mifareClassic.isTagConnected()) {
    if (!mifareClassic.connectTag()) {
        console.log("mifareClassic connectTag failed.");
        return;
A
Annie_wang 已提交
1042
    }
A
Annie_wang 已提交
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
}

try {
    let blockIndex = 1; // Change it as required.
    mifareClassic.readSingleBlock(blockIndex).then((data) => {
        console.log("mifareClassic readSingleBlock Promise data: " + data);
    }).catch((err)=> {
        console.log("mifareClassic readSingleBlock Promise err: " + err);
    });
} catch (busiError) {
    console.log("mifareClassic readSingleBlock Promise catch busiError: " + busiError);
}
A
Annie_wang 已提交
1055 1056 1057 1058
```

### MifareClassicTag.readSingleBlock<sup>9+</sup>

A
Annie_wang 已提交
1059
readSingleBlock(blockIndex: number, callback: AsyncCallback\<number[]>): void
A
Annie_wang 已提交
1060

A
Annie_wang 已提交
1061
Reads a block (16 bytes) on this tag. This API uses an asynchronous callback to return the result.
A
Annie_wang 已提交
1062 1063 1064

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

A
Annie_wang 已提交
1065
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
1066 1067 1068 1069 1070

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
1071
| blockIndex | number | Yes  | Index of the block to read. The block indexes start from **0**.|
A
Annie_wang 已提交
1072
| callback | AsyncCallback\<number[]> | Yes  | Callback invoked to return the data read.|
A
Annie_wang 已提交
1073

A
Annie_wang 已提交
1074 1075 1076 1077 1078 1079 1080 1081
**Error codes**

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

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

A
Annie_wang 已提交
1082 1083 1084 1085 1086
**Example**

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

A
Annie_wang 已提交
1087 1088 1089 1090 1091 1092 1093
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.

// Connect to the tag if it is not connected.
if (!mifareClassic.isTagConnected()) {
    if (!mifareClassic.connectTag()) {
        console.log("mifareClassic connectTag failed.");
        return;
A
Annie_wang 已提交
1094
    }
A
Annie_wang 已提交
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
}

try {
    let blockIndex = 1; // Change it as required.
    mifareClassic.readSingleBlock(blockIndex, (err, data)=> {
        if (err) {
            console.log("mifareClassic readSingleBlock AsyncCallback err: " + err);
        } else {
            console.log("mifareClassic readSingleBlock AsyncCallback data: " + data);
        }
    });
} catch (busiError) {
    console.log("mifareClassic readSingleBlock AsyncCallback catch busiError: " + busiError);
}
A
Annie_wang 已提交
1109 1110 1111 1112
```

### MifareClassicTag.writeSingleBlock<sup>9+</sup>

A
Annie_wang 已提交
1113
writeSingleBlock(blockIndex: number, data: number[]): Promise\<void>
A
Annie_wang 已提交
1114

A
Annie_wang 已提交
1115
Writes data to a block on this tag. This API uses a promise to return the result.
A
Annie_wang 已提交
1116 1117 1118

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

A
Annie_wang 已提交
1119
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
1120 1121 1122 1123 1124

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
1125 1126
| blockIndex | number | Yes  | Index of the target block. The block indexes start from **0**.|
| data | number[] | Yes  | 16-byte data to write.|
A
Annie_wang 已提交
1127

A
Annie_wang 已提交
1128
**Error codes**
A
Annie_wang 已提交
1129

A
Annie_wang 已提交
1130 1131 1132 1133 1134
For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).

| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
1135 1136 1137 1138 1139 1140

**Example**

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

A
Annie_wang 已提交
1141 1142 1143 1144 1145 1146 1147
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.

// Connect to the tag if it is not connected.
if (!mifareClassic.isTagConnected()) {
    if (!mifareClassic.connectTag()) {
        console.log("mifareClassic connectTag failed.");
        return;
A
Annie_wang 已提交
1148
    }
A
Annie_wang 已提交
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
}

try {
    let blockIndex = 1; // Change it as required.
    let rawData = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
        0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]; // MUST be 16 bytes, change it to be correct data.
    mifareClassic.writeSingleBlock(blockIndex, rawData).then(() => {
        console.log("mifareClassic writeSingleBlock Promise success.");
    }).catch((err)=> {
        console.log("mifareClassic writeSingleBlock Promise err: " + err);
    });
} catch (busiError) {
    console.log("mifareClassic writeSingleBlock Promise catch busiError: " + busiError);
}
A
Annie_wang 已提交
1163 1164 1165 1166
```

### MifareClassicTag.writeSingleBlock<sup>9+</sup>

A
Annie_wang 已提交
1167
writeSingleBlock(blockIndex: number, data: number[], callback: AsyncCallback\<void>): void
A
Annie_wang 已提交
1168

A
Annie_wang 已提交
1169
Writes data to a block on this tag. This API uses an asynchronous callback to return the result.
A
Annie_wang 已提交
1170 1171 1172

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

A
Annie_wang 已提交
1173
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
1174 1175 1176 1177 1178

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
| blockIndex | number | Yes  | Index of the target block. The block indexes start from **0**.|
| data | number[] | Yes  | 16-byte data to write.|
| callback | AsyncCallback\<void> | Yes  | Callback invoked to return the result.|

**Error codes**

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

| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
1190 1191 1192 1193 1194 1195

**Example**

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

A
Annie_wang 已提交
1196 1197 1198 1199 1200 1201 1202
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.

// Connect to the tag if it is not connected.
if (!mifareClassic.isTagConnected()) {
    if (!mifareClassic.connectTag()) {
        console.log("mifareClassic connectTag failed.");
        return;
A
Annie_wang 已提交
1203
    }
A
Annie_wang 已提交
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
}

try {
    let blockIndex = 1; // Change it as required.
    let rawData = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
        0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]; // MUST be 16 bytes, change it to be correct data.
    mifareClassic.writeSingleBlock(blockIndex, rawData, (err)=> {
        if (err) {
            console.log("mifareClassic writeSingleBlock AsyncCallback err: " + err);
        } else {
            console.log("mifareClassic writeSingleBlock AsyncCallback success.");
        }
    });
} catch (busiError) {
    console.log("mifareClassic writeSingleBlock AsyncCallback catch busiError: " + busiError);
}
A
Annie_wang 已提交
1220 1221 1222 1223
```

### MifareClassicTag.incrementBlock<sup>9+</sup>

A
Annie_wang 已提交
1224
incrementBlock(blockIndex: number, value: number): Promise\<void>
A
Annie_wang 已提交
1225 1226 1227 1228 1229

Increments a block with data. This API uses a promise to return the result.

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

A
Annie_wang 已提交
1230
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
1231 1232 1233 1234 1235

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
1236 1237
| blockIndex | number | Yes  | Index of the block to increment. The block indexes start from **0**.|
| value | number | Yes  | Block data to increment. The value cannot be a negative number.|
A
Annie_wang 已提交
1238

A
Annie_wang 已提交
1239
**Error codes**
A
Annie_wang 已提交
1240

A
Annie_wang 已提交
1241 1242 1243 1244 1245
For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).

| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
1246 1247 1248 1249 1250 1251

**Example**

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

A
Annie_wang 已提交
1252 1253 1254 1255 1256 1257 1258
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.

// Connect to the tag if it is not connected.
if (!mifareClassic.isTagConnected()) {
    if (!mifareClassic.connectTag()) {
        console.log("mifareClassic connectTag failed.");
        return;
A
Annie_wang 已提交
1259
    }
A
Annie_wang 已提交
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
}

try {
    let blockIndex = 1; // Change it as required.
    let value = 0x20; // Change it as required.
    mifareClassic.incrementBlock(blockIndex, value).then(() => {
        console.log("mifareClassic incrementBlock Promise success.");
    }).catch((err)=> {
        console.log("mifareClassic incrementBlock Promise err: " + err);
    });
} catch (busiError) {
    console.log("mifareClassic incrementBlock Promise catch busiError: " + busiError);
}
A
Annie_wang 已提交
1273 1274 1275 1276
```

### MifareClassicTag.incrementBlock<sup>9+</sup>

A
Annie_wang 已提交
1277
incrementBlock(blockIndex: number, value: number, callback: AsyncCallback\<void>): void
A
Annie_wang 已提交
1278 1279 1280 1281 1282

Increments a block with data. This API uses an asynchronous callback to return the result.

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

A
Annie_wang 已提交
1283
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
1284 1285 1286 1287 1288

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
| blockIndex | number | Yes  | Index of the block to increment. The block indexes start from **0**.|
| value | number | Yes  | Block data to increment. The value cannot be a negative number.|
| callback | AsyncCallback\<void> | Yes  | Callback invoked to return the result.|

**Error codes**

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

| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
1300 1301 1302 1303 1304 1305

**Example**

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

A
Annie_wang 已提交
1306 1307 1308 1309 1310 1311 1312
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.

// Connect to the tag if it is not connected.
if (!mifareClassic.isTagConnected()) {
    if (!mifareClassic.connectTag()) {
        console.log("mifareClassic connectTag failed.");
        return;
A
Annie_wang 已提交
1313
    }
A
Annie_wang 已提交
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
}

try {
    let blockIndex = 1; // Change it as required.
    let value = 0x20; // Change it as required.
    mifareClassic.incrementBlock(blockIndex, value, (err)=> {
        if (err) {
            console.log("mifareClassic incrementBlock AsyncCallback err: " + err);
        } else {
            console.log("mifareClassic incrementBlock AsyncCallback success.");
        }
    });
} catch (busiError) {
    console.log("mifareClassic incrementBlock AsyncCallback catch busiError: " + busiError);
}
A
Annie_wang 已提交
1329 1330 1331 1332
```

### MifareClassicTag.decrementBlock<sup>9+</sup>

A
Annie_wang 已提交
1333
decrementBlock(blockIndex: number, value: number): Promise\<void>
A
Annie_wang 已提交
1334

A
Annie_wang 已提交
1335
Decrements a block. This API uses a promise to return the result.
A
Annie_wang 已提交
1336 1337 1338

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

A
Annie_wang 已提交
1339
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
1340 1341 1342 1343 1344

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
1345 1346
| blockIndex | number | Yes  | Index of the block to decrement. The block indexes start from **0**.|
| value | number | Yes  | Block data to decrement. The value cannot be a negative number.|
A
Annie_wang 已提交
1347

A
Annie_wang 已提交
1348
**Error codes**
A
Annie_wang 已提交
1349

A
Annie_wang 已提交
1350 1351 1352 1353 1354
For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).

| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
1355 1356 1357 1358 1359 1360

**Example**

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

A
Annie_wang 已提交
1361 1362 1363 1364 1365 1366 1367
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.

// Connect to the tag if it is not connected.
if (!mifareClassic.isTagConnected()) {
    if (!mifareClassic.connectTag()) {
        console.log("mifareClassic connectTag failed.");
        return;
A
Annie_wang 已提交
1368
    }
A
Annie_wang 已提交
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
}

try {
    let blockIndex = 1; // Change it as required.
    let value = 0x20; // Change it as required.
    mifareClassic.decrementBlock(blockIndex, value).then(() => {
        console.log("mifareClassic decrementBlock Promise success.");
    }).catch((err)=> {
        console.log("mifareClassic decrementBlock Promise err: " + err);
    });
} catch (busiError) {
    console.log("mifareClassic decrementBlock Promise catch busiError: " + busiError);
}
A
Annie_wang 已提交
1382 1383 1384 1385
```

### MifareClassicTag.decrementBlock<sup>9+</sup>

A
Annie_wang 已提交
1386
decrementBlock(blockIndex: number, value: number, callback: AsyncCallback\<void>): void
A
Annie_wang 已提交
1387

A
Annie_wang 已提交
1388
Decrements a block. This API uses an asynchronous callback to return the result.
A
Annie_wang 已提交
1389 1390 1391

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

A
Annie_wang 已提交
1392
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
1393 1394 1395 1396 1397

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
| blockIndex | number | Yes  | Index of the block to decrement. The block indexes start from **0**.|
| value | number | Yes  | Block data to decrement. The value cannot be a negative number.|
| callback | AsyncCallback\<void> | Yes  | Callback invoked to return the result.|

**Error codes**

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

| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
1409 1410 1411 1412 1413 1414

**Example**

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

A
Annie_wang 已提交
1415 1416 1417 1418 1419 1420 1421
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.

// Connect to the tag if it is not connected.
if (!mifareClassic.isTagConnected()) {
    if (!mifareClassic.connectTag()) {
        console.log("mifareClassic connectTag failed.");
        return;
A
Annie_wang 已提交
1422
    }
A
Annie_wang 已提交
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
}

try {
    let blockIndex = 1; // Change it as required.
    let value = 0x20; // Change it as required.
    mifareClassic.decrementBlock(blockIndex, value, (err)=> {
        if (err) {
            console.log("mifareClassic decrementBlock AsyncCallback err: " + err);
        } else {
            console.log("mifareClassic decrementBlock AsyncCallback success.");
        }
    });
} catch (busiError) {
    console.log("mifareClassic decrementBlock AsyncCallback catch busiError: " + busiError);
}
A
Annie_wang 已提交
1438 1439 1440 1441
```

### MifareClassicTag.transferToBlock<sup>9+</sup>

A
Annie_wang 已提交
1442
transferToBlock(blockIndex: number): Promise\<void>
A
Annie_wang 已提交
1443

A
Annie_wang 已提交
1444
Transfers data from the temporary register to a block. This API uses a promise to return the result.
A
Annie_wang 已提交
1445 1446 1447

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

A
Annie_wang 已提交
1448
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
1449 1450 1451 1452 1453

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
1454
| blockIndex | number | Yes | Index of the destination block. The value starts form **0**.|
A
Annie_wang 已提交
1455

A
Annie_wang 已提交
1456
**Error codes**
A
Annie_wang 已提交
1457

A
Annie_wang 已提交
1458 1459 1460 1461 1462
For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).

| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
1463 1464 1465 1466 1467 1468

**Example**

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

A
Annie_wang 已提交
1469 1470 1471 1472 1473 1474 1475
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.

// Connect to the tag if it is not connected.
if (!mifareClassic.isTagConnected()) {
    if (!mifareClassic.connectTag()) {
        console.log("mifareClassic connectTag failed.");
        return;
A
Annie_wang 已提交
1476
    }
A
Annie_wang 已提交
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
}

try {
    let blockIndex = 1; // Change it as required.
    mifareClassic.transferToBlock(blockIndex).then(() => {
        console.log("mifareClassic transferToBlock Promise success.");
    }).catch((err)=> {
        console.log("mifareClassic transferToBlock Promise err: " + err);
    });
} catch (busiError) {
    console.log("mifareClassic transferToBlock Promise catch busiError: " + busiError);
}
A
Annie_wang 已提交
1489 1490
```

A
Annie_wang 已提交
1491
### MifareClassicTag.transferToBlock<sup>9+</sup>
A
Annie_wang 已提交
1492

A
Annie_wang 已提交
1493
transferToBlock(blockIndex: number, callback: AsyncCallback\<void>): void
A
Annie_wang 已提交
1494

A
Annie_wang 已提交
1495
Transfers data from the temporary register to a block. This API uses an asynchronous callback to return the result.
A
Annie_wang 已提交
1496 1497 1498

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

A
Annie_wang 已提交
1499
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
1500 1501 1502 1503 1504

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
| blockIndex | number | Yes  | Index of the destination block. The value starts form **0**.|
| callback | AsyncCallback\<void> | Yes  | Callback invoked to return the result.|

**Error codes**

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

| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
1515 1516 1517 1518 1519 1520

**Example**

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

A
Annie_wang 已提交
1521 1522 1523 1524 1525 1526 1527
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.

// Connect to the tag if it is not connected.
if (!mifareClassic.isTagConnected()) {
    if (!mifareClassic.connectTag()) {
        console.log("mifareClassic connectTag failed.");
        return;
A
Annie_wang 已提交
1528
    }
A
Annie_wang 已提交
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
}

try {
    let blockIndex = 1; // Change it as required.
    mifareClassic.transferToBlock(blockIndex, (err)=> {
        if (err) {
            console.log("mifareClassic transferToBlock AsyncCallback err: " + err);
        } else {
            console.log("mifareClassic transferToBlock AsyncCallback success.");
        }
    });
} catch (busiError) {
    console.log("mifareClassic transferToBlock AsyncCallback catch busiError: " + busiError);
}
A
Annie_wang 已提交
1543 1544 1545 1546
```

### MifareClassicTag.restoreFromBlock<sup>9+</sup>

A
Annie_wang 已提交
1547
restoreFromBlock(blockIndex: number): Promise\<void>
A
Annie_wang 已提交
1548

A
Annie_wang 已提交
1549
Restores data in the temporary register from a block. This API uses a promise to return the result.
A
Annie_wang 已提交
1550 1551 1552

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

A
Annie_wang 已提交
1553
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
1554 1555 1556 1557 1558

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
1559
| blockIndex | number | Yes  | Index of the target block. The value starts form **0**.|
A
Annie_wang 已提交
1560

A
Annie_wang 已提交
1561
**Error codes**
A
Annie_wang 已提交
1562

A
Annie_wang 已提交
1563 1564 1565 1566 1567
For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).

| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
1568 1569 1570 1571 1572 1573

**Example**

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

A
Annie_wang 已提交
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.

// Connect to the tag if it is not connected.
if (!mifareClassic.isTagConnected()) {
    if (!mifareClassic.connectTag()) {
        console.log("mifareClassic connectTag failed.");
        return;
    }
}

try {
    let blockIndex = 1; // Change it as required.
    mifareClassic.restoreFromBlock(blockIndex).then(() => {
        console.log("mifareClassic restoreFromBlock Promise success.");
A
Annie_wang 已提交
1588
    }).catch((err)=> {
A
Annie_wang 已提交
1589
        console.log("mifareClassic restoreFromBlock Promise err: " + err);
A
Annie_wang 已提交
1590
    });
A
Annie_wang 已提交
1591 1592 1593
} catch (busiError) {
    console.log("mifareClassic restoreFromBlock Promise catch busiError: " + busiError);
}
A
Annie_wang 已提交
1594 1595 1596 1597
```

### MifareClassicTag.restoreFromBlock<sup>9+</sup>

A
Annie_wang 已提交
1598
restoreFromBlock(blockIndex: number, callback: AsyncCallback\<void>): void
A
Annie_wang 已提交
1599

A
Annie_wang 已提交
1600
Restores data in the temporary register from a block. This API uses an asynchronous callback to return the result.
A
Annie_wang 已提交
1601 1602 1603

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

A
Annie_wang 已提交
1604
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
1605 1606 1607 1608 1609

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
| blockIndex | number | Yes  | Index of the target block. The value starts form **0**.|
| callback | AsyncCallback\<void> | Yes  | Callback invoked to return the result.|

**Error codes**

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

| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
1620 1621 1622 1623 1624 1625

**Example**

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

A
Annie_wang 已提交
1626 1627 1628 1629 1630 1631 1632
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.

// Connect to the tag if it is not connected.
if (!mifareClassic.isTagConnected()) {
    if (!mifareClassic.connectTag()) {
        console.log("mifareClassic connectTag failed.");
        return;
A
Annie_wang 已提交
1633
    }
A
Annie_wang 已提交
1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
}

try {
    let blockIndex = 1; // Change it as required.
    mifareClassic.restoreFromBlock(blockIndex, (err)=> {
        if (err) {
            console.log("mifareClassic restoreFromBlock AsyncCallback err: " + err);
        } else {
            console.log("mifareClassic restoreFromBlock AsyncCallback success.");
        }
    });
} catch (busiError) {
    console.log("mifareClassic restoreFromBlock AsyncCallback catch busiError: " + busiError);
}
A
Annie_wang 已提交
1648 1649 1650 1651 1652 1653 1654 1655
```

### MifareClassicTag.getSectorCount<sup>9+</sup>

getSectorCount(): number

Obtains the number of sectors in this MIFARE Classic tag.

A
Annie_wang 已提交
1656
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
| number | Number of sectors obtained.|

**Example**

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

A
Annie_wang 已提交
1669
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.
A
Annie_wang 已提交
1670 1671
let sectorCount = mifareClassic.getSectorCount();
console.log("mifareClassic sectorCount: " + sectorCount);
A
Annie_wang 已提交
1672 1673 1674 1675 1676 1677 1678 1679
```

### MifareClassicTag.getBlockCountInSector<sup>9+</sup>

getBlockCountInSector(sectorIndex: number): number

Obtains the number of blocks in a sector.

A
Annie_wang 已提交
1680
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
1681 1682 1683 1684 1685

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
1686
| sectorIndex | number | Yes  | Index of the target sector. The sector indexes start from **0**.|
A
Annie_wang 已提交
1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
| number | Number of blocks obtained.|

**Example**

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

A
Annie_wang 已提交
1699 1700 1701 1702 1703 1704 1705 1706 1707
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.

try {
    let sectorIndex = 1; // Change it as required.
    let blockCnt = mifareClassic.getBlockCountInSector(sectorIndex);
    console.log("mifareClassic blockCnt: " + blockCnt);
} catch (busiError) {
    console.log("mifareClassic getBlockCountInSector catch busiError: " + busiError);
}
A
Annie_wang 已提交
1708 1709 1710 1711
```

### MifareClassicTag.getType<sup>9+</sup>

A
Annie_wang 已提交
1712
getType(): [tag.MifareClassicType](js-apis-nfcTag.md#mifareclassictype9)
A
Annie_wang 已提交
1713 1714 1715

Obtains the type of this MIFARE Classic tag.

A
Annie_wang 已提交
1716
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
1717 1718 1719 1720 1721

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
A
Annie_wang 已提交
1722
| [tag.MifareClassicType](js-apis-nfcTag.md#mifareclassictype9) | Type of the MIFARE Classic tag obtained.|
A
Annie_wang 已提交
1723 1724 1725 1726 1727 1728

**Example**

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

A
Annie_wang 已提交
1729
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.
A
Annie_wang 已提交
1730 1731
let getType = mifareClassic.getType();
console.log("mifareClassic getType: " + getType);
A
Annie_wang 已提交
1732 1733 1734 1735 1736 1737
```

### MifareClassicTag.getTagSize<sup>9+</sup>

getTagSize(): number

A
Annie_wang 已提交
1738
Obtains the size of this tag. For details, see [MifareClassicSize](js-apis-nfcTag.md#mifareclassicsize9).
A
Annie_wang 已提交
1739

A
Annie_wang 已提交
1740
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
1741 1742 1743 1744 1745

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
A
Annie_wang 已提交
1746
| number | Tag size obtained, in bytes. For details, see [MifareClassicSize](js-apis-nfcTag.md#mifareclassicsize9).|
A
Annie_wang 已提交
1747 1748 1749 1750 1751 1752

**Example**

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

A
Annie_wang 已提交
1753
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.
A
Annie_wang 已提交
1754 1755
let tagSize = mifareClassic.getTagSize();
console.log("mifareClassic tagSize: " + tagSize);
A
Annie_wang 已提交
1756 1757 1758 1759 1760 1761
```

### MifareClassicTag.isEmulatedTag<sup>9+</sup>

isEmulatedTag(): boolean

A
Annie_wang 已提交
1762
Checks whether it is an emulated tag.
A
Annie_wang 已提交
1763

A
Annie_wang 已提交
1764
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
| boolean |Returns **true** if the tag is an emulated tag; returns **false** otherwise.|

**Example**

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

A
Annie_wang 已提交
1777
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.
A
Annie_wang 已提交
1778 1779
let isEmulatedTag = mifareClassic.isEmulatedTag();
console.log("mifareClassic isEmulatedTag: " + isEmulatedTag);
A
Annie_wang 已提交
1780 1781 1782 1783 1784 1785
```

### MifareClassicTag.getBlockIndex<sup>9+</sup>

getBlockIndex(sectorIndex: number): number

A
Annie_wang 已提交
1786
Obtains the index of the first block in a sector.
A
Annie_wang 已提交
1787

A
Annie_wang 已提交
1788
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
1789 1790 1791 1792 1793

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
1794
| sectorIndex | number | Yes  | Index of the target sector. The sector indexes start from **0**.|
A
Annie_wang 已提交
1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
| number | Index of the first block obtained.|

**Example**

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

A
Annie_wang 已提交
1807 1808 1809 1810 1811 1812 1813 1814 1815
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.

try {
    let sectorIndex = 1; // Change it as required.
    let blockIndex = mifareClassic.getBlockIndex(sectorIndex);
    console.log("mifareClassic blockIndex: " + blockIndex);
} catch (busiError) {
    console.log("mifareClassic getBlockIndex catch busiError: " + busiError);
}
A
Annie_wang 已提交
1816 1817 1818 1819 1820 1821
```

### MifareClassicTag.getSectorIndex<sup>9+</sup>

getSectorIndex(blockIndex: number): number

A
Annie_wang 已提交
1822
Obtains the index of the sector that holds the specified block.
A
Annie_wang 已提交
1823

A
Annie_wang 已提交
1824
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
1825 1826 1827 1828 1829

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
1830
| blockIndex | number | Yes| Index of the block. The block indexes start from **0**.|
A
Annie_wang 已提交
1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
| number | Index of the sector obtained.|

**Example**

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

A
Annie_wang 已提交
1843 1844 1845 1846 1847 1848 1849 1850 1851
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly.

try {
    let blockIndex = 1; // Change it as required.
    let sectorIndex = mifareClassic.getSectorIndex(blockIndex);
    console.log("mifareClassic sectorIndex: " + sectorIndex);
} catch (busiError) {
    console.log("mifareClassic getSectorIndex catch busiError: " + busiError);
}
A
Annie_wang 已提交
1852 1853 1854 1855
```

## MifareUltralightTag<sup>9+</sup>

A
Annie_wang 已提交
1856
Provides APIs to access MIFARE Ultralight properties and perform I/O operations on a tag. This class inherits from **TagSession**.
A
Annie_wang 已提交
1857 1858 1859

**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).

A
Annie_wang 已提交
1860
The following describes the unique APIs of **MifareUltralightTag**.
A
Annie_wang 已提交
1861 1862 1863

### MifareUltralightTag.readMultiplePages<sup>9+</sup>

A
Annie_wang 已提交
1864
readMultiplePages(pageIndex: number): Promise\<number[]>
A
Annie_wang 已提交
1865

A
Annie_wang 已提交
1866
Reads four pages of data (4 bytes per page) from this tag. This API uses a promise to return the result.
A
Annie_wang 已提交
1867 1868 1869

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

A
Annie_wang 已提交
1870
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
1871 1872 1873 1874 1875

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | ------------------------------ |
A
Annie_wang 已提交
1876
| pageIndex | number | Yes  | Index of the first page to read. The page indexes start from **0**.|
A
Annie_wang 已提交
1877 1878 1879 1880 1881

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
A
Annie_wang 已提交
1882 1883 1884 1885 1886 1887 1888 1889 1890
| Promise\<number[]> | Promise used to return the data read, which is 16 bytes in total.|

**Error codes**

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

| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
1891 1892 1893 1894 1895 1896 1897

**Example**

```js

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

A
Annie_wang 已提交
1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareUltralight' correctly.

// Connect to the tag if it is not connected.
if (!mifareUltralight.isTagConnected()) {
    if (!mifareUltralight.connectTag()) {
        console.log("mifareUltralight connectTag failed.");
        return;
    }
}

try {
    let pageIndex = 1; // Change it as required.
    mifareUltralight.readMultiplePages(pageIndex).then((data) => {
        console.log("mifareUltralight readMultiplePages Promise data = " + data);
A
Annie_wang 已提交
1912
    }).catch((err)=> {
A
Annie_wang 已提交
1913
        console.log("mifareUltralight readMultiplePages Promise err: " + err);
A
Annie_wang 已提交
1914
    });
A
Annie_wang 已提交
1915 1916 1917
} catch (busiError) {
    console.log("mifareUltralight readMultiplePages Promise catch busiError: " + busiError);
}
A
Annie_wang 已提交
1918 1919 1920 1921
```

### MifareUltralightTag.readMultiplePages<sup>9+</sup>

A
Annie_wang 已提交
1922
readMultiplePages(pageIndex: number, callback: AsyncCallback\<number[]>): void
A
Annie_wang 已提交
1923

A
Annie_wang 已提交
1924
Reads four pages of data (4 bytes per page) from this tag. This API uses an asynchronous callback to return the result.
A
Annie_wang 已提交
1925 1926 1927

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

A
Annie_wang 已提交
1928
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
1929 1930 1931 1932 1933

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
| pageIndex | number | Yes  | Index of the first page to read. The page indexes start from **0**.|
| callback | AsyncCallback\<number[]> | Yes  | Callback invoked to return the data read, which is 16 bytes in total.|

**Error codes**

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

| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
1944 1945 1946 1947 1948 1949

**Example**

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

A
Annie_wang 已提交
1950 1951 1952 1953 1954 1955 1956
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareUltralight' correctly.

// Connect to the tag if it is not connected.
if (!mifareUltralight.isTagConnected()) {
    if (!mifareUltralight.connectTag()) {
        console.log("mifareUltralight connectTag failed.");
        return;
A
Annie_wang 已提交
1957
    }
A
Annie_wang 已提交
1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
}

try {
    let pageIndex = 1; // Change it as required.
    mifareUltralight.readMultiplePages(pageIndex, (err, data)=> {
        if (err) {
            console.log("mifareUltralight readMultiplePages AsyncCallback err: " + err);
        } else {
            console.log("mifareUltralight readMultiplePages AsyncCallback data: " + data);
        }
    });
} catch (busiError) {
    console.log("mifareUltralight readMultiplePages AsyncCallback catch busiError: " + busiError);
}
A
Annie_wang 已提交
1972 1973
```

A
Annie_wang 已提交
1974
### MifareUltralightTag.writeSinglePage<sup>9+</sup>
A
Annie_wang 已提交
1975

A
Annie_wang 已提交
1976
writeSinglePage(pageIndex: number, data: number[]): Promise\<void>
A
Annie_wang 已提交
1977

A
Annie_wang 已提交
1978
Writes one page (4 bytes) of data to this tag. This API uses a promise to return the result.
A
Annie_wang 已提交
1979 1980 1981

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

A
Annie_wang 已提交
1982
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
1983 1984 1985 1986 1987

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
1988 1989
| pageIndex | number | Yes  | Index of the page to write. The page indexes start from **0**.|
| data | number[] | Yes  | 4-byte data to write.|
A
Annie_wang 已提交
1990

A
Annie_wang 已提交
1991
**Error codes**
A
Annie_wang 已提交
1992

A
Annie_wang 已提交
1993 1994 1995 1996 1997
For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).

| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
1998 1999 2000 2001 2002 2003

**Example**

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

A
Annie_wang 已提交
2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareUltralight' correctly.

// Connect to the tag if it is not connected.
if (!mifareUltralight.isTagConnected()) {
    if (!mifareUltralight.connectTag()) {
        console.log("mifareUltralight connectTag failed.");
        return;
    }
}

try {
    let pageIndex = 1; // Change it as required.
    let rawData = [0x01, 0x02, 0x03, 0x04]; // MUST be 4 bytes, change it to be correct raw data.
    mifareUltralight.writeSinglePage(pageIndex, rawData).then(() => {
        console.log("mifareUltralight writeSinglePage Promise success.");
A
Annie_wang 已提交
2019
    }).catch((err)=> {
A
Annie_wang 已提交
2020
        console.log("mifareUltralight writeSinglePage Promise err: " + err);
A
Annie_wang 已提交
2021
    });
A
Annie_wang 已提交
2022 2023 2024
} catch (busiError) {
    console.log("mifareUltralight writeSinglePage Promise catch busiError: " + busiError);
}
A
Annie_wang 已提交
2025 2026
```

A
Annie_wang 已提交
2027
### MifareUltralightTag.writeSinglePage<sup>9+</sup>
A
Annie_wang 已提交
2028

A
Annie_wang 已提交
2029
writeSinglePage(pageIndex: number, data: number[], callback: AsyncCallback\<void>): void
A
Annie_wang 已提交
2030

A
Annie_wang 已提交
2031
Writes one page (4 bytes) of data to this tag. This API uses an asynchronous callback to return the result.
A
Annie_wang 已提交
2032 2033 2034

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

A
Annie_wang 已提交
2035
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
2036 2037 2038 2039 2040

**Parameters**

| Name  | Type                   | Mandatory| Description                    |
| -------- | ----------------------- | ---- | ------------------------ |
A
Annie_wang 已提交
2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051
| pageIndex | number | Yes  | Index of the page to write. The page indexes start from **0**.|
| data | number[] | Yes  | 4-byte data to write.|
| callback|AsyncCallback\<void> |Yes| Callback invoked to return the result.|

**Error codes**

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

| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
2052 2053 2054 2055 2056 2057

**Example**

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

A
Annie_wang 已提交
2058 2059 2060 2061 2062 2063 2064
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareUltralight' correctly.

// Connect to the tag if it is not connected.
if (!mifareUltralight.isTagConnected()) {
    if (!mifareUltralight.connectTag()) {
        console.log("mifareUltralight connectTag failed.");
        return;
A
Annie_wang 已提交
2065
    }
A
Annie_wang 已提交
2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080
}

try {
    let pageIndex = 1; // Change it as required.
    let rawData = [0x01, 0x02, 0x03, 0x04]; // MUST be 4 bytes, change it to be correct raw data.
    mifareUltralight.writeSinglePage(pageIndex, rawData, (err)=> {
        if (err) {
            console.log("mifareUltralight writeSinglePage AsyncCallback err: " + err);
        } else {
            console.log("mifareUltralight writeSinglePage AsyncCallback success.");
        }
    });
} catch (busiError) {
    console.log("mifareUltralight writeSinglePage AsyncCallback catch busiError: " + busiError);
}
A
Annie_wang 已提交
2081 2082 2083 2084
```

### MifareUltralightTag.getType<sup>9+</sup>

A
Annie_wang 已提交
2085
getType(): [tag.MifareUltralightType](js-apis-nfcTag.md#mifareultralighttype9)
A
Annie_wang 已提交
2086

A
Annie_wang 已提交
2087
Obtains the type of this MIFARE Ultralight tag.
A
Annie_wang 已提交
2088

A
Annie_wang 已提交
2089
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
2090 2091 2092 2093 2094

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
A
Annie_wang 已提交
2095
| [tag.MifareUltralightType](js-apis-nfcTag.md#mifareultralighttype9) | Type of the MIFARE Ultralight tag obtained.|
A
Annie_wang 已提交
2096 2097 2098 2099 2100 2101

**Example**

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

A
Annie_wang 已提交
2102
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareUltralight' correctly.
A
Annie_wang 已提交
2103 2104
let getType = mifareClassic.getType();
console.log("mifareUltralight getType: " + getType);
A
Annie_wang 已提交
2105 2106 2107 2108
```

## NdefFormatableTag<sup>9+</sup>

A
Annie_wang 已提交
2109
Provides APIs for formatting NDEF formattable tags. This class inherits from **TagSession**.
A
Annie_wang 已提交
2110 2111 2112

**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).

A
Annie_wang 已提交
2113
The following describes the unique APIs of **NdefFormatableTag**.
A
Annie_wang 已提交
2114 2115 2116

### NdefFormatableTag.format<sup>9+</sup>

A
Annie_wang 已提交
2117
format(message: [NdefMessage](#ndefmessage9)): Promise\<void>
A
Annie_wang 已提交
2118

A
Annie_wang 已提交
2119
Formats this tag as an NDEF tag, and writes an NDEF message to it. This API uses a promise to return the result.
A
Annie_wang 已提交
2120 2121 2122

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

A
Annie_wang 已提交
2123
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
2124 2125 2126 2127 2128

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
2129
| message | [NdefMessage](#ndefmessage9) | Yes  | NDEF message to write. If this parameter is **null**, the tag is formatted only (no data will be written).|
A
Annie_wang 已提交
2130

A
Annie_wang 已提交
2131
**Error codes**
A
Annie_wang 已提交
2132

A
Annie_wang 已提交
2133 2134 2135 2136 2137
For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).

| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
2138 2139 2140 2141 2142 2143

**Example**

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

A
Annie_wang 已提交
2144
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefFormatable' correctly.
A
Annie_wang 已提交
2145

A
Annie_wang 已提交
2146 2147 2148 2149 2150
// Connect to the tag if it is not connected.
if (!ndefFormatable.isTagConnected()) {
    if (!ndefFormatable.connectTag()) {
        console.log("ndefFormatable connectTag failed.");
        return;
A
Annie_wang 已提交
2151
    }
A
Annie_wang 已提交
2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166
}

try {
    // NDEF message created from raw data, such as:
    let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]);  // It must be parsed as NDEF Record.
    // or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[])

    ndefFormatable.format(ndefMessage).then(() => {
        console.log("ndefFormatable format Promise success.");
    }).catch((err)=> {
        console.log("ndefFormatable format Promise err: " + err);
    });
} catch (busiError) {
    console.log("ndefFormatable format Promise catch busiError: " + busiError);
}
A
Annie_wang 已提交
2167 2168 2169 2170
```

### NdefFormatableTag.format<sup>9+</sup>

A
Annie_wang 已提交
2171
format(message: [NdefMessage](#ndefmessage9), callback: AsyncCallback\<void>): void
A
Annie_wang 已提交
2172

A
Annie_wang 已提交
2173
Formats this tag as an NDEF tag, and writes an NDEF message to it. This API uses an asynchronous callback to return the result.
A
Annie_wang 已提交
2174 2175 2176

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

A
Annie_wang 已提交
2177
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
2178 2179 2180 2181 2182

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
2183 2184 2185 2186 2187 2188
| 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).|

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
A
Annie_wang 已提交
2189
| callback: AsyncCallback\<void> | Callback invoked to return the result.|
A
Annie_wang 已提交
2190

A
Annie_wang 已提交
2191 2192 2193 2194 2195 2196 2197
**Error codes**

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

| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
2198 2199 2200 2201 2202 2203

**Example**

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

A
Annie_wang 已提交
2204
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefFormatable' correctly.
A
Annie_wang 已提交
2205

A
Annie_wang 已提交
2206 2207 2208 2209 2210
// Connect to the tag if it is not connected.
if (!ndefFormatable.isTagConnected()) {
    if (!ndefFormatable.connectTag()) {
        console.log("ndefFormatable connectTag failed.");
        return;
A
Annie_wang 已提交
2211
    }
A
Annie_wang 已提交
2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228
}

try {
    // NDEF message created from raw data, such as:
    let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]);  // It must be parsed as NDEF Record.
    // or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[])

    ndefFormatable.format(ndefMessage, (err)=> {
        if (err) {
            console.log("ndefFormatable format AsyncCallback err: " + err);
        } else {
            console.log("ndefFormatable format AsyncCallback success.");
        }
    });
} catch (busiError) {
    console.log("ndefFormatable format AsyncCallback catch busiError: " + busiError);
}
A
Annie_wang 已提交
2229 2230 2231 2232
```

### NdefFormatableTag.formatReadOnly<sup>9+</sup>

A
Annie_wang 已提交
2233
formatReadOnly(message: [NdefMessage](#ndefmessage9)): Promise\<void>
A
Annie_wang 已提交
2234

A
Annie_wang 已提交
2235
Formats 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.
A
Annie_wang 已提交
2236 2237 2238

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

A
Annie_wang 已提交
2239
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
2240 2241 2242 2243 2244

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
2245
| message | [NdefMessage](#ndefmessage9) | Yes  | NDEF message to write. If this parameter is **null**, the tag is formatted only (no data will be written).|
A
Annie_wang 已提交
2246

A
Annie_wang 已提交
2247
**Error codes**
A
Annie_wang 已提交
2248

A
Annie_wang 已提交
2249 2250 2251 2252 2253
For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md).

| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
2254 2255 2256 2257 2258 2259

**Example**

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

A
Annie_wang 已提交
2260
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefFormatable' correctly.
A
Annie_wang 已提交
2261

A
Annie_wang 已提交
2262 2263 2264 2265 2266
// Connect to the tag if it is not connected.
if (!ndefFormatable.isTagConnected()) {
    if (!ndefFormatable.connectTag()) {
        console.log("ndefFormatable connectTag failed.");
        return;
A
Annie_wang 已提交
2267
    }
A
Annie_wang 已提交
2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282
}

try {
    // NDEF message created from raw data, such as:
    let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]);  // It must be parsed as NDEF Record.
    // or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[])

    ndefFormatable.formatReadOnly(ndefMessage).then(() => {
        console.log("ndefFormatable formatReadOnly Promise success.");
    }).catch((err)=> {
        console.log("ndefFormatable formatReadOnly Promise err: " + err);
    });
} catch (busiError) {
    console.log("ndefFormatable formatReadOnly Promise catch busiError: " + busiError);
}
A
Annie_wang 已提交
2283 2284 2285 2286
```

### NdefFormatableTag.formatReadOnly<sup>9+</sup>

A
Annie_wang 已提交
2287
formatReadOnly(message: [NdefMessage](#ndefmessage9), callback: AsyncCallback\<void>): void
A
Annie_wang 已提交
2288 2289 2290 2291 2292

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

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

A
Annie_wang 已提交
2293
**System capability**: SystemCapability.Communication.NFC.Tag
A
Annie_wang 已提交
2294 2295 2296 2297 2298

**Parameters**

| Name  | Type                   | Mandatory| Description                                  |
| -------- | ----------------------- | ---- | -------------------------------------- |
A
Annie_wang 已提交
2299
| message | [NdefMessage](#ndefmessage9) | Yes  | NDEF message to write. If this parameter is **null**, the tag is formatted only (no data will be written).|
A
Annie_wang 已提交
2300 2301 2302 2303 2304

**Return value**

| **Type**| **Description**                            |
| ------------------ | --------------------------|
A
Annie_wang 已提交
2305
| callback: AsyncCallback\<void> | Callback invoked to return the result.|
A
Annie_wang 已提交
2306

A
Annie_wang 已提交
2307 2308 2309 2310 2311 2312 2313
**Error codes**

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

| ID| Error Message|
| ------- | -------|
| 3100201 | Tag running state is abnormal in service. |
A
Annie_wang 已提交
2314 2315 2316 2317 2318 2319

**Example**

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

A
Annie_wang 已提交
2320
// Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefFormatable' correctly.
A
Annie_wang 已提交
2321

A
Annie_wang 已提交
2322 2323 2324 2325 2326
// Connect to the tag if it is not connected.
if (!ndefFormatable.isTagConnected()) {
    if (!ndefFormatable.connectTag()) {
        console.log("ndefFormatable connectTag failed.");
        return;
A
Annie_wang 已提交
2327
    }
A
Annie_wang 已提交
2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344
}

try {
    // NDEF message created from raw data, such as:
    let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]);  // It must be parsed as NDEF Record.
    // or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[])

    ndefFormatable.formatReadOnly(ndefMessage, (err)=> {
        if (err) {
            console.log("ndefFormatable formatReadOnly AsyncCallback err: " + err);
        } else {
            console.log("ndefFormatable formatReadOnly AsyncCallback success.");
        }
    });
} catch (busiError) {
    console.log("ndefFormatable formatReadOnly AsyncCallback catch busiError: " + busiError);
}
A
Annie_wang 已提交
2345
```