The **nfcTag** module provides methods for managing Near-Field Communication (NFC) tags.
The **nfcTag** module provides APIs for managing Near-Field Communication (NFC) tags.
> **NOTE**<br>
> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## **Declaration**
Before developing applications related to tag read and write, you must declare NFC-related attributes in the attribute configuration file of the applications. For example, declare the following attributes in the **module.json5** file:
```js
{
"module":{
// Attributes to declare.
"abilities":[
{
"skills":[
{
"actions":[
// Actions to declare.
// Add the nfc tag action.
"ohos.nfc.tag.action.TAG_FOUND"
]
}
],
"metadata":[
{
"name":"tag-tech",
"value":"NfcA"
},
{
"name":"tag-tech",
"value":"IsoDep"
},
// Add other technologies,
// such as NfcB, NfcF, NfcV, Ndef, MifareClassic, MifareUL, and NdefFormatable.
]
}
],
"requestPermissions":[
"name":"ohos.permission.NFC_TAG",
"reason":"tag",
]
}
}
```
> **CAUTION**<br>
>
> - The **actions** field is mandatory. It must be **ohos.nfc.tag.action.TAG_FOUND** and cannot be changed.
> - The **name** field of **metadata** is mandatory. It must be **tag-tech** and cannot be changed.
> - The **value** field of **metadata** is mandatory. It can be **NfcA**, **NfcB**, **NfcF**, **NfcV**, **IsoDep**, **Ndef**, **MifareClassic**, **MifareUL**, **NdefFormatable** or their combinations. Incorrect setting of this field will cause a parsing failure.
> - The **name** field of **requestPermissions** is mandatory. It must be **ohos.permission.NFC_TAG** and cannot be changed.
## **Modules to Import**
```js
importtagfrom'@ohos.nfc.tag';
```
## **tag.TagInfo**
Before reading or writing data to a card with tags, the application must obtain **TagInfo** to determine the tag technologies supported by the card. Then, the application can invoke the correct API to communicate with the card.
```js
importtagfrom'@ohos.nfc.tag';
onCreate(want,launchParam){
// Add other code here.
// want is initialized by the NFC service and contains taginfo.
vartagInfo=tag.getTagInfo(want);
if(tagInfo==undefined){
console.log("no TagInfo to be created, ignore it.");
return;
}
varisNfcATag=false;
for(vari=0;i<tagInfo.technology.length;i++){
if(tagInfo.technology[i]==tag.NFC_A){
isNfcATag=true;
break;
}
// Also check for technology tag.NFC_B, NFC_F, NFC_V, ISO_DEP, NDEF, MIFARE_CLASSIC, MIFARE_ULTRALIGHT, and NDEF_FORMATABLE.
}
if(isNfcATag){
varnfcA=tag.getNfcATag(taginfo);
// Other code to read or write this tag.
}
// use the same code to handle "NfcA/NfcB/NfcF/NfcV/IsoDep/Ndef/MifareClassic/MifareUL/NdefFormatable", such as,
| ndefRecords | [NdefRecord](#ndefrecord9)[] | Yes| A list of NDEF records.|
| ndefRecords | [NdefRecord](js-apis-nfcTag.md#ndefrecord9)[] | Yes| NDEF records used to create the NDEF message. For details, see *NFCForum-TS-NDEF_1.0*.|
| Promise<boolean> | Promise used to return the result. If the tag can be set to read-only, **true** is returned; otherwise, **false** is returned.|
| boolean| Returns **true** if the tag can be set to read-only; returns **false** otherwise.|
**Example**
```js
importtagfrom'@ohos.nfc.tag';
// tagInfo is an object given by the NFC service when a tag is dispatched.
| callback | AsyncCallback\<boolean> | Yes | Callback invoked to return the result. If the tag can be set to read-only, **true** is returned; otherwise, **false** is returned.|
**Example**
```js
importtagfrom'@ohos.nfc.tag';
// tagInfo is an object given by the NFC service when a tag is dispatched.
Authenticates a sector using the key. The sector can be accessed only after the authentication is successful. This API uses an asynchronous callback to return the result.
...
...
@@ -916,16 +917,21 @@ Authenticates a sector using the key. The sector can be accessed only after the
```js
importtagfrom'@ohos.nfc.tag';
// tagInfo is an object given by the NFC service when a tag is dispatched.
| message | [NdefMessage](#ndefmessage9) | Yes | NDEF message to write when the formatting is successful. If this parameter is **null**, the tag is formatted only.|
| callback | AsyncCallback\<number> |Yes|Callback invoked to return the result.|
| message | [NdefMessage](#ndefmessage9) | Yes | NDEF message to write when the formatting is successful. If this parameter is **null**, the tag is formatted only (no data will be written).|
| message | [NdefMessage](#ndefmessage9) | Yes | NDEF message to write when the formatting is successful. If this parameter is **null**, the tag is formatted only.|
| callback | AsyncCallback\<number> |Yes|Callback invoked to return the result.|
| message | [NdefMessage](#ndefmessage9) | Yes | NDEF message to write when the formatting is successful. If this parameter is **null**, the tag is formatted only (no data will be written).|