• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Active Tag
2
3> ![icon-note.gif](public_sys-resources/icon-note.gif)**NOTE**<br/>
4> 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.
5
6
7## Modules to Import
8
9```
10import connectedTag from '@ohos.connectedTag';
11```
12
13
14## connectedTag.init
15
16init(): boolean
17
18Initializes the active tag chip.
19
20**Required permissions**: ohos.permission.NFC_TAG
21
22**System capability**: SystemCapability.Communication.ConnectedTag
23
24- Return value
25  | **Type**| **Description**|
26  | -------- | -------- |
27  | boolean | Returns **true** if the initialization is successful; returns **false** otherwise.|
28
29
30## connectedTag.uninit
31
32uninit(): boolean
33
34Uninitializes the active tag resources.
35
36**Required permissions**: ohos.permission.NFC_TAG
37
38**System capability**: SystemCapability.Communication.ConnectedTag
39
40- Return value
41  | **Type**| **Description**|
42  | -------- | -------- |
43  | boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
44
45
46## connectedTag.readNdefTag
47
48readNdefTag(): Promise&lt;string&gt;
49
50Reads the content of this active tag. This method uses a promise to return the result.
51
52**Required permissions**: ohos.permission.NFC_TAG
53
54**System capability**: SystemCapability.Communication.ConnectedTag
55
56- Return value
57  | **Type**| **Description**|
58  | -------- | -------- |
59  | Promise&lt;string&gt; | Promise used to return the content of the active tag.|
60
61- Example
62  ```
63  import connectedTag from '@ohos.connectedTag';
64
65  connectedTag.readNdefTag().then(result => {
66      console.log("promise recv ndef response: " + result);
67  });
68  ```
69
70## connectedTag.readNdefTag
71
72readNdefTag(callback: AsyncCallback&lt;string&gt;): void
73
74Reads the content of this active tag. This method uses an asynchronous callback to return the result.
75
76**Required permissions**: ohos.permission.NFC_TAG
77
78**System capability**: SystemCapability.Communication.ConnectedTag
79
80- Parameters
81  | **Name**| **Type**| **Mandatory**| **Description**|
82  | -------- | -------- | -------- | -------- |
83  | callback | AsyncCallback&lt;string&gt; | Yes| Callback invoked to return the active tag content obtained.|
84
85- Example
86  ```
87  import connectedTag from '@ohos.connectedTag';
88
89  connectedTag.readNdefTag(result => {
90      console.log("callback recv ndef response: " + result);
91  });
92  ```
93
94## connectedTag.writeNdefTag
95
96writeNdefTag(data: string): Promise&lt;void&gt;
97
98Writes data to this active tag. This method uses a promise to return the result.
99
100**Required permissions**: ohos.permission.NFC_TAG
101
102**System capability**: SystemCapability.Communication.ConnectedTag
103
104- Parameters
105  | **Name**| **Type**| **Mandatory**| **Description**|
106  | -------- | -------- | -------- | -------- |
107  | data | string | Yes| Data to write. The maximum length is 1024 bytes.|
108
109- Return value
110  | **Type**| **Description**|
111  | -------- | -------- |
112  | Promise&lt;void&gt; | Promise used to return the result. This method returns no value.|
113
114- Example
115  ```
116  import connectedTag from '@ohos.connectedTag';
117
118  writeNdefTag.write("010203")
119      .then((value) => {
120          // Data is written to the tag.
121          console.log(`success to write event: ${value}`);
122      }).catch((err) => {
123          // Failed to write data to the tag.
124          console.error(`failed to write event because ${err.code}`);
125      });
126  ```
127
128## connectedTag.writeNdefTag
129
130writeNdefTag(data: string, callback: AsyncCallback&lt;string&gt;): void
131
132Writes data to this active tag. This method uses an asynchronous callback to return the result.
133
134**Required permissions**: ohos.permission.NFC_TAG
135
136**System capability**: SystemCapability.Communication.ConnectedTag
137
138- Parameters
139  | **Name**| **Type**| **Mandatory**| **Description**|
140  | -------- | -------- | -------- | -------- |
141  | data | string | Yes| Data to write. The maximum length is 1024 bytes.|
142  | callback | AsyncCallback&lt;string&gt; | Yes| Callback invoked to return the operation result.|
143
144- Example
145  ```
146  import connectedTag from '@ohos.connectedTag';
147
148  connectedTag.writeNdefTag("010203", (err, value) => {
149      if (err) {
150          // Failed to write data to the tag.
151          console.error(`failed to write event because ${err.code}`);
152          return;
153      }
154
155      // Data is written to the tag.
156      console.log(`success to write event: ${value}`);
157  });
158  ```
159
160## connectedTag.on('notify')
161
162on(type: "notify", callback: Callback&lt;number&gt;): void
163
164Registers the NFC field strength state events.
165
166**Required permissions**: ohos.permission.NFC_TAG
167
168**System capability**: SystemCapability.Communication.ConnectedTag
169
170- Parameters
171  | **Name**| **Type**| **Mandatory**| **Description**|
172  | -------- | -------- | -------- | -------- |
173  | type | string | Yes| Event type. The value is **notify**.|
174  | callback | Callback&lt;number&gt; | Yes| Callback invoked to return the field strength state.|
175
176- Enumerates the field strength states.
177  | **Value**| **Description**|
178  | -------- | -------- |
179  | 0 | Field off. |
180  | 1 | Field on. |
181
182
183## connectedTag.off('notify')
184
185off(type: "notify", callback?: Callback&lt;number&gt;): void
186
187Unregisters the NFC field strength state events.
188
189**Required permissions**: ohos.permission.NFC_TAG
190
191**System capability**: SystemCapability.Communication.ConnectedTag
192
193- Parameters
194  | **Name**| **Type**| **Mandatory**| **Description**|
195  | -------- | -------- | -------- | -------- |
196  | type | string | Yes| Event type. The value is **notify**.|
197  | 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.|
198
199- Example
200  ```
201  import connectedTag from '@ohos.connectedTag';
202
203  var NFC_RF_NOTIFY = "notify";
204
205  var recvNfcRfNotifyFunc = result => {
206      console.info("nfc rf receive state: " + result);
207  }
208
209  // Register event
210  connectedTag.on(NFC_RF_NOTIFY, recvNfcRfNotifyFunc);
211
212  // Unregister event
213  connectedTag.off(NFC_RF_NOTIFY, recvNfcRfNotifyFunc);
214  ```
215
216## NfcRfType
217
218Enumerates the NFC states.
219
220| Name| Default Value| Description|
221| -------- | -------- | -------- |
222| NFC_RF_LEAVE | 0 | Field off. |
223| NFC_RF_ENTER | 1 | Field on. |
224