• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.connectedTag (Active Tags)
2
3The **connectedTag** module provides APIs for using active tags. You can use the APIs to initialize the active tag chip and read and write active tags.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9## Modules to Import
10
11```js
12import { connectedTag } from '@kit.ConnectivityKit';
13```
14
15## connectedTag.init<sup>(deprecated)</sup>
16
17init(): boolean
18
19Initializes the active tag chip.
20
21> **NOTE**
22>
23> This API is supported since API version 8 and deprecated since API version 9. Use [initialize](#connectedtaginitialize9) instead.
24
25**Required permissions**: ohos.permission.NFC_TAG
26
27**System capability**: SystemCapability.Communication.ConnectedTag
28
29**Return value**
30
31| **Type**| **Description**|
32| -------- | -------- |
33| boolean | **true**: The initialization is successful.&nbsp;<br>**false**: The initialization fails.|
34
35## connectedTag.initialize<sup>9+</sup>
36
37initialize(): void
38
39Initializes the active tag chip.
40
41**Required permissions**: ohos.permission.NFC_TAG
42
43**System capability**: SystemCapability.Communication.ConnectedTag
44
45**Error codes**
46
47For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [NFC Error Codes](errorcode-nfc.md).
48
49| ID| Error Message|
50| -------- | -------- |
51|201 | Permission denied.                 |
52|801 | Capability not supported.          |
53|3200101 | Connected NFC tag running state is abnormal in service. |
54
55## connectedTag.uninit<sup>(deprecated)</sup>
56
57uninit(): boolean
58
59Uninitializes the active tag resources.
60
61> **NOTE**
62>
63> This API is supported since API version 8 and deprecated since API version 9. Use [uninitialize](#connectedtaguninitialize9) instead.
64
65**Required permissions**: ohos.permission.NFC_TAG
66
67**System capability**: SystemCapability.Communication.ConnectedTag
68
69**Return value**
70
71| **Type**| **Description**|
72| -------- | -------- |
73| boolean | **true**: The uninstallation is successful.&nbsp;<br>**false**: The uninstallation fails.|
74
75## connectedTag.uninitialize<sup>9+</sup>
76
77uninitialize(): void
78
79Uninitializes the active tag resources.
80
81**Required permissions**: ohos.permission.NFC_TAG
82
83**System capability**: SystemCapability.Communication.ConnectedTag
84
85**Error codes**
86
87For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [NFC Error Codes](errorcode-nfc.md).
88
89| ID| Error Message|
90| -------- | -------- |
91|201 | Permission denied.                 |
92|801 | Capability not supported.          |
93|3200101 | Connected NFC tag running state is abnormal in service. |
94
95## connectedTag.readNdefTag<sup>(deprecated)</sup>
96
97readNdefTag(): Promise&lt;string&gt;
98
99Reads the content of this active tag. This API uses a promise to return the result.
100
101> **NOTE**
102>
103> This API is supported since API version 8 and deprecated since API version 9. Use [uninitialize](#connectedtaguninitialize9) instead.
104
105**Required permissions**: ohos.permission.NFC_TAG
106
107**System capability**: SystemCapability.Communication.ConnectedTag
108
109**Return value**
110
111| **Type**| **Description**|
112| -------- | -------- |
113| Promise&lt;string&gt; | Promise used to return the content of the active tag.|
114
115**Example**
116
117```js
118import { connectedTag } from '@kit.ConnectivityKit';
119import { BusinessError } from '@kit.BasicServicesKit';
120
121connectedTag.readNdefTag().then((data) => {
122    console.log("connectedTag readNdefTag Promise data = " + data);
123}).catch((err: BusinessError)=> {
124    console.error("connectedTag readNdefTag Promise err: " + err);
125});
126```
127
128## connectedTag.read<sup>9+</sup>
129
130read(): Promise&lt;number[]&gt;
131
132Reads the content of this active tag. This API uses a promise to return the result.
133
134**Required permissions**: ohos.permission.NFC_TAG
135
136**System capability**: SystemCapability.Communication.ConnectedTag
137
138**Return value**
139
140| **Type**| **Description**|
141| -------- | -------- |
142| Promise&lt;number[]&gt; | Promise used to return the content of the active tag.|
143
144**Error codes**
145
146For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [NFC Error Codes](errorcode-nfc.md).
147
148| ID| Error Message|
149| -------- | -------- |
150|201 | Permission denied.                 |
151|801 | Capability not supported.          |
152|3200101 | Connected NFC tag running state is abnormal in service. |
153
154**Example**
155
156```js
157import { connectedTag } from '@kit.ConnectivityKit';
158import { BusinessError } from '@kit.BasicServicesKit';
159
160connectedTag.read().then((data) => {
161    console.log("connectedTag read Promise data = " + data);
162}).catch((err: BusinessError)=> {
163    console.error("connectedTag read Promise err: " + err);
164});
165```
166
167## connectedTag.readNdefTag<sup>(deprecated)</sup>
168
169readNdefTag(callback: AsyncCallback&lt;string&gt;): void
170
171Reads the content of this active tag. This API uses an asynchronous callback to return the result.
172
173> **NOTE**
174>
175> This API is supported since API version 8 and deprecated since API version 9. Use [uninitialize](#connectedtaguninitialize9) instead.
176
177**Required permissions**: ohos.permission.NFC_TAG
178
179**System capability**: SystemCapability.Communication.ConnectedTag
180
181**Parameters**
182
183| **Name**| **Type**| **Mandatory**| **Description**|
184| -------- | -------- | -------- | -------- |
185| callback | AsyncCallback&lt;string&gt; | Yes| Callback used to return the active tag content obtained.|
186
187**Example**
188
189```js
190import { connectedTag } from '@kit.ConnectivityKit';
191
192connectedTag.readNdefTag((err, data)=> {
193    if (err) {
194        console.error("connectedTag readNdefTag AsyncCallback err: " + err);
195    } else {
196        console.log("connectedTag readNdefTag AsyncCallback data: " + data);
197    }
198});
199```
200
201## connectedTag.read<sup>9+</sup>
202
203read(callback: AsyncCallback&lt;number[]&gt;): void
204
205Reads the content of this active tag. This API uses an asynchronous callback to return the result.
206
207**Required permissions**: ohos.permission.NFC_TAG
208
209**System capability**: SystemCapability.Communication.ConnectedTag
210
211**Parameters**
212
213| **Name**| **Type**| **Mandatory**| **Description**|
214| -------- | -------- | -------- | -------- |
215| callback | AsyncCallback&lt;number[]&gt; | Yes| Callback used to return the active tag content obtained.|
216
217**Error codes**
218
219For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [NFC Error Codes](errorcode-nfc.md).
220
221| ID| Error Message|
222| -------- | -------- |
223|201 | Permission denied.                 |
224|801 | Capability not supported.          |
225|3200101 | Connected NFC tag running state is abnormal in service. |
226
227**Example**
228
229```js
230import { connectedTag } from '@kit.ConnectivityKit';
231
232connectedTag.read((err, data)=> {
233    if (err) {
234        console.error("connectedTag read AsyncCallback err: " + err);
235    } else {
236        console.log("connectedTag read AsyncCallback data: " + data);
237    }
238});
239```
240
241## connectedTag.writeNdefTag<sup>(deprecated)</sup>
242
243writeNdefTag(data: string): Promise&lt;void&gt;
244
245Writes data to this active tag. This API uses a promise to return the result.
246
247> **NOTE**
248>
249> This API is supported since API version 8 and deprecated since API version 9. Use [connectedTag.write](#connectedtagwrite9) instead.
250
251**Required permissions**: ohos.permission.NFC_TAG
252
253**System capability**: SystemCapability.Communication.ConnectedTag
254
255**Parameters**
256
257| **Name**| **Type**| **Mandatory**| **Description**|
258| -------- | -------- | -------- | -------- |
259| data | string | Yes| Data to be written to the active tag. The maximum length is 1024 bytes.|
260
261**Return value**
262
263| **Type**| **Description**|
264| -------- | -------- |
265| Promise&lt;void&gt; | Promise that returns no value.|
266
267**Example**
268
269```js
270import { connectedTag } from '@kit.ConnectivityKit';
271import { BusinessError } from '@kit.BasicServicesKit';
272
273let rawData = "010203"; // change it to be correct.
274connectedTag.writeNdefTag(rawData).then(() => {
275    console.log("connectedTag.writeNdefTag Promise success.");
276}).catch((err: BusinessError)=> {
277    console.error("connectedTag.writeNdefTag Promise err: " + err);
278});
279```
280
281## connectedTag.write<sup>9+</sup>
282
283write(data: number[]): Promise&lt;void&gt;
284
285Writes data to this active tag. This API uses a promise to return the result.
286
287**Required permissions**: ohos.permission.NFC_TAG
288
289**System capability**: SystemCapability.Communication.ConnectedTag
290
291**Parameters**
292
293| **Name**| **Type**| **Mandatory**| **Description**|
294| -------- | -------- | -------- | -------- |
295| data | number[] | Yes| Data to be written to the active tag. The value is a hexadecimal number ranging from 0x00 to 0xFF.|
296
297**Return value**
298
299| **Type**| **Description**|
300| -------- | -------- |
301| Promise&lt;void&gt; | Promise that returns no value.|
302
303**Error codes**
304
305For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [NFC Error Codes](errorcode-nfc.md).
306
307| ID| Error Message|
308| -------- | -------- |
309|201 | Permission denied.                 |
310|401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. |
311|801 | Capability not supported.          |
312|3200101 | Connected NFC tag running state is abnormal in service. |
313
314**Example**
315
316```js
317import { connectedTag } from '@kit.ConnectivityKit';
318import { BusinessError } from '@kit.BasicServicesKit';
319
320let rawData = [0x01, 0x02, 0x03]; // change it to be correct.
321connectedTag.write(rawData).then(() => {
322    console.log("connectedTag.writeNdefTag Promise success.");
323}).catch((err: BusinessError)=> {
324    console.error("connectedTag.writeNdefTag Promise err: " + err);
325});
326```
327
328## connectedTag.writeNdefTag<sup>(deprecated)</sup>
329
330writeNdefTag(data: string, callback: AsyncCallback&lt;void&gt;): void
331
332Writes data to this active tag. This API uses an asynchronous callback to return the result.
333
334> **NOTE**
335>
336> This API is supported since API version 8 and deprecated since API version 9. Use [connectedTag.write](#connectedtagwrite9) instead.
337
338**Required permissions**: ohos.permission.NFC_TAG
339
340**System capability**: SystemCapability.Communication.ConnectedTag
341
342**Parameters**
343
344| **Name**| **Type**| **Mandatory**| **Description**|
345| -------- | -------- | -------- | -------- |
346| data | string | Yes| Data to be written to the active tag. The maximum length is 1024 bytes.|
347| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the active tag content obtained.|
348
349**Example**
350
351```js
352import { connectedTag } from '@kit.ConnectivityKit';
353
354let rawData = "010203"; // change it to be correct.
355connectedTag.writeNdefTag(rawData, (err)=> {
356    if (err) {
357        console.error("connectedTag.writeNdefTag AsyncCallback err: " + err);
358    } else {
359        console.log("connectedTag.writeNdefTag AsyncCallback success.");
360    }
361});
362```
363
364## connectedTag.write<sup>9+</sup>
365
366write(data: number[], callback: AsyncCallback&lt;void&gt;): void
367
368Writes data to this active tag. This API uses an asynchronous callback to return the result.
369
370**Required permissions**: ohos.permission.NFC_TAG
371
372**System capability**: SystemCapability.Communication.ConnectedTag
373
374**Parameters**
375
376| **Name**| **Type**| **Mandatory**| **Description**|
377| -------- | -------- | -------- | -------- |
378| data | number[] | Yes| Data to be written to the active tag. The value is a hexadecimal number ranging from 0x00 to 0xFF.|
379| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the active tag content obtained.|
380
381**Error codes**
382
383For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [NFC Error Codes](errorcode-nfc.md).
384
385| ID| Error Message|
386| -------- | -------- |
387|201 | Permission denied.                 |
388|401 | The parameter check failed. Possible causes: <br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameters types.<br>3. Parameter verification failed. |
389|801 | Capability not supported.          |
390|3200101 | Connected NFC tag running state is abnormal in service. |
391
392**Example**
393
394```js
395import { connectedTag } from '@kit.ConnectivityKit';
396
397let rawData = [0x01, 0x02, 0x03]; // change it to be correct.
398connectedTag.write(rawData, (err)=> {
399    if (err) {
400        console.error("connectedTag.writeNdefTag AsyncCallback err: " + err);
401    } else {
402        console.log("connectedTag.writeNdefTag AsyncCallback success.");
403    }
404});
405```
406
407## connectedTag.on('notify')
408
409on(type: "notify", callback: Callback&lt;number&gt;): void
410
411Registers the NFC field strength state events.
412
413**Required permissions**: ohos.permission.NFC_TAG
414
415**System capability**: SystemCapability.Communication.ConnectedTag
416
417**Parameters**
418
419| **Name**| **Type**| **Mandatory**| **Description**|
420| -------- | -------- | -------- | -------- |
421| type | string | Yes| Event type. This parameter has a fixed value of **notify**.|
422| callback | Callback&lt;number&gt; | Yes| Callback used to return the [NfcRfType](#nfcrftype).|
423
424## connectedTag.off('notify')
425
426off(type: "notify", callback?: Callback&lt;number&gt;): void
427
428Unregisters the NFC field strength state events.
429
430**Required permissions**: ohos.permission.NFC_TAG
431
432**System capability**: SystemCapability.Communication.ConnectedTag
433
434**Parameters**
435
436| **Name**| **Type**| **Mandatory**| **Description**|
437| -------- | -------- | -------- | -------- |
438| type | string | Yes| Event type. This parameter has a fixed value of **notify**.|
439| callback | Callback&lt;number&gt; | No| Callback used to return the field strength state. If this parameter is not specified, all callbacks associated with the specified event will be unregistered.|
440
441**Example**
442
443```js
444import { connectedTag } from '@kit.ConnectivityKit';
445
446// Register the event.
447connectedTag.on("notify", (rfState : number)=> {
448  console.log("connectedTag on Callback rfState: " + rfState);
449});
450try {
451    connectedTag.initialize();
452    let tag = [3, 1, 0];
453    console.log("connectedTag write: tag=" + tag);
454    await connectedTag.write(tag);
455    let data = await connectedTag.read();
456    console.log("connectedTag read: data=" + data);
457    connectedTag.uninitialize();
458} catch (error) {
459    console.error("connectedTag error: " + error);
460}
461
462// Unregister the event.
463connectedTag.off("notify", (rfState : number)=> {
464  console.log("connectedTag off Callback rfState: " + rfState);
465});
466```
467
468## NfcRfType
469
470Enumerates the NFC field strength states.
471
472**System capability**: SystemCapability.Communication.ConnectedTag
473
474| Name| Value| Description|
475| -------- | -------- | -------- |
476| NFC_RF_LEAVE | 0 | NFC exit.|
477| NFC_RF_ENTER | 1 | NFC entry.|
478