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