標(biāo)準(zhǔn)NFC-Tag Nfc 技術(shù)

2024-01-23 17:12 更新

本模塊主要用于采用不同Nfc技術(shù)的Tag的讀寫操作。

說明

本模塊首批接口從API version 7開始支持。后續(xù)版本的新增接口,采用上角標(biāo)單獨(dú)標(biāo)記接口的起始版本。

導(dǎo)入模塊

  1. import tag from '@ohos.nfc.tag';

NfcATag

NfcATag 提供 NFC-A(ISO 14443-3A)技術(shù)的屬性和I/O操作的訪問,繼承自TagSession。

TagSession是所有NFC Tag技術(shù)類型的基類, 提供建立連接和發(fā)送數(shù)據(jù)等共同接口。具體請(qǐng)參見TagSession

以下是NfcATag的獨(dú)有接口。

NfcATag.getSak

getSak(): number

獲取NFC-A標(biāo)簽的SAK值。

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

返回值:

類型說明
numberNfcA 標(biāo)簽的SAK值,十六進(jìn)制表示,范圍是0x00~0xFF。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'nfcA' correctly.
  3. let sak = nfcA.getSak();
  4. console.log("nfcA sak: " + sak);

NfcATag.getAtqa

getAtqa(): number[]

獲取NFC-A標(biāo)簽的Atqa值。

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

返回值:

類型說明
number[]NfcA 標(biāo)簽的Atqa值,每個(gè)number十六進(jìn)制表示,范圍是0x00~0xFF。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'nfcA' correctly.
  3. let atqa = nfcA.getAtqa();
  4. console.log("nfcA atqa: " + atqa);

NfcBTag

NfcBTag 提供對(duì)NFC-B(ISO 14443-3B)技術(shù)的屬性和I/O操作的訪問,繼承自TagSession。

TagSession是所有NFC Tag技術(shù)類型的基類,提供建立連接和發(fā)送數(shù)據(jù)等共同接口。具體請(qǐng)參見TagSession

以下是NfcBTag的獨(dú)有接口。

NfcBTag.getRespAppData

getRespAppData(): number[]

獲取標(biāo)簽的應(yīng)用程序數(shù)據(jù)。

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

返回值:

類型說明
number[]NfcB 標(biāo)簽的應(yīng)用程序數(shù)據(jù),每個(gè)number十六進(jìn)制表示,范圍是0x00~0xFF。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'nfcB' correctly.
  3. let respAppData = nfcB.getRespAppData();
  4. console.log("nfcB respAppData: " + respAppData);

NfcBTag.getRespProtocol

getRespProtocol(): number[]

獲取標(biāo)簽的協(xié)議信息。

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

返回值:

類型說明
number[]NfcB 標(biāo)簽的協(xié)議信息,每個(gè)number十六進(jìn)制表示,范圍是0x00~0xFF。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'nfcB' correctly.
  3. let respProtocol = nfcB.getRespProtocol();
  4. console.log("nfcB respProtocol: " + respProtocol);

NfcFTag

NfcFTag 提供對(duì)NFC-F(JIS 6319-4)技術(shù)的屬性和I/O操作的訪問,繼承自TagSession。

TagSession是所有NFC Tag技術(shù)類型的基類, 提供建立連接和發(fā)送數(shù)據(jù)等共同接口。具體請(qǐng)參見TagSession。

以下是NfcFTag的獨(dú)有接口。

NfcFTag.getSystemCode

getSystemCode(): number[]

從標(biāo)簽實(shí)例獲取系統(tǒng)代碼。

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

返回值:

類型說明
number[]NfcF 標(biāo)簽的系統(tǒng)代碼,每個(gè)number十六進(jìn)制表示,范圍是0x00~0xFF。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'nfcF' correctly.
  3. let systemCode = nfcF.getSystemCode();
  4. console.log("nfcF systemCode: " + systemCode);

NfcFTag.getPmm

getPmm(): number[]

從標(biāo)簽實(shí)例獲取PMm(由IC代碼和制造商參數(shù)組成)。

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

返回值:

類型說明
number[]NfcF 標(biāo)簽的PMm信息,每個(gè)number十六進(jìn)制表示,范圍是0x00~0xFF。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'nfcF' correctly.
  3. let pmm = nfcF.getPmm();
  4. console.log("nfcF pmm: " + pmm);

NfcVTag

NfcVTag 提供對(duì)NFC-V(ISO 15693)技術(shù)的屬性和I/O操作的訪問,繼承自TagSession。

TagSession是所有NFC Tag技術(shù)類型的基類, 提供建立連接和發(fā)送數(shù)據(jù)等共同接口。具體請(qǐng)參見TagSession。

以下是NfcVTag的獨(dú)有接口。

NfcvTag.getResponseFlags

getResponseFlags(): number

從標(biāo)簽實(shí)例實(shí)例獲取響應(yīng)標(biāo)志。

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

返回值:

類型說明
numberNfcV 標(biāo)簽的響應(yīng)標(biāo)志,十六進(jìn)制表示,范圍是0x00~0xFF。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'nfcV' correctly.
  3. let responseFlags = nfcV.getResponseFlags();
  4. console.log("nfcV responseFlags: " + responseFlags);

NfcvTag.getDsfId

getDsfId(): number

從標(biāo)簽實(shí)例實(shí)例獲取數(shù)據(jù)存儲(chǔ)格式標(biāo)識(shí)符(DSFID)。

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

返回值:

類型說明
numberNfcV 標(biāo)簽的數(shù)據(jù)存儲(chǔ)格式標(biāo)識(shí)符,十六進(jìn)制表示,范圍是0x00~0xFF。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'nfcV' correctly.
  3. let dsfId = nfcV.getDsfId();
  4. console.log("nfcV dsfId: " + dsfId);

IsoDepTag9+

IsoDepTag 提供對(duì)ISO-DEP(ISO 14443-4)技術(shù)的屬性和I/O操作的訪問,繼承自TagSession。

TagSession是所有NFC Tag技術(shù)類型的基類, 提供建立連接和發(fā)送數(shù)據(jù)等共同接口。具體請(qǐng)參見TagSession。

以下是IsoDepTag的獨(dú)有接口。

IsoDepTag.getHistoricalBytes9+

getHistoricalBytes(): number[]

獲取標(biāo)簽的歷史字節(jié),針對(duì)基于NfcA通信技術(shù)的IsoDep卡片。

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

