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