標(biāo)準(zhǔn)NFC-Tag TagSession

2024-01-23 17:13 更新

本模塊是對(duì)NFC TagSession的使用說(shuō)明。

說(shuō)明

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

導(dǎo)入模塊

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

tagSession

tagSession是所有NFC Tag技術(shù)類型的基類, 提供建立連接和發(fā)送數(shù)據(jù)等共同接口。

需要通過(guò)其子類來(lái)訪問(wèn)以下接口。在下面的示例中 統(tǒng)一用 getXXX()表示獲取子類實(shí)例的方法。

具體使用時(shí),請(qǐng)根據(jù)實(shí)際采用的Nfc tag技術(shù),選擇對(duì)應(yīng)的方法,具體請(qǐng)參見nfcTag文檔。

tagSession.getTagInfo

getTagInfo(): tag.TagInfo

獲取該Tag被分發(fā)時(shí),NFC服務(wù)所提供的Tag數(shù)據(jù)對(duì)象。

說(shuō)明

從 API version 7 開始支持,從 API version 9 開始廢棄,建議使用tag.getTagInfo替代。

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

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

返回值:

類型說(shuō)明
TagInfoNFC服務(wù)所提供的Tag數(shù)據(jù)對(duì)象。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
  3. // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
  4. let tagInfo = tag.getIsoDep(tagInfo).getTagInfo();
  5. console.log("tag tagInfo: " + tagInfo);

tagSession.connectTag

connectTag(): boolean;

和標(biāo)簽建立連接。在從標(biāo)簽讀取數(shù)據(jù)或?qū)?shù)據(jù)寫入標(biāo)簽之前,必須調(diào)用此方法。

說(shuō)明

從 API version 7 開始支持,從 API version 9 開始廢棄,建議使用tagSession.connect替代。

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

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

返回值:

類型說(shuō)明
boolean連接建立成功返回true,失敗返回false。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
  3. // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
  4. let connectStatus = tag.getIsoDep(tagInfo).connectTag();
  5. console.log("connectStatus: " + connectStatus);

tagSession.connect9+

connect(): void;

和標(biāo)簽建立連接。在從標(biāo)簽讀取數(shù)據(jù)或?qū)?shù)據(jù)寫入標(biāo)簽之前,必須調(diào)用此方法。

