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