返回值:

類型說明
number[]IsoDepTag 標(biāo)簽的歷史字節(jié),每個(gè)number十六進(jìn)制表示,范圍是0x00~0xFF。如果該IsoDep類型Tag是基于NfcB技術(shù)的,則該返回值為空。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'isoDep' correctly.
  3. let historicalBytes = isoDep.getHistoricalBytes();
  4. console.log("isoDep historicalBytes: " + historicalBytes);

IsoDepTag.getHiLayerResponse9+

getHiLayerResponse(): number[]

獲取標(biāo)簽的更高層響應(yīng)字節(jié),針對(duì)基于NfcB通信技術(shù)的IsoDep卡片。

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

返回值:

類型說明
number[]IsoDepTag 標(biāo)簽的更高層響應(yīng)字節(jié),每個(gè)number十六進(jìn)制表示,范圍是0x00~0xFF。如果該IsoDep類型Tag是基于NfcA技術(shù)的,則該返回值為空。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'isoDep' correctly.
  3. let hiLayerResponse = isoDep.getHiLayerResponse();
  4. console.log("isoDep hiLayerResponse: " + hiLayerResponse);

IsoDepTag.isExtendedApduSupported9+

isExtendedApduSupported(): Promise<boolean>

檢查是否支持?jǐn)U展的APDU,使用Promise方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

返回值:

類型說明
Promise<boolean>檢查結(jié)果,true: 支持, false: 不支持。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼。

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'isoDep' correctly.
  3. // connect the tag at first if not connected.
  4. if (!isoDep.isTagConnected()) {
  5. if (!isoDep.connectTag()) {
  6. console.log("isoDep connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. isoDep.isExtendedApduSupported().then((response) => {
  12. console.log("isoDep isExtendedApduSupported Promise response: " + response);
  13. }).catch((err)=> {
  14. console.log("isoDep isExtendedApduSupported Promise err: " + err);
  15. });
  16. } catch (busiError) {
  17. console.log("isoDep isExtendedApduSupported Promise busiError: " + busiError);
  18. }

IsoDepTag.isExtendedApduSupported9+

isExtendedApduSupported(callback: AsyncCallback<boolean>): void

檢查是否支持?jǐn)U展的APDU,使用AsyncCallback方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
callbackAsyncCallback<boolean>回調(diào)函數(shù),true: 支持, false: 不支持。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'isoDep' correctly.
  3. // connect the tag at first if not connected.
  4. if (!isoDep.isTagConnected()) {
  5. if (!isoDep.connectTag()) {
  6. console.log("isoDep connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. isoDep.isExtendedApduSupported((err, response)=> {
  12. if (err) {
  13. console.log("isoDep isExtendedApduSupported AsyncCallback err: " + err);
  14. } else {
  15. console.log("isoDep isExtendedApduSupported AsyncCallback response: " + response);
  16. }
  17. });
  18. } catch (busiError) {
  19. console.log("isoDep isExtendedApduSupported AsyncCallback busiError: " + busiError);
  20. }

NdefMessage9+

NdefMessage.getNdefRecords9+

getNdefRecords(): tag.NdefRecord[]

獲取NDEF消息中的所有記錄。

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

返回值:

類型說明
tag.NdefRecord[]NDEF標(biāo)簽的Record列表,詳見NDEF技術(shù)規(guī)范《NFCForum-TS-NDEF_1.0》。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // Obtains ndefMessage from tag.ndef.createNdefMessage or ndefTag.getNdefMessage.
  3. // var ndefMessage = tag.ndef.createNdefMessage(...);
  4. // var ndefMessage = ndefTag.getNdefMessage();
  5. let ndefRecords = ndefMessage.getNdefRecords();
  6. console.log("ndef ndefRecords number: " + ndefRecords.length);

NdefTag9+

提供對(duì)已格式化為NDEF的NFC標(biāo)簽的數(shù)據(jù)和操作的訪問,繼承自TagSession。

TagSession是所有NFC Tag技術(shù)類型的基類, 提供建立連接和發(fā)送數(shù)據(jù)等共同接口。具體請(qǐng)參見TagSession

以下是NdefTag的獨(dú)有接口。

NdefTag.getNdefTagType9+

getNdefTagType(): tag.NfcForumType

獲取NDEF標(biāo)簽的類型。

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

返回值:

類型說明
tag.NfcForumTypeNDEF標(biāo)簽類型,包括NFC FORUM TYPE 1/2/3/4等。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly.
  3. let ndefTagType = ndefTag.getNdefTagType();
  4. console.log("ndef ndefTagType: " + ndefTagType);

NdefTag.getNdefMessage9+

getNdefMessage(): NdefMessage

獲取發(fā)現(xiàn)NDEF標(biāo)簽時(shí),從標(biāo)簽讀取的Message。

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

返回值:

類型說明
NdefMessageNDEF標(biāo)簽的Message,詳見NDEF技術(shù)規(guī)范《NFCForum-TS-NDEF_1.0》。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly.
  3. let ndefMessage = ndefTag.getNdefMessage();
  4. console.log("ndef ndefMessage: " + ndefMessage);

NdefTag.isNdefWritable9+

isNdefWritable(): boolean;

檢查NDEF標(biāo)簽是否可寫。在調(diào)用寫數(shù)據(jù)接口前,需要先判斷是否支持寫操作。

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

返回值:

類型說明
boolean檢查結(jié)果,true: 可寫, false: 不可寫。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly.
  3. var isWritable = ndefTag.isNdefWritable();
  4. console.log("ndef isNdefWritable: " + isWritable);

NdefTag.readNdef9+

readNdef(): Promise<NdefMessage>

讀取標(biāo)簽上的NDEF消息,使用Promise方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

返回值:

類型說明
Promise<NdefMessage>以Promise形式返回從NDEF標(biāo)簽中讀取到的Message數(shù)據(jù)對(duì)象。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼。

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly.
  3. // connect the tag at first if not connected.
  4. if (!ndefTag.isTagConnected()) {
  5. if (!ndefTag.connectTag()) {
  6. console.log("ndefTag connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. ndefTag.readNdef().then((ndefmessage) => {
  12. console.log("ndef readNdef Promise ndefmessage: " + ndefmessage);
  13. }).catch((err)=> {
  14. console.log("ndef readNdef Promise err: " + err);
  15. });
  16. } catch (busiError) {
  17. console.log("ndef readNdef Promise catched busiError: " + busiError);
  18. }

