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 //其他已声明的属性 18 19 "abilities": [ 20 { 21 "skills": [ 22 { 23 "actions": [ 24 // 其它已经声明的actions 25 26 // 添加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 // 有必要可添加其他技术 37 // 比如: 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 // 添加其他功能代码... 75 76 // want由nfc服务初始化,包含找到的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 // 获取发现标签的支持技术 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 // 检查其他技术类型: tag.NFC_B/NFC_F/NFC_V/NDEF/MIFARE_CLASSIC/MIFARE_ULTRALIGHT/NDEF_FORMATABLE 99 } 100 101 // 使用 NfcA APIs 去访问发现的标签 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 // 其他代码:对发现的标签执行读取或写入 110 } 111 112 // 使用 IsoDep APIs 去访问发现的标签 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 // 其他代码:对发现的标签执行读取或写入 121 } 122 // 使用相同的代码来处理 "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 | The tag running state is abnormal in the 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 | The tag running state is abnormal in the 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 | The tag running state is abnormal in the 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 | The tag running state is abnormal in the 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 | The tag running state is abnormal in the 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 | The tag running state is abnormal in the 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 | The tag running state is abnormal in the 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 | The tag running state is abnormal in the 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 | The tag running state is abnormal in the service. | 508 509## tag.getBarcodeTag<sup>18+</sup> 510 511getBarcodeTag(tagInfo: [TagInfo](#taginfo)): [BarcodeTag](js-apis-nfctech.md#barcodetag18) 512 513获取BarcodeTag类型Tag对象,通过该对象可访问BarcodeTag技术类型的Tag。 514 515 516**系统能力:** SystemCapability.Communication.NFC.Tag 517 518**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 519 520**参数:** 521| 参数名 | 类型 | 必填 | 说明 | 522| ------- | ------------------- | ---- | ------------------------------------------------------------- | 523| tagInfo | [TagInfo](#taginfo) | 是 | 包含Tag技术类型和相关参数,从[tag.getTagInfo(want: Want)](#taggettaginfo9)获取。 | 524 525**返回值:** 526 527| 类型 | 说明 | 528| ------------------------- | ------------------ | 529| [BartcodeTag](js-apis-nfctech.md#barcodetag18) | BarcodeTag类型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| 3100201 | The tag running state is abnormal in the service. | 540 541 542## tag.getTagInfo<sup>9+</sup> 543 544getTagInfo(want: [Want](../apis-ability-kit/js-apis-app-ability-want.md#want)): [TagInfo](#taginfo) 545 546从Want中获取TagInfo,Want是被NFC服务初始化,包含了TagInfo所需的属性值。 547 548**系统能力:** SystemCapability.Communication.NFC.Tag 549 550**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 551 552**参数:** 553 554| 参数名 | 类型 | 必填 | 说明 | 555| ------ | ---------------------------------------- | ---- | --------------------------------------------------- | 556| want | [Want](../apis-ability-kit/js-apis-app-ability-want.md#want) | 是 | 分发Ability时,在系统onCreate入口函数的参数中获取。 | 557 558**返回值:** 559 560| **类型** | **说明** | 561| ------------------- | -------------------------------------------- | 562| [TagInfo](#taginfo) | TagInfo对象,用于获取不同技术类型的Tag对象。 | 563 564**错误码:** 565 566以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 567 568| 错误码ID | 错误信息 | 569| -------- | ----------------------------------------- | 570| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 571| 801 | Capability not supported. | 572 573## tag.registerForegroundDispatch<sup>10+</sup> 574 575registerForegroundDispatch(elementName: [ElementName](../apis-ability-kit/js-apis-bundleManager-elementName.md), discTech: number[], callback: AsyncCallback<[TagInfo](#taginfo)>): void 576 577注册对NFC Tag读卡事件的监听,实现前台应用优先分发的目的。通过discTech设置支持的读卡技术类型,通过Callback方式获取读取到Tag的[TagInfo](#taginfo)信息。应用必须在前台才能调用。需要与取消监听接口[tag.unregisterForegroundDispatch](#tagunregisterforegrounddispatch10)成对使用。如果已注册事件监听,需要在页面退出前台或页面销毁前调用取消注册。 578 579**需要权限:** ohos.permission.NFC_TAG 580 581**系统能力:** SystemCapability.Communication.NFC.Tag 582 583**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 584 585**参数:** 586 587| 参数名 | 类型 | 必填 | 说明 | 588| ------------ | -------- | ---- | ------------------------------------------------------- | 589| elementName | [ElementName](../apis-ability-kit/js-apis-bundleManager-elementName.md) | 是 | 所属应用读卡的页面信息(至少包含bundleName、abilityName这两项的赋值),不可以为空。 | 590| discTech | number[] | 是 | 前台应用指定的NFC读卡技术类型,不可以为空,至少指定一种读卡技术类型。每个number值表示所支持技术类型的常量值型,根据number值设置NFC读卡轮询的Tag技术类型(仅包含[NFC_A](#常量), [NFC_B](#常量), [NFC_F](#常量), [NFC_V](#常量)中的一种或多种)。 | 591| callback | AsyncCallback<[TagInfo](#taginfo)> | 是 | 前台读卡监听回调函数,返回读到的Tag信息,不可以为空。 | 592 593**错误码:** 594 595以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 596 597| 错误码ID | 错误信息 | 598| -------- | ----------------------------------------- | 599| 201 | Permission denied. | 600| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 601| 801 | Capability not supported. | 602| 3100201 | The tag running state is abnormal in the service. | 603| 3100202 | The element state is invalid. | 604 605**示例:** 606 607示例请参见[tag.unregisterForegroundDispatch](#tagunregisterforegrounddispatch10)接口的示例。 608 609## tag.unregisterForegroundDispatch<sup>10+</sup> 610 611unregisterForegroundDispatch(elementName: [ElementName](../apis-ability-kit/js-apis-bundleManager-elementName.md)): void 612 613取消注册对NFC Tag读卡事件的监听,退出前台应用优先分发。如果已注册事件监听,需要在页面退出前台或页面销毁前调用取消注册。 614 615**需要权限:** ohos.permission.NFC_TAG 616 617**系统能力:** SystemCapability.Communication.NFC.Tag 618 619**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 620 621**参数:** 622 623| 参数名 | 类型 | 必填 | 说明 | 624| ------------ | -------- | ---- | ------------------------------------------------------- | 625| elementName | [ElementName](../apis-ability-kit/js-apis-bundleManager-elementName.md) | 是 | 所属应用读卡的页面信息(至少包含bundleName、abilityName这两项的赋值),不可以为空。 | 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| 801 | Capability not supported. | 636| 3100201 | The tag running state is abnormal in the service. | 637 638**示例:** 639 640```js 641 642import { tag } from '@kit.ConnectivityKit'; 643import { BusinessError } from '@kit.BasicServicesKit'; 644import { AbilityConstant, UIAbility, Want, bundleManager } from '@kit.AbilityKit'; 645 646let discTech : number[] = [tag.NFC_A, tag.NFC_B]; // 用前台ability时所需要的技术代替 647let elementName : bundleManager.ElementName; 648function foregroundCb(err : BusinessError, tagInfo : tag.TagInfo) { 649 if (!err) { 650 console.log("foreground callback: tag found tagInfo = ", JSON.stringify(tagInfo)); 651 } else { 652 console.log("foreground callback err: " + err.message); 653 return; 654 } 655 // taginfo的其他操作 656} 657 658export default class MainAbility extends UIAbility { 659 OnCreate(want : Want, launchParam : AbilityConstant.LaunchParam) { 660 console.log("OnCreate"); 661 elementName = { 662 bundleName: want.bundleName as string, 663 abilityName: want.abilityName as string, 664 moduleName: want.moduleName as string 665 } 666 } 667 668 onForeground() { 669 console.log("onForeground"); 670 try { 671 tag.registerForegroundDispatch(elementName, discTech, foregroundCb); 672 } catch (e) { 673 console.error("registerForegroundDispatch error: " + (e as BusinessError).message); 674 } 675 } 676 677 onBackground() { 678 console.log("onBackground"); 679 try { 680 tag.unregisterForegroundDispatch(elementName); 681 } catch (e) { 682 console.error("registerForegroundDispatch error: " + (e as BusinessError).message); 683 } 684 } 685 686 onWindowStageDestroy() { 687 console.log("onWindowStageDestroy"); 688 try { 689 tag.unregisterForegroundDispatch(elementName); 690 } catch (e) { 691 console.error("registerForegroundDispatch error: " + (e as BusinessError).message); 692 } 693 } 694 695 // ability生命周期内的其他功能 696} 697``` 698 699## tag.on<sup>11+</sup> 700 701on(type: 'readerMode', elementName: [ElementName](../apis-ability-kit/js-apis-bundleManager-elementName.md), discTech: number[], callback: AsyncCallback<[TagInfo](#taginfo)>): void 702 703订阅NFC Tag读卡事件,实现前台应用优先分发。设备会进入读卡器模式,同时关闭卡模拟。通过discTech设置支持的读卡技术类型,通过Callback方式获取到Tag的[TagInfo](#taginfo)信息。需要与取消读卡器模式的[tag.off](#tagoff11)成对使用,如果已通过on进行设置,需要在页面退出前台或页面销毁时调用[tag.off](#tagoff11)。 704 705**需要权限:** ohos.permission.NFC_TAG 706 707**系统能力:** SystemCapability.Communication.NFC.Tag 708 709**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 710 711**参数:** 712 713| 参数名 | 类型 | 必填 | 说明 | 714| ------------ | -------- | ---- | ------------------------------------------------------- | 715| type | string | 是 | 要注册的回调类型,固定填"readerMode"字符串。 | 716| elementName | [ElementName](../apis-ability-kit/js-apis-bundleManager-elementName.md) | 是 | 所属应用读卡的页面信息(至少包含bundleName、abilityName这两项的赋值),不可以为空。 | 717| discTech | number[] | 是 | 前台应用指定的NFC读卡技术类型,不可以为空,至少指定一种读卡技术类型。每个number值表示所支持技术类型的常量值型,根据number值设置NFC读卡轮询的Tag技术类型(仅包含[NFC_A](#常量), [NFC_B](#常量), [NFC_F](#常量), [NFC_V](#常量)中的一种或多种)。 | 718| callback | AsyncCallback<[TagInfo](#taginfo)> | 是 | 读卡器模式监听回调函数,返回读到的Tag信息,不可以为空。 | 719 720**错误码:** 721 722以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 723 724| 错误码ID | 错误信息 | 725| -------- | ----------------------------------------- | 726| 201 | Permission denied. | 727| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 728| 801 | Capability not supported. | 729| 3100201 | The tag running state is abnormal in the service. | 730| 3100202 | The element state is invalid. | 731 732**示例:** 733 734示例请参见[tag.off](#tagoff11)接口的示例。 735 736## tag.off<sup>11+</sup> 737 738off(type: 'readerMode', elementName: [ElementName](../apis-ability-kit/js-apis-bundleManager-elementName.md), callback?: AsyncCallback<[TagInfo](#taginfo)>): void 739 740取消订阅NFC Tag读卡事件。设备退出读卡模式,并恢复卡模拟。如果已通过[tag.on](#tagon11)设置NFC的读卡器模式,需要在页面退出前台或页面销毁时调用off进行取消。 741 742**需要权限:** ohos.permission.NFC_TAG 743 744**系统能力:** SystemCapability.Communication.NFC.Tag 745 746**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 747 748**参数:** 749 750| 参数名 | 类型 | 必填 | 说明 | 751| ------------ | -------- | ---- | ------------------------------------------------------- | 752| type | string | 是 | 要注销的回调类型,固定填"readerMode"字符串。| 753| elementName | [ElementName](../apis-ability-kit/js-apis-bundleManager-elementName.md) | 是 | 所属应用读卡的页面信息(至少包含bundleName、abilityName这两项的赋值),不可以为空。 | 754| callback | AsyncCallback<[TagInfo](#taginfo)> | 否 | 前台读卡监听回调函数,返回读到的Tag信息。 | 755 756**错误码:** 757 758以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 759 760| 错误码ID | 错误信息 | 761| -------- | ----------------------------------------- | 762| 201 | Permission denied. | 763| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 764| 801 | Capability not supported. | 765| 3100201 | The tag running state is abnormal in the service. | 766| 3100203 | The off() API can be called only when the on() has been called. | 767 768**示例:** 769 770```js 771import { tag } from '@kit.ConnectivityKit'; 772import { BusinessError } from '@kit.BasicServicesKit'; 773import { AbilityConstant, UIAbility, Want, bundleManager } from '@kit.AbilityKit'; 774 775let discTech : number[] = [tag.NFC_A, tag.NFC_B]; // 用前台ability时所需要的技术代替 776let elementName : bundleManager.ElementName; 777 778function readerModeCb(err : BusinessError, tagInfo : tag.TagInfo) { 779 if (!err) { 780 console.log("offCallback: tag found tagInfo = ", JSON.stringify(tagInfo)); 781 } else { 782 console.error("offCallback err: " + err.message); 783 return; 784 } 785 // taginfo的其他操作 786} 787 788export default class MainAbility extends UIAbility { 789 OnCreate(want : Want, launchParam : AbilityConstant.LaunchParam) { 790 console.log("OnCreate"); 791 elementName = { 792 bundleName: want.bundleName as string, 793 abilityName: want.abilityName as string, 794 moduleName: want.moduleName as string 795 } 796 } 797 798 onForeground() { 799 console.log("on start"); 800 try { 801 tag.on('readerMode', elementName, discTech, readerModeCb); 802 } catch (e) { 803 console.error("tag.on error: " + (e as BusinessError).message); 804 } 805 } 806 807 onBackground() { 808 console.log("onBackground"); 809 try { 810 tag.off('readerMode', elementName, readerModeCb); 811 } catch (e) { 812 console.error("tag.off error: " + (e as BusinessError).message); 813 } 814 } 815 816 onWindowStageDestroy() { 817 console.log("onWindowStageDestroy"); 818 try { 819 tag.off('readerMode', elementName, readerModeCb); 820 } catch (e) { 821 console.error("tag.off error: " + (e as BusinessError).message); 822 } 823 } 824 825 // ability生命周期内的其他功能 826} 827``` 828 829## tag.ndef.makeUriRecord<sup>9+</sup> 830 831makeUriRecord(uri: string): NdefRecord 832 833根据输入的URI,构建NDEF标签的Record数据对象。 834 835**系统能力:** SystemCapability.Communication.NFC.Tag 836 837**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 838 839**参数:** 840 841| 参数名 | 类型 | 必填 | 说明 | 842| ------ | ------ | ---- | --------------------------------- | 843| uri | string | 是 | 写入到NDEF Record里面的数据内容。 | 844 845**返回值:** 846 847| **类型** | **说明** | 848| -------------------------- | ------------------------------------------------------------ | 849| [NdefRecord](#ndefrecord9) | NDEF标签的Record,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 | 850 851**错误码:** 852 853以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 854 855| 错误码ID | 错误信息 | 856| -------- | ----------------------------------------- | 857| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 858 859**示例:** 860 861```js 862import { tag } from '@kit.ConnectivityKit'; 863 864try { 865 let uri = "https://www.example.com"; // 修改为正确可用的uri 866 let ndefRecord : tag.NdefRecord = tag.ndef.makeUriRecord(uri); 867 if (ndefRecord != undefined) { 868 console.log("ndefMessage makeUriRecord rtdType: " + ndefRecord.rtdType); 869 console.log("ndefMessage makeUriRecord payload: " + ndefRecord.payload); 870 } else { 871 console.log("ndefMessage makeUriRecord ndefRecord: " + ndefRecord); 872 } 873} catch (businessError) { 874 console.error("ndefMessage makeUriRecord catch businessError: " + businessError); 875} 876``` 877 878## tag.ndef.makeTextRecord<sup>9+</sup> 879 880makeTextRecord(text: string, locale: string): NdefRecord 881 882根据输入的文本数据和编码类型,构建NDEF标签的Record。 883 884**系统能力:** SystemCapability.Communication.NFC.Tag 885 886**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 887 888**参数:** 889 890| 参数名 | 类型 | 必填 | 说明 | 891| ------ | ------ | ---- | ------------------------------------- | 892| text | string | 是 | 写入到NDEF Record里面的文本数据内容。 | 893| locale | string | 是 | 文本数据内容的编码方式。 | 894 895**返回值:** 896 897| **类型** | **说明** | 898| -------------------------- | ------------------------------------------------------------ | 899| [NdefRecord](#ndefrecord9) | NDEF标签的Record,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 | 900 901**错误码:** 902 903以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 904 905| 错误码ID | 错误信息 | 906| -------- | ----------------------------------------- | 907| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 908 909**示例:** 910 911```js 912import { tag } from '@kit.ConnectivityKit'; 913 914try { 915 let text = "Hello World"; // 修改为想要写入的文本 916 let locale = "en"; // 修改为预期的编码格式 917 let ndefRecord : tag.NdefRecord = tag.ndef.makeTextRecord(text, locale); 918 if (ndefRecord != undefined) { 919 console.log("ndefMessage makeTextRecord rtdType: " + ndefRecord.rtdType); 920 console.log("ndefMessage makeTextRecord payload: " + ndefRecord.payload); 921 } else { 922 console.log("ndefMessage makeTextRecord ndefRecord: " + ndefRecord); 923 } 924} catch (businessError) { 925 console.error("ndefMessage makeTextRecord catch businessError: " + businessError); 926} 927``` 928 929## tag.ndef.makeApplicationRecord<sup>18+</sup> 930 931makeApplicationRecord(bundleName: string): NdefRecord 932 933根据OpenHarmony应用的bundlename,构建NDEF标签的Record。 934 935**系统能力:** SystemCapability.Communication.NFC.Tag 936 937**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 938 939**参数:** 940 941| 参数名 | 类型 | 必填 | 说明 | 942| ------ | ------ | ---- | ------------------------------------- | 943| bundleName | string | 是 | 要创建标签的应用包名。 | 944 945**返回值:** 946 947| **类型** | **说明** | 948| -------------------------- | ------------------------------------------------------------ | 949| [NdefRecord](#ndefrecord9) | NDEF标签的Record,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 | 950 951**错误码:** 952 953以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 954 955| 错误码ID | 错误信息 | 956| -------- | ----------------------------------------- | 957| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 958 959**示例:** 960 961```js 962import { tag } from '@kit.ConnectivityKit'; 963 964try { 965 let bundleName: string = 'com.demo.test'; 966 let ndefRecord : tag.NdefRecord = tag.ndef.makeApplicationRecord(bundleName); 967 if (ndefRecord != undefined) { 968 console.log("ndefMessage makeApplicationRecord rtdType: " + ndefRecord.rtdType); 969 console.log("ndefMessage makeApplicationRecord payload: " + ndefRecord.payload); 970 } else { 971 console.log("ndefMessage makeApplicationRecord ndefRecord: " + ndefRecord); 972 } 973} catch (businessError) { 974 console.error("ndefMessage makeApplicationRecord catch businessError: " + businessError); 975} 976``` 977 978## tag.ndef.makeMimeRecord<sup>9+</sup> 979 980makeMimeRecord(mimeType: string, mimeData: number[]): NdefRecord 981 982根据输入的MIME数据和类型,构建NDEF标签的Record。 983 984**系统能力:** SystemCapability.Communication.NFC.Tag 985 986**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 987 988**参数:** 989 990| 参数名 | 类型 | 必填 | 说明 | 991| -------- | -------- | ---- | ------------------------------------------------------- | 992| mimeType | string | 是 | 符合RFC规则的MIME类型,比如"text/plain"或"image/jpeg"。 | 993| mimeData | number[] | 是 | MIME数据内容,每个number十六进制表示,范围是0x00~0xFF。 | 994 995**返回值:** 996 997| **类型** | **说明** | 998| -------------------------- | ------------------------------------------------------------ | 999| [NdefRecord](#ndefrecord9) | NDEF标签的Record,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 | 1000 1001**错误码:** 1002 1003以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 1004 1005| 错误码ID | 错误信息 | 1006| -------- | ----------------------------------------- | 1007| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1008 1009**示例:** 1010 1011```js 1012import { tag } from '@kit.ConnectivityKit'; 1013 1014try { 1015 let mimeType = "text/plain"; // 修改为预期的符合规则的MIME类型 1016 let mimeData = [0x01, 0x02, 0x03, 0x04]; // 修改为预期的符合格式的数据 1017 let ndefRecord : tag.NdefRecord = tag.ndef.makeMimeRecord(mimeType, mimeData); 1018 if (ndefRecord != undefined) { 1019 console.log("ndefMessage makeMimeRecord rtdType: " + ndefRecord.rtdType); 1020 console.log("ndefMessage makeMimeRecord payload: " + ndefRecord.payload); 1021 } else { 1022 console.log("ndefMessage makeMimeRecord ndefRecord: " + ndefRecord); 1023 } 1024} catch (businessError) { 1025 console.error("ndefMessage makeMimeRecord catch businessError: " + businessError); 1026} 1027``` 1028## tag.ndef.makeExternalRecord<sup>9+</sup> 1029 1030makeExternalRecord(domainName: string, type: string, externalData: number[]): NdefRecord 1031 1032根据应用程序特定的外部数据,构建NDEF标签的Record。 1033 1034**系统能力:** SystemCapability.Communication.NFC.Tag 1035 1036**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1037 1038**参数:** 1039 1040| 参数名 | 类型 | 必填 | 说明 | 1041| ------------ | -------- | ---- | ------------------------------------------------------- | 1042| domainName | string | 是 | 外部数据发布组织的域名,一般是应用程序的包名。 | 1043| type | string | 是 | 外部数据的指定类型。 | 1044| externalData | number[] | 是 | 外部数据内容,每个number十六进制表示,范围是0x00~0xFF。 | 1045 1046**返回值:** 1047 1048| **类型** | **说明** | 1049| -------------------------- | ------------------------------------------------------------ | 1050| [NdefRecord](#ndefrecord9) | NDEF标签的Record,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 | 1051 1052**错误码:** 1053 1054以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 1055 1056| 错误码ID | 错误信息 | 1057| -------- | ----------------------------------------- | 1058| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1059 1060**示例:** 1061 1062```js 1063import { tag } from '@kit.ConnectivityKit'; 1064 1065try { 1066 let domainName = "ohos.nfc.application"; // 修改为符合规范的包名 1067 let type = "test"; // 修改为正确的数据类型 1068 let externalData = [0x01, 0x02, 0x03, 0x04]; // 修改为正确的外部数据内容 1069 let ndefRecord : tag.NdefRecord = tag.ndef.makeExternalRecord(domainName, type, externalData); 1070 if (ndefRecord != undefined) { 1071 console.log("ndefMessage makeExternalRecord rtdType: " + ndefRecord.rtdType); 1072 console.log("ndefMessage makeExternalRecord payload: " + ndefRecord.payload); 1073 } else { 1074 console.log("ndefMessage makeExternalRecord ndefRecord: " + ndefRecord); 1075 } 1076} catch (businessError) { 1077 console.error("ndefMessage makeExternalRecord catch businessError: " + businessError); 1078} 1079``` 1080 1081## tag.ndef.messageToBytes<sup>9+</sup> 1082 1083messageToBytes(ndefMessage: [NdefMessage](js-apis-nfctech.md#ndefmessage9)): number[] 1084 1085把输入的NDEF消息数据对象,转换为字节格式的数据。 1086 1087**系统能力:** SystemCapability.Communication.NFC.Tag 1088 1089**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1090 1091**参数:** 1092 1093| 参数名 | 类型 | 必填 | 说明 | 1094| ----------- | ---------------------------------------------- | ---- | ------------------ | 1095| ndefMessage | [NdefMessage](js-apis-nfctech.md#ndefmessage9) | 是 | NDEF消息数据对象。 | 1096 1097**返回值:** 1098 1099| **类型** | **说明** | 1100| -------- | ------------------------------------------------------------------------------------- | 1101| number[] | NDEF消息数据对象,所转换成的字节格式的数据。每个number十六进制表示,范围是0x00~0xFF。 | 1102 1103**错误码:** 1104 1105以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 1106 1107| 错误码ID | 错误信息 | 1108| -------- | ----------------------------------------- | 1109| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1110 1111**示例:** 1112 1113```js 1114import { tag } from '@kit.ConnectivityKit'; 1115 1116let rawData = [0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]; // 必须符合NDEF格式的数据 1117try { 1118 let ndefMessage : tag.NdefMessage = tag.ndef.createNdefMessage(rawData); 1119 console.log("ndef createNdefMessage, ndefMessage: " + ndefMessage); 1120 let rawData2 = tag.ndef.messageToBytes(ndefMessage); 1121 console.log("ndefMessage messageToBytes rawData2: " + rawData2); 1122} catch (businessError) { 1123 console.error("ndef createNdefMessage businessError: " + businessError); 1124} 1125``` 1126## tag.ndef.createNdefMessage<sup>9+</sup> 1127 1128createNdefMessage(data: number[]): [NdefMessage](js-apis-nfctech.md#ndefmessage9) 1129 1130使用原始字节数据创建NDEF标签的Message。该数据必须符合NDEF Record数据格式,如果不符合格式,则返回的NdeMessage数据对象,所包含的NDE Record列表会为空。 1131 1132**系统能力:** SystemCapability.Communication.NFC.Tag 1133 1134**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1135 1136**参数:** 1137 1138| **参数名** | **类型** | **必填** | **说明** | 1139| ---------- | -------- | -------- | ---------------------------------------------------------------------------------- | 1140| data | number[] | 是 | 原始字节,每个number十六进制表示,范围是0x00~0xFF。要求必须满足NDEF Record的格式。 | 1141 1142**返回值:** 1143 1144| **类型** | **说明** | 1145| ---------------------------------------------- | ------------------------------------------------------------- | 1146| [NdefMessage](js-apis-nfctech.md#ndefmessage9) | NDEF标签的Message,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 | 1147 1148**错误码:** 1149 1150以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 1151 1152| 错误码ID | 错误信息 | 1153| -------- | ----------------------------------------- | 1154| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1155 1156**示例:** 1157```js 1158import { tag } from '@kit.ConnectivityKit'; 1159 1160let rawData = [0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]; //必须是可以被解析的NDEF记录 1161try { 1162 let ndefMessage : tag.NdefMessage = tag.ndef.createNdefMessage(rawData); 1163 console.log("ndef createNdefMessage, ndefMessage: " + ndefMessage); 1164} catch (businessError) { 1165 console.error("ndef createNdefMessage businessError: " + businessError); 1166} 1167``` 1168 1169## tag.ndef.createNdefMessage<sup>9+</sup> 1170 1171createNdefMessage(ndefRecords: NdefRecord[]): [NdefMessage](js-apis-nfctech.md#ndefmessage9) 1172 1173使用NDEF Records列表,创建NDEF Message。 1174 1175**系统能力:** SystemCapability.Communication.NFC.Tag 1176 1177**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1178 1179**参数:** 1180 1181| **参数名** | **类型** | **必填** | **说明** | 1182| ----------- | --------------------------------------------- | -------- | ---------------------------------------------------------------- | 1183| ndefRecords | [NdefRecord](js-apis-nfcTag.md#ndefrecord9)[] | 是 | NDEF标签的Record列表,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 | 1184 1185**返回值:** 1186 1187| **类型** | **说明** | 1188| ---------------------------------------------- | ------------------------------------------------------------- | 1189| [NdefMessage](js-apis-nfctech.md#ndefmessage9) | NDEF标签的Message,详见NDEF技术规范《NFCForum-TS-NDEF_1.0》。 | 1190 1191**错误码:** 1192 1193以下错误码的详细介绍请参见[NFC错误码](errorcode-nfc.md)。 1194 1195| 错误码ID | 错误信息 | 1196| -------- | ----------------------------------------- | 1197| 401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. | 1198 1199**示例:** 1200 1201```js 1202import { tag } from '@kit.ConnectivityKit'; 1203 1204let uriRecord : tag.NdefRecord = tag.ndef.makeUriRecord("https://www.example.com"); 1205let textRecord : tag.NdefRecord = tag.ndef.makeTextRecord("Hello World", "en"); 1206let ndefRecords : tag.NdefRecord[] = [uriRecord, textRecord]; 1207try { 1208 let ndefMessage : tag.NdefMessage = tag.ndef.createNdefMessage(ndefRecords); 1209 console.log("ndef createNdefMessage ndefMessage: " + ndefMessage); 1210} catch (businessError) { 1211 console.error("ndef createNdefMessage businessError: " + businessError); 1212} 1213``` 1214 1215## TagInfo 1216 1217NFC服务在读取到标签时给出的对象,通过改对象属性,应用知道该标签支持哪些技术类型,并使用匹配的技术类型来调用相关接口。 1218 1219**系统能力:** SystemCapability.Communication.NFC.Tag 1220 1221**需要权限:** ohos.permission.NFC_TAG 1222 1223| **名称** | **类型** | **可读** | **可写** | **说明** | 1224| ----------------------------- | ------------------------------------------------------------- | -------- | -------- | -------------------------------------------------------------------------------------------- | 1225| uid<sup>9+</sup> | number[] | 是 | 否 | 标签的uid,每个number值是十六进制表示,范围是0x00~0xFF。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 1226| technology<sup>9+</sup> | number[] | 是 | 否 | 支持的技术类型,每个number值表示所支持技术类型的常量值。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 1227| supportedProfiles | number[] | 是 | 否 | 支持的技术类型,从API9开始不支持,使用[tag.TagInfo#technology](#taginfo)替代。 | 1228 1229## NdefRecord<sup>9+</sup> 1230NDEF标签Record属性的定义,参考NDEF标签技术规范《NFCForum-TS-NDEF_1.0》的定义细节。 1231 1232**系统能力:** SystemCapability.Communication.NFC.Tag 1233 1234**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1235 1236| **名称** | **类型** | **可读** | **可写** | **说明** | 1237| -------- | -------- | -------- | -------- | ----------------------------------------------------------------------------------------- | 1238| tnf | number | 是 | 否 | NDEF Record的TNF(Type Name Field)。 | 1239| rtdType | number[] | 是 | 否 | NDEF Record的RTD(Record Type Definition)类型值,每个number十六进制表示,范围是0x00~0xFF。 | 1240| id | number[] | 是 | 否 | NDEF Record的ID,每个number十六进制表示,范围是0x00~0xFF。 | 1241| payload | number[] | 是 | 否 | NDEF Record的PAYLOAD,每个number十六进制表示,范围是0x00~0xFF。 | 1242 1243## 常量 1244NFC Tag有多种不同的技术类型,定义常量描述不同的技术类型。 1245 1246**系统能力:** SystemCapability.Communication.NFC.Tag 1247 1248| **名称** |**类型**| **值** | **说明** | 1249| ---------------------------- | ------ | ------ | --------------------------- | 1250| NFC_A<sup>12+</sup> | number | 1 | NFC-A (ISO 14443-3A)技术。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 1251| NFC_B<sup>12+</sup> | number | 2 | NFC-B (ISO 14443-3B)技术。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 1252| ISO_DEP<sup>12+</sup> | number | 3 | ISO-DEP (ISO 14443-4)技术。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 1253| NFC_F<sup>12+</sup> | number | 4 | NFC-F (JIS 6319-4)技术。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 1254| NFC_V<sup>12+</sup> | number | 5 | NFC-V (ISO 15693)技术。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 1255| NDEF<sup>12+</sup> | number | 6 | NDEF技术。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 1256| NDEF_FORMATABLE<sup>9+</sup> | number | 7 | 可以格式化的NDEF技术。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 1257| MIFARE_CLASSIC<sup>12+</sup> | number | 8 | MIFARE Classic技术。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 1258| MIFARE_ULTRALIGHT<sup>12+</sup> | number | 9 | MIFARE Utralight技术。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 1259| NFC_BARCODE<sup>18+</sup> | number | 10 | BARCODE技术。<br>**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 | 1260 1261## TnfType<sup>9+</sup> 1262NDEF Record的TNF(Type Name Field)类型值,参考NDEF标签技术规范《NFCForum-TS-NDEF_1.0》的定义细节。 1263 1264**系统能力:** SystemCapability.Communication.NFC.Tag 1265 1266**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1267 1268| **名称** | **值** | **说明** | 1269| ---------------- | ------ | ------------------------------------------------ | 1270| TNF_EMPTY | 0x0 | Empty。 | 1271| TNF_WELL_KNOWN | 0x1 | NFC Forum well-known type [NFC RTD]。 | 1272| TNF_MEDIA | 0x2 | Media-type as defined in RFC 2046 [RFC 2046]。 | 1273| TNF_ABSOLUTE_URI | 0x3 | Absolute URI as defined in RFC 3986 [RFC 3986]。 | 1274| TNF_EXT_APP | 0x4 | NFC Forum external type [NFC RTD]。 | 1275| TNF_UNKNOWN | 0x5 | Unknown。 | 1276| TNF_UNCHANGED | 0x6 | Unchanged (see section 2.3.3)。 | 1277 1278## NDEF Record RTD类型定义 1279NDEF Record的RTD(Record Type Definition)类型值,参考NDEF标签技术规范《NFCForum-TS-NDEF_1.0》的定义细节。 1280 1281**系统能力:** SystemCapability.Communication.NFC.Tag 1282 1283**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1284 1285| **名称** |**类型**| **值** | **说明** | 1286| --------------------- | ------ | ------ | ----------------------- | 1287| RTD_TEXT<sup>9+</sup> |number[]| [0x54] | 文本类型的NDEF Record。 | 1288| RTD_URI<sup>9+</sup> |number[]| [0x55] | URI类型的NDEF Record。 | 1289 1290## NfcForumType<sup>9+</sup> 1291NFC Forum标准里面Tag类型的定义。 1292 1293**系统能力:** SystemCapability.Communication.NFC.Tag 1294 1295**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1296 1297| **名称** | **值** | **说明** | 1298| ---------------- | ------ | -------------------- | 1299| NFC_FORUM_TYPE_1 | 1 | NFC论坛类型1。 | 1300| NFC_FORUM_TYPE_2 | 2 | NFC论坛类型2。 | 1301| NFC_FORUM_TYPE_3 | 3 | NFC论坛类型3。 | 1302| NFC_FORUM_TYPE_4 | 4 | NFC论坛类型4。 | 1303| MIFARE_CLASSIC | 101 | MIFARE Classic类型。 | 1304 1305## MifareClassicType<sup>9+</sup> 1306MIFARE Classic标签类型的定义。 1307 1308**系统能力:** SystemCapability.Communication.NFC.Tag 1309 1310**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1311 1312| **名称** | **值** | **说明** | 1313| ------------ | ------ | -------------------- | 1314| TYPE_UNKNOWN | 0 | 未知的MIFARE类型。 | 1315| TYPE_CLASSIC | 1 | MIFARE Classic类型。 | 1316| TYPE_PLUS | 2 | MIFARE Plus类型。 | 1317| TYPE_PRO | 3 | MIFARE Pro类型。 | 1318 1319## MifareClassicSize<sup>9+</sup> 1320MIFARE Classic标签存储大小的定义。 1321 1322**系统能力:** SystemCapability.Communication.NFC.Tag 1323 1324**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1325 1326| **名称** | **值** | **说明** | 1327| ------------ | ------ | --------------------------------- | 1328| MC_SIZE_MINI | 320 | 每个标签5个扇区,每个扇区4个块。 | 1329| MC_SIZE_1K | 1024 | 每个标签16个扇区,每个扇区4个块。 | 1330| MC_SIZE_2K | 2048 | 每个标签32个扇区,每个扇区4个块。 | 1331| MC_SIZE_4K | 4096 | 每个标签40个扇区,每个扇区4个块。 | 1332 1333## MifareUltralightType<sup>9+</sup> 1334MIFARE Ultralight标签类型的定义。 1335 1336**系统能力:** SystemCapability.Communication.NFC.Tag 1337 1338**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1339 1340| **名称** | **值** | **说明** | 1341| ----------------- | ------ | ------------------------- | 1342| TYPE_UNKNOWN | 0 | 未知的 MIFARE 类型。 | 1343| TYPE_ULTRALIGHT | 1 | MIFARE Ultralight类型。 | 1344| TYPE_ULTRALIGHT_C | 2 | MIFARE UltralightC 类型。 | 1345 1346## NfcATag<sup>7+</sup> 1347 1348type NfcATag = _NfcATag 1349 1350获取NfcATag。 1351 1352**系统能力**:SystemCapability.Communication.NFC.Tag 1353**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1354 1355| 类型 | 说明 | 1356| ------ | ------------------------------------------------------------ | 1357| [_NfcATag](./js-apis-nfctech.md#nfcatag) | NfcATag 提供 NFC-A(ISO 14443-3A)技术的属性和I/O操作的访问。 | 1358 1359## NfcBTag<sup>7+</sup> 1360 1361type NfcBTag = _NfcBTag 1362 1363获取NfcBTag。 1364 1365**系统能力**:SystemCapability.Communication.NFC.Tag 1366**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1367 1368| 类型 | 说明 | 1369| ------ | ------------------------------------------------------------ | 1370| [_NfcBTag](./js-apis-nfctech.md#nfcbtag) | NfcBTag 提供 NFC-B(ISO 14443-3B)技术的属性和I/O操作的访问。 | 1371 1372## NfcFTag<sup>7+</sup> 1373 1374type NfcFTag = _NfcFTag 1375 1376获取NfcFTag。 1377 1378**系统能力**:SystemCapability.Communication.NFC.Tag 1379**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1380 1381| 类型 | 说明 | 1382| ------ | ------------------------------------------------------------ | 1383| [_NfcFTag](./js-apis-nfctech.md#nfcftag) | NfcFTag 提供对 NFC-F(JIS 6319-4)技术的属性和I/O操作的访问。 | 1384 1385## NfcVTag<sup>7+</sup> 1386 1387type NfcVTag = _NfcVTag 1388 1389获取NfcVTag。 1390 1391**系统能力**:SystemCapability.Communication.NFC.Tag 1392**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1393 1394| 类型 | 说明 | 1395| ------ | ------------------------------------------------------------ | 1396| [_NfcVTag](./js-apis-nfctech.md#nfcvtag) | NfcVTag 提供对 NFC-V(ISO 15693)技术的属性和I/O操作的访问。 | 1397 1398## IsoDepTag<sup>9+</sup> 1399 1400type IsoDepTag = _IsoDepTag 1401 1402获取IsoDepTag。 1403 1404**系统能力**:SystemCapability.Communication.NFC.Tag 1405**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1406 1407| 类型 | 说明 | 1408| ------ | ------------------------------------------------------------ | 1409| [_IsoDepTag](./js-apis-nfctech.md#isodeptag9) | IsoDepTag 提供 ISO-DEP(ISO 14443-4)技术的属性和I/O操作的访问。 | 1410 1411## NdefTag<sup>9+</sup> 1412 1413type NdefTag = _NdefTag 1414 1415获取NdefTag。 1416 1417**系统能力**:SystemCapability.Communication.NFC.Tag 1418**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1419 1420| 类型 | 说明 | 1421| ------ | ------------------------------------------------------------ | 1422| [_NdefTag](./js-apis-nfctech.md#ndeftag9) | 提供对已格式化为NDEF的NFC标签的数据和操作的访问。 | 1423 1424## MifareClassicTag<sup>9+</sup> 1425 1426type MifareClassicTag = _MifareClassicTag 1427 1428获取MifareClassicTag。 1429 1430**系统能力**:SystemCapability.Communication.NFC.Tag 1431**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1432 1433| 类型 | 说明 | 1434| ------ | ------------------------------------------------------------ | 1435| [_MifareClassicTag](./js-apis-nfctech.md#mifareclassictag9) | MifareClassicTag提供对MIFARE Classic属性和I/O操作的访问。 | 1436 1437## MifareUltralightTag<sup>9+</sup> 1438 1439type MifareUltralightTag = _MifareUltralightTag; 1440 1441获取MifareUltralightTag。 1442 1443**系统能力**:SystemCapability.Communication.NFC.Tag 1444**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1445 1446| 类型 | 说明 | 1447| ------ | ------------------------------------------------------------ | 1448| [_MifareUltralightTag](./js-apis-nfctech.md#mifareultralighttag9) | MifareUltralightTag 提供对MIFARE Ultralight属性和I/O操作的访问。 | 1449 1450## NdefFormatableTag<sup>9+</sup> 1451 1452type NdefFormatableTag = _NdefFormatableTag 1453 1454获取NdefFormatableTag。 1455 1456**系统能力**:SystemCapability.Communication.NFC.Tag 1457**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1458 1459| 类型 | 说明 | 1460| ------ | ------------------------------------------------------------ | 1461| [_NdefFormatableTag](./js-apis-nfctech.md#ndefformatabletag9) | NdefFormatableTag为NDEF Formattable的标签提供格式化操作。 | 1462 1463## BarcodeTag<sup>18+</sup> 1464 1465type BarcodeTag = _BarcodeTag 1466 1467获取BarcodeTag。 1468 1469**系统能力**:SystemCapability.Communication.NFC.Tag 1470**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 1471 1472| 类型 | 说明 | 1473| ------ | ------------------------------------------------------------ | 1474| [_BarcodeTag](./js-apis-nfctech.md#barcodetag18) | 提供对条形码标签的属性和I/O操作的访问。 | 1475 1476## NdefMessage<sup>9+</sup> 1477 1478type NdefMessage = _NdefMessage 1479 1480获取NdefMessage。 1481 1482**系统能力**:SystemCapability.Communication.NFC.Tag 1483**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1484 1485| 类型 | 说明 | 1486| ------ | ------------------------------------------------------------ | 1487| [_NdefMessage](./js-apis-nfctech.md#ndefmessage9) | 获取NDEF消息中的所有记录。 | 1488 1489## TagSession<sup>7+</sup> 1490 1491type TagSession = _TagSession 1492 1493获取TagSession。 1494 1495**系统能力**:SystemCapability.Communication.NFC.Tag 1496**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1497 1498| 类型 | 说明 | 1499| ------ | ------------------------------------------------------------ | 1500| [_TagSession](./js-apis-tagSession.md#tagsession) | TagSession是所有[NFC Tag技术类型](js-apis-nfctech.md)的基类, 提供建立连接和发送数据等共同接口。 | 1501<!--no_check-->