1# @ohos.connectedTag 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.uninit 32 33uninit(): boolean 34 35Uninitializes the active tag resources. 36 37**Required permissions**: ohos.permission.NFC_TAG 38 39**System capability**: SystemCapability.Communication.ConnectedTag 40 41**Return value** 42 43| **Type**| **Description**| 44| -------- | -------- | 45| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 46 47## connectedTag.readNdefTag 48 49readNdefTag(): Promise<string> 50 51Reads the content of this active tag. This API uses a promise to return the result. 52 53**Required permissions**: ohos.permission.NFC_TAG 54 55**System capability**: SystemCapability.Communication.ConnectedTag 56 57**Return value** 58 59| **Type**| **Description**| 60| -------- | -------- | 61| Promise<string> | Promise used to return the content of the active tag.| 62 63**Example** 64 65```js 66import connectedTag from '@ohos.connectedTag'; 67 68connectedTag.readNdefTag().then((data) => { 69 console.log("connectedTag readNdefTag Promise data = " + data); 70}).catch((err)=> { 71 console.log("connectedTag readNdefTag Promise err: " + err); 72}); 73``` 74 75## connectedTag.readNdefTag 76 77readNdefTag(callback: AsyncCallback<string>): void 78 79Reads the content of this active tag. This API uses an asynchronous callback to return the result. 80 81**Required permissions**: ohos.permission.NFC_TAG 82 83**System capability**: SystemCapability.Communication.ConnectedTag 84 85**Parameters** 86 87| **Name**| **Type**| **Mandatory**| **Description**| 88| -------- | -------- | -------- | -------- | 89| callback | AsyncCallback<string> | Yes| Callback invoked to return the active tag content obtained.| 90 91**Example** 92 93```js 94import connectedTag from '@ohos.connectedTag'; 95 96connectedTag.readNdefTag((err, data)=> { 97 if (err) { 98 console.log("connectedTag readNdefTag AsyncCallback err: " + err); 99 } else { 100 console.log("connectedTag readNdefTag AsyncCallback data: " + data); 101 } 102}); 103``` 104 105## connectedTag.writeNdefTag 106 107writeNdefTag(data: string): Promise<void> 108 109Writes data to this active tag. This API uses a promise to return the result. 110 111**Required permissions**: ohos.permission.NFC_TAG 112 113**System capability**: SystemCapability.Communication.ConnectedTag 114 115**Parameters** 116 117| **Name**| **Type**| **Mandatory**| **Description**| 118| -------- | -------- | -------- | -------- | 119| data | string | Yes| Data to write. The maximum length is 1024 bytes.| 120 121**Return value** 122 123| **Type**| **Description**| 124| -------- | -------- | 125| Promise<void> | Promise that returns no value.| 126 127**Example** 128 129```js 130import connectedTag from '@ohos.connectedTag'; 131 132var rawData = "010203"; // change it tobe correct. 133connectedTag.writeNdefTag(rawData).then(() => { 134 console.log("connectedTag writeNdefTag Promise success."); 135}).catch((err)=> { 136 console.log("connectedTag writeNdefTag Promise err: " + err); 137}); 138``` 139 140## connectedTag.writeNdefTag 141 142writeNdefTag(data: string, callback: AsyncCallback<void>): void 143 144Writes data to this active tag. This API uses an asynchronous callback to return the result. 145 146**Required permissions**: ohos.permission.NFC_TAG 147 148**System capability**: SystemCapability.Communication.ConnectedTag 149 150**Parameters** 151 152| **Name**| **Type**| **Mandatory**| **Description**| 153| -------- | -------- | -------- | -------- | 154| data | string | Yes| Data to write. The maximum length is 1024 bytes.| 155| callback | AsyncCallback<void> | Yes| Callback invoked to return the active tag content obtained.| 156 157**Example** 158 159```js 160import connectedTag from '@ohos.connectedTag'; 161 162var rawData = "010203"; // change it tobe correct. 163connectedTag.writeNdefTag(rawData, (err)=> { 164 if (err) { 165 console.log("connectedTag writeNdefTag AsyncCallback err: " + err); 166 } else { 167 console.log("connectedTag writeNdefTag AsyncCallback success."); 168 } 169}); 170``` 171 172## connectedTag.on('notify') 173 174on(type: "notify", callback: Callback<number>): void 175 176Registers the NFC field strength state events. 177 178**Required permissions**: ohos.permission.NFC_TAG 179 180**System capability**: SystemCapability.Communication.ConnectedTag 181 182**Parameters** 183 184| **Name**| **Type**| **Mandatory**| **Description**| 185| -------- | -------- | -------- | -------- | 186| type | string | Yes| Event type. The value is **notify**.| 187| callback | Callback<number> | Yes| Callback used to return the [NfcRfType](#nfcrftype).| 188 189## connectedTag.off('notify') 190 191off(type: "notify", callback?: Callback<number>): void 192 193Unregisters the NFC field strength state events. 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| type | string | Yes| Event type. The value is **notify**.| 204| 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.| 205 206**Example** 207 208```js 209import connectedTag from '@ohos.connectedTag'; 210 211// Register the event. 212connectedTag.on("notify", (err, rfState)=> { 213 if (err) { 214 console.log("connectedTag on Callback err: " + err); 215 } else { 216 console.log("connectedTag on Callback rfState: " + rfState); 217 } 218}); 219 220var initStatus = connectedTag.init(); 221console.log("connectedTag init status: " + initStatus); 222 223// Add nfc connecected tag business oprations here... 224// connectedTag.writeNdefTag(rawData) 225// connectedTag.readNdefTag() 226 227var uninitStatus = connectedTag.uninit(); 228console.log("connectedTag uninit status: " + uninitStatus); 229 230// Unregister the event. 231connectedTag.off("notify", (err, rfState)=> { 232 if (err) { 233 console.log("connectedTag off Callback err: " + err); 234 } else { 235 console.log("connectedTag off Callback rfState: " + rfState); 236 } 237}); 238``` 239 240## NfcRfType 241 242Enumerates the NFC field strength states. 243 244**System capability**: SystemCapability.Communication.ConnectedTag 245 246| Name| Value| Description| 247| -------- | -------- | -------- | 248| NFC_RF_LEAVE | 0 | Field off.| 249| NFC_RF_ENTER | 1 | Field on.| 250