NdefTag.readNdef9+

readNdef(callback: AsyncCallback<NdefMessage>): void

讀取標(biāo)簽上的NDEF消息,使用AsyncCallback方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
callbackAsyncCallback<NdefMessage>回調(diào)函數(shù),返回從NDEF標(biāo)簽中讀取到的Message信息。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼。

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly.
  3. // connect the tag at first if not connected.
  4. if (!ndefTag.isTagConnected()) {
  5. if (!ndefTag.connectTag()) {
  6. console.log("ndefTag connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. ndefTag.readNdef((err, ndefmessage)=> {
  12. if (err) {
  13. console.log("ndef readNdef AsyncCallback err: " + err);
  14. } else {
  15. console.log("ndef readNdef AsyncCallback ndefmessage: " + ndefmessage);
  16. }
  17. });
  18. } catch (busiError) {
  19. console.log("ndef readNdef AsyncCallback catched busiError: " + busiError);
  20. }

NdefTag.writeNdef9+

writeNdef(msg: NdefMessage): Promise<void>;

將NDEF Messsage數(shù)據(jù)對(duì)象寫入標(biāo)簽,使用Promise方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
msgNdefMessageNDEF Message數(shù)據(jù)對(duì)象。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼。

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly.
  3. // ndefMessage created from raw data, such as:
  4. let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // MUST can be parsed as NDEF Record.
  5. // or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[])
  6. // connect the tag at first if not connected.
  7. if (!ndefTag.isTagConnected()) {
  8. if (!ndefTag.connectTag()) {
  9. console.log("ndefTag connectTag failed.");
  10. return;
  11. }
  12. }
  13. try {
  14. ndefTag.writeNdef(ndefMessage).then(() => {
  15. console.log("ndef writeNdef Promise success.");
  16. }).catch((err)=> {
  17. console.log("ndef writeNdef err: " + err);
  18. });
  19. } catch (busiError) {
  20. console.log("ndef writeNdef Promise catch busiError: " + busiError);
  21. }

NdefTag.writeNdef9+

writeNdef(msg: NdefMessage, callback: AsyncCallback<void>): void

將NDEF Message數(shù)據(jù)對(duì)象寫入此標(biāo)簽,使用AsyncCallback方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
msgNdefMessageNDEF Message數(shù)據(jù)對(duì)象。
callbackAsyncCallback<void>回調(diào)函數(shù)。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼。

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly.
  3. // ndefMessage created from raw data, such as:
  4. let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // MUST can be parsed as NDEF Record.
  5. // or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[])
  6. // connect the tag at first if not connected.
  7. if (!ndefTag.isTagConnected()) {
  8. if (!ndefTag.connectTag()) {
  9. console.log("ndefTag connectTag failed.");
  10. return;
  11. }
  12. }
  13. try {
  14. ndefTag.writeNdef(ndefMessage, (err)=> {
  15. if (err) {
  16. console.log("ndef writeNdef AsyncCallback err: " + err);
  17. } else {
  18. console.log("ndef writeNdef AsyncCallback success.");
  19. }
  20. });
  21. } catch (busiError) {
  22. console.log("ndef writeNdef AsyncCallback catch busiError: " + busiError);
  23. }

NdefTag.canSetReadOnly9+

canSetReadOnly(): boolean

檢查NDEF標(biāo)簽是否可以設(shè)置為只讀。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

返回值:

類型說明
booleantrue: NDEF標(biāo)簽可設(shè)置為只讀, false: NDEF標(biāo)簽不可設(shè)置為只讀。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly.
  3. var canSetReadOnly = ndefTag.canSetReadOnly();
  4. console.log("ndef canSetReadOnly: " + canSetReadOnly);

NdefTag.setReadOnly9+

setReadOnly(): Promise<void>

將NDEF標(biāo)簽設(shè)置為只讀,使用Promise方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼。

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly.
  3. // connect the tag at first if not connected.
  4. if (!ndefTag.isTagConnected()) {
  5. if (!ndefTag.connectTag()) {
  6. console.log("ndefTag connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. ndefTag.setReadOnly().then(() => {
  12. console.log("ndef setReadOnly Promise success.");
  13. }).catch((err)=> {
  14. console.log("ndef setReadOnly Promise err: " + err);
  15. });
  16. } catch (busiError) {
  17. console.log("ndef setReadOnly Promise catch busiError: " + busiError);
  18. }

NdefTag.setReadOnly9+

setReadOnly(callback: AsyncCallback<void>): void

將NDEF標(biāo)簽設(shè)置為只讀,使用AsyncCallback方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
callbackAsyncCallback<void>回調(diào)函數(shù)。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼。

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly.
  3. // connect the tag at first if not connected.
  4. if (!ndefTag.isTagConnected()) {
  5. if (!ndefTag.connectTag()) {
  6. console.log("ndefTag connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. ndefTag.setReadOnly((err)=> {
  12. if (err) {
  13. console.log("ndef setReadOnly AsyncCallback err: " + err);
  14. } else {
  15. console.log("ndef setReadOnly AsyncCallback success.");
  16. }
  17. });
  18. } catch (busiError) {
  19. console.log("ndef setReadOnly AsyncCallback catch busiError: " + busiError);
  20. }

NdefTag.getNdefTagTypeString9+

getNdefTagTypeString(type: tag.NfcForumType): string

將NFC論壇類型,轉(zhuǎn)換為NFC論壇中定義的字符串描述。

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
typetag.NfcForumTypeNDEF標(biāo)簽類型,包括NFC FORUM TYPE 1/2/3/4等。

返回值:

類型說明
stringNFC論壇類型的字符串描述。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefTag' correctly.
  3. try {
  4. let ndefTypeString = ndefTag.getNdefTagTypeString(tag.NFC_FORUM_TYPE_1);
  5. console.log("ndef ndefTypeString: " + ndefTypeString);
  6. } catch (busiError) {
  7. console.log("ndef getNdefTagTypeString catch busiError: " + busiError);
  8. }

MifareClassicTag9+

MifareClassicTag提供對(duì)MIFARE Classic屬性和I/O操作的訪問,繼承自TagSession

TagSession是所有NFC Tag技術(shù)類型的基類, 提供建立連接和發(fā)送數(shù)據(jù)等共同接口。具體請(qǐng)參見TagSession

以下是MifareClassicTag的獨(dú)有接口。

