• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.connectedTag (有源标签)
2
3本模块提供有源标签的使用,包括初始化有源标签芯片、读取有源标签内容、写入内容到有源标签等。
4
5> **说明:**
6>
7> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8
9## 导入模块
10
11```js
12import connectedTag from '@ohos.connectedTag';
13```
14
15## connectedTag.init
16
17init(): boolean
18
19初始化有源标签芯片。
20
21**需要权限**:ohos.permission.NFC_TAG
22
23**系统能力**:SystemCapability.Communication.ConnectedTag
24
25**返回值:**
26
27| **类型** | **说明** |
28| -------- | -------- |
29| boolean | true:初始化成功, false:初始化失败。 |
30
31## connectedTag.initialize<sup>9+</sup>
32
33initialize(): void
34
35初始化有源标签芯片。
36
37**需要权限:** ohos.permission.NFC_TAG
38
39**系统能力:** SystemCapability.Communication.ConnectedTag
40
41**错误码:**
42以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。
43
44| 错误码ID | 错误信息|
45| -------- | -------- |
46| 3200101 | Connected NFC tag running state is abnormal in service. |
47
48## connectedTag.uninit
49
50uninit(): boolean
51
52卸载有源标签芯片资源。
53
54**需要权限**:ohos.permission.NFC_TAG
55
56**系统能力**:SystemCapability.Communication.ConnectedTag
57
58**返回值:**
59
60| **类型** | **说明** |
61| -------- | -------- |
62| boolean | true:卸载操作成功,&nbsp;false:卸载操作失败。 |
63
64## connectedTag.uninitialize<sup>9+</sup>
65
66uninitialize(): void
67
68卸载有源标签芯片资源。
69
70**需要权限:** ohos.permission.NFC_TAG
71
72**系统能力:** SystemCapability.Communication.ConnectedTag
73
74**错误码:**
75以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。
76
77| 错误码ID | 错误信息|
78| -------- | -------- |
79| 3200101 | Connected NFC tag running state is abnormal in service. |
80
81## connectedTag.readNdefTag
82
83readNdefTag(): Promise&lt;string&gt;
84
85读取有源标签内容,使用promise方式作为异步方法。
86
87**需要权限**:ohos.permission.NFC_TAG
88
89**系统能力**:SystemCapability.Communication.ConnectedTag
90
91**返回值:**
92
93| **类型** | **说明** |
94| -------- | -------- |
95| Promise&lt;string&gt; | 返回读取有源标签内容。 |
96
97**示例:**
98
99```js
100import connectedTag from '@ohos.connectedTag';
101
102connectedTag.readNdefTag().then((data) => {
103    console.log("connectedTag readNdefTag Promise data = " + data);
104}).catch((err)=> {
105    console.log("connectedTag readNdefTag Promise err: " + err);
106});
107```
108
109## connectedTag.read<sup>9+</sup>
110
111read(): Promise&lt;number[]&gt;
112
113读取有源标签内容,使用promise方式作为异步方法。
114
115**需要权限:** ohos.permission.NFC_TAG
116
117**系统能力:** SystemCapability.Communication.ConnectedTag
118
119**返回值:**
120
121| **类型** | **说明** |
122| -------- | -------- |
123| Promise&lt;number[]&gt; | 返回读取有源标签内容。 |
124
125**错误码:**
126以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。
127
128| 错误码ID | 错误信息|
129| -------- | -------- |
130| 3200101 | Connected NFC tag running state is abnormal in service. |
131
132**示例:**
133
134```js
135import connectedTag from '@ohos.connectedTag';
136
137connectedTag.read().then((data) => {
138    console.log("connectedTag read Promise data = " + data);
139}).catch((err)=> {
140    console.log("connectedTag read Promise err: " + err);
141});
142```
143
144## connectedTag.readNdefTag
145
146readNdefTag(callback: AsyncCallback&lt;string&gt;): void
147
148读取有源标签内容,使用AsyncCallback方式作为异步方法。
149
150**需要权限**:ohos.permission.NFC_TAG
151
152**系统能力**:SystemCapability.Communication.ConnectedTag
153
154**参数:**
155
156| **参数名** | **类型** | **必填** | **说明** |
157| -------- | -------- | -------- | -------- |
158| callback | AsyncCallback&lt;string&gt; | 是 | 读取有源标签内容回调函数。 |
159
160**示例:**
161
162```js
163import connectedTag from '@ohos.connectedTag';
164
165connectedTag.readNdefTag((err, data)=> {
166    if (err) {
167        console.log("connectedTag readNdefTag AsyncCallback err: " + err);
168    } else {
169        console.log("connectedTag readNdefTag AsyncCallback data: " + data);
170    }
171});
172```
173
174## connectedTag.read<sup>9+</sup>
175
176read(callback: AsyncCallback&lt;number[]&gt;): void
177
178读取有源标签内容,使用AsyncCallback方式作为异步方法。
179
180**需要权限:** ohos.permission.NFC_TAG
181
182**系统能力:** SystemCapability.Communication.ConnectedTag
183
184**参数:**
185
186| **参数名** | **类型** | **必填** | **说明** |
187| -------- | -------- | -------- | -------- |
188| callback | AsyncCallback&lt;number[]&gt; | 是 | 读取有源标签内容回调函数。 |
189
190**错误码:**
191以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。
192
193| 错误码ID | 错误信息|
194| -------- | -------- |
195| 3200101 | Connected NFC tag running state is abnormal in service. |
196
197**示例:**
198
199```js
200import connectedTag from '@ohos.connectedTag';
201
202connectedTag.read((err, data)=> {
203    if (err) {
204        console.log("connectedTag read AsyncCallback err: " + err);
205    } else {
206        console.log("connectedTag read AsyncCallback data: " + data);
207    }
208});
209```
210
211## connectedTag.writeNdefTag
212
213writeNdefTag(data: string): Promise&lt;void&gt;
214
215写入内容到有源标签,使用promise方式作为异步方法。
216
217**需要权限**:ohos.permission.NFC_TAG
218
219**系统能力**:SystemCapability.Communication.ConnectedTag
220
221**参数:**
222
223| **参数名** | **类型** | **必填** | **说明** |
224| -------- | -------- | -------- | -------- |
225| data | string | 是 | 有源标签内容, 长度最大是1024个字节。 |
226
227**返回值:**
228
229| **类型** | **说明** |
230| -------- | -------- |
231| Promise&lt;void&gt; | 无返回值。 |
232
233**示例:**
234
235```js
236import connectedTag from '@ohos.connectedTag';
237
238var rawData = "010203"; // change it tobe correct.
239connectedTag.writeNdefTag(rawData).then(() => {
240    console.log("connectedTag writeNdefTag Promise success.");
241}).catch((err)=> {
242    console.log("connectedTag writeNdefTag Promise err: " + err);
243});
244```
245
246## connectedTag.write<sup>9+</sup>
247
248write(data: number[]): Promise&lt;void&gt;
249
250写入内容到有源标签,使用promise方式作为异步方法。
251
252**需要权限:** ohos.permission.NFC_TAG
253
254**系统能力:** SystemCapability.Communication.ConnectedTag
255
256**参数:**
257
258| **参数名** | **类型** | **必填** | **说明** |
259| -------- | -------- | -------- | -------- |
260| data | number[] | 是 | 有源标签内容, 由十六进制数字组成,范围从0x00至0xFF。 |
261
262**返回值:**
263
264| **类型** | **说明** |
265| -------- | -------- |
266| Promise&lt;void&gt; | 无返回值。 |
267
268**错误码:**
269以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。
270
271| 错误码ID | 错误信息|
272| -------- | -------- |
273| 3200101 | Connected NFC tag running state is abnormal in service. |
274
275**示例:**
276
277```js
278import connectedTag from '@ohos.connectedTag';
279
280var rawData = [0x01, 0x02, 0x03]; // change it tobe correct.
281connectedTag.write(rawData).then(() => {
282    console.log("connectedTag write NdefTag Promise success.");
283}).catch((err)=> {
284    console.log("connectedTag write NdefTag Promise err: " + err);
285});
286```
287
288## connectedTag.writeNdefTag
289
290writeNdefTag(data: string, callback: AsyncCallback&lt;void&gt;): void
291
292写入内容到有源标签,使用AsyncCallback方式作为异步方法。
293
294**需要权限**:ohos.permission.NFC_TAG
295
296**系统能力**:SystemCapability.Communication.ConnectedTag
297
298**参数:**
299
300| **参数名** | **类型** | **必填** | **说明** |
301| -------- | -------- | -------- | -------- |
302| data | string | 是 | 有源标签内容, 长度最大是1024个字节。 |
303| callback | AsyncCallback&lt;void&gt; | 是 | 读取有源标签内容回调函数。 |
304
305**示例:**
306
307```js
308import connectedTag from '@ohos.connectedTag';
309
310var rawData = "010203"; // change it tobe correct.
311connectedTag.writeNdefTag(rawData, (err)=> {
312    if (err) {
313        console.log("connectedTag writeNdefTag AsyncCallback err: " + err);
314    } else {
315        console.log("connectedTag writeNdefTag AsyncCallback success.");
316    }
317});
318```
319
320## connectedTag.write<sup>9+</sup>
321
322write(data: number[], callback: AsyncCallback&lt;void&gt;): void
323
324写入内容到有源标签,使用AsyncCallback方式作为异步方法。
325
326**需要权限:** ohos.permission.NFC_TAG
327
328**系统能力:** SystemCapability.Communication.ConnectedTag
329
330**参数:**
331
332| **参数名** | **类型** | **必填** | **说明** |
333| -------- | -------- | -------- | -------- |
334| data | number[] | 是 | 有源标签内容, 由十六进制数字组成,范围从0x00至0xFF。 |
335| callback | AsyncCallback&lt;void&gt; | 是 | 读取有源标签内容回调函数。 |
336
337**错误码:**
338以下错误码的详细介绍请参见[NFC错误码](../errorcodes/errorcode-nfc.md)。
339
340| 错误码ID | 错误信息|
341| -------- | -------- |
342| 3200101 | Connected NFC tag running state is abnormal in service. |
343
344**示例:**
345
346```js
347import connectedTag from '@ohos.connectedTag';
348
349var rawData = [0x01, 0x02, 0x03]; // change it tobe correct.
350connectedTag.write(rawData, (err)=> {
351    if (err) {
352        console.log("connectedTag write NdefTag AsyncCallback err: " + err);
353    } else {
354        console.log("connectedTag write NdefTag AsyncCallback success.");
355    }
356});
357```
358
359## connectedTag.on('notify')
360
361on(type: "notify", callback: Callback&lt;number&gt;): void
362
363注册NFC场强状态事件。
364
365**需要权限**:ohos.permission.NFC_TAG
366
367**系统能力**:SystemCapability.Communication.ConnectedTag
368
369**参数:**
370
371| **参数名** | **类型** | **必填** | **说明** |
372| -------- | -------- | -------- | -------- |
373| type | string | 是 | 固定填"notify"字符串 |
374| callback | Callback&lt;number&gt; | 是 | 状态改变回调函数,返回值参见[NfcRfType](#nfcrftype)。 |
375
376## connectedTag.off('notify')
377
378off(type: "notify", callback?: Callback&lt;number&gt;): void
379
380取消NFC场强状态事件的注册。
381
382**需要权限**:ohos.permission.NFC_TAG
383
384**系统能力**:SystemCapability.Communication.ConnectedTag
385
386**参数:**
387
388| **参数名** | **类型** | **必填** | **说明** |
389| -------- | -------- | -------- | -------- |
390| type | string | 是 | 固定填"notify"字符串 |
391| callback | Callback&lt;number&gt; | 否 | 状态改变回调函数。如果callback不填,将“去注册”该事件关联的所有回调函数。|
392
393**示例:**
394
395```js
396import connectedTag from '@ohos.connectedTag';
397
398// Register event
399connectedTag.on("notify", (err, rfState)=> {
400    if (err) {
401        console.log("connectedTag on Callback err: " + err);
402    } else {
403        console.log("connectedTag on Callback rfState: " + rfState);
404    }
405});
406
407var initStatus = connectedTag.init();
408console.log("connectedTag init status: " + initStatus);
409
410// Add nfc connecected tag business oprations here...
411// connectedTag.writeNdefTag(rawData)
412// connectedTag.readNdefTag()
413
414var uninitStatus = connectedTag.uninit();
415console.log("connectedTag uninit status: " + uninitStatus);
416
417// Unregister event
418connectedTag.off("notify", (err, rfState)=> {
419    if (err) {
420        console.log("connectedTag off Callback err: " + err);
421    } else {
422        console.log("connectedTag off Callback rfState: " + rfState);
423    }
424});
425```
426
427## NfcRfType
428
429表示NFC场强状态的枚举。
430
431**系统能力**:SystemCapability.Communication.ConnectedTag
432
433| 名称 | 值 | 说明 |
434| -------- | -------- | -------- |
435| NFC_RF_LEAVE | 0 | NFC离场事件 |
436| NFC_RF_ENTER | 1 | NFC进场事件 |
437