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