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<string> 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<string> | Promise used to return the content of the active tag.| 96 97**Example** 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<number[]> 112 113Reads the content of this active tag. This API uses a promise to return the result. 114 115**Required permissions**: ohos.permission.NFC_TAG 116 117**System capability**: SystemCapability.Communication.ConnectedTag 118 119**Return value** 120 121| **Type**| **Description**| 122| -------- | -------- | 123| Promise<number[]> | Promise used to return the content of the active tag.| 124 125**Error codes** 126For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 127 128| ID| Error Message| 129| -------- | -------- | 130| 3200101 | Connected NFC tag running state is abnormal in service. | 131 132**Example** 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<string>): void 147 148Reads the content of this active tag. This API uses an asynchronous callback to return the result. 149 150**Required permissions**: ohos.permission.NFC_TAG 151 152**System capability**: SystemCapability.Communication.ConnectedTag 153 154**Parameters** 155 156| **Name**| **Type**| **Mandatory**| **Description**| 157| -------- | -------- | -------- | -------- | 158| callback | AsyncCallback<string> | Yes| Callback invoked to return the active tag content obtained.| 159 160**Example** 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<number[]>): void 177 178Reads the content of this active tag. This API uses an asynchronous callback to return the result. 179 180**Required permissions**: ohos.permission.NFC_TAG 181 182**System capability**: SystemCapability.Communication.ConnectedTag 183 184**Parameters** 185 186| **Name**| **Type**| **Mandatory**| **Description**| 187| -------- | -------- | -------- | -------- | 188| callback | AsyncCallback<number[]> | Yes| Callback invoked to return the active tag content obtained.| 189 190**Error codes** 191For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 192 193| ID| Error Message| 194| -------- | -------- | 195| 3200101 | Connected NFC tag running state is abnormal in service. | 196 197**Example** 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<void> 214 215Writes data to this active tag. This API uses a promise to return the result. 216 217**Required permissions**: ohos.permission.NFC_TAG 218 219**System capability**: SystemCapability.Communication.ConnectedTag 220 221**Parameters** 222 223| **Name**| **Type**| **Mandatory**| **Description**| 224| -------- | -------- | -------- | -------- | 225| data | string | Yes| Data to write. The maximum length is 1024 bytes.| 226 227**Return value** 228 229| **Type**| **Description**| 230| -------- | -------- | 231| Promise<void> | Promise that returns no value.| 232 233**Example** 234 235```js 236import connectedTag from '@ohos.connectedTag'; 237 238var rawData = "010203"; // Set it as required. 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<void> 249 250Writes data to this active tag. This API uses a promise to return the result. 251 252**Required permissions**: ohos.permission.NFC_TAG 253 254**System capability**: SystemCapability.Communication.ConnectedTag 255 256**Parameters** 257 258| **Name**| **Type**| **Mandatory**| **Description**| 259| -------- | -------- | -------- | -------- | 260| data | number[] | Yes| Data to be written to the active tag. The value is a hexadecimal number ranging from 0x00 to 0xFF.| 261 262**Return value** 263 264| **Type**| **Description**| 265| -------- | -------- | 266| Promise<void> | Promise that returns no value.| 267 268**Error codes** 269For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 270 271| ID| Error Message| 272| -------- | -------- | 273| 3200101 | Connected NFC tag running state is abnormal in service. | 274 275**Example** 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<void>): void 291 292Writes data to this active tag. This API uses an asynchronous callback to return the result. 293 294**Required permissions**: ohos.permission.NFC_TAG 295 296**System capability**: SystemCapability.Communication.ConnectedTag 297 298**Parameters** 299 300| **Name**| **Type**| **Mandatory**| **Description**| 301| -------- | -------- | -------- | -------- | 302| data | string | Yes| Data to write. The maximum length is 1024 bytes.| 303| callback | AsyncCallback<void> | Yes| Callback invoked to return the active tag content obtained.| 304 305**Example** 306 307```js 308import connectedTag from '@ohos.connectedTag'; 309 310var rawData = "010203"; // Set it as required. 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<void>): void 323 324Writes data to this active tag. This API uses an asynchronous callback to return the result. 325 326**Required permissions**: ohos.permission.NFC_TAG 327 328**System capability**: SystemCapability.Communication.ConnectedTag 329 330**Parameters** 331 332| **Name**| **Type**| **Mandatory**| **Description**| 333| -------- | -------- | -------- | -------- | 334| data | number[] | Yes| Data to be written to the active tag. The value is a hexadecimal number ranging from 0x00 to 0xFF.| 335| callback | AsyncCallback<void> | Yes| Callback invoked to return the active tag content obtained.| 336 337**Error codes** 338For details about the error codes, see [NFC Error Codes](../errorcodes/errorcode-nfc.md). 339 340| ID| Error Message| 341| -------- | -------- | 342| 3200101 | Connected NFC tag running state is abnormal in service. | 343 344**Example** 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<number>): void 362 363Registers the NFC field strength state events. 364 365**Required permissions**: ohos.permission.NFC_TAG 366 367**System capability**: SystemCapability.Communication.ConnectedTag 368 369**Parameters** 370 371| **Name**| **Type**| **Mandatory**| **Description**| 372| -------- | -------- | -------- | -------- | 373| type | string | Yes| Event type. The value is **notify**.| 374| callback | Callback<number> | Yes| Callback used to return the [NfcRfType](#nfcrftype).| 375 376## connectedTag.off('notify') 377 378off(type: "notify", callback?: Callback<number>): void 379 380Unregisters the NFC field strength state events. 381 382**Required permissions**: ohos.permission.NFC_TAG 383 384**System capability**: SystemCapability.Communication.ConnectedTag 385 386**Parameters** 387 388| **Name**| **Type**| **Mandatory**| **Description**| 389| -------- | -------- | -------- | -------- | 390| type | string | Yes| Event type. The value is **notify**.| 391| callback | Callback<number> | 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.| 392 393**Example** 394 395```js 396import connectedTag from '@ohos.connectedTag'; 397 398// Register the 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 connected tag business operations here. 411// connectedTag.writeNdefTag(rawData) 412// connectedTag.readNdefTag() 413 414var uninitStatus = connectedTag.uninit(); 415console.log("connectedTag uninit status: " + uninitStatus); 416 417// Unregister the 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 429Enumerates the NFC field strength states. 430 431**System capability**: SystemCapability.Communication.ConnectedTag 432 433| Name| Value| Description| 434| -------- | -------- | -------- | 435| NFC_RF_LEAVE | 0 | Field off.| 436| NFC_RF_ENTER | 1 | Field on.| 437