MifareClassicTag.authenticateSector9+

authenticateSector(sectorIndex: number, key: number[], isKeyA: boolean): Promise<void>

使用密鑰對(duì)扇區(qū)進(jìn)行身份驗(yàn)證,只有身份驗(yàn)證成功的扇區(qū)可以進(jìn)行操作。使用Promise方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
sectorIndexnumber待驗(yàn)證的扇區(qū)索引,從0開始。
keynumber[]用于扇區(qū)驗(yàn)證的密鑰(6字節(jié))。
isKeyAbooleanisKeyA標(biāo)志。true 表示KeyA,false 表示KeyB。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼。

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
  3. // connect the tag at first if not connected.
  4. if (!mifareClassic.isTagConnected()) {
  5. if (!mifareClassic.connectTag()) {
  6. console.log("mifareClassic connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. let sectorIndex = 1; // change it to be correct index.
  12. let key = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06] // MUST be 6 bytes, change it to be correct key.
  13. mifareClassic.authenticateSector(sectorIndex, key, true).then(() => {
  14. console.log("mifareClassic authenticateSector Promise success.");
  15. }).catch((err)=> {
  16. console.log("mifareClassic authenticateSector Promise err: " + err);
  17. });
  18. } catch (busiError) {
  19. console.log("mifareClassic authenticateSector Promise catch busiError: " + busiError);
  20. }

MifareClassicTag.authenticateSector9+

authenticateSector(sectorIndex: number, key: number[], isKeyA: boolean, callback: AsyncCallback<void>): void

使用密鑰對(duì)扇區(qū)進(jìn)行身份驗(yàn)證,只有身份驗(yàn)證成功的扇區(qū)可以進(jìn)行操作。使用AsyncCallback方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
sectorIndexnumber待驗(yàn)證的扇區(qū)索引,從0開始。
keynumber[]用于扇區(qū)驗(yàn)證的密鑰(6字節(jié))。
isKeyAbooleanisKeyA標(biāo)志。true 表示KeyA,false 表示KeyB。
callbackAsyncCallback<void>回調(diào)函數(shù)。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
  3. // connect the tag at first if not connected.
  4. if (!mifareClassic.isTagConnected()) {
  5. if (!mifareClassic.connectTag()) {
  6. console.log("mifareClassic connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. let sectorIndex = 1; // change it to be correct index.
  12. let key = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06] // MUST be 6 bytes, change it to be correct key.
  13. mifareClassic.authenticateSector(sectorIndex, key, true, (err)=> {
  14. if (err) {
  15. console.log("mifareClassic authenticateSector AsyncCallback err: " + err);
  16. } else {
  17. console.log("mifareClassic authenticateSector AsyncCallback success.");
  18. }
  19. });
  20. } catch (busiError) {
  21. console.log("mifareClassic authenticateSector AsyncCallback catch busiError: " + busiError);
  22. }

MifareClassicTag.readSingleBlock9+

readSingleBlock(blockIndex: number): Promise<number[]>

讀取標(biāo)簽中一個(gè)塊存儲(chǔ)的內(nèi)容,一個(gè)塊大小為16字節(jié)。使用Promise方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
blockIndexnumber要讀取的塊索引,從0開始。

返回值:

類型說明
Promise<number[]>讀取的塊數(shù)據(jù)。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
  3. // connect the tag at first if not connected.
  4. if (!mifareClassic.isTagConnected()) {
  5. if (!mifareClassic.connectTag()) {
  6. console.log("mifareClassic connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. let blockIndex = 1; // change it to be correct index.
  12. mifareClassic.readSingleBlock(blockIndex).then((data) => {
  13. console.log("mifareClassic readSingleBlock Promise data: " + data);
  14. }).catch((err)=> {
  15. console.log("mifareClassic readSingleBlock Promise err: " + err);
  16. });
  17. } catch (busiError) {
  18. console.log("mifareClassic readSingleBlock Promise catch busiError: " + busiError);
  19. }

MifareClassicTag.readSingleBlock9+

readSingleBlock(blockIndex: number, callback: AsyncCallback<number[]>): void

讀取標(biāo)簽中一個(gè)塊存儲(chǔ)的內(nèi)容,一個(gè)塊大小為16字節(jié)。使用AsyncCallback方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
blockIndexnumber要讀取的塊索引,從0開始。
callbackAsyncCallback<number[]>回調(diào)函數(shù),返回讀取到的數(shù)據(jù)。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼。

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
  3. // connect the tag at first if not connected.
  4. if (!mifareClassic.isTagConnected()) {
  5. if (!mifareClassic.connectTag()) {
  6. console.log("mifareClassic connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. let blockIndex = 1; // change it to be correct index.
  12. mifareClassic.readSingleBlock(blockIndex, (err, data)=> {
  13. if (err) {
  14. console.log("mifareClassic readSingleBlock AsyncCallback err: " + err);
  15. } else {
  16. console.log("mifareClassic readSingleBlock AsyncCallback data: " + data);
  17. }
  18. });
  19. } catch (busiError) {
  20. console.log("mifareClassic readSingleBlock AsyncCallback catch busiError: " + busiError);
  21. }

MifareClassicTag.writeSingleBlock9+

writeSingleBlock(blockIndex: number, data: number[]): Promise<void>

向標(biāo)簽中一個(gè)塊存儲(chǔ)寫入內(nèi)容,一個(gè)塊大小為16字節(jié)。使用Promise方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
blockIndexnumber要寫入的塊索引,從0開始。
datanumber[]要寫入的數(shù)據(jù),大小必須是16個(gè)字節(jié)。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼。

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
  3. // connect the tag at first if not connected.
  4. if (!mifareClassic.isTagConnected()) {
  5. if (!mifareClassic.connectTag()) {
  6. console.log("mifareClassic connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. let blockIndex = 1; // change it to be correct index.
  12. let rawData = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
  13. 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]; // MUST be 16 bytes, change it to be correct data.
  14. mifareClassic.writeSingleBlock(blockIndex, rawData).then(() => {
  15. console.log("mifareClassic writeSingleBlock Promise success.");
  16. }).catch((err)=> {
  17. console.log("mifareClassic writeSingleBlock Promise err: " + err);
  18. });
  19. } catch (busiError) {
  20. console.log("mifareClassic writeSingleBlock Promise catch busiError: " + busiError);
  21. }

MifareClassicTag.writeSingleBlock9+

writeSingleBlock(blockIndex: number, data: number[], callback: AsyncCallback<void>): void

向標(biāo)簽中一個(gè)塊存儲(chǔ)寫入內(nèi)容,一個(gè)塊大小為16字節(jié)。使用AsyncCallback方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
blockIndexnumber要寫入的塊索引,從0開始。
datanumber[]要寫入的數(shù)據(jù),大小必須是16個(gè)字節(jié)。
callbackAsyncCallback<void>回調(diào)函數(shù)。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
  3. // connect the tag at first if not connected.
  4. if (!mifareClassic.isTagConnected()) {
  5. if (!mifareClassic.connectTag()) {
  6. console.log("mifareClassic connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. let blockIndex = 1; // change it to be correct index.
  12. let rawData = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
  13. 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]; // MUST be 16 bytes, change it to be correct data.
  14. mifareClassic.writeSingleBlock(blockIndex, rawData, (err)=> {
  15. if (err) {
  16. console.log("mifareClassic writeSingleBlock AsyncCallback err: " + err);
  17. } else {
  18. console.log("mifareClassic writeSingleBlock AsyncCallback success.");
  19. }
  20. });
  21. } catch (busiError) {
  22. console.log("mifareClassic writeSingleBlock AsyncCallback catch busiError: " + busiError);
  23. }

MifareClassicTag.incrementBlock9+

incrementBlock(blockIndex: number, value: number): Promise<void>

對(duì)指定塊的內(nèi)容,增加指定的數(shù)值。使用Promise方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
blockIndexnumber要指定增加的塊索引,從0開始。
valuenumber要指定增加的數(shù)據(jù),非負(fù)數(shù)。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
  3. // connect the tag at first if not connected.
  4. if (!mifareClassic.isTagConnected()) {
  5. if (!mifareClassic.connectTag()) {
  6. console.log("mifareClassic connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. let blockIndex = 1; // change it to be correct index.
  12. let value = 0x20; // change it to be correct data.
  13. mifareClassic.incrementBlock(blockIndex, value).then(() => {
  14. console.log("mifareClassic incrementBlock Promise success.");
  15. }).catch((err)=> {
  16. console.log("mifareClassic incrementBlock Promise err: " + err);
  17. });
  18. } catch (busiError) {
  19. console.log("mifareClassic incrementBlock Promise catch busiError: " + busiError);
  20. }

MifareClassicTag.incrementBlock9+

incrementBlock(blockIndex: number, value: number, callback: AsyncCallback<void>): void

對(duì)指定塊的內(nèi)容,增加指定的數(shù)值。使用AsyncCallback方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
blockIndexnumber要被運(yùn)算的塊索引,從0開始。
valuenumber要增加的數(shù)值,非負(fù)數(shù)。
callbackAsyncCallback<void>回調(diào)函數(shù)。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
  3. // connect the tag at first if not connected.
  4. if (!mifareClassic.isTagConnected()) {
  5. if (!mifareClassic.connectTag()) {
  6. console.log("mifareClassic connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. let blockIndex = 1; // change it to be correct index.
  12. let value = 0x20; // change it to be correct data.
  13. mifareClassic.incrementBlock(blockIndex, value, (err)=> {
  14. if (err) {
  15. console.log("mifareClassic incrementBlock AsyncCallback err: " + err);
  16. } else {
  17. console.log("mifareClassic incrementBlock AsyncCallback success.");
  18. }
  19. });
  20. } catch (busiError) {
  21. console.log("mifareClassic incrementBlock AsyncCallback catch busiError: " + busiError);
  22. }

MifareClassicTag.decrementBlock9+

decrementBlock(blockIndex: number, value: number): Promise<void>

對(duì)指定塊的內(nèi)容,減少指定的數(shù)值。使用Promise方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
blockIndexnumber要被運(yùn)算的塊索引,從0開始。
valuenumber要減少的數(shù)值,非負(fù)數(shù)。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
  3. // connect the tag at first if not connected.
  4. if (!mifareClassic.isTagConnected()) {
  5. if (!mifareClassic.connectTag()) {
  6. console.log("mifareClassic connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. let blockIndex = 1; // change it to be correct index.
  12. let value = 0x20; // change it to be correct data.
  13. mifareClassic.decrementBlock(blockIndex, value).then(() => {
  14. console.log("mifareClassic decrementBlock Promise success.");
  15. }).catch((err)=> {
  16. console.log("mifareClassic decrementBlock Promise err: " + err);
  17. });
  18. } catch (busiError) {
  19. console.log("mifareClassic decrementBlock Promise catch busiError: " + busiError);
  20. }

MifareClassicTag.decrementBlock9+

decrementBlock(blockIndex: number, value: number, callback: AsyncCallback<void>): void

對(duì)指定塊的內(nèi)容,減少指定的數(shù)值。使用AsyncCallback方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
blockIndexnumber要被運(yùn)算的塊索引,從0開始。
valuenumber要減少的數(shù)值,非負(fù)數(shù)。
callbackAsyncCallback<void>回調(diào)函數(shù)。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
  3. // connect the tag at first if not connected.
  4. if (!mifareClassic.isTagConnected()) {
  5. if (!mifareClassic.connectTag()) {
  6. console.log("mifareClassic connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. let blockIndex = 1; // change it to be correct index.
  12. let value = 0x20; // change it to be correct data.
  13. mifareClassic.decrementBlock(blockIndex, value, (err)=> {
  14. if (err) {
  15. console.log("mifareClassic decrementBlock AsyncCallback err: " + err);
  16. } else {
  17. console.log("mifareClassic decrementBlock AsyncCallback success.");
  18. }
  19. });
  20. } catch (busiError) {
  21. console.log("mifareClassic decrementBlock AsyncCallback catch busiError: " + busiError);
  22. }

MifareClassicTag.transferToBlock9+

transferToBlock(blockIndex: number): Promise<void>

將臨時(shí)寄存器的值轉(zhuǎn)移到指定的塊。使用Promise方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
blockIndexnumber被操作的塊的索引,從0開始。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼。

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
  3. // connect the tag at first if not connected.
  4. if (!mifareClassic.isTagConnected()) {
  5. if (!mifareClassic.connectTag()) {
  6. console.log("mifareClassic connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. let blockIndex = 1; // change it to be correct index.
  12. mifareClassic.transferToBlock(blockIndex).then(() => {
  13. console.log("mifareClassic transferToBlock Promise success.");
  14. }).catch((err)=> {
  15. console.log("mifareClassic transferToBlock Promise err: " + err);
  16. });
  17. } catch (busiError) {
  18. console.log("mifareClassic transferToBlock Promise catch busiError: " + busiError);
  19. }

MifareClassicTag.transferToBlock9+

transferToBlock(blockIndex: number, callback: AsyncCallback<void>): void

將臨時(shí)寄存器的值轉(zhuǎn)移到指定的塊。使用AsyncCallback方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
blockIndexnumber被操作的塊的索引,從0開始。
callbackAsyncCallback<void>回調(diào)函數(shù)。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼。

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
  3. // connect the tag at first if not connected.
  4. if (!mifareClassic.isTagConnected()) {
  5. if (!mifareClassic.connectTag()) {
  6. console.log("mifareClassic connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. let blockIndex = 1; // change it to be correct index.
  12. mifareClassic.transferToBlock(blockIndex, (err)=> {
  13. if (err) {
  14. console.log("mifareClassic transferToBlock AsyncCallback err: " + err);
  15. } else {
  16. console.log("mifareClassic transferToBlock AsyncCallback success.");
  17. }
  18. });
  19. } catch (busiError) {
  20. console.log("mifareClassic transferToBlock AsyncCallback catch busiError: " + busiError);
  21. }

MifareClassicTag.restoreFromBlock9+

restoreFromBlock(blockIndex: number): Promise<void>

將指定塊的值復(fù)制到臨時(shí)寄存器。使用Promise方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
blockIndexnumber被操作的塊的索引,從0開始。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
  3. // connect the tag at first if not connected.
  4. if (!mifareClassic.isTagConnected()) {
  5. if (!mifareClassic.connectTag()) {
  6. console.log("mifareClassic connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. let blockIndex = 1; // change it to be correct index.
  12. mifareClassic.restoreFromBlock(blockIndex).then(() => {
  13. console.log("mifareClassic restoreFromBlock Promise success.");
  14. }).catch((err)=> {
  15. console.log("mifareClassic restoreFromBlock Promise err: " + err);
  16. });
  17. } catch (busiError) {
  18. console.log("mifareClassic restoreFromBlock Promise catch busiError: " + busiError);
  19. }

MifareClassicTag.restoreFromBlock9+

restoreFromBlock(blockIndex: number, callback: AsyncCallback<void>): void

將指定塊的值復(fù)制到臨時(shí)寄存器。使用AsyncCallback方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
blockIndexnumber被操作的塊的索引,從0開始。
callbackAsyncCallback<void>回調(diào)函數(shù)。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼。

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
  3. // connect the tag at first if not connected.
  4. if (!mifareClassic.isTagConnected()) {
  5. if (!mifareClassic.connectTag()) {
  6. console.log("mifareClassic connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. let blockIndex = 1; // change it to be correct index.
  12. mifareClassic.restoreFromBlock(blockIndex, (err)=> {
  13. if (err) {
  14. console.log("mifareClassic restoreFromBlock AsyncCallback err: " + err);
  15. } else {
  16. console.log("mifareClassic restoreFromBlock AsyncCallback success.");
  17. }
  18. });
  19. } catch (busiError) {
  20. console.log("mifareClassic restoreFromBlock AsyncCallback catch busiError: " + busiError);
  21. }

MifareClassicTag.getSectorCount9+

getSectorCount(): number

獲取MIFARE Classic標(biāo)簽中的扇區(qū)數(shù)。

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

返回值:

類型說明
number標(biāo)簽中的扇區(qū)數(shù)量。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
  3. let sectorCount = mifareClassic.getSectorCount();
  4. console.log("mifareClassic sectorCount: " + sectorCount);

MifareClassicTag.getBlockCountInSector9+

getBlockCountInSector(sectorIndex: number): number

獲取指定扇區(qū)中的塊數(shù)。

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
sectorIndexnumber扇區(qū)序號(hào),從0開始。

返回值:

類型說明
number該扇區(qū)內(nèi)的塊數(shù)量。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
  3. try {
  4. let sectorIndex = 1; // change it to be correct index.
  5. let blockCnt = mifareClassic.getBlockCountInSector(sectorIndex);
  6. console.log("mifareClassic blockCnt: " + blockCnt);
  7. } catch (busiError) {
  8. console.log("mifareClassic getBlockCountInSector catch busiError: " + busiError);
  9. }

MifareClassicTag.getType9+

getType(): tag.MifareClassicType

獲取MIFARE Classic標(biāo)簽的類型。

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

返回值:

類型說明
tag.MifareClassicTypeMifareClassic標(biāo)簽的類型。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
  3. let getType = mifareClassic.getType();
  4. console.log("mifareClassic getType: " + getType);

MifareClassicTag.getTagSize9+

getTagSize(): number

獲取標(biāo)簽的存儲(chǔ)空間大小,具體請(qǐng)參見MifareClassicSize。

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

返回值:

類型說明
number標(biāo)簽的大小,單位為字節(jié),請(qǐng)參見MifareClassicSize。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
  3. let tagSize = mifareClassic.getTagSize();
  4. console.log("mifareClassic tagSize: " + tagSize);

MifareClassicTag.isEmulatedTag9+

isEmulatedTag(): boolean

檢查標(biāo)簽是不是被模擬的。

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

返回值:

類型說明
boolean檢查結(jié)果,true: 是;false:否。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
  3. let isEmulatedTag = mifareClassic.isEmulatedTag();
  4. console.log("mifareClassic isEmulatedTag: " + isEmulatedTag);

MifareClassicTag.getBlockIndex9+

getBlockIndex(sectorIndex: number): number

獲取特定扇區(qū)的第一個(gè)塊的序號(hào)。

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
sectorIndexnumber扇區(qū)序號(hào),從0開始。

返回值:

類型說明
number該扇區(qū)內(nèi)的第一個(gè)塊的序號(hào),從0開始。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
  3. try {
  4. let sectorIndex = 1; // change it to be correct index.
  5. let blockIndex = mifareClassic.getBlockIndex(sectorIndex);
  6. console.log("mifareClassic blockIndex: " + blockIndex);
  7. } catch (busiError) {
  8. console.log("mifareClassic getBlockIndex catch busiError: " + busiError);
  9. }

MifareClassicTag.getSectorIndex9+

getSectorIndex(blockIndex: number): number

獲取包含指定塊號(hào)的扇區(qū)序號(hào)。

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
blockIndexnumber塊序號(hào),從0開始。

返回值:

類型說明
number扇區(qū)序號(hào),從0開始。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareClassic' correctly.
  3. try {
  4. let blockIndex = 1; // change it to be correct index.
  5. let sectorIndex = mifareClassic.getSectorIndex(blockIndex);
  6. console.log("mifareClassic sectorIndex: " + sectorIndex);
  7. } catch (busiError) {
  8. console.log("mifareClassic getSectorIndex catch busiError: " + busiError);
  9. }

MifareUltralightTag9+

MifareUltralightTag 提供對(duì)MIFARE Ultralight屬性和I/O操作的訪問,繼承自TagSession。

TagSession是所有NFC Tag技術(shù)類型的基類, 提供建立連接和發(fā)送數(shù)據(jù)等共同接口。具體請(qǐng)參見TagSession

以下是MifareUltralightTag的獨(dú)有接口。

MifareUltralightTag.readMultiplePages9+

readMultiplePages(pageIndex: number): Promise<number[]>

讀取標(biāo)簽的4頁(yè)數(shù)據(jù),共16字節(jié)的數(shù)據(jù)。每個(gè)頁(yè)面數(shù)據(jù)大小為4字節(jié)。使用Promise方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
pageIndexnumber要讀取頁(yè)面的索引,從0開始。

返回值:

類型說明
Promise<number[]>讀取的4頁(yè)的數(shù)據(jù),共16字節(jié)。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼。

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareUltralight' correctly.
  3. // connect the tag at first if not connected.
  4. if (!mifareUltralight.isTagConnected()) {
  5. if (!mifareUltralight.connectTag()) {
  6. console.log("mifareUltralight connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. let pageIndex = 1; // change it to be correct index.
  12. mifareUltralight.readMultiplePages(pageIndex).then((data) => {
  13. console.log("mifareUltralight readMultiplePages Promise data = " + data);
  14. }).catch((err)=> {
  15. console.log("mifareUltralight readMultiplePages Promise err: " + err);
  16. });
  17. } catch (busiError) {
  18. console.log("mifareUltralight readMultiplePages Promise catch busiError: " + busiError);
  19. }

MifareUltralightTag.readMultiplePages9+

readMultiplePages(pageIndex: number, callback: AsyncCallback<number[]>): void

讀取標(biāo)簽的4頁(yè)數(shù)據(jù),共16字節(jié)的數(shù)據(jù)。每個(gè)頁(yè)面數(shù)據(jù)大小為4字節(jié)。使用AsyncCallback方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
pageIndexnumber要讀取頁(yè)面的索引,從0開始。
callbackAsyncCallback<number[]>回調(diào)函數(shù),返回讀取到的數(shù)據(jù),共16字節(jié)。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareUltralight' correctly.
  3. // connect the tag at first if not connected.
  4. if (!mifareUltralight.isTagConnected()) {
  5. if (!mifareUltralight.connectTag()) {
  6. console.log("mifareUltralight connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. let pageIndex = 1; // change it to be correct index.
  12. mifareUltralight.readMultiplePages(pageIndex, (err, data)=> {
  13. if (err) {
  14. console.log("mifareUltralight readMultiplePages AsyncCallback err: " + err);
  15. } else {
  16. console.log("mifareUltralight readMultiplePages AsyncCallback data: " + data);
  17. }
  18. });
  19. } catch (busiError) {
  20. console.log("mifareUltralight readMultiplePages AsyncCallback catch busiError: " + busiError);
  21. }

MifareUltralightTag.writeSinglePage9+

writeSinglePage(pageIndex: number, data: number[]): Promise<void>

寫入一頁(yè)數(shù)據(jù),數(shù)據(jù)大小為4字節(jié)。使用Promise方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
pageIndexnumber要寫入頁(yè)面的索引,從0開始。
datanumber[]要寫入頁(yè)面的數(shù)據(jù)內(nèi)容,必須是4個(gè)字節(jié)大小。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼。

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareUltralight' correctly.
  3. // connect the tag at first if not connected.
  4. if (!mifareUltralight.isTagConnected()) {
  5. if (!mifareUltralight.connectTag()) {
  6. console.log("mifareUltralight connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. let pageIndex = 1; // change it to be correct index.
  12. let rawData = [0x01, 0x02, 0x03, 0x04]; // MUST be 4 bytes, change it to be correct raw data.
  13. mifareUltralight.writeSinglePage(pageIndex, rawData).then(() => {
  14. console.log("mifareUltralight writeSinglePage Promise success.");
  15. }).catch((err)=> {
  16. console.log("mifareUltralight writeSinglePage Promise err: " + err);
  17. });
  18. } catch (busiError) {
  19. console.log("mifareUltralight writeSinglePage Promise catch busiError: " + busiError);
  20. }

MifareUltralightTag.writeSinglePage9+

writeSinglePage(pageIndex: number, data: number[], callback: AsyncCallback<void>): void

寫入一頁(yè)數(shù)據(jù),數(shù)據(jù)大小為4字節(jié)。使用AsyncCallback方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
pageIndexnumber要寫入頁(yè)面的索引,從0開始。
datanumber[]要寫入頁(yè)面的數(shù)據(jù)內(nèi)容,必須是4個(gè)字節(jié)大小。
callbackAsyncCallback<void>回調(diào)函數(shù)。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼。

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareUltralight' correctly.
  3. // connect the tag at first if not connected.
  4. if (!mifareUltralight.isTagConnected()) {
  5. if (!mifareUltralight.connectTag()) {
  6. console.log("mifareUltralight connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. let pageIndex = 1; // change it to be correct index.
  12. let rawData = [0x01, 0x02, 0x03, 0x04]; // MUST be 4 bytes, change it to be correct raw data.
  13. mifareUltralight.writeSinglePage(pageIndex, rawData, (err)=> {
  14. if (err) {
  15. console.log("mifareUltralight writeSinglePage AsyncCallback err: " + err);
  16. } else {
  17. console.log("mifareUltralight writeSinglePage AsyncCallback success.");
  18. }
  19. });
  20. } catch (busiError) {
  21. console.log("mifareUltralight writeSinglePage AsyncCallback catch busiError: " + busiError);
  22. }

MifareUltralightTag.getType9+

getType(): tag.MifareUltralightType

獲取MIFARE Ultralight標(biāo)簽的類型。

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

返回值:

類型說明
tag.MifareUltralightTypeMIFARE Ultralight標(biāo)簽的類型。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'mifareUltralight' correctly.
  3. let getType = mifareClassic.getType();
  4. console.log("mifareUltralight getType: " + getType);

NdefFormatableTag9+

NdefFormatableTag為NDEF Formattable的標(biāo)簽提供格式化操作,繼承自TagSession。

TagSession是所有NFC Tag 技術(shù)類型的基類, 提供建立連接和發(fā)送數(shù)據(jù)等共同接口。具體請(qǐng)參見TagSession。

以下是NdefFormatableTag的獨(dú)有接口。

NdefFormatableTag.format9+

format(message: NdefMessage): Promise<void>

將標(biāo)簽格式化為NDEF標(biāo)簽,將NDEF消息寫入NDEF標(biāo)簽。使用Promise方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
messageNdefMessage格式化成功時(shí)要寫入的NDEF消息。可以為null,為null時(shí)僅格式化標(biāo)簽,不寫入內(nèi)容。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼。

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefFormatable' correctly.
  3. // connect the tag at first if not connected.
  4. if (!ndefFormatable.isTagConnected()) {
  5. if (!ndefFormatable.connectTag()) {
  6. console.log("ndefFormatable connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. // ndefMessage created from raw data, such as:
  12. let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // MUST can be parsed as NDEF Record.
  13. // or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[])
  14. ndefFormatable.format(ndefMessage).then(() => {
  15. console.log("ndefFormatable format Promise success.");
  16. }).catch((err)=> {
  17. console.log("ndefFormatable format Promise err: " + err);
  18. });
  19. } catch (busiError) {
  20. console.log("ndefFormatable format Promise catch busiError: " + busiError);
  21. }

NdefFormatableTag.format9+

format(message: NdefMessage, callback: AsyncCallback<void>): void

將標(biāo)簽格式化為NDEF標(biāo)簽,然后將NDEF消息寫入NDEF標(biāo)簽。使用AsyncCallback方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
messageNdefMessage格式化成功時(shí)要寫入的Ndef消息??梢詾閚ull,為null時(shí)僅格式化標(biāo)簽,不寫入內(nèi)容。

返回值:

類型說明
callback: AsyncCallback<void>回調(diào)函數(shù)。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefFormatable' correctly.
  3. // connect the tag at first if not connected.
  4. if (!ndefFormatable.isTagConnected()) {
  5. if (!ndefFormatable.connectTag()) {
  6. console.log("ndefFormatable connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. // ndefMessage created from raw data, such as:
  12. let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // MUST can be parsed as NDEF Record.
  13. // or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[])
  14. ndefFormatable.format(ndefMessage, (err)=> {
  15. if (err) {
  16. console.log("ndefFormatable format AsyncCallback err: " + err);
  17. } else {
  18. console.log("ndefFormatable format AsyncCallback success.");
  19. }
  20. });
  21. } catch (busiError) {
  22. console.log("ndefFormatable format AsyncCallback catch busiError: " + busiError);
  23. }

NdefFormatableTag.formatReadOnly9+

formatReadOnly(message: NdefMessage): Promise<void>

將標(biāo)簽格式化為NDEF標(biāo)簽,將NDEF消息寫入NDEF標(biāo)簽,之后將標(biāo)簽設(shè)置為只讀。使用Promise方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
messageNdefMessage格式化成功時(shí)要寫入的NDEF消息??梢詾閚ull,為null時(shí)僅格式化標(biāo)簽,不寫入內(nèi)容。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼。

錯(cuò)誤碼ID錯(cuò)誤信息
3100201Tag running state is abnormal in service.

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefFormatable' correctly.
  3. // connect the tag at first if not connected.
  4. if (!ndefFormatable.isTagConnected()) {
  5. if (!ndefFormatable.connectTag()) {
  6. console.log("ndefFormatable connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. // ndefMessage created from raw data, such as:
  12. let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // MUST can be parsed as NDEF Record.
  13. // or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[])
  14. ndefFormatable.formatReadOnly(ndefMessage).then(() => {
  15. console.log("ndefFormatable formatReadOnly Promise success.");
  16. }).catch((err)=> {
  17. console.log("ndefFormatable formatReadOnly Promise err: " + err);
  18. });
  19. } catch (busiError) {
  20. console.log("ndefFormatable formatReadOnly Promise catch busiError: " + busiError);
  21. }

NdefFormatableTag.formatReadOnly9+

formatReadOnly(message: NdefMessage, callback: AsyncCallback<void>): void

將標(biāo)簽格式化為NDEF標(biāo)簽,然后將NDEF消息寫入NDEF標(biāo)簽,之后將標(biāo)簽設(shè)置為只讀。使用callback方式作為異步方法。

需要權(quán)限: ohos.permission.NFC_TAG

系統(tǒng)能力: SystemCapability.Communication.NFC.Tag

參數(shù):

參數(shù)名類型必填說明
messageNdefMessage格式化成功時(shí)要寫入的NDEF消息??梢詾閚ull,為null時(shí)僅格式化標(biāo)簽,不寫入內(nèi)容。

返回值:

類型說明
callback: AsyncCallback<void>回調(diào)函數(shù)。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', obtains the 'ndefFormatable' correctly.
  3. // connect the tag at first if not connected.
  4. if (!ndefFormatable.isTagConnected()) {
  5. if (!ndefFormatable.connectTag()) {
  6. console.log("ndefFormatable connectTag failed.");
  7. return;
  8. }
  9. }
  10. try {
  11. // ndefMessage created from raw data, such as:
  12. let ndefMessage = tag.ndef.createNdefMessage([0xD1, 0x01, 0x03, 0x54, 0x4E, 0x46, 0x43]); // MUST can be parsed as NDEF Record.
  13. // or ndefMessage created from tag.ndef.createNdefMessage(ndefRecords: NdefRecord[])
  14. ndefFormatable.formatReadOnly(ndefMessage, (err)=> {
  15. if (err) {
  16. console.log("ndefFormatable formatReadOnly AsyncCallback err: " + err);
  17. } else {
  18. console.log("ndefFormatable formatReadOnly AsyncCallback success.");
  19. }
  20. });
  21. } catch (busiError) {
  22. console.log("ndefFormatable formatReadOnly AsyncCallback catch busiError: " + busiError);
  23. }
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)