# nfctech (Standard NFC Technologies) The **nfctech** module provides APIs for reading and writing tags that use different Near-Field Communication (NFC) technologies. > **NOTE** > > 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. ## Modules to Import ```js import { tag } from '@kit.ConnectivityKit'; ``` ## NfcATag Provides APIs to access NFC-A (ISO 14443-3A) properties and perform I/O operations on a tag. This class inherits from **[TagSession](js-apis-tagSession.md)**. **TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md). The following describes the unique APIs of **NfcATag**. ### NfcATag.getSak getSak(): number Obtains the SAK value of this NFC-A tag. **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | number | SAK value obtained. The SAK is a hexadecimal number ranging from **0x00** to **0xFF**. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcA' correctly. let sak : number = nfcA.getSak(); console.info("nfcA sak: " + sak); ``` ### NfcATag.getAtqa getAtqa(): number[] Obtains the ATQA value of this NFC-A tag. **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | number[] | ATQA value obtained. Each number of the ATQA is a hexadecimal number ranging from **0x00** to **0xFF**. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcA' correctly. let atqa : number[] = nfcA.getAtqa(); console.info("nfcA atqa: " + atqa); ``` ## NfcBTag Provides APIs to access NFC-B (ISO 14443-3B) properties and perform I/O operations on a tag. This class inherits from **TagSession**. **TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md). The following describes the unique APIs of **NfcBTag**. ### NfcBTag.getRespAppData getRespAppData(): number[] Obtains the application data of this NFC-B tag. **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | number[] | Application data obtained, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcB' correctly. let respAppData : number[] = nfcB.getRespAppData(); console.info("nfcB respAppData: " + respAppData); ``` ### NfcBTag.getRespProtocol getRespProtocol(): number[] Obtains the protocol information of this NFC-B tag. **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | number[] | Protocol information obtained, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.| **Example** ```js import { tag } from '@kit.ConnectivityKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcB' correctly. let respProtocol : number[] = nfcB.getRespProtocol(); console.info("nfcB respProtocol: " + respProtocol); ``` ## NfcFTag Provides APIs to access NFC-F (JIS 6319-4) properties and perform I/O operations on a tag. This class inherits from **TagSession**. **TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md). The following describes the unique APIs of **NfcFTag**. ### NfcFTag.getSystemCode getSystemCode(): number[] Obtains the system code from this NFC-F tag. **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | number[] | System code obtained, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.| **Example** ```js import { tag } from '@kit.ConnectivityKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcF' correctly. let systemCode : number[] = nfcF.getSystemCode(); console.info("nfcF systemCode: " + systemCode); ``` ### NfcFTag.getPmm getPmm(): number[] Obtains the PMm (consisting of the IC code and manufacturer parameters) information from this NFC-F tag. **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | number[] | PMm information obtained, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.| **Example** ```js import { tag } from '@kit.ConnectivityKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcF' correctly. let pmm : number[] = nfcF.getPmm(); console.info("nfcF pmm: " + pmm); ``` ## NfcVTag Provides APIs to access NFC-V (ISO 15693) properties and perform I/O operations on a tag. This class inherits from **TagSession**. **TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md). The following describes the unique APIs of **NfcVTag**. ### NfcvTag.getResponseFlags getResponseFlags(): number Obtains the response flags from this NFC-V tag. **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | number | Response flags obtained, which consist of hexadecimal numbers ranging from **0x00** to **0xFF**.| **Example** ```js import { tag } from '@kit.ConnectivityKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcV' correctly. let responseFlags : number = nfcV.getResponseFlags(); console.info("nfcV responseFlags: " + responseFlags); ``` ### NfcvTag.getDsfId getDsfId(): number Obtains the data storage format identifier (DSFID) from this NFC-V tag. **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | number | DSFID obtained, which consists of hexadecimal numbers ranging from **0x00** to **0xFF**.| **Example** ```js import { tag } from '@kit.ConnectivityKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'nfcV' correctly. let dsfId : number = nfcV.getDsfId(); console.info("nfcV dsfId: " + dsfId); ``` ## IsoDepTag9+ Provides APIs to access ISO-DEP (ISO 14443-4) properties and I/O operations on a tag. This class inherits from **TagSession**. **TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md). The following describes the unique APIs of **IsoDepTag**. ### IsoDepTag.getHistoricalBytes9+ getHistoricalBytes(): number[] Obtains the historical bytes for the given tag. This API applies only to the IsoDep cards that use the NFC-A technology. **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | number[] | Historical bytes obtained, which consist of hexadecimal numbers ranging from **0x00** to **0xFF**. If the IsoDep tag uses the NFC-B technology, **null** will be returned.| **Example** ```js import { tag } from '@kit.ConnectivityKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'isoDep' correctly. let historicalBytes : number[] = isoDep.getHistoricalBytes(); console.info("isoDep historicalBytes: " + historicalBytes); ``` ### IsoDepTag.getHiLayerResponse9+ getHiLayerResponse(): number[] Obtains the higher-layer response bytes for the given tag. This API applies only to the IsoDep cards that use the NFC-B technology. **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | number[] | Higher-layer response bytes obtained, which consist of hexadecimal numbers ranging from **0x00** to **0xFF**. If the IsoDep tag uses the NFC-A technology, **null** will be returned.| **Example** ```js import { tag } from '@kit.ConnectivityKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'isoDep' correctly. let hiLayerResponse : number[] = isoDep.getHiLayerResponse(); console.info("isoDep hiLayerResponse: " + hiLayerResponse); ``` ### IsoDepTag.isExtendedApduSupported9+ isExtendedApduSupported(): Promise<boolean> Checks whether an extended application protocol data unit (APDU) is supported. This API uses a promise to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | Promise<boolean> | Promise used to return the result. The value **true** means the extended APDU is supported; the value **false** means the opposite.| **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'isoDep' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!isoDep.isTagConnected()) { if (!isoDep.connectTag()) { console.error("isoDep connectTag failed."); return; } } try { isoDep.isExtendedApduSupported().then((response: boolean) => { console.info("isoDep isExtendedApduSupported Promise response: " + response); }).catch((err: BusinessError) => { console.error("isoDep isExtendedApduSupported Promise Code: ${err.code}, message: ${err.message}"); }); } catch (businessError) { console.error("isoDep isExtendedApduSupported Promise Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } } ``` ### IsoDepTag.isExtendedApduSupported9+ isExtendedApduSupported(callback: AsyncCallback\): void Checks whether an extended APDU is supported. This API uses an asynchronous callback to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | callback | AsyncCallback\ | Yes | Callback used to return the result. If the extended APDU is supported, **true** is returned; otherwise, **false** is returned. | **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'isoDep' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!isoDep.isTagConnected()) { if (!isoDep.connectTag()) { console.error("isoDep connectTag failed."); return; } } try { isoDep.isExtendedApduSupported((err: BusinessError, response: boolean) => { if (err) { console.error("isoDep isExtendedApduSupported AsyncCallback Code: ${err.code}, message: ${err. message}"); } else { console.info("isoDep isExtendedApduSupported AsyncCallback response: " + response); } }); } catch (busiErr) { console.error("isoDep isExtendedApduSupported AsyncCallback Code: ${(businessError as Business).code}, " + "message: ${(businessError as Business).message}"); } } ``` ## NdefMessage9+ ### NdefMessage.getNdefRecords9+ getNdefRecords(): [tag.NdefRecord](js-apis-nfcTag.md#ndefrecord9)[] Obtains all NDEF records. **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | [tag.NdefRecord](js-apis-nfcTag.md#ndefrecord9)[] | List of NDEF records obtained. For details, see *NFCForum-TS-NDEF_1.0*. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; // Obtain ndefMessage from tag.ndef.createNdefMessage or ndefTag.getNdefMessage. // let ndefMessage : tag.NdefMessage = tag.ndef.createNdefMessage(...); // let ndefMessage : tag.NdefMessage = ndefTag.getNdefMessage(); let ndefRecords : tag.NdefRecord[] = ndefMessage.getNdefRecords(); console.info("ndef ndefRecords number: " + ndefRecords.length); ``` ## NdefTag9+ Provides APIs to access the tags in the NFC Data Exchange Format (NDEF). This class inherits from **TagSession**. **TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md). The following describes the unique APIs of **NdefTag**. ### NdefTag.getNdefTagType9+ getNdefTagType(): [tag.NfcForumType](js-apis-nfcTag.md#nfcforumtype9) Obtains the NDEF tag type. **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | [tag.NfcForumType](js-apis-nfcTag.md#nfcforumtype9) | NDEF tag type obtained. It can be NFC FORUM TYPE 1, 2, 3, or 4.| **Example** ```js import { tag } from '@kit.ConnectivityKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly. let ndefTagType : tag.NfcForumType = ndefTag.getNdefTagType(); console.info("ndef ndefTagType: " + ndefTagType); ``` ### NdefTag.getNdefMessage9+ getNdefMessage(): [NdefMessage](#ndefmessage9) Obtains the NDEF message from this NDEF tag. **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | [NdefMessage](#ndefmessage9) | NDEF message created. For details, see *NFCForum-TS-NDEF_1.0*.| **Example** ```js import { tag } from '@kit.ConnectivityKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly. let ndefMessage : tag.NdefMessage = ndefTag.getNdefMessage(); console.info("ndef ndefMessage: " + ndefMessage); ``` ### NdefTag.isNdefWritable9+ isNdefWritable(): boolean Check whether this NDEF tag is writable. Before calling the data write API, check whether the write operation is supported. **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | boolean | Promise used to return the result. If the tag is writable, **true** is returned; otherwise, **false** is returned.| **Example** ```js import { tag } from '@kit.ConnectivityKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly. let isWritable : boolean = ndefTag.isNdefWritable(); console.info("ndef isNdefWritable: " + isWritable); ``` ### NdefTag.readNdef9+ readNdef(): Promise\<[NdefMessage](#ndefmessage9)> Reads the NDEF message from this tag. This API uses a promise to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | Promise\<[NdefMessage](#ndefmessage9)> | Promise used to return the message object read.| **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly. function nfcTechDemo(){ // Connect to the tag if it is not connected. if (!ndefTag.isTagConnected()) { if (!ndefTag.connectTag()) { console.error("ndefTag connectTag failed."); return; } } try { ndefTag.readNdef().then((ndefmessage : tag.NdefMessage) => { console.info("ndef readNdef Promise ndefmessage: " + ndefmessage); }).catch((err : BusinessError)=> { console.error("ndef readNdef Promise err Code: ${err.code}, message: ${err.message}"); }); } catch (businessError) { console.error("ndef readNdef Promise catch businessError Code: ${(businessError as BusinessError).code}, " + "message: ${(businessError as BusinessError).message}"); } } ``` ### NdefTag.readNdef9+ readNdef(callback: AsyncCallback\<[NdefMessage](#ndefmessage9)>): void Reads the NDEF message from this tag. This API uses an asynchronous callback to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | callback | AsyncCallback\<[NdefMessage](#ndefmessage9)> | Yes | Callback used to return the NDEF message read.| **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!ndefTag.isTagConnected()) { if (!ndefTag.connectTag()) { console.error("ndefTag connectTag failed."); return; } } try { ndefTag.readNdef((err : BusinessError, ndefmessage : tag.NdefMessage)=> { if (err) { console.error("ndef readNdef AsyncCallback err Code: ${err.code}, message: ${err.message}"); } else { console.info("ndef readNdef AsyncCallback ndefmessage: " + ndefmessage); } }); } catch (businessError) { console.error("ndef readNdef AsyncCallback catch Code: ${(businessError : BusinessError).code}," + " message: ${(businessError : BusinessError).message}"); } } ``` ### NdefTag.writeNdef9+ writeNdef(msg: [NdefMessage](#ndefmessage9)): Promise\ Writes an NDEF message to this tag. This API uses a promise to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | msg | [NdefMessage](#ndefmessage9) | Yes | NDEF message to write.| **Return value** | Type | Description | | ------------------------- | ------------------ | | Promise\ | Promise that returns no value. | **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly. // NDEF message created from raw data, such as: let ndefMessage : tag.NdefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // It must be parsed as NDEF Record. // or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[]) function nfcTechDemo() { // Connect to the tag if it is not connected. if (!ndefTag.isTagConnected()) { if (!ndefTag.connectTag()) { console.error("ndefTag connectTag failed."); return; } } try { ndefTag.writeNdef(ndefMessage).then(() => { console.info("ndef writeNdef Promise success."); }).catch((err : BusinessError)=> { console.error("ndef writeNdef err Code: ${err.code}, message: ${err.message}"); }); } catch (businessError) { console.error("ndef writeNdef Promise catch businessError Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } } ``` ### NdefTag.writeNdef9+ writeNdef(msg: [NdefMessage](#ndefmessage9), callback: AsyncCallback\): void Writes an NDEF message to this tag. This API uses an asynchronous callback to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | msg | [NdefMessage](#ndefmessage9) | Yes | NDEF message to write. | | callback | AsyncCallback\ | Yes | Callback used to return the result. | **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly. // NDEF message created from raw data, such as: let ndefMessage : tag.NdefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // It must be parsed as NDEF Record. // or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[]) function nfcTechDemo() { // Connect to the tag if it is not connected. if (!ndefTag.isTagConnected()) { if (!ndefTag.connectTag()) { console.error("ndefTag connectTag failed."); return; } } try { ndefTag.writeNdef(ndefMessage, (err : BusinessError)=> { if (err) { console.error("ndef writeNdef AsyncCallback Code: ${err.code}, message: ${err.message}"); } else { console.info("ndef writeNdef AsyncCallback success."); } }); } catch (businessError) { console.error("ndef writeNdef AsyncCallback catch businessError Code: ${(businessError as Businsess).code}," + " message: ${(businessError as Businsess).message}"); } } ``` ### NdefTag.canSetReadOnly9+ canSetReadOnly(): boolean Checks whether this NDEF tag can be set to read-only. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | boolean| Returns **true** if the tag can be set to read-only; returns **false** otherwise. | **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 3100201 | Tag running state is abnormal in service. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly. let canSetReadOnly : boolean = ndefTag.canSetReadOnly(); console.info("ndef canSetReadOnly: " + canSetReadOnly); ``` ### NdefTag.setReadOnly9+ setReadOnly(): Promise\ Sets this NDEF tag to read-only. This API uses a promise to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Return value** | Type | Description | | ------------------------- | ------------------ | | Promise\ | Promise that returns no value. | **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!ndefTag.isTagConnected()) { if (!ndefTag.connectTag()) { console.error("ndefTag connectTag failed."); return; } } try { ndefTag.setReadOnly().then(() => { console.info("ndef setReadOnly Promise success."); }).catch((err : BusinessError)=> { console.error("ndef setReadOnly Promise err Code: ${err.code}, message: ${err.message}"); }); } catch (businessError) { console.error("ndef setReadOnly Promise catch businessError Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } } ``` ### NdefTag.setReadOnly9+ setReadOnly(callback: AsyncCallback\): void Sets this NDEF tag to read-only. This API uses an asynchronous callback to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | callback | AsyncCallback\ | Yes | Callback used to return the result. | **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!ndefTag.isTagConnected()) { if (!ndefTag.connectTag()) { console.error("ndefTag connectTag failed."); return; } } try { ndefTag.setReadOnly((err : BusinessError)=> { if (err) { console.error("ndef setReadOnly AsyncCallback err Code: ${err.code}, message: ${err.message}"); } else { console.info("ndef setReadOnly AsyncCallback success."); } }); } catch (businessError) { console.error("ndef setReadOnly AsyncCallback catch businessError Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } } ``` ### NdefTag.getNdefTagTypeString9+ getNdefTagTypeString(type: [tag.NfcForumType](js-apis-nfcTag.md#nfcforumtype9)): string Converts an NFC Forum Type tag to a string defined in the NFC Forum. **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | type | [tag.NfcForumType](js-apis-nfcTag.md#nfcforumtype9) | Yes | NDEF tag type. It can be NFC FORUM type 1, 2, 3, or 4. | **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | string | Byte array obtained.| **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefTag' correctly. try { let ndefTypeString : string = ndefTag.getNdefTagTypeString(tag.NfcForumType.NFC_FORUM_TYPE_1); console.info("ndef ndefTypeString: " + ndefTypeString); } catch (businessError) { console.error("ndef getNdefTagTypeString catch businessError Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } ``` ## MifareClassicTag9+ Provides APIs to access MIFARE Classic properties and perform I/O operations on a tag. This class inherits from [TagSession](js-apis-tagSession.md). **TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md). The following describes the unique APIs of **MifareClassicTag**. ### MifareClassicTag.authenticateSector9+ authenticateSector(sectorIndex: number, key: number[], isKeyA: boolean): Promise\ Authenticates a sector using a key. The sector can be accessed only after the authentication is successful. This API uses a promise to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | sectorIndex | number | Yes | Index of the sector to authenticate. The sector indexes start from **0**. | | key | number[]| Yes | Key (6 bytes) used for sector authentication. | | isKeyA | boolean | Yes | Whether the key is key A. The value **true** indicates key A, and **false** indicates key B.| **Return value** | Type | Description | | ------------------------- | ------------------ | | Promise\ | Promise that returns no value. | **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!mifareClassic.isTagConnected()) { if (!mifareClassic.connectTag()) { console.error("mifareClassic connectTag failed."); return; } } try { let sectorIndex = 1; // Change it as required. let key = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06] // The key must be of 6 bytes. mifareClassic.authenticateSector(sectorIndex, key, true).then(() => { console.info("mifareClassic authenticateSector Promise success."); }).catch((err : BusinessError)=> { console.error("mifareClassic authenticateSector Promise errCode: ${err.code}, " + "message: ${err.message}"); }); } catch (businessError) { console.error("mifareClassic authenticateSector Promise catch businessError Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } } ``` ### MifareClassicTag.authenticateSector9+ authenticateSector(sectorIndex: number, key: number[], isKeyA: boolean, callback: AsyncCallback\): void Authenticates a sector using a key. The sector can be accessed only after the authentication is successful. This API uses an asynchronous callback to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | sectorIndex | number | Yes | Index of the sector to authenticate. The sector indexes start from **0**. | | key | number[]| Yes | Key (6 bytes) used for sector authentication. | | isKeyA | boolean | Yes | Whether the key is key A. The value **true** indicates key A, and **false** indicates key B.| | callback | AsyncCallback\ | Yes | Callback used to return the result.| **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!mifareClassic.isTagConnected()) { if (!mifareClassic.connectTag()) { console.error("mifareClassic connectTag failed."); return; } } try { let sectorIndex = 1; // Change it as required. let key = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06] // The key must be of 6 bytes. mifareClassic.authenticateSector(sectorIndex, key, true, (err : BusinessError)=> { if (err) { console.error("mifareClassic authenticateSector AsyncCallback errCode: ${err.code}, message: ${err.message}"); } else { console.info("mifareClassic authenticateSector AsyncCallback success."); } }); } catch (businessError) { console.error("mifareClassic authenticateSector AsyncCallback catch Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } } ``` ### MifareClassicTag.readSingleBlock9+ readSingleBlock(blockIndex: number): Promise\ Reads a block (16 bytes) on this tag. This API uses a promise to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | blockIndex | number | Yes | Index of the block to read. The block indexes start from **0**. | **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | Promise\ | Promise used to return the block data read.| **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!mifareClassic.isTagConnected()) { if (!mifareClassic.connectTag()) { console.error("mifareClassic connectTag failed."); return; } } try { let blockIndex = 1; // Change it as required. mifareClassic.readSingleBlock(blockIndex).then((data : number[]) => { console.info("mifareClassic readSingleBlock Promise data: " + data); }).catch((err : BusinessError)=> { console.error("mifareClassic readSingleBlock Promise errCode: ${err.code}, message: ${err.message}"); }); } catch (businessError) { console.error("mifareClassic readSingleBlock Promise catch businessError Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } } ``` ### MifareClassicTag.readSingleBlock9+ readSingleBlock(blockIndex: number, callback: AsyncCallback\): void Reads a block (16 bytes) on this tag. This API uses an asynchronous callback to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | blockIndex | number | Yes | Index of the block to read. The block indexes start from **0**. | | callback | AsyncCallback\ | Yes |Callback used to return the block data read. | **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!mifareClassic.isTagConnected()) { if (!mifareClassic.connectTag()) { console.error("mifareClassic connectTag failed."); return; } } try { let blockIndex = 1; // Change it as required. mifareClassic.readSingleBlock(blockIndex, (err : BusinessError, data : number[])=> { if (err) { console.error("mifareClassic readSingleBlock AsyncCallback err: " + err); } else { console.info("mifareClassic readSingleBlock AsyncCallback data: " + data); } }); } catch (businessError) { console.error("mifareClassic readSingleBlock AsyncCallback catch businessError Code: " + " ${(businessError as Businsess).code}, message: ${(businessError as Businsess).message}"); } } ``` ### MifareClassicTag.writeSingleBlock9+ writeSingleBlock(blockIndex: number, data: number[]): Promise\ Writes data to a block on this tag. This API uses a promise to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | blockIndex | number | Yes | Index of the target block. The block indexes start from **0**.| | data | number[] | Yes | 16-byte data to write. | **Return value** | Type | Description | | ------------------------- | ------------------ | | Promise\ | Promise that returns no value. | **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!mifareClassic.isTagConnected()) { if (!mifareClassic.connectTag()) { console.error("mifareClassic connectTag failed."); return; } } try { let blockIndex = 1; // Change it as required. let rawData = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]; // It must be 16 bytes. mifareClassic.writeSingleBlock(blockIndex, rawData).then(() => { console.info("mifareClassic writeSingleBlock Promise success."); }).catch((err : BusinessError)=> { console.error("mifareClassic writeSingleBlock Promise errCode: ${err.code}, message: ${err.message}"); }); } catch (businessError) { console.error("mifareClassic writeSingleBlock Promise catch businessError Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } } ``` ### MifareClassicTag.writeSingleBlock9+ writeSingleBlock(blockIndex: number, data: number[], callback: AsyncCallback\): void Writes data to a block on this tag. This API uses an asynchronous callback to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | blockIndex | number | Yes | Index of the target block. The block indexes start from **0**.| | data | number[] | Yes | 16-byte data to write. | | callback | AsyncCallback\ | Yes | Callback used to return the result. | **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!mifareClassic.isTagConnected()) { if (!mifareClassic.connectTag()) { console.error("mifareClassic connectTag failed."); return; } } try { let blockIndex = 1; // Change it as required. let rawData = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]; // It must be 16 bytes. mifareClassic.writeSingleBlock(blockIndex, rawData, (err : BusinessError)=> { if (err) { console.error("mifareClassic writeSingleBlock AsyncCallback err Code:" + "${err.code}, message: ${err.message}"); } else { console.info("mifareClassic writeSingleBlock AsyncCallback success."); } }); } catch (businessError) { console.error("mifareClassic writeSingleBlock AsyncCallback catch Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } } ``` ### MifareClassicTag.incrementBlock9+ incrementBlock(blockIndex: number, value: number): Promise\ Increments a block with the specified value and saves the result in a buffer for internal transmission. This API uses a promise to return the result. This API uses a promise to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | blockIndex | number | Yes | Index of the block to increment. The block indexes start from **0**. | | value | number | Yes | Block data to increment. The value cannot be a negative number. | **Return value** | Type | Description | | ------------------------- | ------------------ | | Promise\ | Promise that returns no value. | **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!mifareClassic.isTagConnected()) { if (!mifareClassic.connectTag()) { console.error("mifareClassic connectTag failed."); return; } } try { let blockIndex = 1; // Change it as required. let value = 0x20; // Change it as required. mifareClassic.incrementBlock(blockIndex, value).then(() => { console.info("mifareClassic incrementBlock Promise success."); }).catch((err : BusinessError)=> { console.error("mifareClassic incrementBlock Promise err Code: ${err.code}, message: ${err.message}"); }); } catch (businessError) { console.error("mifareClassic incrementBlock Promise catch Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } } ``` ### MifareClassicTag.incrementBlock9+ incrementBlock(blockIndex: number, value: number, callback: AsyncCallback\): void Increments a block with the specified value and saves the result in a buffer for internal transmission. This API uses an asynchronous callback to return the result. This API uses an asynchronous callback to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | blockIndex | number | Yes | Index of the block to increment. The block indexes start from **0**. | | value | number | Yes | Block data to increment. The value cannot be a negative number. | | callback | AsyncCallback\ | Yes | Callback used to return the result. | **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!mifareClassic.isTagConnected()) { if (!mifareClassic.connectTag()) { console.error("mifareClassic connectTag failed."); return; } } try { let blockIndex = 1; // Change it as required. let value = 0x20; // Change it as required. mifareClassic.incrementBlock(blockIndex, value, (err : BusinessError)=> { if (err) { console.error("mifareClassic incrementBlock AsyncCallback err Code: ${err.code}, message: ${err.message}"); } else { console.info("mifareClassic incrementBlock AsyncCallback success."); } }); } catch (businessError) { console.error("mifareClassic incrementBlock AsyncCallback catch businessError Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } } ``` ### MifareClassicTag.decrementBlock9+ decrementBlock(blockIndex: number, value: number): Promise\ Decrements a block with the specified value and saves the result in a buffer for internal transmission. This API uses a promise to return the result. This API uses a promise to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | blockIndex | number | Yes | Index of the block to decrement. The block indexes start from **0**.| | value | number | Yes | Block data to decrement. The value cannot be a negative number. | **Return value** | Type | Description | | ------------------------- | ------------------ | | Promise\ | Promise that returns no value. | **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!mifareClassic.isTagConnected()) { if (!mifareClassic.connectTag()) { console.error("mifareClassic connectTag failed."); return; } } try { let blockIndex = 1; // Change it as required. let value = 0x20; // Change it as required. mifareClassic.decrementBlock(blockIndex, value).then(() => { console.info("mifareClassic decrementBlock Promise success."); }).catch((err : BusinessError)=> { console.error("mifareClassic decrementBlock Promise errCode: ${err.code}, message: ${err.message}"); }); } catch (businessError) { console.error("mifareClassic decrementBlock Promise catch businessError: Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } } ``` ### MifareClassicTag.decrementBlock9+ decrementBlock(blockIndex: number, value: number, callback: AsyncCallback\): void Decrements a block with the specified value. This API uses an asynchronous callback to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | blockIndex | number | Yes | Index of the block to decrement. The block indexes start from **0**.| | value | number | Yes | Block data to decrement. The value cannot be a negative number. | | callback | AsyncCallback\ | Yes | Callback used to return the result. | **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!mifareClassic.isTagConnected()) { if (!mifareClassic.connectTag()) { console.error("mifareClassic connectTag failed."); return; } } try { let blockIndex = 1; // Change it as required. let value = 0x20; // Change it as required. mifareClassic.decrementBlock(blockIndex, value, (err : BusinessError)=> { if (err) { console.error("mifareClassic decrementBlock AsyncCallback errCode:" + "${err.code}, message: ${err.message}"); } else { console.info("mifareClassic decrementBlock AsyncCallback success."); } }); } catch (businessError) { console.error("mifareClassic decrementBlock AsyncCallback catch Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } } ``` ### MifareClassicTag.transferToBlock9+ transferToBlock(blockIndex: number): Promise\ Transfers data from the temporary register to a block. This API uses a promise to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | blockIndex | number | Yes | Index of the block, to which data is transferred. The value starts form **0**.| **Return value** | Type | Description | | ------------------------- | ------------------ | | Promise\ | Promise that returns no value. | **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!mifareClassic.isTagConnected()) { if (!mifareClassic.connectTag()) { console.error("mifareClassic connectTag failed."); return; } } try { let blockIndex = 1; // Change it as required. mifareClassic.transferToBlock(blockIndex).then(() => { console.info("mifareClassic transferToBlock Promise success."); }).catch((err : BusinessError)=> { console.error("mifareClassic transferToBlock Promise err Code: ${err.code}, message: ${err.message}"); }); } catch (businessError) { console.error("mifareClassic transferToBlock Promise catch Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } } ``` ### MifareClassicTag.transferToBlock9+ transferToBlock(blockIndex: number, callback: AsyncCallback\): void Transfers data from the temporary register to a block. This API uses an asynchronous callback to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | blockIndex | number | Yes | Index of the block, to which data is transferred. The value starts form **0**.| | callback | AsyncCallback\ | Yes | Callback used to return the result. | **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!mifareClassic.isTagConnected()) { if (!mifareClassic.connectTag()) { console.error("mifareClassic connectTag failed."); return; } } try { let blockIndex = 1; // Change it as required. mifareClassic.transferToBlock(blockIndex, (err : BusinessError)=> { if (err) { console.error("mifareClassic transferToBlock AsyncCallback errCode: ${err.code}," + "message: ${err.message}"); } else { console.info("mifareClassic transferToBlock AsyncCallback success."); } }); } catch (businessError) { console.error("mifareClassic transferToBlock AsyncCallback catch Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } } ``` ### MifareClassicTag.restoreFromBlock9+ restoreFromBlock(blockIndex: number): Promise\ Restores data in the temporary register from a block. This API uses a promise to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | blockIndex | number | Yes | Index of the block, from which data is restored. The value starts form **0**.| **Return value** | Type | Description | | ------------------------- | ------------------ | | Promise\ | Promise that returns no value. | **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!mifareClassic.isTagConnected()) { if (!mifareClassic.connectTag()) { console.error("mifareClassic connectTag failed."); return; } } try { let blockIndex = 1; // Change it as required. mifareClassic.restoreFromBlock(blockIndex).then(() => { console.info("mifareClassic restoreFromBlock Promise success."); }).catch((err : BusinessError)=> { console.error("mifareClassic restoreFromBlock Promise errCode: ${err.code}, message: ${err.message}"); }); } catch (businessError) { console.error("mifareClassic restoreFromBlock Promise catch businessError Code: ${(businessError as Businsess).code}," + " message: ${(businessError as Businsess).message}"); } } ``` ### MifareClassicTag.restoreFromBlock9+ restoreFromBlock(blockIndex: number, callback: AsyncCallback\): void Restores data in the temporary register from a block. This API uses an asynchronous callback to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | blockIndex | number | Yes | Index of the block, from which data is restored. The value starts form **0**.| | callback | AsyncCallback\ | Yes | Callback used to return the result.| **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!mifareClassic.isTagConnected()) { if (!mifareClassic.connectTag()) { console.error("mifareClassic connectTag failed."); return; } } try { let blockIndex = 1; // Change it as required. mifareClassic.restoreFromBlock(blockIndex, (err : BusinessError)=> { if (err) { console.error("mifareClassic restoreFromBlock AsyncCallback err Code: ${err.code}," + " message: ${err.message}"); } else { console.info("mifareClassic restoreFromBlock AsyncCallback success."); } }); } catch (businessError) { console.error("mifareClassic restoreFromBlock AsyncCallback catch Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } } ``` ### MifareClassicTag.getSectorCount9+ getSectorCount(): number Obtains the number of sectors in this MIFARE Classic tag. **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | number | Number of sectors obtained.| **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. let sectorCount : number = mifareClassic.getSectorCount(); console.info("mifareClassic sectorCount: " + sectorCount); ``` ### MifareClassicTag.getBlockCountInSector9+ getBlockCountInSector(sectorIndex: number): number Obtains the number of blocks in a sector. **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | sectorIndex | number | Yes | Index of the target sector. The sector indexes start from **0**.| **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | number | Number of blocks obtained.| **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. try { let sectorIndex = 1; // Change it as required. let blockCnt : number = mifareClassic.getBlockCountInSector(sectorIndex); console.info("mifareClassic blockCnt: " + blockCnt); } catch (businessError) { console.error("mifareClassic getBlockCountInSector catch businessError Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } ``` ### MifareClassicTag.getType9+ getType(): [tag.MifareClassicType](js-apis-nfcTag.md#mifareclassictype9) Obtains the type of this MIFARE Classic tag. **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | [tag.MifareClassicType](js-apis-nfcTag.md#mifareclassictype9) | Type of the MIFARE Classic tag obtained.| **Example** ```js import { tag } from '@kit.ConnectivityKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. let getType : tag.MifareClassicType = mifareClassic.getType(); console.info("mifareClassic getType: " + getType); ``` ### MifareClassicTag.getTagSize9+ getTagSize(): number Obtains the size of this tag. For details, see [MifareClassicSize](js-apis-nfcTag.md#mifareclassicsize9). **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | number | Tag size obtained, in bytes. For details, see [MifareClassicSize](js-apis-nfcTag.md#mifareclassicsize9).| **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. let tagSize : number = mifareClassic.getTagSize(); console.info("mifareClassic tagSize: " + tagSize); ``` ### MifareClassicTag.isEmulatedTag9+ isEmulatedTag(): boolean Checks whether it is an emulated tag. **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | boolean |Returns **true** if the tag is an emulated tag; returns **false** otherwise. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. let isEmulatedTag : boolean = mifareClassic.isEmulatedTag(); console.info("mifareClassic isEmulatedTag: " + isEmulatedTag); ``` ### MifareClassicTag.getBlockIndex9+ getBlockIndex(sectorIndex: number): number Obtains the index of the first block in a sector. **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | sectorIndex | number | Yes | Index of the target sector. The sector indexes start from **0**. | **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | number | Index of the first block obtained. | **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. try { let sectorIndex = 1; // Change it as required. let blockIndex : number = mifareClassic.getBlockIndex(sectorIndex); console.info("mifareClassic blockIndex: " + blockIndex); } catch (businessError) { console.error("mifareClassic getBlockIndex catch businessError Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } ``` ### MifareClassicTag.getSectorIndex9+ getSectorIndex(blockIndex: number): number Obtains the index of the sector that holds the specified block. **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | blockIndex | number | Yes | Index of the block. The block indexes start from **0**. | **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | number | Index of the sector obtained. | **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareClassic' correctly. try { let blockIndex = 1; // Change it as required. let sectorIndex : number = mifareClassic.getSectorIndex(blockIndex); console.info("mifareClassic sectorIndex: " + sectorIndex); } catch (businessError) { console.error("mifareClassic getSectorIndex catch businessError Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } ``` ## MifareUltralightTag9+ Provides APIs to access MIFARE Ultralight properties and perform I/O operations on a tag. This class inherits from **TagSession**. **TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md). The following describes the unique APIs of **MifareUltralightTag**. ### MifareUltralightTag.readMultiplePages9+ readMultiplePages(pageIndex: number): Promise\ Reads four pages of data (4 bytes per page) from this tag. This API uses a promise to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | ------------------------------ | | pageIndex | number | Yes | Index of the first page to read. The page indexes start from **0**. | **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | Promise\ | Promise used to return the data read, which is 16 bytes in total.| **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareUltralight' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!mifareUltralight.isTagConnected()) { if (!mifareUltralight.connectTag()) { console.error("mifareUltralight connectTag failed."); return; } } try { let pageIndex = 1; // Change it as required. mifareUltralight.readMultiplePages(pageIndex).then((data : number[]) => { console.info("mifareUltralight readMultiplePages Promise data = " + data); }).catch((err : BusinessError)=> { console.error("mifareUltralight readMultiplePages Promise Code: ${err.code}, message: ${err.message}"); }); } catch (businessError) { console.error("mifareUltralight readMultiplePages Promise catch businessError" + " Code: ${(businessError as Businsess).code}, message: ${(businessError as Businsess).message}"); } } ``` ### MifareUltralightTag.readMultiplePages9+ readMultiplePages(pageIndex: number, callback: AsyncCallback\): void Reads four pages of data (4 bytes per page) from this tag. This API uses an asynchronous callback to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | pageIndex | number | Yes | Index of the first page to read. The page indexes start from **0**. | | callback | AsyncCallback\ | Yes | Callback used to return the data read, which is 16 bytes in total.| **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareUltralight' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!mifareUltralight.isTagConnected()) { if (!mifareUltralight.connectTag()) { console.error("mifareUltralight connectTag failed."); return; } } try { let pageIndex = 1; // Change it as required. mifareUltralight.readMultiplePages(pageIndex, (err : BusinessError, data : number[])=> { if (err) { console.log("mifareUltralight readMultiplePages AsyncCallback Code: ${err.code}, message: ${err.message}"); } else { console.info("mifareUltralight readMultiplePages AsyncCallback data: " + data); } }); } catch (businessError) { console.error("mifareUltralight readMultiplePages AsyncCallback catch Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } } ``` ### MifareUltralightTag.writeSinglePage9+ writeSinglePage(pageIndex: number, data: number[]): Promise\ Writes one page (4 bytes) of data to this tag. This API uses a promise to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | pageIndex | number | Yes | Index of the page to write. The page indexes start from **0**. | | data | number[] | Yes | 4-byte data to write. | **Return value** | Type | Description | | ------------------------- | ------------------ | | Promise\ | Promise that returns no value. | **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareUltralight' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!mifareUltralight.isTagConnected()) { if (!mifareUltralight.connectTag()) { console.error("mifareUltralight connectTag failed."); return; } } try { let pageIndex = 1; // Change it as required. let rawData = [0x01, 0x02, 0x03, 0x04]; // MUST be 4 bytes, change it to be correct raw data. mifareUltralight.writeSinglePage(pageIndex, rawData).then(() => { console.info("mifareUltralight writeSinglePage Promise success."); }).catch((err : BusinessError)=> { console.error("mifareUltralight writeSinglePage Promise err Code: ${err.code}, message: ${err.message}"); }); } catch (businessError) { console.error("mifareUltralight writeSinglePage Promise catch Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } } ``` ### MifareUltralightTag.writeSinglePage9+ writeSinglePage(pageIndex: number, data: number[], callback: AsyncCallback\): void Writes one page (4 bytes) of data to this tag. This API uses an asynchronous callback to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | ------------------------ | | pageIndex | number | Yes | Index of the page to write. The page indexes start from **0**. | | data | number[] | Yes | 4-byte data to write. | | callback|AsyncCallback\ |Yes| Callback used to return the result. | **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareUltralight' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!mifareUltralight.isTagConnected()) { if (!mifareUltralight.connectTag()) { console.error("mifareUltralight connectTag failed."); return; } } try { let pageIndex = 1; // Change it as required. let rawData = [0x01, 0x02, 0x03, 0x04]; // MUST be 4 bytes, change it to be correct raw data. mifareUltralight.writeSinglePage(pageIndex, rawData, (err : BusinessError)=> { if (err) { console.error("mifareUltralight writeSinglePage AsyncCallback Code: ${err.code}," + "message: ${err.message}"); } else { console.info("mifareUltralight writeSinglePage AsyncCallback success."); } }); } catch (businessError) { console.error("mifareUltralight writeSinglePage AsyncCallback catch Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } } ``` ### MifareUltralightTag.getType9+ getType(): [tag.MifareUltralightType](js-apis-nfcTag.md#mifareultralighttype9) Obtains the type of this MIFARE Ultralight tag. **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | [tag.MifareUltralightType](js-apis-nfcTag.md#mifareultralighttype9) | Type of the MIFARE Ultralight tag obtained.| **Example** ```js import { tag } from '@kit.ConnectivityKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'mifareUltralight' correctly. let getType : tag.MifareUltralightType = mifareClassic.getType(); console.info("mifareUltralight getType: " + getType); ``` ## NdefFormatableTag9+ Provides APIs for formatting NDEF formattable tags. This class inherits from **TagSession**. **TagSession** is the base class of all NFC tag technologies. It provides common interfaces for establishing connections and transferring data. For more details, see [TagSession](js-apis-tagSession.md). The following describes the unique APIs of **NdefFormatableTag**. ### NdefFormatableTag.format9+ format(message: [NdefMessage](#ndefmessage9)): Promise\ Formats this tag as an NDEF tag, and writes an NDEF message to it. This API uses a promise to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | message | [NdefMessage](#ndefmessage9) | Yes | NDEF message to write. If this parameter is **null**, the tag is formatted only (no data will be written). | **Return value** | Type | Description | | ------------------------- | ------------------ | | Promise\ | Promise that returns no value. | **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefFormatable' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!ndefFormatable.isTagConnected()) { if (!ndefFormatable.connectTag()) { console.error("ndefFormatable connectTag failed."); return; } } try { // NDEF message created from raw data, such as: let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // It must be parsed as NDEF Record. // or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[]) ndefFormatable.format(ndefMessage).then(() => { console.info("ndefFormatable format Promise success."); }).catch((err : BusinessError)=> { console.error("ndefFormatable format Promise err Code: ${err.code}, message: ${err.message}"); }); } catch (businessError) { console.error("ndefFormatable format Promise catch businessError Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } } ``` ### NdefFormatableTag.format9+ format(message: [NdefMessage](#ndefmessage9), callback: AsyncCallback\): void Formats this tag as an NDEF tag, and writes an NDEF message to it. This API uses an asynchronous callback to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | 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). | **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | callback: AsyncCallback\ | Callback used to return the result. | **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefFormatable' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!ndefFormatable.isTagConnected()) { if (!ndefFormatable.connectTag()) { console.error("ndefFormatable connectTag failed."); return; } } try { // NDEF message created from raw data, such as: let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // It must be parsed as NDEF Record. // or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[]) ndefFormatable.format(ndefMessage, (err : BusinessError)=> { if (err) { console.error("ndefFormatable format AsyncCallback Code: ${err.code}, message: ${err.message}"); } else { console.info("ndefFormatable format AsyncCallback success."); } }); } catch (businessError) { console.error("ndefFormatable format AsyncCallback catch Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } } ``` ### NdefFormatableTag.formatReadOnly9+ formatReadOnly(message: [NdefMessage](#ndefmessage9)): Promise\ Formats this tag as an NDEF tag, writes an NDEF message to it, and then sets the tag to read-only. This API uses a promise to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | message | [NdefMessage](#ndefmessage9) | Yes | NDEF message to write. If this parameter is **null**, the tag is formatted only (no data will be written). | **Return value** | Type | Description | | ------------------------- | ------------------ | | Promise\ | Promise that returns no value. | **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefFormatable' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!ndefFormatable.isTagConnected()) { if (!ndefFormatable.connectTag()) { console.error("ndefFormatable connectTag failed."); return; } } try { // NDEF message created from raw data, such as: let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // It must be parsed as NDEF Record. // or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[]) ndefFormatable.formatReadOnly(ndefMessage).then(() => { console.info("ndefFormatable formatReadOnly Promise success."); }).catch((err : BusinessError)=> { console.error("ndefFormatable formatReadOnly Promise Code: ${err.code}, message: ${err.message}"); }); } catch (businessError) { console.error("ndefFormatable formatReadOnly Promise catch Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } } ``` ### NdefFormatableTag.formatReadOnly9+ formatReadOnly(message: [NdefMessage](#ndefmessage9), callback: AsyncCallback\): void Formats this tag as an NDEF tag, writes an NDEF message to the NDEF tag, and then sets the tag to read-only. This API uses an asynchronous callback to return the result. **Required permissions**: ohos.permission.NFC_TAG **System capability**: SystemCapability.Communication.NFC.Tag **Atomic service API**: This API can be used in atomic services since API version 12. **Parameters** | Name | Type | Mandatory | Description | | -------- | ----------------------- | ---- | -------------------------------------- | | message | [NdefMessage](#ndefmessage9) | Yes | NDEF message to write. If this parameter is **null**, the tag is formatted only (no data will be written).| **Return value** | **Type** | **Description** | | ------------------ | --------------------------| | callback: AsyncCallback\ | Callback used to return the result. | **Error codes** For details about the error codes, see [NFC Error Codes](errorcode-nfc.md). | ID | Error Message| | ------- | -------| | 201 | Permission denied. | | 401 | The parameter check failed. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameters types.
3. Parameter verification failed. | | 3100201 | Tag running state is abnormal in service. | | 3100204 | Tag I/O operation failed. | **Example** ```js import { tag } from '@kit.ConnectivityKit'; import { BusinessError } from '@kit.BasicServicesKit'; // Check whether 'tag.TagInfo' in 'js-apis-nfcTag.md' has obtained 'ndefFormatable' correctly. function nfcTechDemo() { // Connect to the tag if it is not connected. if (!ndefFormatable.isTagConnected()) { if (!ndefFormatable.connectTag()) { console.error("ndefFormatable connectTag failed."); return; } } try { // NDEF message created from raw data, such as: let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // It must be parsed as NDEF Record. // or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[]) ndefFormatable.formatReadOnly(ndefMessage, (err : BusinessError)=> { if (err) { console.error("ndefFormatable formatReadOnly AsyncCallback err Code: ${err.code}, message: ${err.message}"); } else { console.info("ndefFormatable formatReadOnly AsyncCallback success."); } }); } catch (businessError) { console.error("ndefFormatable formatReadOnly AsyncCallback catch Code: ${(businessError as Businsess).code}, " + "message: ${(businessError as Businsess).message}"); } } ```