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