需要權(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', tagInfo is an Object given by nfc service when tag is dispatched.
  3. // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
  4. try {
  5. tag.getIsoDep(tagInfo).connect();
  6. console.log("tag connect success");
  7. } catch (busiError) {
  8. console.log("tag connect busiError: " + busiError);
  9. }

tagSession.reset()

reset(): void

重置與標(biāo)簽的連接。

說(shuō)明

從 API version 7 開始支持,從 API version 9 開始廢棄,建議使用tagSession.resetConnection替代。

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

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

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
  3. // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
  4. tag.getIsoDep(tagInfo).reset();

tagSession.resetConnection()9+

resetConnection(): void

重置與標(biāo)簽的連接。

需要權(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', tagInfo is an Object given by nfc service when tag is dispatched.
  3. // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
  4. try {
  5. tag.getIsoDep(tagInfo).resetConnection();
  6. console.log("tag resetConnection success");
  7. } catch (busiError) {
  8. console.log("tag resetConnection busiError: " + busiError);
  9. }

tagSession.isTagConnected

isTagConnected(): boolean

檢查是否已與標(biāo)簽建立連接。

說(shuō)明

從 API version 7 開始支持,從 API version 9 開始廢棄,建議使用tagSession.isConnected替代。

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

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

返回值:

類型說(shuō)明
boolean已建立連接返回 true,未建立連接返回false。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
  3. // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
  4. let isTagConnected = tag.getIsoDep(tagInfo).isTagConnected();
  5. console.log("isTagConnected: " + isTagConnected);

tagSession.isConnected9+

isConnected(): boolean

檢查是否已與標(biāo)簽建立連接。

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

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

返回值:

類型說(shuō)明
boolean已建立連接返回 true,未建立連接返回false。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
  3. // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
  4. try {
  5. var isConnected = tag.getIsoDep(tagInfo).isConnected();
  6. console.log("tag isConnected = " + isConnected);
  7. } catch (busiError) {
  8. console.log("tag isConnected busiError: " + busiError);
  9. }

tagSession.getMaxSendLength

getMaxSendLength(): number

查詢可以發(fā)送到標(biāo)簽的最大數(shù)據(jù)長(zhǎng)度。

說(shuō)明

從 API version 7 開始支持,從 API version 9 開始廢棄,建議使用tagSession.getMaxTransmitSize替代。

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

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

返回值:

類型說(shuō)明
number可以發(fā)送到標(biāo)簽的最大數(shù)據(jù)長(zhǎng)度,非負(fù)數(shù)。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
  3. // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
  4. let maxSendLen = tag.getIsoDep(tagInfo).getMaxSendLength();
  5. console.log("tag maxSendLen: " + maxSendLen);

tagSession.getMaxTransmitSize9+

getMaxTransmitSize(): number

查詢可以發(fā)送到標(biāo)簽的最大數(shù)據(jù)長(zhǎng)度。

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

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

返回值:

類型說(shuō)明
number可以發(fā)送到標(biāo)簽的最大數(shù)據(jù)長(zhǎng)度,非負(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', tagInfo is an Object given by nfc service when tag is dispatched.
  3. // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
  4. try {
  5. var maxTransmitSize = tag.getIsoDep(tagInfo).getMaxTransmitSize();
  6. console.log("tag maxTransmitSize = " + maxTransmitSize);
  7. } catch (busiError) {
  8. console.log("tag getMaxTransmitSize busiError: " + busiError);
  9. }

tagSession.getSendDataTimeout

getSendDataTimeout(): number

查詢發(fā)送數(shù)據(jù)到Tag的等待超時(shí)時(shí)間,單位是毫秒。

說(shuō)明

從 API version 7 開始支持,從 API version 9 開始廢棄,建議使用tagSession.getTimeout替代。

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

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

返回值:

類型說(shuō)明
number發(fā)送數(shù)據(jù)到Tag的等待超時(shí)時(shí)間,單位是毫秒,非負(fù)數(shù)。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
  3. // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
  4. let sendDataTimeout = tag.getIsoDep(tagInfo).getSendDataTimeout();
  5. console.log("tag sendDataTimeout: " + sendDataTimeout);

tagSession.getTimeout9+

getTimeout(): number

查詢發(fā)送數(shù)據(jù)到Tag的等待超時(shí)時(shí)間,單位是毫秒。

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

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

返回值:

類型說(shuō)明
number發(fā)送數(shù)據(jù)到Tag的等待超時(shí)時(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', tagInfo is an Object given by nfc service when tag is dispatched.
  3. // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
  4. try {
  5. var timeout = tag.getIsoDep(tagInfo).getTimeout();
  6. console.log("tag timeout = " + timeout);
  7. } catch (busiError) {
  8. console.log("tag getTimeout busiError: " + busiError);
  9. }

tagSession.setSendDataTimeout

setSendDataTimeout(timeout: number): boolean

查詢發(fā)送數(shù)據(jù)到Tag的等待超時(shí)時(shí)間,單位是毫秒。

說(shuō)明

從 API version 7 開始支持,從 API version 9 開始廢棄,建議使用tagSession.setTimeout替代。

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

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

參數(shù):

參數(shù)名類型必填說(shuō)明
timeoutnumber超時(shí)時(shí)間,單位毫秒,非負(fù)值。

返回值:

類型說(shuō)明
boolean設(shè)置超時(shí)時(shí)間成功返回true,設(shè)置失敗返回false。

示例:

  1. import tag from '@ohos.nfc.tag';
  2. // see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
  3. // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
  4. let timeoutMs = 700; // change it to be correct.
  5. let setStatus = tag.getIsoDep(tagInfo).setSendDataTimeout(timeoutMs);
  6. console.log("tag setSendDataTimeout setStatus: " + setStatus);

tagSession.setTimeout9+

setTimeout(timeout: number): void

查詢發(fā)送數(shù)據(jù)到Tag的等待超時(shí)時(shí)間,單位是毫秒。

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

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

參數(shù):

參數(shù)名類型必填說(shuō)明
timeoutnumber超時(shí)時(shí)間,單位毫秒,非負(fù)值。

錯(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', tagInfo is an Object given by nfc service when tag is dispatched.
  3. // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
  4. let timeoutMs = 700; // change it to be correct.
  5. try {
  6. tag.getIsoDep(tagInfo).setTimeout(timeoutMs);
  7. console.log("tag setTimeout success");
  8. } catch (busiError) {
  9. console.log("tag setTimeout busiError: " + busiError);
  10. }

tagSession.sendData

sendData(data: number[]): Promise<number[]>

發(fā)送指令到Tag上,使用Promise方式作為異步方法。

說(shuō)明

從 API version 7 開始支持,從 API version 9 開始廢棄,建議使用tagSession.transmit替代。

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

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

參數(shù):

參數(shù)名類型必填說(shuō)明
datanumber[]要發(fā)送的指令。每個(gè)number十六進(jìn)制表示,范圍是0x00~0xFF。

返回值:

類型說(shuō)明
Promise<number[]>對(duì)端Tag對(duì)指令的響應(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', tagInfo is an Object given by nfc service when tag is dispatched.
  3. // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
  4. // connect the tag at first if not connected.
  5. if (!tag.getIsoDep(tagInfo).isTagConnected()) {
  6. if (!tag.getIsoDep(tagInfo).connectTag()) {
  7. console.log("tagSession connectTag failed.");
  8. return;
  9. }
  10. }
  11. let cmdData = [0x01, 0x02, 0x03, 0x04]; // change the raw data to be correct.
  12. tag.getIsoDep(tagInfo).sendData(cmdData).then((response) => {
  13. console.log("tagSession sendData Promise response: " + response);
  14. }).catch((err)=> {
  15. console.log("tagSession sendData Promise err: " + err);
  16. });

tagSession.sendData

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

發(fā)送指令到Tag上,使用AsyncCallback方式作為異步方法。

說(shuō)明

從 API version 7 開始支持,從 API version 9 開始廢棄,建議使用tagSession.transmit替代。

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

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

參數(shù):

參數(shù)名類型必填說(shuō)明
datanumber[]要發(fā)送的指令。每個(gè)number十六進(jìn)制表示,范圍是0x00~0xFF。
callbackAsyncCallback<number[]>回調(diào)函數(shù),返回響應(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', tagInfo is an Object given by nfc service when tag is dispatched.
  3. // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
  4. // connect the tag at first if not connected.
  5. if (!tag.getIsoDep(tagInfo).isTagConnected()) {
  6. if (!tag.getIsoDep(tagInfo).connectTag()) {
  7. console.log("tagSession connectTag failed.");
  8. return;
  9. }
  10. }
  11. let cmdData = [0x01, 0x02, 0x03, 0x04]; // change the raw data to be correct.
  12. tag.getIsoDep(tagInfo).sendData(cmdData, (err, response)=> {
  13. if (err) {
  14. console.log("tagSession sendData AsyncCallback err: " + err);
  15. } else {
  16. console.log("tagSession sendData AsyncCallback response: " + response);
  17. }
  18. });

tagSession.transmit9+

transmit(data: number[]): Promise<number[]>

發(fā)送指令到Tag上,使用Promise方式作為異步方法。

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

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

參數(shù):

參數(shù)名類型必填說(shuō)明
datanumber[]要發(fā)送的指令。每個(gè)number十六進(jìn)制表示,范圍是0x00~0xFF。

返回值:

類型說(shuō)明
Promise<number[]>對(duì)端Tag對(duì)指令的響應(yīng)數(shù)據(jù)。每個(gè)number十六進(jìn)制表示,范圍是0x00~0xFF。

錯(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', tagInfo is an Object given by nfc service when tag is dispatched.
  3. // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
  4. // connect the tag at first if not connected.
  5. try {
  6. if (!tag.getIsoDep(tagInfo).isConnected()) {
  7. tag.getIsoDep(tagInfo).connect();
  8. }
  9. } catch (busiError) {
  10. console.log("tag connect busiError: " + busiError);
  11. return;
  12. }
  13. let cmdData = [0x01, 0x02, 0x03, 0x04]; // change the raw data to be correct.
  14. try {
  15. tag.getIsoDep(tagInfo).transmit(cmdData).then((response) => {
  16. console.log("tagSession transmit Promise response: " + response);
  17. }).catch((err)=> {
  18. console.log("tagSession transmit Promise err: " + err);
  19. });
  20. } catch (busiError) {
  21. console.log("tag transmit busiError: " + busiError);
  22. return;
  23. }

tagSession.transmit9+

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

發(fā)送指令到Tag上,使用AsyncCallback方式作為異步方法。

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

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

參數(shù):

參數(shù)名類型必填說(shuō)明
datanumber[]要發(fā)送的指令。每個(gè)number十六進(jìn)制表示,范圍是0x00~0xFF。
callbackAsyncCallback<number[]>回調(diào)函數(shù),返回響應(yīng)數(shù)據(jù)。每個(gè)number十六進(jìn)制表示,范圍是0x00~0xFF。

錯(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', tagInfo is an Object given by nfc service when tag is dispatched.
  3. // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
  4. // connect the tag at first if not connected.
  5. try {
  6. if (!tag.getIsoDep(tagInfo).isConnected()) {
  7. tag.getIsoDep(tagInfo).connect();
  8. }
  9. } catch (busiError) {
  10. console.log("tag connect busiError: " + busiError);
  11. return;
  12. }
  13. let cmdData = [0x01, 0x02, 0x03, 0x04]; // change the raw data to be correct.
  14. try {
  15. tag.getIsoDep(tagInfo).transmit(cmdData, (err, response)=> {
  16. if (err) {
  17. console.log("tagSession transmit AsyncCallback err: " + err);
  18. } else {
  19. console.log("tagSession transmit AsyncCallback response: " + response);
  20. }
  21. });
  22. } catch (busiError) {
  23. console.log("tag transmit busiError: " + busiError);
  24. return;
  25. }
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

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

編程獅公眾號(hào)