1# @ohos.nfc.tag (标准NFC-Tag) 2 3本模块主要用于操作及管理NFC Tag,提供后台读卡和前台应用优先分发两种读卡模式。 4后台读卡是指不需要打开应用程序,电子设备通过NFC读取标签卡片后,根据标签卡片的类型匹配到一个或多个应用程序。如果仅匹配到一个,则直接拉起应用程序的读卡页面;如果是多个则弹出应用选择器,让用户选择指定的读卡应用。 5前台读卡是指提前打开应用程序,并进入对应的NFC读卡页面后读卡,只会把读到的标签卡片信息分发给前台应用程序。 6 7> **说明:** 8> 9> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 10 11## 后台读卡方式的声明 12 13应用程序需要支持后台读卡时,需要在应用的属性配置文件中,声明与NFC相关的属性值。比如,在module.json5文件中,声明下面属性值: 14```json 15{ 16 "module": { 17 // other declared attributes. 18 19 "abilities": [ 20 { 21 "skills": [ 22 { 23 "actions": [ 24 // other declared actions, 25 26 // add the nfc tag action 27 "ohos.nfc.tag.action.TAG_FOUND" 28 ], 29 "uris": [ 30 { 31 "type":"tag-tech/NfcA" 32 }, 33 { 34 "type":"tag-tech/IsoDep" 35 } 36 // Add other technology if neccessary, 37 // such as: NfcB/NfcF/NfcV/Ndef/MifareClassic/MifareUL/NdefFormatable 38 ] 39 } 40 ] 41 } 42 ], 43 "requestPermissions": [ 44 { 45 "name": "ohos.permission.NFC_TAG", 46 "reason": "$string:app_name", 47 } 48 ] 49 } 50} 51``` 52> **注意:** 53> 54>1. 声明"actions"字段的内容填写,必须包含"ohos.nfc.tag.action.TAG_FOUND",不能更改。 55>2. 声明技术时"uris"中"type"字段的内容填写,前缀必须是"tag-tech/",后面接着NfcA/NfcB/NfcF/NfcV/IsoDep/Ndef/MifareClassic/MifareUL/NdefFormatable"中的一个。如果存在多个"type"时,需要分行填写。填写错误会造成解析失败。 56>3. 声明权限时"requestPermissions"中的"name"字段的内容填写,必须是"ohos.permission.NFC_TAG",不能更改。 57>4. 调用本模块接口和常量时请使用canIUse("SystemCapability.Communication.NFC.Tag")判断设备是否支持NFC能力,否则可能导致应用运行稳定性问题,参考[nfc-tag开发指南](../../connectivity/nfc/nfc-tag-access-guide.md)。 58 59## **导入模块** 60 61```js 62import { tag } from '@kit.ConnectivityKit'; 63``` 64 65## **tag.TagInfo** 66 67在对相关Tag类型卡片进行读写之前,必须先获取[TagInfo](#taginfo)相关属性值,以确认设备读取到的Tag卡片支持哪些技术类型。这样Tag应用程序才能调用正确的接口和所读取到的Tag卡片进行通信。 68```js 69import { tag } from '@kit.ConnectivityKit'; 70import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 71 72export default class EntryAbility extends UIAbility { 73 onCreate(want : Want, launchParam: AbilityConstant.LaunchParam) { 74 // add other code here... 75 76 // want is initialized by nfc service, contains tag info for this found tag 77 let tagInfo : tag.TagInfo | null = null; 78 try { 79 tagInfo = tag.getTagInfo(want); 80 } catch (error) { 81 console.error("tag.getTagInfo catch error: " + error); 82 } 83 if (tagInfo == null || tagInfo == undefined) { 84 console.log("no TagInfo to be created, ignore it."); 85 return; 86 } 87 88 // get the supported technologies for this found tag. 89 let isNfcATag = false; 90 let isIsoDepTag = false; 91 for (let i = 0; i < tagInfo.technology.length; i++) { 92 if (tagInfo.technology[i] == tag.NFC_A) { 93 isNfcATag = true; 94 } 95 if (tagInfo.technology[i] == tag.ISO_DEP) { 96 isIsoDepTag = true; 97 } 98 // also check for technology: tag.NFC_B/NFC_F/NFC_V/NDEF/MIFARE_CLASSIC/MIFARE_ULTRALIGHT/NDEF_FORMATABLE 99 } 100 101 // use NfcA APIs to access the found tag. 102 if (isNfcATag) { 103 let nfcA : tag.NfcATag | null = null; 104 try { 105 nfcA = tag.getNfcA(tagInfo); 106 } catch (error) { 107 console.error("tag.getNfcA catch error: " + error); 108 } 109 // other code to read or write this found tag. 110 } 111 112 // use getIsoDep APIs to access the found tag. 113 if (isIsoDepTag) { 114 let isoDep : tag.IsoDepTag | null = null; 115 try { 116 isoDep = tag.getIsoDep(tagInfo); 117 } catch (error) { 118 console.error("tag.getIsoDep catch error: " + error); 119 } 120 // other code to read or write this found tag. 121 } 122 // use the same code to handle for "NfcA/NfcB/NfcF/NfcV/Ndef/MifareClassic/MifareUL/NdefFormatable". 123 } 124} 125``` 126 127## tag.getNfcATag<sup>(deprecated)</sup> 128 129getNfcATag(tagInfo: [TagInfo](#taginfo)): [NfcATag](js-apis-nfctech.md#nfcatag) 130 131获取NFC A类型Tag对象,通过该对象可访问NfcA技术类型的Tag。 132 133> **说明:** 134> 135> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用[tag.getNfcA](#taggetnfca9)替代。 136 137**系统能力:** SystemCapability.Communication.NFC.Tag 138 139**参数:** 140 141| 参数名 | 类型 | 必填 | 说明 | 142| ------- | ------------------- | ---- | ------------------------------------------------------------- | 143| tagInfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从[tag.getTagInfo(want: Want)](#taggettaginfo9)获取。 | 144 145**返回值:** 146 147| **类型** | **说明** | 148| ------------------------------------- | ------------------ | 149| [NfcATag](js-apis-nfctech.md#nfcatag) | NFC A类型Tag对象。 | 150 151## tag.getNfcA<sup>9+</sup> 152 153getNfcA(tagInfo: [TagInfo](#taginfo)): [NfcATag](js-apis-nfctech.md#nfcatag) 154 155获取NFC A类型Tag对象,通过该对象可访问NfcA技术类型的Tag。 156 157**系统能力:** SystemCapability.Communication.NFC.Tag 158 159**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 160 161**参数:** 162 163| 参数名 | 类型 | 必填 | 说明 | 164| ------- | ------------------- | ---- | ------------------------------------------------------------- | 165| tagInfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从[tag.getTagInfo(want: Want)](#taggettaginfo9)获取。 | 166 167**返回值:** 168 169| **类型** | **说明** | 170| ------------------------------------- | ------------------ | 171| [NfcATag](js-apis-nfctech.md#nfcatag) | NFC A类型Tag对象。 | 172 173**错误码:** 174 175以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 176 177| 错误码ID | 错误信息 | 178| -------- | ----------------------------------------- | 179| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 180| 801 | Capability not supported. | 181| 3100201 | Tag running state is abnormal in service. | 182 183## tag.getNfcBTag<sup>(deprecated)</sup> 184 185getNfcBTag(tagInfo: [TagInfo](#taginfo)): [NfcBTag](js-apis-nfctech.md#nfcbtag) 186 187获取NFC B类型Tag对象,通过该对象可访问NfcB技术类型的Tag。 188 189> **说明:** 190> 191> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用[tag.getNfcB](#taggetnfcb9)替代。 192 193**系统能力:** SystemCapability.Communication.NFC.Tag 194 195**参数:** 196 197| 参数名 | 类型 | 必填 | 说明 | 198| ------- | ------------------- | ---- | ------------------------------------------------------------- | 199| tagInfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从[tag.getTagInfo(want: Want)](#taggettaginfo9)获取。 | 200 201**返回值:** 202 203| **类型** | **说明** | 204| ------------------------------------- | ------------------ | 205| [NfcBTag](js-apis-nfctech.md#nfcbtag) | NFC B类型Tag对象。 | 206 207## tag.getNfcB<sup>9+</sup> 208 209getNfcB(tagInfo: [TagInfo](#taginfo)): [NfcBTag](js-apis-nfctech.md#nfcbtag) 210 211获取NFC B类型Tag对象,通过该对象可访问NfcB技术类型的Tag。 212 213**系统能力:** SystemCapability.Communication.NFC.Tag 214 215**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 216 217**参数:** 218 219| 参数名 | 类型 | 必填 | 说明 | 220| ------- | ------------------- | ---- | ------------------------------------------------------------- | 221| tagInfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从[tag.getTagInfo(want: Want)](#taggettaginfo9)获取。 | 222 223**返回值:** 224 225| **类型** | **说明** | 226| ------------------------------------- | ------------------ | 227| [NfcBTag](js-apis-nfctech.md#nfcbtag) | NFC B类型Tag对象。 | 228 229**错误码:** 230 231以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 232 233| 错误码ID | 错误信息 | 234| -------- | ----------------------------------------- | 235| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 236| 801 | Capability not supported. | 237| 3100201 | Tag running state is abnormal in service. | 238 239## tag.getNfcFTag<sup>(deprecated)</sup> 240 241getNfcFTag(tagInfo: [TagInfo](#taginfo)): [NfcFTag](js-apis-nfctech.md#nfcftag) 242 243获取NFC F类型Tag对象,通过该对象可访问NfcF技术类型的Tag。 244 245> **说明:** 246> 247> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用[tag.getNfcF](#taggetnfcf9)替代。 248 249**系统能力:** SystemCapability.Communication.NFC.Tag 250 251**参数:** 252 253| 参数名 | 类型 | 必填 | 说明 | 254| ------- | ------------------- | ---- | ------------------------------------------------------------- | 255| tagInfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从[tag.getTagInfo(want: Want)](#taggettaginfo9)获取。 | 256 257**返回值:** 258 259| **类型** | **说明** | 260| ------------------------------------- | ------------------ | 261| [NfcFTag](js-apis-nfctech.md#nfcftag) | NFC F类型Tag对象。 | 262 263## tag.getNfcF<sup>9+</sup> 264 265getNfcF(tagInfo: [TagInfo](#taginfo)): [NfcFTag](js-apis-nfctech.md#nfcftag) 266 267获取NFC F类型Tag对象,通过该对象可访问NfcF技术类型的Tag。 268 269**系统能力:** SystemCapability.Communication.NFC.Tag 270 271**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 272 273**参数:** 274 275| 参数名 | 类型 | 必填 | 说明 | 276| ------- | ------------------- | ---- | ------------------------------------------------------------- | 277| tagInfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从[tag.getTagInfo(want: Want)](#taggettaginfo9)获取。 | 278 279**返回值:** 280 281| **类型** | **说明** | 282| ------------------------------------- | ------------------ | 283| [NfcFTag](js-apis-nfctech.md#nfcftag) | NFC F类型Tag对象。 | 284 285**错误码:** 286 287以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 288 289| 错误码ID | 错误信息 | 290| -------- | ----------------------------------------- | 291| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 292| 801 | Capability not supported. | 293| 3100201 | Tag running state is abnormal in service. | 294 295## tag.getNfcVTag<sup>(deprecated)</sup> 296 297getNfcVTag(tagInfo: [TagInfo](#taginfo)): [NfcVTag](js-apis-nfctech.md#nfcvtag) 298 299获取NFC V类型Tag对象,通过该对象可访问NfcV技术类型的Tag。 300 301> **说明:** 302> 303> 从 API version 7 开始支持,从 API version 9 开始废弃,建议使用[tag.getNfcV](#taggetnfcv9)替代。 304 305**系统能力:** SystemCapability.Communication.NFC.Tag 306 307**参数:** 308 309| 参数名 | 类型 | 必填 | 说明 | 310| ------- | ------------------- | ---- | ------------------------------------------------------------- | 311| tagInfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从[tag.getTagInfo(want: Want)](#taggettaginfo9)获取。 | 312 313**返回值:** 314 315| **类型** | **说明** | 316| ------------------------------------- | ------------------ | 317| [NfcVTag](js-apis-nfctech.md#nfcvtag) | NFC V类型Tag对象。 | 318 319## tag.getNfcV<sup>9+</sup> 320 321getNfcV(tagInfo: [TagInfo](#taginfo)): [NfcVTag](js-apis-nfctech.md#nfcvtag) 322 323获取NFC V类型Tag对象,通过该对象可访问NfcV技术类型的Tag。 324 325**系统能力:** SystemCapability.Communication.NFC.Tag 326 327**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 328 329**参数:** 330 331| 参数名 | 类型 | 必填 | 说明 | 332| ------- | ------------------- | ---- | ------------------------------------------------------------- | 333| tagInfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从[tag.getTagInfo(want: Want)](#taggettaginfo9)获取。 | 334 335**返回值:** 336 337| **类型** | **说明** | 338| ------------------------------------- | ------------------ | 339| [NfcVTag](js-apis-nfctech.md#nfcvtag) | NFC V类型Tag对象。 | 340 341**错误码:** 342 343以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 344 345| 错误码ID | 错误信息 | 346| -------- | ----------------------------------------- | 347| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 348| 801 | Capability not supported. | 349| 3100201 | Tag running state is abnormal in service. | 350 351## tag.getIsoDep<sup>9+</sup> 352 353getIsoDep(tagInfo: [TagInfo](#taginfo)): [IsoDepTag](js-apis-nfctech.md#isoDepTag9 ) 354 355获取IsoDep类型Tag对象,通过该对象可访问支持IsoDep技术类型的Tag。 356 357**系统能力:** SystemCapability.Communication.NFC.Tag 358 359**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 360 361**参数:** 362 363| 参数名 | 类型 | 必填 | 说明 | 364| ------- | ------------------- | ---- | ------------------------------------------------------------- | 365| tagInfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从[tag.getTagInfo(want: Want)](#taggettaginfo9)获取。 | 366 367**返回值:** 368 369| **类型** | **说明** | 370| ------------------------------------------ | ------------------------------------------------------- | 371| [IsoDepTag](js-apis-nfctech.md#isodeptag9) | IsoDep类型Tag对象,通过该对象访问IsoDep类型的相关接口。 | 372 373**错误码:** 374 375以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 376 377| 错误码ID | 错误信息 | 378| -------- | ----------------------------------------- | 379| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 380| 801 | Capability not supported. | 381| 3100201 | Tag running state is abnormal in service. | 382 383## tag.getNdef<sup>9+</sup> 384 385getNdef(tagInfo: [TagInfo](#taginfo)): [NdefTag](js-apis-nfctech.md#ndeftag9) 386 387获取NDEF类型Tag对象,通过该对象可访问支持NDEF技术类型的Tag。 388 389**系统能力:** SystemCapability.Communication.NFC.Tag 390 391**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 392 393**参数:** 394 395| 参数名 | 类型 | 必填 | 说明 | 396| ------- | ------------------- | ---- | ------------------------------------------------------------- | 397| tagInfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从[tag.getTagInfo(want: Want)](#taggettaginfo9)获取。 | 398 399**返回值:** 400 401| **类型** | **说明** | 402| -------------------------------------- | --------------------------------------------------- | 403| [NdefTag](js-apis-nfctech.md#ndeftag9) | NDEF类型Tag对象,通过该对象访问NDEF类型的相关接口。 | 404 405**错误码:** 406 407以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 408 409| 错误码ID | 错误信息 | 410| -------- | ----------------------------------------- | 411| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 412| 801 | Capability not supported. | 413| 3100201 | Tag running state is abnormal in service. | 414 415## tag.getMifareClassic<sup>9+</sup> 416 417getMifareClassic(tagInfo: [TagInfo](#taginfo)): [MifareClassicTag](js-apis-nfctech.md#mifareclassictag9) 418 419获取MIFARE Classic类型Tag对象,通过该对象访问支持MIFARE Classic技术类型的Tag。 420 421**系统能力:** SystemCapability.Communication.NFC.Tag 422 423**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 424 425**参数:** 426 427| 参数名 | 类型 | 必填 | 说明 | 428| ------- | ------------------- | ---- | ------------------------------------------------------------- | 429| tagInfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从[tag.getTagInfo(want: Want)](#taggettaginfo9)获取。 | 430 431**返回值:** 432 433| **类型** | **说明** | 434| --------------------------------------------------------- | ----------------------------------------------------------------------- | 435| [MifareClassicTag](js-apis-nfctech.md#mifareclassictag-9) | MIFARE Classic类型Tag对象,通过该对象访问MIFARE Classic类型的相关接口。 | 436 437**错误码:** 438 439以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 440 441| 错误码ID | 错误信息 | 442| -------- | ----------------------------------------- | 443| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 444| 801 | Capability not supported. | 445| 3100201 | Tag running state is abnormal in service. | 446 447## tag.getMifareUltralight<sup>9+</sup> 448 449getMifareUltralight(tagInfo: [TagInfo](#taginfo)): [MifareUltralightTag](js-apis-nfctech.md#mifareultralighttag9) 450 451获取MIFARE Ultralight类型Tag对象,通过该对象可访问支持MIFARE Ultralight技术类型的Tag。 452 453**系统能力:** SystemCapability.Communication.NFC.Tag 454 455**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 456 457**参数:** 458| 参数名 | 类型 | 必填 | 说明 | 459| ------- | ------------------- | ---- | ------------------------------------------------------------- | 460| tagInfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从[tag.getTagInfo(want: Want)](#taggettaginfo9)获取。 | 461 462**返回值:** 463 464| **类型** | **说明** | 465| -------------------------------------------------------------- | ----------------------------------------------------------------------------- | 466| [MifareUltralightTag](js-apis-nfctech.md#mifareultralighttag9) | MIFARE Ultralight类型Tag对象,通过该对象访问MIFARE Ultralight类型的相关接口。 | 467 468**错误码:** 469 470以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 471 472| 错误码ID | 错误信息 | 473| -------- | ----------------------------------------- | 474| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 475| 801 | Capability not supported. | 476| 3100201 | Tag running state is abnormal in service. | 477 478## tag.getNdefFormatable<sup>9+</sup> 479 480getNdefFormatable(tagInfo: [TagInfo](#taginfo)): [NdefFormatableTag](js-apis-nfctech.md#ndefformatabletag9) 481 482获取NDEF Formatable类型Tag对象,通过该对象可访问支持NDEF Formatable技术类型的Tag。 483 484**系统能力:** SystemCapability.Communication.NFC.Tag 485 486**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 487 488**参数:** 489| 参数名 | 类型 | 必填 | 说明 | 490| ------- | ------------------- | ---- | ------------------------------------------------------------- | 491| tagInfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从[tag.getTagInfo(want: Want)](#taggettaginfo9)获取。 | 492 493**返回值:** 494 495| **类型** | **说明** | 496| --------------------------------------------------------- | ------------------------------------------------------------------------- | 497| [NdefFormatableTag](js-apis-nfctech.md#ndefformatabletag) | NDEF Formatable类型Tag对象,通过该对象访问NDEF Formatable类型的相关接口。 | 498 499**错误码:** 500 501以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 502 503| 错误码ID | 错误信息 | 504| -------- | ----------------------------------------- | 505| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 506| 801 | Capability not supported. | 507| 3100201 | Tag running state is abnormal in service. | 508 509## tag.getTagInfo<sup>9+</sup> 510 511getTagInfo(want: [Want](../apis-ability-kit/js-apis-app-ability-want.md#want)): [TagInfo](#taginfo) 512 513从Want中获取TagInfo,Want是被NFC服务初始化,包含了TagInfo所需的属性值。 514 515**系统能力:** SystemCapability.Communication.NFC.Tag 516 517**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 518 519**参数:** 520 521| 参数名 | 类型 | 必填 | 说明 | 522| ------ | ---------------------------------------- | ---- | --------------------------------------------------- | 523| want | [Want](../apis-ability-kit/js-apis-app-ability-want.md#want) | 是 | 分发Ability时,在系统onCreate入口函数的参数中获取。 | 524 525**返回值:** 526 527| **类型** | **说明** | 528| ------------------- | -------------------------------------------- | 529| [TagInfo](#taginfo) | TagInfo对象,用于获取不同技术类型的Tag对象。 | 530 531**错误码:** 532 533以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 534 535| 错误码ID | 错误信息 | 536| -------- | ----------------------------------------- | 537| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 538| 801 | Capability not supported. | 539 540## tag.registerForegroundDispatch<sup>10+</sup> 541 542registerForegroundDispatch(elementName: [ElementName](../apis-ability-kit/js-apis-bundleManager-elementName.md), discTech: number[], callback: AsyncCallback<[TagInfo](#taginfo)>): void 543 544注册对NFC Tag读卡事件的监听,实现前台应用优先分发的目的。通过discTech设置支持的读卡技术类型,通过Callback方式获取读取到Tag的[TagInfo](#taginfo)信息。应用必须在前台才能调用。需要与取消监听接口[tag.unregisterForegroundDispatch](#tagunregisterforegrounddispatch10)成对使用。如果已注册事件监听,需要在页面退出前台或页面销毁前调用取消注册。 545 546**需要权限:** ohos.permission.NFC_TAG 547 548**系统能力:** SystemCapability.Communication.NFC.Tag 549 550**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 551 552**参数:** 553 554| 参数名 | 类型 | 必填 | 说明 | 555| ------------ | -------- | ---- | ------------------------------------------------------- | 556| elementName | [ElementName](../apis-ability-kit/js-apis-bundleManager-elementName.md) | 是 | 所属应用读卡的页面信息(至少包含bundleName、abilityName这两项的赋值),不可以为空。 | 557| discTech | number[] | 是 | 前台应用指定的NFC读卡技术类型,不可以为空,至少指定一种读卡技术类型。每个number值表示所支持技术类型的常量值型,根据number值设置NFC读卡轮询的Tag技术类型(仅包含[NFC_A](#常量), [NFC_B](#常量), [NFC_F](#常量), [NFC_V](#常量)中的一种或多种)。 | 558| callback | AsyncCallback<[TagInfo](#taginfo)> | 是 | 前台读卡监听回调函数,返回读到的Tag信息,不可以为空。 | 559 560**错误码:** 561 562以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 563 564| 错误码ID | 错误信息 | 565| -------- | ----------------------------------------- | 566| 201 | Permission denied. | 567| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 568| 801 | Capability not supported. | 569| 3100201 | The tag running state is abnormal in the service. | 570| 3100202 | The element state is invalid. | 571 572**示例:** 573 574示例请参见[tag.unregisterForegroundDispatch](#tagunregisterforegrounddispatch10)接口的示例。 575 576## tag.unregisterForegroundDispatch<sup>10+</sup> 577 578unregisterForegroundDispatch(elementName: [ElementName](../apis-ability-kit/js-apis-bundleManager-elementName.md)): void 579 580取消注册对NFC Tag读卡事件的监听,退出前台应用优先分发。如果已注册事件监听,需要在页面退出前台或页面销毁前调用取消注册。 581 582**需要权限:** ohos.permission.NFC_TAG 583 584**系统能力:** SystemCapability.Communication.NFC.Tag 585 586**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 587 588**参数:** 589 590| 参数名 | 类型 | 必填 | 说明 | 591| ------------ | -------- | ---- | ------------------------------------------------------- | 592| elementName | [ElementName](../apis-ability-kit/js-apis-bundleManager-elementName.md) | 是 | 所属应用读卡的页面信息(至少包含bundleName、abilityName这两项的赋值),不可以为空。 | 593 594**错误码:** 595 596以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 597 598| 错误码ID | 错误信息 | 599| -------- | ----------------------------------------- | 600| 201 | Permission denied. | 601| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 602| 801 | Capability not supported. | 603| 3100201 | The tag running state is abnormal in the service. | 604 605**示例:** 606 607```js 608 609import { tag } from '@kit.ConnectivityKit'; 610import { BusinessError } from '@kit.BasicServicesKit'; 611import { AbilityConstant, UIAbility, Want, bundleManager } from '@kit.AbilityKit'; 612 613let discTech : number[] = [tag.NFC_A, tag.NFC_B]; // replace with the tech(s) that is needed by foreground ability 614let elementName : bundleManager.ElementName; 615function foregroundCb(err : BusinessError, tagInfo : tag.TagInfo) { 616 if (!err) { 617 console.log("foreground callback: tag found tagInfo = ", JSON.stringify(tagInfo)); 618 } else { 619 console.log("foreground callback err: " + err.message); 620 return; 621 } 622 // other Operations of taginfo 623} 624 625export default class MainAbility extends UIAbility { 626 OnCreate(want : Want, launchParam : AbilityConstant.LaunchParam) { 627 console.log("OnCreate"); 628 elementName = { 629 bundleName: want.bundleName as string, 630 abilityName: want.abilityName as string, 631 moduleName: want.moduleName as string 632 } 633 } 634 635 onForeground() { 636 console.log("onForeground"); 637 try { 638 tag.registerForegroundDispatch(elementName, discTech, foregroundCb); 639 } catch (e) { 640 console.error("registerForegroundDispatch error: " + (e as BusinessError).message); 641 } 642 } 643 644 onBackground() { 645 console.log("onBackground"); 646 try { 647 tag.unregisterForegroundDispatch(elementName); 648 } catch (e) { 649 console.error("registerForegroundDispatch error: " + (e as BusinessError).message); 650 } 651 } 652 653 onWindowStageDestroy() { 654 console.log("onWindowStageDestroy"); 655 try { 656 tag.unregisterForegroundDispatch(elementName); 657 } catch (e) { 658 console.error("registerForegroundDispatch error: " + (e as BusinessError).message); 659 } 660 } 661 662 // override other lifecycle functions 663} 664``` 665 666## tag.on<sup>11+</sup> 667 668on(type: 'readerMode', elementName: [ElementName](../apis-ability-kit/js-apis-bundleManager-elementName.md), discTech: number[], callback: AsyncCallback<[TagInfo](#taginfo)>): void 669 670订阅NFC Tag读卡事件,实现前台应用优先分发。设备会进入读卡器模式,同时关闭卡模拟。通过discTech设置支持的读卡技术类型,通过Callback方式获取到Tag的[TagInfo](#taginfo)信息。需要与取消读卡器模式的[tag.off](#tagoff11)成对使用,如果已通过on进行设置,需要在页面退出前台或页面销毁时调用[tag.off](#tagoff11)。 671 672**需要权限:** ohos.permission.NFC_TAG 673 674**系统能力:** SystemCapability.Communication.NFC.Tag 675 676**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 677 678**参数:** 679 680| 参数名 | 类型 | 必填 | 说明 | 681| ------------ | -------- | ---- | ------------------------------------------------------- | 682| type | string | 是 | 要注册的回调类型,固定填"readerMode"字符串。 | 683| elementName | [ElementName](../apis-ability-kit/js-apis-bundleManager-elementName.md) | 是 | 所属应用读卡的页面信息(至少包含bundleName、abilityName这两项的赋值),不可以为空。 | 684| discTech | number[] | 是 | 前台应用指定的NFC读卡技术类型,不可以为空,至少指定一种读卡技术类型。每个number值表示所支持技术类型的常量值型,根据number值设置NFC读卡轮询的Tag技术类型(仅包含[NFC_A](#常量), [NFC_B](#常量), [NFC_F](#常量), [NFC_V](#常量)中的一种或多种)。 | 685| callback | AsyncCallback<[TagInfo](#taginfo)> | 是 | 读卡器模式监听回调函数,返回读到的Tag信息,不可以为空。 | 686 687**错误码:** 688 689以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 690 691| 错误码ID | 错误信息 | 692| -------- | ----------------------------------------- | 693| 201 | Permission denied. | 694| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 695| 801 | Capability not supported. | 696| 3100201 | The tag running state is abnormal in the service. | 697| 3100202 | The element state is invalid. | 698 699**示例:** 700 701示例请参见[tag.off](#tagoff11)接口的示例。 702 703## tag.off<sup>11+</sup> 704 705off(type: 'readerMode', elementName: [ElementName](../apis-ability-kit/js-apis-bundleManager-elementName.md), callback?: AsyncCallback<[TagInfo](#taginfo)>): void 706 707取消订阅NFC Tag读卡事件。设备退出读卡模式,并恢复卡模拟。如果已通过[tag.on](#tagon11)设置NFC的读卡器模式,需要在页面退出前台或页面销毁时调用off进行取消。 708 709**需要权限:** ohos.permission.NFC_TAG 710 711**系统能力:** SystemCapability.Communication.NFC.Tag 712 713**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 714 715**参数:** 716 717| 参数名 | 类型 | 必填 | 说明 | 718| ------------ | -------- | ---- | ------------------------------------------------------- | 719| type | string | 是 | 要注销的回调类型,固定填"readerMode"字符串。| 720| elementName | [ElementName](../apis-ability-kit/js-apis-bundleManager-elementName.md) | 是 | 所属应用读卡的页面信息(至少包含bundleName、abilityName这两项的赋值),不可以为空。 | 721| callback | AsyncCallback<[TagInfo](#taginfo)> | 否 | 前台读卡监听回调函数,返回读到的Tag信息。 | 722 723**错误码:** 724 725以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 726 727| 错误码ID | 错误信息 | 728| -------- | ----------------------------------------- | 729| 201 | Permission denied. | 730| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 731| 801 | Capability not supported. | 732| 3100201 | The tag running state is abnormal in the service. | 733| 3100203 | The off() can be called only when the on() has been called. | 734 735**示例:** 736 737```js 738import { tag } from '@kit.ConnectivityKit'; 739import { BusinessError } from '@kit.BasicServicesKit'; 740import { AbilityConstant, UIAbility, Want, bundleManager } from '@kit.AbilityKit'; 741 742let discTech : number[] = [tag.NFC_A, tag.NFC_B]; // replace with the tech(s) that is needed by foreground ability 743let elementName : bundleManager.ElementName; 744 745function readerModeCb(err : BusinessError, tagInfo : tag.TagInfo) { 746 if (!err) { 747 console.log("offCallback: tag found tagInfo = ", JSON.stringify(tagInfo)); 748 } else { 749 console.error("offCallback err: " + err.message); 750 return; 751 } 752 // other Operations of taginfo 753} 754 755export default class MainAbility extends UIAbility { 756 OnCreate(want : Want, launchParam : AbilityConstant.LaunchParam) { 757 console.log("OnCreate"); 758 elementName = { 759 bundleName: want.bundleName as string, 760 abilityName: want.abilityName as string, 761 moduleName: want.moduleName as string 762 } 763 } 764 765 onForeground() { 766 console.log("on start"); 767 try { 768 tag.on('readerMode', elementName, discTech, readerModeCb); 769 } catch (e) { 770 console.error("tag.on error: " + (e as BusinessError).message); 771 } 772 } 773 774 onBackground() { 775 console.log("onBackground"); 776 try { 777 tag.off('readerMode', elementName, readerModeCb); 778 } catch (e) { 779 console.error("tag.off error: " + (e as BusinessError).message); 780 } 781 } 782 783 onWindowStageDestroy() { 784 console.log("onWindowStageDestroy"); 785 try { 786 tag.off('readerMode', elementName, readerModeCb); 787 } catch (e) { 788 console.error("tag.off error: " + (e as BusinessError).message); 789 } 790 } 791 792 // override other lifecycle functions 793} 794``` 795 796## tag.ndef.makeUriRecord<sup>9+</sup> 797 798makeUriRecord(uri: string): [NdefRecord](#ndefrecord9) 799 800根据输入的URI,构建NDEF标签的Record数据对象。 801 802**系统能力:** SystemCapability.Communication.NFC.Tag 803 804**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 805 806**参数:** 807 808| 参数名 | 类型 | 必填 | 说明 | 809| ------ | ------ | ---- | --------------------------------- | 810| uri | string | 是 | 写入到NDEF Record里面的数据内容。 | 811 812**返回值:** 813 814| **类型** | **说明** | 815| -------------------------- | ------------------------------------------------------------ | 816| [NdefRecord](#ndefrecord9) | NDEF标签的Record,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 | 817 818**错误码:** 819 820以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 821 822| 错误码ID | 错误信息 | 823| -------- | ----------------------------------------- | 824| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 825 826**示例:** 827 828```js 829import { tag } from '@kit.ConnectivityKit'; 830 831try { 832 let uri = "https://www.example.com"; // change it to be correct. 833 let ndefRecord : tag.NdefRecord = tag.ndef.makeUriRecord(uri); 834 if (ndefRecord != undefined) { 835 console.log("ndefMessage makeUriRecord rtdType: " + ndefRecord.rtdType); 836 console.log("ndefMessage makeUriRecord payload: " + ndefRecord.payload); 837 } else { 838 console.log("ndefMessage makeUriRecord ndefRecord: " + ndefRecord); 839 } 840} catch (businessError) { 841 console.error("ndefMessage makeUriRecord catch businessError: " + businessError); 842} 843``` 844 845## tag.ndef.makeTextRecord<sup>9+</sup> 846 847makeTextRecord(text: string, locale: string): [NdefRecord](#ndefrecord9) 848 849根据输入的文本数据和编码类型,构建NDEF标签的Record。 850 851**系统能力:** SystemCapability.Communication.NFC.Tag 852 853**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 854 855**参数:** 856 857| 参数名 | 类型 | 必填 | 说明 | 858| ------ | ------ | ---- | ------------------------------------- | 859| text | string | 是 | 写入到NDEF Record里面的文本数据内容。 | 860| locale | string | 是 | 文本数据内容的编码方式。 | 861 862**返回值:** 863 864| **类型** | **说明** | 865| -------------------------- | ------------------------------------------------------------ | 866| [NdefRecord](#ndefrecord9) | NDEF标签的Record,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 | 867 868**错误码:** 869 870以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 871 872| 错误码ID | 错误信息 | 873| -------- | ----------------------------------------- | 874| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 875 876**示例:** 877 878```js 879import { tag } from '@kit.ConnectivityKit'; 880 881try { 882 let text = "Hello World"; // change it to be correct. 883 let locale = "en"; // change it to be correct. 884 let ndefRecord : tag.NdefRecord = tag.ndef.makeTextRecord(text, locale); 885 if (ndefRecord != undefined) { 886 console.log("ndefMessage makeTextRecord rtdType: " + ndefRecord.rtdType); 887 console.log("ndefMessage makeTextRecord payload: " + ndefRecord.payload); 888 } else { 889 console.log("ndefMessage makeTextRecord ndefRecord: " + ndefRecord); 890 } 891} catch (businessError) { 892 console.error("ndefMessage makeTextRecord catch businessError: " + businessError); 893} 894``` 895 896 897## tag.ndef.makeMimeRecord<sup>9+</sup> 898 899makeMimeRecord(mimeType: string, mimeData: number[]): [NdefRecord](#ndefrecord9) 900 901根据输入的MIME数据和类型,构建NDEF标签的Record。 902 903**系统能力:** SystemCapability.Communication.NFC.Tag 904 905**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 906 907**参数:** 908 909| 参数名 | 类型 | 必填 | 说明 | 910| -------- | -------- | ---- | ------------------------------------------------------- | 911| mimeType | string | 是 | 符合RFC规则的MIME类型,比如"text/plain"或"image/jpeg"。 | 912| mimeData | number[] | 是 | MIME数据内容,每个number十六进制表示,范围是0x00~0xFF。 | 913 914**返回值:** 915 916| **类型** | **说明** | 917| -------------------------- | ------------------------------------------------------------ | 918| [NdefRecord](#ndefrecord9) | NDEF标签的Record,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 | 919 920**错误码:** 921 922以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 923 924| 错误码ID | 错误信息 | 925| -------- | ----------------------------------------- | 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 928**示例:** 929 930```js 931import { tag } from '@kit.ConnectivityKit'; 932 933try { 934 let mimeType = "text/plain"; // change it to be correct. 935 let mimeData = [0x01, 0x02, 0x03, 0x04]; // change it to be correct. 936 let ndefRecord : tag.NdefRecord = tag.ndef.makeMimeRecord(mimeType, mimeData); 937 if (ndefRecord != undefined) { 938 console.log("ndefMessage makeMimeRecord rtdType: " + ndefRecord.rtdType); 939 console.log("ndefMessage makeMimeRecord payload: " + ndefRecord.payload); 940 } else { 941 console.log("ndefMessage makeMimeRecord ndefRecord: " + ndefRecord); 942 } 943} catch (businessError) { 944 console.error("ndefMessage makeMimeRecord catch businessError: " + businessError); 945} 946``` 947## tag.ndef.makeExternalRecord<sup>9+</sup> 948 949makeExternalRecord(domainName: string, type: string, externalData: number[]): [NdefRecord](#ndefrecord9) 950 951根据应用程序特定的外部数据,构建NDEF标签的Record。 952 953**系统能力:** SystemCapability.Communication.NFC.Tag 954 955**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 956 957**参数:** 958 959| 参数名 | 类型 | 必填 | 说明 | 960| ------------ | -------- | ---- | ------------------------------------------------------- | 961| domainName | string | 是 | 外部数据发布组织的域名,一般是应用程序的包名。 | 962| type | string | 是 | 外部数据的指定类型。 | 963| externalData | number[] | 是 | 外部数据内容,每个number十六进制表示,范围是0x00~0xFF。 | 964 965**返回值:** 966 967| **类型** | **说明** | 968| -------------------------- | ------------------------------------------------------------ | 969| [NdefRecord](#ndefrecord9) | NDEF标签的Record,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 | 970 971**错误码:** 972 973以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 974 975| 错误码ID | 错误信息 | 976| -------- | ----------------------------------------- | 977| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 978 979**示例:** 980 981```js 982import { tag } from '@kit.ConnectivityKit'; 983 984try { 985 let domainName = "ohos.nfc.application"; // change it to be correct. 986 let type = "test"; // change it to be correct. 987 let externalData = [0x01, 0x02, 0x03, 0x04]; // change it to be correct. 988 let ndefRecord : tag.NdefRecord = tag.ndef.makeExternalRecord(domainName, type, externalData); 989 if (ndefRecord != undefined) { 990 console.log("ndefMessage makeExternalRecord rtdType: " + ndefRecord.rtdType); 991 console.log("ndefMessage makeExternalRecord payload: " + ndefRecord.payload); 992 } else { 993 console.log("ndefMessage makeExternalRecord ndefRecord: " + ndefRecord); 994 } 995} catch (businessError) { 996 console.error("ndefMessage makeExternalRecord catch businessError: " + businessError); 997} 998``` 999 1000## tag.ndef.messageToBytes<sup>9+</sup> 1001 1002messageToBytes(ndefMessage: [NdefMessage](js-apis-nfctech.md#ndefmessage9)): number[] 1003 1004把输入的NDEF消息数据对象,转换为字节格式的数据。 1005 1006**系统能力:** SystemCapability.Communication.NFC.Tag 1007 1008**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1009 1010**参数:** 1011 1012| 参数名 | 类型 | 必填 | 说明 | 1013| ----------- | ---------------------------------------------- | ---- | ------------------ | 1014| ndefMessage | [NdefMessage](js-apis-nfctech.md#ndefmessage9) | 是 | NDEF消息数据对象。 | 1015 1016**返回值:** 1017 1018| **类型** | **说明** | 1019| -------- | ------------------------------------------------------------------------------------- | 1020| number[] | NDEF消息数据对象,所转换成的字节格式的数据。每个number十六进制表示,范围是0x00~0xFF。 | 1021 1022**错误码:** 1023 1024以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 1025 1026| 错误码ID | 错误信息 | 1027| -------- | ----------------------------------------- | 1028| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1029 1030**示例:** 1031 1032```js 1033import { tag } from '@kit.ConnectivityKit'; 1034 1035let rawData = [0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]; // MUST can be parsed as NDEF Record. 1036try { 1037 let ndefMessage : tag.NdefMessage = tag.ndef.createNdefMessage(rawData); 1038 console.log("ndef createNdefMessage, ndefMessage: " + ndefMessage); 1039 let rawData2 = tag.ndef.messageToBytes(ndefMessage); 1040 console.log("ndefMessage messageToBytes rawData2: " + rawData2); 1041} catch (businessError) { 1042 console.error("ndef createNdefMessage businessError: " + businessError); 1043} 1044``` 1045## tag.ndef.createNdefMessage<sup>9+</sup> 1046 1047createNdefMessage(data: number[]): [NdefMessage](js-apis-nfctech.md#ndefmessage9) 1048 1049使用原始字节数据创建NDEF标签的Message。该数据必须符合NDEF Record数据格式,如果不符合格式,则返回的NdeMessage数据对象,所包含的NDE Record列表会为空。 1050 1051**系统能力:** SystemCapability.Communication.NFC.Tag 1052 1053**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1054 1055**参数:** 1056 1057| **参数名** | **类型** | **必填** | **说明** | 1058| ---------- | -------- | -------- | ---------------------------------------------------------------------------------- | 1059| data | number[] | 是 | 原始字节,每个number十六进制表示,范围是0x00~0xFF。要求必须满足NDEF Record的格式。 | 1060 1061**返回值:** 1062 1063| **类型** | **说明** | 1064| ---------------------------------------------- | ------------------------------------------------------------- | 1065| [NdefMessage](js-apis-nfctech.md#ndefmessage9) | NDEF标签的Message,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 | 1066 1067**错误码:** 1068 1069以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 1070 1071| 错误码ID | 错误信息 | 1072| -------- | ----------------------------------------- | 1073| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1074 1075**示例:** 1076```js 1077import { tag } from '@kit.ConnectivityKit'; 1078 1079let rawData = [0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]; // MUST can be parsed as NDEF Record. 1080try { 1081 let ndefMessage : tag.NdefMessage = tag.ndef.createNdefMessage(rawData); 1082 console.log("ndef createNdefMessage, ndefMessage: " + ndefMessage); 1083} catch (businessError) { 1084 console.error("ndef createNdefMessage businessError: " + businessError); 1085} 1086``` 1087 1088## tag.ndef.createNdefMessage<sup>9+</sup> 1089 1090createNdefMessage(ndefRecords: NdefRecord[]): [NdefMessage](js-apis-nfctech.md#ndefmessage9) 1091 1092使用NDEF Records列表,创建NDEF Message。 1093 1094**系统能力:** SystemCapability.Communication.NFC.Tag 1095 1096**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1097 1098**参数:** 1099 1100| **参数名** | **类型** | **必填** | **说明** | 1101| ----------- | --------------------------------------------- | -------- | ---------------------------------------------------------------- | 1102| ndefRecords | [NdefRecord](js-apis-nfcTag.md#ndefrecord9)[] | 是 | NDEF标签的Record列表,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 | 1103 1104**返回值:** 1105 1106| **类型** | **说明** | 1107| ---------------------------------------------- | ------------------------------------------------------------- | 1108| [NdefMessage](js-apis-nfctech.md#ndefmessage9) | NDEF标签的Message,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 | 1109 1110**错误码:** 1111 1112以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 1113 1114| 错误码ID | 错误信息 | 1115| -------- | ----------------------------------------- | 1116| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1117 1118**示例:** 1119 1120```js 1121import { tag } from '@kit.ConnectivityKit'; 1122 1123let uriRecord : tag.NdefRecord = tag.ndef.makeUriRecord("https://www.example.com"); 1124let textRecord : tag.NdefRecord = tag.ndef.makeTextRecord("Hello World", "en"); 1125let ndefRecords : tag.NdefRecord[] = [uriRecord, textRecord]; 1126try { 1127 let ndefMessage : tag.NdefMessage = tag.ndef.createNdefMessage(ndefRecords); 1128 console.log("ndef createNdefMessage ndefMessage: " + ndefMessage); 1129} catch (businessError) { 1130 console.error("ndef createNdefMessage businessError: " + businessError); 1131} 1132``` 1133 1134## TagInfo 1135 1136NFC服务在读取到标签时给出的对象,通过改对象属性,应用知道该标签支持哪些技术类型,并使用匹配的技术类型来调用相关接口。 1137 1138**系统能力:** SystemCapability.Communication.NFC.Tag 1139 1140**需要权限:** ohos.permission.NFC_TAG 1141 1142| **名称** | **类型** | **可读** | **可写** | **说明** | 1143| ----------------------------- | ------------------------------------------------------------- | -------- | -------- | -------------------------------------------------------------------------------------------- | 1144| uid<sup>9+</sup> | number[] | 是 | 否 | 标签的uid,每个number值是十六进制表示,范围是0x00~0xFF。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 1145| technology<sup>9+</sup> | number[] | 是 | 否 | 支持的技术类型,每个number值表示所支持技术类型的常量值。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 1146| supportedProfiles | number[] | 是 | 否 | 支持的技术类型,从API9开始不支持,使用[tag.TagInfo#technology](#taginfo)替代。 | 1147 1148## NdefRecord<sup>9+</sup> 1149NDEF标签Record属性的定义,参考NDEF标签技术规范《NFCForum-TS-NDEF_1.0》的定义细节。 1150 1151**系统能力:** SystemCapability.Communication.NFC.Tag 1152 1153**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1154 1155| **名称** | **类型** | **可读** | **可写** | **说明** | 1156| -------- | -------- | -------- | -------- | ----------------------------------------------------------------------------------------- | 1157| tnf | number | 是 | 否 | NDEF Record的TNF(Type Name Field)。 | 1158| rtdType | number[] | 是 | 否 | NDEF Record的RTD(Record Type Definition)类型值,每个number十六进制表示,范围是0x00~0xFF。 | 1159| id | number[] | 是 | 否 | NDEF Record的ID,每个number十六进制表示,范围是0x00~0xFF。 | 1160| payload | number[] | 是 | 否 | NDEF Record的PAYLOAD,每个number十六进制表示,范围是0x00~0xFF。 | 1161 1162## 常量 1163NFC Tag有多种不同的技术类型,定义常量描述不同的技术类型。 1164 1165**系统能力:** SystemCapability.Communication.NFC.Tag 1166 1167**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1168 1169| **名称** |**类型**| **值** | **说明** | 1170| ---------------------------- | ------ | ------ | --------------------------- | 1171| NFC_A | number | 1 | NFC-A (ISO 14443-3A)技术。 | 1172| NFC_B | number | 2 | NFC-B (ISO 14443-3B)技术。 | 1173| ISO_DEP | number | 3 | ISO-DEP (ISO 14443-4)技术。 | 1174| NFC_F | number | 4 | NFC-F (JIS 6319-4)技术。 | 1175| NFC_V | number | 5 | NFC-V (ISO 15693)技术。 | 1176| NDEF | number | 6 | NDEF技术。 | 1177| NDEF_FORMATABLE<sup>9+</sup> | number | 7 | 可以格式化的NDEF技术。 | 1178| MIFARE_CLASSIC | number | 8 | MIFARE Classic技术。 | 1179| MIFARE_ULTRALIGHT | number | 9 | MIFARE Utralight技术。 | 1180 1181## TnfType<sup>9+</sup> 1182NDEF Record的TNF(Type Name Field)类型值,参考NDEF标签技术规范《NFCForum-TS-NDEF_1.0》的定义细节。 1183 1184**系统能力:** SystemCapability.Communication.NFC.Tag 1185 1186**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1187 1188| **名称** | **值** | **说明** | 1189| ---------------- | ------ | ------------------------------------------------ | 1190| TNF_EMPTY | 0x0 | Empty。 | 1191| TNF_WELL_KNOWN | 0x1 | NFC Forum well-known type [NFC RTD]。 | 1192| TNF_MEDIA | 0x2 | Media-type as defined in RFC 2046 [RFC 2046]。 | 1193| TNF_ABSOLUTE_URI | 0x3 | Absolute URI as defined in RFC 3986 [RFC 3986]。 | 1194| TNF_EXT_APP | 0x4 | NFC Forum external type [NFC RTD]。 | 1195| TNF_UNKNOWN | 0x5 | Unknown。 | 1196| TNF_UNCHANGED | 0x6 | Unchanged (see section 2.3.3)。 | 1197 1198## NDEF Record RTD类型定义 1199NDEF Record的RTD(Record Type Definition)类型值,参考NDEF标签技术规范《NFCForum-TS-NDEF_1.0》的定义细节。 1200 1201**系统能力:** SystemCapability.Communication.NFC.Tag 1202 1203**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1204 1205| **名称** |**类型**| **值** | **说明** | 1206| --------------------- | ------ | ------ | ----------------------- | 1207| RTD_TEXT<sup>9+</sup> |number[]| [0x54] | 文本类型的NDEF Record。 | 1208| RTD_URI<sup>9+</sup> |number[]| [0x55] | URI类型的NDEF Record。 | 1209 1210## NfcForumType<sup>9+</sup> 1211NFC Forum标准里面Tag类型的定义。 1212 1213**系统能力:** SystemCapability.Communication.NFC.Tag 1214 1215**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1216 1217| **名称** | **值** | **说明** | 1218| ---------------- | ------ | -------------------- | 1219| NFC_FORUM_TYPE_1 | 1 | NFC论坛类型1。 | 1220| NFC_FORUM_TYPE_2 | 2 | NFC论坛类型2。 | 1221| NFC_FORUM_TYPE_3 | 3 | NFC论坛类型3。 | 1222| NFC_FORUM_TYPE_4 | 4 | NFC论坛类型4。 | 1223| MIFARE_CLASSIC | 101 | MIFARE Classic类型。 | 1224 1225## MifareClassicType<sup>9+</sup> 1226MIFARE Classic标签类型的定义。 1227 1228**系统能力:** SystemCapability.Communication.NFC.Tag 1229 1230**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1231 1232| **名称** | **值** | **说明** | 1233| ------------ | ------ | -------------------- | 1234| TYPE_UNKNOWN | 0 | 未知的MIFARE类型。 | 1235| TYPE_CLASSIC | 1 | MIFARE Classic类型。 | 1236| TYPE_PLUS | 2 | MIFARE Plus类型。 | 1237| TYPE_PRO | 3 | MIFARE Pro类型。 | 1238 1239## MifareClassicSize<sup>9+</sup> 1240MIFARE Classic标签存储大小的定义。 1241 1242**系统能力:** SystemCapability.Communication.NFC.Tag 1243 1244**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1245 1246| **名称** | **值** | **说明** | 1247| ------------ | ------ | --------------------------------- | 1248| MC_SIZE_MINI | 320 | 每个标签5个扇区,每个扇区4个块。 | 1249| MC_SIZE_1K | 1024 | 每个标签16个扇区,每个扇区4个块。 | 1250| MC_SIZE_2K | 2048 | 每个标签32个扇区,每个扇区4个块。 | 1251| MC_SIZE_4K | 4096 | 每个标签40个扇区,每个扇区4个块。 | 1252 1253## MifareUltralightType<sup>9+</sup> 1254MIFARE Ultralight标签类型的定义。 1255 1256**系统能力:** SystemCapability.Communication.NFC.Tag 1257 1258**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1259 1260| **名称** | **值** | **说明** | 1261| ----------------- | ------ | ------------------------- | 1262| TYPE_UNKNOWN | 0 | 未知的 MIFARE 类型。 | 1263| TYPE_ULTRALIGHT | 1 | MIFARE Ultralight类型。 | 1264| TYPE_ULTRALIGHT_C | 2 | MIFARE UltralightC 类型。 | 1265 1266## NfcATag<sup>7+</sup> 1267 1268type NfcATag = _NfcATag 1269 1270获取NfcATag。 1271 1272**系统能力**:SystemCapability.Communication.NFC.Tag 1273**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1274 1275| 类型 | 说明 | 1276| ------ | ------------------------------------------------------------ | 1277| [_NfcATag](./js-apis-nfctech.md#nfcatag) | NfcATag 提供 NFC-A(ISO 14443-3A)技术的属性和I/O操作的访问。 | 1278 1279## NfcBTag<sup>7+</sup> 1280 1281type NfcBTag = _NfcBTag 1282 1283获取NfcBTag。 1284 1285**系统能力**:SystemCapability.Communication.NFC.Tag 1286**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1287 1288| 类型 | 说明 | 1289| ------ | ------------------------------------------------------------ | 1290| [_NfcBTag](./js-apis-nfctech.md#nfcbtag) | NfcBTag 提供 NFC-B(ISO 14443-3B)技术的属性和I/O操作的访问。 | 1291 1292## NfcFTag<sup>7+</sup> 1293 1294type NfcFTag = _NfcFTag 1295 1296获取NfcFTag。 1297 1298**系统能力**:SystemCapability.Communication.NFC.Tag 1299**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1300 1301| 类型 | 说明 | 1302| ------ | ------------------------------------------------------------ | 1303| [_NfcFTag](./js-apis-nfctech.md#nfcftag) | NfcFTag 提供对 NFC-F(JIS 6319-4)技术的属性和I/O操作的访问。 | 1304 1305## NfcVTag<sup>7+</sup> 1306 1307type NfcVTag = _NfcVTag 1308 1309获取NfcVTag。 1310 1311**系统能力**:SystemCapability.Communication.NFC.Tag 1312**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1313 1314| 类型 | 说明 | 1315| ------ | ------------------------------------------------------------ | 1316| [_NfcVTag](./js-apis-nfctech.md#nfcvtag) | NfcVTag 提供对 NFC-V(ISO 15693)技术的属性和I/O操作的访问。 | 1317 1318## IsoDepTag<sup>9+</sup> 1319 1320type IsoDepTag = _IsoDepTag 1321 1322获取IsoDepTag。 1323 1324**系统能力**:SystemCapability.Communication.NFC.Tag 1325**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1326 1327| 类型 | 说明 | 1328| ------ | ------------------------------------------------------------ | 1329| [_IsoDepTag](./js-apis-nfctech.md#isodeptag9) | IsoDepTag 提供 ISO-DEP(ISO 14443-4)技术的属性和I/O操作的访问。 | 1330 1331## NdefTag<sup>9+</sup> 1332 1333type NdefTag = _NdefTag 1334 1335获取NdefTag。 1336 1337**系统能力**:SystemCapability.Communication.NFC.Tag 1338**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1339 1340| 类型 | 说明 | 1341| ------ | ------------------------------------------------------------ | 1342| [_NdefTag](./js-apis-nfctech.md#ndeftag9) | 提供对已格式化为NDEF的NFC标签的数据和操作的访问。 | 1343 1344## MifareClassicTag<sup>9+</sup> 1345 1346type MifareClassicTag = _MifareClassicTag 1347 1348获取MifareClassicTag。 1349 1350**系统能力**:SystemCapability.Communication.NFC.Tag 1351**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1352 1353| 类型 | 说明 | 1354| ------ | ------------------------------------------------------------ | 1355| [_MifareClassicTag](./js-apis-nfctech.md#mifareclassictag9) | MifareClassicTag提供对MIFARE Classic属性和I/O操作的访问。 | 1356 1357## MifareUltralightTag<sup>9+</sup> 1358 1359type MifareUltralightTag = _MifareUltralightTag; 1360 1361获取MifareUltralightTag。 1362 1363**系统能力**:SystemCapability.Communication.NFC.Tag 1364**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1365 1366| 类型 | 说明 | 1367| ------ | ------------------------------------------------------------ | 1368| [_MifareUltralightTag](./js-apis-nfctech.md#mifareultralighttag9) | MifareUltralightTag 提供对MIFARE Ultralight属性和I/O操作的访问。 | 1369 1370## NdefFormatableTag<sup>9+</sup> 1371 1372type NdefFormatableTag = _NdefFormatableTag 1373 1374获取NdefFormatableTag。 1375 1376**系统能力**:SystemCapability.Communication.NFC.Tag 1377**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1378 1379| 类型 | 说明 | 1380| ------ | ------------------------------------------------------------ | 1381| [_NdefFormatableTag](./js-apis-nfctech.md#ndefformatabletag9) | NdefFormatableTag为NDEF Formattable的标签提供格式化操作。 | 1382 1383## NdefMessage<sup>9+</sup> 1384 1385type NdefMessage = _NdefMessage 1386 1387获取NdefMessage。 1388 1389**系统能力**:SystemCapability.Communication.NFC.Tag 1390**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1391 1392| 类型 | 说明 | 1393| ------ | ------------------------------------------------------------ | 1394| [_NdefMessage](./js-apis-nfctech.md#ndefmessage9) | 获取NDEF消息中的所有记录。 | 1395 1396## TagSession<sup>7+</sup> 1397 1398type TagSession = _TagSession 1399 1400获取TagSession。 1401 1402**系统能力**:SystemCapability.Communication.NFC.Tag 1403**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1404 1405| 类型 | 说明 | 1406| ------ | ------------------------------------------------------------ | 1407| [_TagSession](./js-apis-tagSession.md#tagsession) | TagSession是所有[NFC Tag技术类型](js-apis-nfctech.md)的基类, 提供建立连接和发送数据等共同接口。 | 1408<!--no_check-->