1# @ohos.nfc.tag (标准NFC-Tag) 2 3本模块主要用于操作及管理NFC Tag。 4 5> **说明:** 6> 7> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9## **声明技术** 10 11开发Tag读写相关应用时,需要在应用的属性配置文件中,声明与NFC相关的属性值,比如,在module.json5文件中,声明下面属性值: 12```js 13{ 14 "module": { 15 // other declared attributes. 16 17 "abilities": [ 18 { 19 "skills": [ 20 { 21 "actions": [ 22 // other declared actions, 23 24 // add the nfc tag action 25 "ohos.nfc.tag.action.TAG_FOUND" 26 ] 27 } 28 ], 29 "metadata": [ 30 { 31 "name": "tag-tech", 32 "value": "NfcA" 33 }, 34 { 35 "name": "tag-tech", 36 "value": "IsoDep" 37 }, 38 // add other technology if neccessary, 39 // such as: NfcB/NfcF/NfcV/Ndef/MifareClassic/MifareUL/NdefFormatable 40 ] 41 } 42 ], 43 "requestPermissions": [ 44 "name": "ohos.permission.NFC_TAG", 45 "reason": "tag", 46 ] 47 } 48} 49``` 50> **注意:** 511. 声明"actions"字段的内容填写,必须是"ohos.nfc.tag.action.TAG_FOUND",不能更改。 522. 声明技术时"metadata"中的"name"字段的内容填写,必须是"tag-tech",不能更改。 533. 声明技术时"metadata"中的"value"字段的内容填写,必须是"NfcA/NfcB/NfcF/NfcV/IsoDep/Ndef/MifareClassic/MifareUL/NdefFormatable"中的一个或多个。填写错误会造成解析失败。 544. 声明权限时"requestPermissions"中的"name"字段的内容填写,必须是"ohos.permission.NFC_TAG",不能更改。 55 56## **导入模块** 57 58```js 59import tag from '@ohos.nfc.tag'; 60``` 61 62## **tag.TagInfo** 63 64在对相关Tag类型卡片进行读写之前,必须先获取TagInfo相关属性值,以确认设备读取到的Tag卡片支持哪些技术类型。这样Tag应用程序才能调用正确的接口和所读取到的Tag卡片进行通信。 65```js 66import tag from '@ohos.nfc.tag'; 67 68onCreate(want, launchParam) { 69 // add other code here... 70 71 // want is initialized by nfc service, contains tag info for this found tag 72 var tagInfo; 73 try { 74 tagInfo = tag.getTagInfo(want); 75 } catch (error) { 76 console.log("tag.getTagInfo catched error: " + error); 77 } 78 if (tagInfo == null || tagInfo == undefined) { 79 console.log("no TagInfo to be created, ignore it."); 80 return; 81 } 82 83 // get the supported technologies for this found tag. 84 var isNfcATag = false; 85 var isIsoDepTag = false; 86 for (var i = 0; i < tagInfo.technology.length; i++) { 87 if (tagInfo.technology[i] == tag.NFC_A) { 88 isNfcATag = true; 89 } 90 91 if (tagInfo.technology[i] == tag.ISO_DEP) { 92 isIsoDepTag = true; 93 } 94 // also check for technology: tag.NFC_B/NFC_F/NFC_V/NDEF/MIFARE_CLASSIC/MIFARE_ULTRALIGHT/NDEF_FORMATABLE 95 } 96 97 // use NfcA APIs to access the found tag. 98 if (isNfcATag) { 99 var nfcA; 100 try { 101 nfcA = tag.getNfcATag(tagInfo); 102 } catch (error) { 103 console.log("tag.getNfcATag catched error: " + error); 104 } 105 // other code to read or write this found tag. 106 } 107 108 // use getIsoDep APIs to access the found tag. 109 if (isIsoDepTag) { 110 var isoDep; 111 try { 112 isoDep = tag.getIsoDep(tagInfo); 113 } catch (error) { 114 console.log("tag.getIsoDep catched error: " + error); 115 } 116 // other code to read or write this found tag. 117 } 118 119 // use the same code to handle for "NfcA/NfcB/NfcF/NfcV/Ndef/MifareClassic/MifareUL/NdefFormatable". 120} 121``` 122 123## tag.getNfcATag 124 125getNfcATag(tagInfo: [TagInfo](#taginfo)): [NfcATag](js-apis-nfctech.md#nfcatag) 126 127获取NFC A类型Tag对象,通过该对象可访问NfcA技术类型的Tag。 128 129> **说明:** 130> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用[tag.getNfcA](#taggetnfca9)替代。 131 132**系统能力:** SystemCapability.Communication.NFC.Tag 133 134**参数:** 135 136| 参数名 | 类型 | 必填 | 说明 | 137| --------- | ------------------------- | ---- | ---------------------------------------- | 138| taginfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从tag.getTagInfo(want: Want)获取。 139 140**返回值:** 141 142| **类型** | **说明** | 143| ------------------------------------- | ------------- | 144| [NfcATag](js-apis-nfctech.md#nfcatag) | NFC A类型Tag对象。 | 145 146## tag.getNfcA<sup>9+</sup> 147 148getNfcA(tagInfo: [TagInfo](#taginfo)): [NfcATag](js-apis-nfctech.md#nfcatag) 149 150获取NFC A类型Tag对象,通过该对象可访问NfcA技术类型的Tag。 151 152**系统能力:** SystemCapability.Communication.NFC.Tag 153 154**参数:** 155 156| 参数名 | 类型 | 必填 | 说明 | 157| --------- | ------------------------- | ---- | ---------------------------------------- | 158| taginfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从tag.getTagInfo(want: Want)获取。 159 160**返回值:** 161 162| **类型** | **说明** | 163| ------------------------------------- | ------------- | 164| [NfcATag](js-apis-nfctech.md#nfcatag) | NFC A类型Tag对象。 | 165 166**错误码:** 167 168以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 169 170| 错误码ID | 错误信息 | 171| ------- | ---------------------------------------- | 172| 3100201 | Tag running state is abnormal in service. | 173 174## tag.getNfcBTag 175 176getNfcBTag(tagInfo: [TagInfo](#taginfo)): [NfcBTag](js-apis-nfctech.md#nfcbtag) 177 178获取NFC B类型Tag对象,通过该对象可访问NfcB技术类型的Tag。 179 180> **说明:** 181> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用[tag.getNfcB](#taggetnfcb9)替代。 182 183**系统能力:** SystemCapability.Communication.NFC.Tag 184 185**参数:** 186 187| 参数名 | 类型 | 必填 | 说明 | 188| --------- | ------------------------- | ---- | ---------------------------------------- | 189| taginfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从tag.getTagInfo(want: Want)获取。 190 191**返回值:** 192 193| **类型** | **说明** | 194| ------------------------------------- | ------------- | 195| [NfcBTag](js-apis-nfctech.md#nfcbtag) | NFC B类型Tag对象。 | 196 197## tag.getNfcB<sup>9+</sup> 198 199getNfcB(tagInfo: [TagInfo](#taginfo)): [NfcBTag](js-apis-nfctech.md#nfcbtag) 200 201获取NFC B类型Tag对象,通过该对象可访问NfcB技术类型的Tag。 202 203**系统能力:** SystemCapability.Communication.NFC.Tag 204 205**参数:** 206 207| 参数名 | 类型 | 必填 | 说明 | 208| --------- | ------------------------- | ---- | ---------------------------------------- | 209| taginfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从tag.getTagInfo(want: Want)获取。 210 211**返回值:** 212 213| **类型** | **说明** | 214| ------------------------------------- | ------------- | 215| [NfcBTag](js-apis-nfctech.md#nfcbtag) | NFC B类型Tag对象。 | 216 217**错误码:** 218 219以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 220 221| 错误码ID | 错误信息 | 222| ------- | ---------------------------------------- | 223| 3100201 | Tag running state is abnormal in service. | 224 225## tag.getNfcFTag 226 227getNfcFTag(tagInfo: [TagInfo](#taginfo)): [NfcFTag](js-apis-nfctech.md#nfcftag) 228 229获取NFC F类型Tag对象,通过该对象可访问NfcF技术类型的Tag。 230 231> **说明:** 232> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用[tag.getNfcF](#taggetnfcf9)替代。 233 234**系统能力:** SystemCapability.Communication.NFC.Tag 235 236**参数:** 237 238| 参数名 | 类型 | 必填 | 说明 | 239| --------- | ------------------------- | ---- | ---------------------------------------- | 240| taginfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从tag.getTagInfo(want: Want)获取。 241 242**返回值:** 243 244| **类型** | **说明** | 245| ------------------------------------- | ------------- | 246| [NfcFTag](js-apis-nfctech.md#nfcftag) | NFC F类型Tag对象。 | 247 248## tag.getNfcF<sup>9+</sup> 249 250getNfcF(tagInfo: [TagInfo](#taginfo)): [NfcFTag](js-apis-nfctech.md#nfcftag) 251 252获取NFC F类型Tag对象,通过该对象可访问NfcF技术类型的Tag。 253 254**系统能力:** SystemCapability.Communication.NFC.Tag 255 256**参数:** 257 258| 参数名 | 类型 | 必填 | 说明 | 259| --------- | ------------------------- | ---- | ---------------------------------------- | 260| taginfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从tag.getTagInfo(want: Want)获取。 261 262**返回值:** 263 264| **类型** | **说明** | 265| ------------------------------------- | ------------- | 266| [NfcFTag](js-apis-nfctech.md#nfcftag) | NFC F类型Tag对象。 | 267 268**错误码:** 269 270以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 271 272| 错误码ID | 错误信息 | 273| ------- | ---------------------------------------- | 274| 3100201 | Tag running state is abnormal in service. | 275 276## tag.getNfcVTag 277 278getNfcVTag(tagInfo: [TagInfo](#taginfo)): [NfcVTag](js-apis-nfctech.md#nfcvtag) 279 280获取NFC V类型Tag对象,通过该对象可访问NfcV技术类型的Tag。 281 282> **说明:** 283> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用[tag.getNfcV](#taggetnfcv9)替代。 284 285**系统能力:** SystemCapability.Communication.NFC.Tag 286 287**参数:** 288 289| 参数名 | 类型 | 必填 | 说明 | 290| --------- | ------------------------- | ---- | ---------------------------------------- | 291| taginfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从tag.getTagInfo(want: Want)获取。 292 293**返回值:** 294 295| **类型** | **说明** | 296| ------------------------------------- | ------------- | 297| [NfcVTag](js-apis-nfctech.md#nfcvtag) | NFC V类型Tag对象。 | 298 299## tag.getNfcV<sup>9+</sup> 300 301getNfcV(tagInfo: [TagInfo](#taginfo)): [NfcVTag](js-apis-nfctech.md#nfcvtag) 302 303获取NFC V类型Tag对象,通过该对象可访问NfcV技术类型的Tag。 304 305**系统能力:** SystemCapability.Communication.NFC.Tag 306 307**参数:** 308 309| 参数名 | 类型 | 必填 | 说明 | 310| --------- | ------------------------- | ---- | ---------------------------------------- | 311| taginfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从tag.getTagInfo(want: Want)获取。 312 313**返回值:** 314 315| **类型** | **说明** | 316| ------------------------------------- | ------------- | 317| [NfcVTag](js-apis-nfctech.md#nfcvtag) | NFC V类型Tag对象。 | 318 319**错误码:** 320 321以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 322 323| 错误码ID | 错误信息 | 324| ------- | ---------------------------------------- | 325| 3100201 | Tag running state is abnormal in service. | 326 327## tag.getIsoDep<sup>9+</sup> 328 329getIsoDep(tagInfo: [TagInfo](#taginfo)): [IsoDepTag](js-apis-nfctech.md#isoDepTag9 ) 330 331获取IsoDep类型Tag对象,通过该对象可访问支持IsoDep技术类型的Tag。 332 333**系统能力:** SystemCapability.Communication.NFC.Tag 334 335**参数:** 336 337| 参数名 | 类型 | 必填 | 说明 | 338| ------- | ------------------- | ---- | ---------------------------------------- | 339| taginfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从tag.getTagInfo(want: Want)获取。 | 340 341**返回值:** 342 343| **类型** | **说明** | 344| ---------------------------------------- | ----------------------------------- | 345| [IsoDepTag](js-apis-nfctech.md#isodeptag9) | IsoDep类型Tag对象,通过该对象访问IsoDep类型的相关接口。 | 346 347**错误码:** 348 349以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 350 351| 错误码ID | 错误信息 | 352| ------- | ---------------------------------------- | 353| 3100201 | Tag running state is abnormal in service. | 354 355## tag.getNdef<sup>9+</sup> 356 357getNdef(tagInfo: [TagInfo](#taginfo)): [NdefTag](js-apis-nfctech.md#ndeftag9) 358 359获取NDEF类型Tag对象,通过该对象可访问支持NDEF技术类型的Tag。 360 361**系统能力:** SystemCapability.Communication.NFC.Tag 362 363**参数:** 364 365| 参数名 | 类型 | 必填 | 说明 | 366| ------- | ------------------- | ---- | ---------------------------------------- | 367| taginfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从tag.getTagInfo(want: Want)获取。 | 368 369**返回值:** 370 371| **类型** | **说明** | 372| -------------------------------------- | ------------------------------- | 373| [NdefTag](js-apis-nfctech.md#ndeftag9) | NDEF类型Tag对象,通过该对象访问NDEF类型的相关接口。 | 374 375**错误码:** 376 377以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 378 379| 错误码ID | 错误信息 | 380| ------- | ---------------------------------------- | 381| 3100201 | Tag running state is abnormal in service. | 382 383## tag.getMifareClassic<sup>9+</sup> 384 385getMifareClassic(tagInfo: [TagInfo](#taginfo)): [MifareClassicTag](js-apis-nfctech.md#mifareclassictag-9) 386 387获取MIFARE Classic类型Tag对象,通过该对象访问支持MIFARE Classic技术类型的Tag。 388 389**系统能力:** SystemCapability.Communication.NFC.Tag 390 391**参数:** 392 393| 参数名 | 类型 | 必填 | 说明 | 394| ------- | ------------------- | ---- | ---------------------------------------- | 395| taginfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从tag.getTagInfo(want: Want)获取。 | 396 397**返回值:** 398 399| **类型** | **说明** | 400| ---------------------------------------- | ---------------------------------------- | 401| [MifareClassicTag](js-apis-nfctech.md#mifareclassictag-9) | MIFARE Classic类型Tag对象,通过该对象访问MIFARE Classic类型的相关接口。 | 402 403**错误码:** 404 405以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 406 407| 错误码ID | 错误信息 | 408| ------- | ---------------------------------------- | 409| 3100201 | Tag running state is abnormal in service. | 410 411## tag.getMifareUltralight<sup>9+</sup> 412 413getMifareUltralight(tagInfo: [TagInfo](#taginfo)): [MifareUltralightTag](js-apis-nfctech.md#mifareultralighttag9) 414 415获取MIFARE Ultralight类型Tag对象,通过该对象可访问支持MIFARE Ultralight技术类型的Tag。 416 417**系统能力:** SystemCapability.Communication.NFC.Tag 418 419**参数:** 420| 参数名 | 类型 | 必填 | 说明 | 421| ------- | ------------------- | ---- | ---------------------------------------- | 422| taginfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从tag.getTagInfo(want: Want)获取。 | 423 424**返回值:** 425 426| **类型** | **说明** | 427| ---------------------------------------- | ---------------------------------------- | 428| [MifareUltralightTag](js-apis-nfctech.md#mifareultralighttag9) | MIFARE Ultralight类型Tag对象,通过该对象访问MIFARE Ultralight类型的相关接口。 | 429 430**错误码:** 431 432以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 433 434| 错误码ID | 错误信息 | 435| ------- | ---------------------------------------- | 436| 3100201 | Tag running state is abnormal in service. | 437 438## tag.getNdefFormatable<sup>9+</sup> 439 440getNdefFormatable(tagInfo: [TagInfo](#taginfo)): [NdefFormatableTag](js-apis-nfctech.md#ndefformatabletag9) 441 442获取NDEF Formatable类型Tag对象,通过该对象可访问支持NDEF Formatable技术类型的Tag。 443 444**系统能力:** SystemCapability.Communication.NFC.Tag 445 446**返回值:** 447 448| **类型** | **说明** | 449| ---------------------------------------- | ---------------------------------------- | 450| [NdefFormatableTag](js-apis-nfctech.md#ndefformatabletag) | NDEF Formatable类型Tag对象,通过该对象访问NDEF Formatable类型的相关接口。 | 451 452**错误码:** 453 454以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。 455 456| 错误码ID | 错误信息 | 457| ------- | ---------------------------------------- | 458| 3100201 | Tag running state is abnormal in service. | 459 460## tag.getTagInfo<sup>9+</sup> 461 462getTagInfo(want: [Want](js-apis-app-ability-want.md#Want)): [TagInfo](#taginfo) 463 464从Want中获取TagInfo,Want是被NFC服务初始化,包含了TagInfo所需的属性值。 465 466**系统能力:** SystemCapability.Communication.NFC.Tag 467 468**参数:** 469 470| 参数名 | 类型 | 必填 | 说明 | 471| ---- | ---------------------------------------- | ---- | --------------------------------- | 472| want | [Want](js-apis-app-ability-want.md#Want) | 是 | 分发Ability时,在系统onCreate入口函数的参数中获取。 | 473 474**返回值:** 475 476| **类型** | **说明** | 477| ------------------- | --------------------------- | 478| [TagInfo](#taginfo) | TagInfo对象,用于获取不同技术类型的Tag对象。 | 479 480 481## tag.ndef.makeUriRecord<sup>9+</sup> 482 483makeUriRecord(uri: string): [NdefRecord](#ndefrecord9); 484 485根据输入的URI,构建NDEF标签的Record数据对象。 486 487**系统能力:** SystemCapability.Communication.NFC.Tag 488 489**参数:** 490 491| 参数名 | 类型 | 必填 | 说明 | 492| ---- | ------ | ---- | ---------------------- | 493| uri | string | 是 | 写入到NDEF Record里面的数据内容。 | 494 495**返回值:** 496 497| **类型** | **说明** | 498| -------------------------- | ---------------------------------------- | 499| [NdefRecord](#ndefrecord9) | NDEF标签的Record,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 | 500 501**示例:** 502 503```js 504import tag from '@ohos.nfc.tag'; 505 506try { 507 let uri = "https://gitee.com/openharmony"; // change it to be correct. 508 let ndefRecord = tag.ndef.makeUriRecord(uri); 509 if (ndefRecord != undefined) { 510 console.log("ndefMessage makeUriRecord rtdType: " + ndefRecord.rtdType); 511 console.log("ndefMessage makeUriRecord payload: " + ndefRecord.payload); 512 } else { 513 console.log("ndefMessage makeUriRecord ndefRecord: " + ndefRecord); 514 } 515} catch (busiError) { 516 console.log("ndefMessage makeUriRecord catched busiError: " + busiError); 517} 518``` 519 520## tag.ndef.makeTextRecord<sup>9+</sup> 521 522makeTextRecord(text: string, locale: string): [NdefRecord](#ndefrecord9); 523 524根据输入的文本数据和编码类型,构建NDEF标签的Record。 525 526**系统能力:** SystemCapability.Communication.NFC.Tag 527 528**参数:** 529 530| 参数名 | 类型 | 必填 | 说明 | 531| ------ | ------ | ---- | ------------------------ | 532| text | string | 是 | 写入到NDEF Record里面的文本数据内容。 | 533| locale | string | 是 | 文本数据内容的编码方式。 | 534 535**返回值:** 536 537| **类型** | **说明** | 538| -------------------------- | ---------------------------------------- | 539| [NdefRecord](#ndefrecord9) | NDEF标签的Record,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 | 540 541**示例:** 542 543```js 544import tag from '@ohos.nfc.tag'; 545 546try { 547 let text = "Hello World"; // change it to be correct. 548 let locale = "en"; // change it to be correct. 549 let ndefRecord = tag.ndef.makeTextRecord(text, locale); 550 if (ndefRecord != undefined) { 551 console.log("ndefMessage makeTextRecord rtdType: " + ndefRecord.rtdType); 552 console.log("ndefMessage makeTextRecord payload: " + ndefRecord.payload); 553 } else { 554 console.log("ndefMessage makeTextRecord ndefRecord: " + ndefRecord); 555 } 556} catch (busiError) { 557 console.log("ndefMessage makeTextRecord catched busiError: " + busiError); 558} 559``` 560 561 562## tag.ndef.makeMimeRecord<sup>9+</sup> 563 564makeMimeRecord(mimeType: string, mimeData: number[]): [NdefRecord](#ndefrecord9); 565 566根据输入的MIME数据和类型,构建NDEF标签的Record。 567 568**系统能力:** SystemCapability.Communication.NFC.Tag 569 570**参数:** 571 572| 参数名 | 类型 | 必填 | 说明 | 573| -------- | -------- | ---- | ---------------------------------------- | 574| mimeType | string | 是 | 符合RFC规则的MIME类型,比如"text/plain"或"image/jpeg"。 | 575| mimeData | number[] | 是 | MIME数据内容,每个number十六进制表示,范围是0x00~0xFF。 | 576 577**返回值:** 578 579| **类型** | **说明** | 580| -------------------------- | ---------------------------------------- | 581| [NdefRecord](#ndefrecord9) | NDEF标签的Record,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 | 582 583**示例:** 584 585```js 586import tag from '@ohos.nfc.tag'; 587 588try { 589 let mimeType = "text/plain"; // change it to be correct. 590 let mimeData = [0x01, 0x02, 0x03, 0x04]; // change it to be correct. 591 let ndefRecord = tag.ndef.makeMimeRecord(mimeType, mimeData); 592 if (ndefRecord != undefined) { 593 console.log("ndefMessage makeMimeRecord rtdType: " + ndefRecord.rtdType); 594 console.log("ndefMessage makeMimeRecord payload: " + ndefRecord.payload); 595 } else { 596 console.log("ndefMessage makeMimeRecord ndefRecord: " + ndefRecord); 597 } 598} catch (busiError) { 599 console.log("ndefMessage makeMimeRecord catched busiError: " + busiError); 600} 601``` 602## tag.ndef.makeExternalRecord<sup>9+</sup> 603 604makeExternalRecord(domainName: string, type: string, externalData: number[]): [NdefRecord](#ndefrecord9); 605 606根据应用程序特定的外部数据,构建NDEF标签的Record。 607 608**系统能力:** SystemCapability.Communication.NFC.Tag 609 610**参数:** 611 612| 参数名 | 类型 | 必填 | 说明 | 613| ------------ | -------- | ---- | ----------------------------------- | 614| domainName | string | 是 | 外部数据发布组织的域名,一般是应用程序的包名。 | 615| type | string | 是 | 外部数据的指定类型。 | 616| externalData | number[] | 是 | 外部数据内容,每个number十六进制表示,范围是0x00~0xFF。 | 617 618**返回值:** 619 620| **类型** | **说明** | 621| -------------------------- | ---------------------------------------- | 622| [NdefRecord](#ndefrecord9) | NDEF标签的Record,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 | 623 624**示例:** 625 626```js 627import tag from '@ohos.nfc.tag'; 628 629try { 630 let domainName = "ohos.nfc.application"; // change it to be correct. 631 let type = "test"; // change it to be correct. 632 let externalData = [0x01, 0x02, 0x03, 0x04]; // change it to be correct. 633 let ndefRecord = tag.ndef.makeExternalRecord(domainName, type, externalData); 634 if (ndefRecord != undefined) { 635 console.log("ndefMessage makeExternalRecord rtdType: " + ndefRecord.rtdType); 636 console.log("ndefMessage makeExternalRecord payload: " + ndefRecord.payload); 637 } else { 638 console.log("ndefMessage makeExternalRecord ndefRecord: " + ndefRecord); 639 } 640} catch (busiError) { 641 console.log("ndefMessage makeExternalRecord catched busiError: " + busiError); 642} 643``` 644 645## tag.ndef.messageToBytes<sup>9+</sup> 646 647messageToBytes(ndefMessage: [NdefMessage](js-apis-nfctech.md#ndefmessage9)): number[]; 648 649把输入的NDEF消息数据对象,转换为字节格式的数据。 650 651**系统能力:** SystemCapability.Communication.NFC.Tag 652 653**参数:** 654 655| 参数名 | 类型 | 必填 | 说明 | 656| ----------- | ---------------------------------------- | ---- | ----------- | 657| ndefMessage | [NdefMessage](js-apis-nfctech.md#ndefmessage9) | 是 | NDEF消息数据对象。 | 658 659**返回值:** 660 661| **类型** | **说明** | 662| -------- | ---------------------------------------- | 663| number[] | NDEF消息数据对象,所转换成的字节格式的数据。每个number十六进制表示,范围是0x00~0xFF。 | 664 665**示例:** 666 667```js 668import tag from '@ohos.nfc.tag'; 669 670let rawData = [0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]; // MUST can be parsed as NDEF Record. 671let ndefMessage; 672try { 673 ndefMessage = tag.ndef.createNdefMessage(rawData); 674 console.log("ndef createNdefMessage, ndefMessage: " + ndefMessage); 675} catch (busiError) { 676 console.log("ndef createNdefMessage busiError: " + busiError); 677} 678 679try { 680 let rawData2 = tag.ndef.messageToBytes(ndefMessage); 681 console.log("ndefMessage messageToBytes rawData2: " + rawData2); 682} catch (busiError) { 683 console.log("ndefMessage messageToBytes catched busiError: " + busiError); 684} 685``` 686## tag.ndef.createNdefMessage<sup>9+</sup> 687 688createNdefMessage(data: number[]): [NdefMessage](js-apis-nfctech.md#ndefmessage9) 689 690使用原始字节数据创建NDEF标签的Message。该数据必须符合NDEF Record数据格式,如果不符合格式,则返回的NdeMessage数据对象,所包含的NDE Record列表会为空。 691 692**系统能力:** SystemCapability.Communication.NFC.Tag 693 694**参数:** 695 696| **参数名** | **类型** | **必填** | **说明** | 697| ------- | -------- | ------ | ---------------------------------------- | 698| data | number[] | 是 | 原始字节,每个number十六进制表示,范围是0x00~0xFF。要求必须满足NDEF Record的格式。 | 699 700**返回值:** 701 702| **类型** | **说明** | 703| ---------------------------------------- | ---------------------------------------- | 704| [NdefMessage](js-apis-nfctech.md#ndefmessage9) | NDEF标签的Message,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 | 705 706**示例:** 707```js 708import tag from '@ohos.nfc.tag'; 709 710let rawData = [0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]; // MUST can be parsed as NDEF Record. 711let ndefMessage; 712try { 713 ndefMessage = tag.ndef.createNdefMessage(rawData); 714 console.log("ndef createNdefMessage, ndefMessage: " + ndefMessage); 715} catch (busiError) { 716 console.log("ndef createNdefMessage busiError: " + busiError); 717} 718``` 719 720## tag.ndef.createNdefMessage<sup>9+</sup> 721 722createNdefMessage(ndefRecords: NdefRecord[]): [NdefMessage](js-apis-nfctech.md#ndefmessage9) 723 724使用NDEF Records列表,创建NDEF Message。 725 726**系统能力:** SystemCapability.Communication.NFC.Tag 727 728**参数:** 729 730| **参数名** | **类型** | **必填** | **说明** | 731| ----------- | ---------------------------------------- | ------ | ---------------------------------------- | 732| ndefRecords | [NdefRecord](js-apis-nfcTag.md#ndefrecord9)[] | 是 | NDEF标签的Record列表,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 | 733 734**返回值:** 735 736| **类型** | **说明** | 737| ---------------------------------------- | ---------------------------------------- | 738| [NdefMessage](js-apis-nfctech.md#ndefmessage9) | NDEF标签的Message,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 | 739 740**示例:** 741 742```js 743import tag from '@ohos.nfc.tag'; 744 745let uriRecord = tag.ndef.makeUriRecord("https://gitee.com/openharmony"); 746let textRecord = tag.ndef.makeTextRecord("Hello World", "en"); 747let ndefRecords = [uriRecord, textRecord]; 748let ndefMessage; 749try { 750 ndefMessage = tag.ndef.createNdefMessage(ndefRecords); 751 console.log("ndef createNdefMessage ndefMessage: " + ndefMessage); 752} catch (busiError) { 753 console.log("ndef createNdefMessage busiError: " + busiError); 754} 755``` 756 757## TagInfo 758 759NFC服务在读取到标签时给出的对象,通过改对象属性,应用知道该标签支持哪些技术类型,并使用匹配的技术类型来调用相关接口。 760 761**系统能力:** SystemCapability.Communication.NFC.Tag 762 763**需要权限:** ohos.permission.NFC_TAG 764 765| **名称** | **类型** | **可读** | **可写** | **说明** | 766| ----------------------------- | ---------------------------------------- | ------ | ------ | ---------------------------------------- | 767| uid<sup>9+</sup> | number[] | 是 | 否 | 标签的uid,每个number值是十六进制表示,范围是0x00~0xFF。 | 768| technology<sup>9+</sup> | number[] | 是 | 否 | 支持的技术类型,每个number值表示所支持技术类型的常量值。 | 769| supportedProfiles | number[] | 是 | 否 | 支持的技术类型,从API9开始不支持,使用[tag.TagInfo#technology](#tagtaginfo)替代。 | 770| extrasData<sup>9+</sup> | [PacMap](js-apis-inner-ability-dataAbilityHelper.md#pacmap)[] | 是 | 否 | 标签所支持技术的扩展属性值。<br>**系统接口:** 此接口为系统接口。 | 771| tagRfDiscId<sup>9+</sup> | number | 是 | 否 | 标签发现时分配的ID值。<br>**系统接口:** 此接口为系统接口。 | 772| remoteTagService<sup>9+</sup> | [rpc.RemoteObject](js-apis-rpc.md#remoteobject) | 是 | 否 | NFC服务进程的远端对象,用于客户端和服务之间的接口通信。<br>**系统接口:** 此接口为系统接口。 | 773## NdefRecord<sup>9+</sup> 774NDEF标签Record属性的定义,参考NDEF标签技术规范《NFCForum-TS-NDEF_1.0》的定义细节。 775 776**系统能力:** SystemCapability.Communication.NFC.Tag 777 778| **名称** | **类型** | **可读** | **可写** | **说明** | 779| ------- | -------- | ------ | ------ | ---------------------------------------- | 780| tnf | number | 是 | 否 | NDEF Record的TNF(Type Name Field)。 | 781| rtdType | number[] | 是 | 否 | NDEF Record的RTD(Record Type Definition)类型值,每个number十六进制表示,范围是0x00~0xFF。 | 782| id | number[] | 是 | 否 | NDEF Record的ID,每个number十六进制表示,范围是0x00~0xFF。 | 783| payload | number[] | 是 | 否 | NDEF Record的PAYLOAD,每个number十六进制表示,范围是0x00~0xFF。 | 784 785## 技术类型定义 786NFC Tag有多种不同的技术类型,定义常量描述不同的技术类型。 787 788**系统能力:** SystemCapability.Communication.NFC.Tag 789 790| **名称** | **值** | **说明** | 791| ---------------------------- | ----- | ------------------------ | 792| NFC_A | 1 | NFC-A (ISO 14443-3A)技术。 | 793| NFC_B | 2 | NFC-A (ISO 14443-3B)技术。 | 794| ISO_DEP | 3 | ISO-DEP (ISO 14443-4)技术。 | 795| NFC_F | 4 | NFC-F (JIS 6319-4)技术。 | 796| NFC_V | 5 | NFC-V (ISO 15693)技术。 | 797| NDEF | 6 | NDEF技术。 | 798| NDEF_FORMATABLE<sup>9+</sup> | 7 | 可以格式化的NDEF技术。 | 799| MIFARE_CLASSIC | 8 | MIFARE Classic技术。 | 800| MIFARE_ULTRALIGHT | 9 | MIFARE Utralight技术。 | 801 802## TnfType<sup>9+</sup> 803NDEF Record的TNF(Type Name Field)类型值,参考NDEF标签技术规范《NFCForum-TS-NDEF_1.0》的定义细节。 804 805**系统能力:** SystemCapability.Communication.NFC.Tag 806 807| **名称** | **值** | **说明** | 808| ---------------- | ----- | ---------------------------------------- | 809| TNF_EMPTY | 0x0 | Empty。 | 810| TNF_WELL_KNOWN | 0x1 | NFC Forum well-known type [NFC RTD]。 | 811| TNF_MEDIA | 0x2 | Media-type as defined in RFC 2046 [RFC 2046]。 | 812| TNF_ABSOLUTE_URI | 0x3 | Absolute URI as defined in RFC 3986 [RFC 3986]。 | 813| TNF_EXT_APP | 0x4 | NFC Forum external type [NFC RTD]。 | 814| TNF_UNKNOWN | 0x5 | Unknown。 | 815| TNF_UNCHANGED | 0x6 | Unchanged (see section 2.3.3)。 | 816 817## NDEF Record RTD类型定义 818NDEF Record的RTD(Record Type Definition)类型值,参考NDEF标签技术规范《NFCForum-TS-NDEF_1.0》的定义细节。 819 820**系统能力:** SystemCapability.Communication.NFC.Tag 821 822| **名称** | **值** | **说明** | 823| --------------------- | ------ | ------------------ | 824| RTD_TEXT<sup>9+</sup> | [0x54] | 文本类型的NDEF Record。 | 825| RTD_URI<sup>9+</sup> | [0x55] | URI类型的NDEF Record。 | 826 827## NfcForumType<sup>9+</sup> 828NFC Forum标准里面Tag类型的定义。 829 830**系统能力:** SystemCapability.Communication.NFC.Tag 831 832| **名称** | **值** | **说明** | 833| ---------------- | ----- | ----------------- | 834| NFC_FORUM_TYPE_1 | 1 | NFC论坛类型1。 | 835| NFC_FORUM_TYPE_2 | 2 | NFC论坛类型2。 | 836| NFC_FORUM_TYPE_3 | 3 | NFC论坛类型3。 | 837| NFC_FORUM_TYPE_4 | 4 | NFC论坛类型4。 | 838| MIFARE_CLASSIC | 101 | MIFARE Classic类型。 | 839 840## MifareClassicType<sup>9+</sup> 841MIFARE Classic标签类型的定义。 842 843**系统能力:** SystemCapability.Communication.NFC.Tag 844 845| **名称** | **值** | **说明** | 846| ------------ | ----- | ----------------- | 847| TYPE_UNKNOWN | 0 | 未知的MIFARE类型。 | 848| TYPE_CLASSIC | 1 | MIFARE Classic类型。 | 849| TYPE_PLUS | 2 | MIFARE Plus类型。 | 850| TYPE_PRO | 3 | MIFARE Pro类型。 | 851 852## MifareClassicSize<sup>9+</sup> 853MIFARE Classic标签存储大小的定义。 854 855**系统能力:** SystemCapability.Communication.NFC.Tag 856 857| **名称** | **值** | **说明** | 858| ------------ | ----- | ------------------ | 859| MC_SIZE_MINI | 320 | 每个标签5个扇区,每个扇区4个块。 | 860| MC_SIZE_1K | 1024 | 每个标签16个扇区,每个扇区4个块。 | 861| MC_SIZE_2K | 2048 | 每个标签32个扇区,每个扇区4个块。 | 862| MC_SIZE_4K | 4096 | 每个标签40个扇区,每个扇区4个块。 | 863 864## MifareUltralightType<sup>9+</sup> 865MIFARE Ultralight标签类型的定义。 866 867**系统能力:** SystemCapability.Communication.NFC.Tag 868 869| **名称** | **值** | **说明** | 870| ----------------- | ----- | ---------------------- | 871| TYPE_UNKNOWN | 0 | 未知的 MIFARE 类型。 | 872| TYPE_ULTRALIGHT | 1 | MIFARE Ultralight类型。 | 873| TYPE_ULTRALIGHT_C | 2 | MIFARE UltralightC 类型。 | 874<!--no_check-->