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>
> **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.
> 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**
## **Modules to Import**
```js
```js
importtagfrom'@ohos.nfc.tag';
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,