W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
本模塊是對(duì)NFC TagSession的使用說明。
本模塊首批接口從API version 7開始支持。后續(xù)版本的新增接口,采用上角標(biāo)單獨(dú)標(biāo)記接口的起始版本。
tagSession是所有NFC Tag技術(shù)類型的基類, 提供建立連接和發(fā)送數(shù)據(jù)等共同接口。
需要通過其子類來(lái)訪問以下接口。在下面的示例中 統(tǒng)一用 getXXX()表示獲取子類實(shí)例的方法。
具體使用時(shí),請(qǐng)根據(jù)實(shí)際采用的Nfc tag技術(shù),選擇對(duì)應(yīng)的方法,具體請(qǐng)參見nfcTag文檔。
getTagInfo(): tag.TagInfo
獲取該Tag被分發(fā)時(shí),NFC服務(wù)所提供的Tag數(shù)據(jù)對(duì)象。
從 API version 7 開始支持,從 API version 9 開始廢棄,建議使用tag.getTagInfo替代。
需要權(quán)限: ohos.permission.NFC_TAG
系統(tǒng)能力: SystemCapability.Communication.NFC.Tag
返回值:
類型 | 說明 |
---|---|
TagInfo | NFC服務(wù)所提供的Tag數(shù)據(jù)對(duì)象。 |
示例:
- import tag from '@ohos.nfc.tag';
- // see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
- // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
- let tagInfo = tag.getIsoDep(tagInfo).getTagInfo();
- console.log("tag tagInfo: " + tagInfo);
connectTag(): boolean;
和標(biāo)簽建立連接。在從標(biāo)簽讀取數(shù)據(jù)或?qū)?shù)據(jù)寫入標(biāo)簽之前,必須調(diào)用此方法。
從 API version 7 開始支持,從 API version 9 開始廢棄,建議使用tagSession.connect替代。
需要權(quán)限: ohos.permission.NFC_TAG
系統(tǒng)能力: SystemCapability.Communication.NFC.Tag
返回值:
類型 | 說明 |
---|---|
boolean | 連接建立成功返回true,失敗返回false。 |
示例:
- import tag from '@ohos.nfc.tag';
- // see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
- // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
- let connectStatus = tag.getIsoDep(tagInfo).connectTag();
- console.log("connectStatus: " + connectStatus);
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ò)誤信息 |
---|---|
3100201 | Tag running state is abnormal in service. |
示例:
- import tag from '@ohos.nfc.tag';
- // see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
- // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
- try {
- tag.getIsoDep(tagInfo).connect();
- console.log("tag connect success");
- } catch (busiError) {
- console.log("tag connect busiError: " + busiError);
- }
reset(): void
重置與標(biāo)簽的連接。
從 API version 7 開始支持,從 API version 9 開始廢棄,建議使用tagSession.resetConnection替代。
需要權(quán)限: ohos.permission.NFC_TAG
系統(tǒng)能力: SystemCapability.Communication.NFC.Tag
示例:
- import tag from '@ohos.nfc.tag';
- // see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
- // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
- tag.getIsoDep(tagInfo).reset();
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ò)誤信息 |
---|---|
3100201 | Tag running state is abnormal in service. |
示例:
- import tag from '@ohos.nfc.tag';
- // see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
- // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
- try {
- tag.getIsoDep(tagInfo).resetConnection();
- console.log("tag resetConnection success");
- } catch (busiError) {
- console.log("tag resetConnection busiError: " + busiError);
- }
isTagConnected(): boolean
檢查是否已與標(biāo)簽建立連接。
從 API version 7 開始支持,從 API version 9 開始廢棄,建議使用tagSession.isConnected替代。
需要權(quán)限: ohos.permission.NFC_TAG
系統(tǒng)能力: SystemCapability.Communication.NFC.Tag
返回值:
類型 | 說明 |
---|---|
boolean | 已建立連接返回 true,未建立連接返回false。 |
示例:
- import tag from '@ohos.nfc.tag';
- // see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
- // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
- let isTagConnected = tag.getIsoDep(tagInfo).isTagConnected();
- console.log("isTagConnected: " + isTagConnected);
isConnected(): boolean
檢查是否已與標(biāo)簽建立連接。
需要權(quán)限: ohos.permission.NFC_TAG
系統(tǒng)能力: SystemCapability.Communication.NFC.Tag
返回值:
類型 | 說明 |
---|---|
boolean | 已建立連接返回 true,未建立連接返回false。 |
示例:
- import tag from '@ohos.nfc.tag';
- // see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
- // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
- try {
- var isConnected = tag.getIsoDep(tagInfo).isConnected();
- console.log("tag isConnected = " + isConnected);
- } catch (busiError) {
- console.log("tag isConnected busiError: " + busiError);
- }
getMaxSendLength(): number
查詢可以發(fā)送到標(biāo)簽的最大數(shù)據(jù)長(zhǎng)度。
從 API version 7 開始支持,從 API version 9 開始廢棄,建議使用tagSession.getMaxTransmitSize替代。
需要權(quán)限: ohos.permission.NFC_TAG
系統(tǒng)能力: SystemCapability.Communication.NFC.Tag
返回值:
類型 | 說明 |
---|---|
number | 可以發(fā)送到標(biāo)簽的最大數(shù)據(jù)長(zhǎng)度,非負(fù)數(shù)。 |
示例:
- import tag from '@ohos.nfc.tag';
- // see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
- // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
- let maxSendLen = tag.getIsoDep(tagInfo).getMaxSendLength();
- console.log("tag maxSendLen: " + maxSendLen);
getMaxTransmitSize(): number
查詢可以發(fā)送到標(biāo)簽的最大數(shù)據(jù)長(zhǎng)度。
需要權(quán)限: ohos.permission.NFC_TAG
系統(tǒng)能力: SystemCapability.Communication.NFC.Tag
返回值:
類型 | 說明 |
---|---|
number | 可以發(fā)送到標(biāo)簽的最大數(shù)據(jù)長(zhǎng)度,非負(fù)數(shù)。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
3100201 | Tag running state is abnormal in service. |
示例:
- import tag from '@ohos.nfc.tag';
- // see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
- // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
- try {
- var maxTransmitSize = tag.getIsoDep(tagInfo).getMaxTransmitSize();
- console.log("tag maxTransmitSize = " + maxTransmitSize);
- } catch (busiError) {
- console.log("tag getMaxTransmitSize busiError: " + busiError);
- }
getSendDataTimeout(): number
查詢發(fā)送數(shù)據(jù)到Tag的等待超時(shí)時(shí)間,單位是毫秒。
從 API version 7 開始支持,從 API version 9 開始廢棄,建議使用tagSession.getTimeout替代。
需要權(quán)限: ohos.permission.NFC_TAG
系統(tǒng)能力: SystemCapability.Communication.NFC.Tag
返回值:
類型 | 說明 |
---|---|
number | 發(fā)送數(shù)據(jù)到Tag的等待超時(shí)時(shí)間,單位是毫秒,非負(fù)數(shù)。 |
示例:
- import tag from '@ohos.nfc.tag';
- // see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
- // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
- let sendDataTimeout = tag.getIsoDep(tagInfo).getSendDataTimeout();
- console.log("tag sendDataTimeout: " + sendDataTimeout);
getTimeout(): number
查詢發(fā)送數(shù)據(jù)到Tag的等待超時(shí)時(shí)間,單位是毫秒。
需要權(quán)限: ohos.permission.NFC_TAG
系統(tǒng)能力: SystemCapability.Communication.NFC.Tag
返回值:
類型 | 說明 |
---|---|
number | 發(fā)送數(shù)據(jù)到Tag的等待超時(shí)時(shí)間,單位是毫秒,非負(fù)數(shù)。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
3100201 | Tag running state is abnormal in service. |
示例:
- import tag from '@ohos.nfc.tag';
- // see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
- // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
- try {
- var timeout = tag.getIsoDep(tagInfo).getTimeout();
- console.log("tag timeout = " + timeout);
- } catch (busiError) {
- console.log("tag getTimeout busiError: " + busiError);
- }
setSendDataTimeout(timeout: number): boolean
查詢發(fā)送數(shù)據(jù)到Tag的等待超時(shí)時(shí)間,單位是毫秒。
從 API version 7 開始支持,從 API version 9 開始廢棄,建議使用tagSession.setTimeout替代。
需要權(quán)限: ohos.permission.NFC_TAG
系統(tǒng)能力: SystemCapability.Communication.NFC.Tag
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
timeout | number | 是 | 超時(shí)時(shí)間,單位毫秒,非負(fù)值。 |
返回值:
類型 | 說明 |
---|---|
boolean | 設(shè)置超時(shí)時(shí)間成功返回true,設(shè)置失敗返回false。 |
示例:
- import tag from '@ohos.nfc.tag';
- // see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
- // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
- let timeoutMs = 700; // change it to be correct.
- let setStatus = tag.getIsoDep(tagInfo).setSendDataTimeout(timeoutMs);
- console.log("tag setSendDataTimeout setStatus: " + setStatus);
setTimeout(timeout: number): void
查詢發(fā)送數(shù)據(jù)到Tag的等待超時(shí)時(shí)間,單位是毫秒。
需要權(quán)限: ohos.permission.NFC_TAG
系統(tǒng)能力: SystemCapability.Communication.NFC.Tag
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
timeout | number | 是 | 超時(shí)時(shí)間,單位毫秒,非負(fù)值。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
3100201 | Tag running state is abnormal in service. |
示例:
- import tag from '@ohos.nfc.tag';
- // see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
- // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
- let timeoutMs = 700; // change it to be correct.
- try {
- tag.getIsoDep(tagInfo).setTimeout(timeoutMs);
- console.log("tag setTimeout success");
- } catch (busiError) {
- console.log("tag setTimeout busiError: " + busiError);
- }
sendData(data: number[]): Promise<number[]>
發(fā)送指令到Tag上,使用Promise方式作為異步方法。
從 API version 7 開始支持,從 API version 9 開始廢棄,建議使用tagSession.transmit替代。
需要權(quán)限: ohos.permission.NFC_TAG
系統(tǒng)能力: SystemCapability.Communication.NFC.Tag
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
data | number[] | 是 | 要發(fā)送的指令。每個(gè)number十六進(jìn)制表示,范圍是0x00~0xFF。 |
返回值:
類型 | 說明 |
---|---|
Promise<number[]> | 對(duì)端Tag對(duì)指令的響應(yīng)數(shù)據(jù)。每個(gè)number十六進(jìn)制表示,范圍是0x00~0xFF。 |
示例:
- import tag from '@ohos.nfc.tag';
- // see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
- // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
- // connect the tag at first if not connected.
- if (!tag.getIsoDep(tagInfo).isTagConnected()) {
- if (!tag.getIsoDep(tagInfo).connectTag()) {
- console.log("tagSession connectTag failed.");
- return;
- }
- }
- let cmdData = [0x01, 0x02, 0x03, 0x04]; // change the raw data to be correct.
- tag.getIsoDep(tagInfo).sendData(cmdData).then((response) => {
- console.log("tagSession sendData Promise response: " + response);
- }).catch((err)=> {
- console.log("tagSession sendData Promise err: " + err);
- });
sendData(data: number[], callback: AsyncCallback<number[]>): void
發(fā)送指令到Tag上,使用AsyncCallback方式作為異步方法。
從 API version 7 開始支持,從 API version 9 開始廢棄,建議使用tagSession.transmit替代。
需要權(quán)限: ohos.permission.NFC_TAG
系統(tǒng)能力: SystemCapability.Communication.NFC.Tag
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
data | number[] | 是 | 要發(fā)送的指令。每個(gè)number十六進(jìn)制表示,范圍是0x00~0xFF。 |
callback | AsyncCallback<number[]> | 是 | 回調(diào)函數(shù),返回響應(yīng)數(shù)據(jù)。每個(gè)number十六進(jìn)制表示,范圍是0x00~0xFF。 |
示例:
- import tag from '@ohos.nfc.tag';
- // see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
- // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
- // connect the tag at first if not connected.
- if (!tag.getIsoDep(tagInfo).isTagConnected()) {
- if (!tag.getIsoDep(tagInfo).connectTag()) {
- console.log("tagSession connectTag failed.");
- return;
- }
- }
- let cmdData = [0x01, 0x02, 0x03, 0x04]; // change the raw data to be correct.
- tag.getIsoDep(tagInfo).sendData(cmdData, (err, response)=> {
- if (err) {
- console.log("tagSession sendData AsyncCallback err: " + err);
- } else {
- console.log("tagSession sendData AsyncCallback response: " + response);
- }
- });
transmit(data: number[]): Promise<number[]>
發(fā)送指令到Tag上,使用Promise方式作為異步方法。
需要權(quán)限: ohos.permission.NFC_TAG
系統(tǒng)能力: SystemCapability.Communication.NFC.Tag
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
data | number[] | 是 | 要發(fā)送的指令。每個(gè)number十六進(jìn)制表示,范圍是0x00~0xFF。 |
返回值:
類型 | 說明 |
---|---|
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ò)誤信息 |
---|---|
3100201 | Tag running state is abnormal in service. |
示例:
- import tag from '@ohos.nfc.tag';
- // see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
- // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
- // connect the tag at first if not connected.
- try {
- if (!tag.getIsoDep(tagInfo).isConnected()) {
- tag.getIsoDep(tagInfo).connect();
- }
- } catch (busiError) {
- console.log("tag connect busiError: " + busiError);
- return;
- }
- let cmdData = [0x01, 0x02, 0x03, 0x04]; // change the raw data to be correct.
- try {
- tag.getIsoDep(tagInfo).transmit(cmdData).then((response) => {
- console.log("tagSession transmit Promise response: " + response);
- }).catch((err)=> {
- console.log("tagSession transmit Promise err: " + err);
- });
- } catch (busiError) {
- console.log("tag transmit busiError: " + busiError);
- return;
- }
transmit(data: number[], callback: AsyncCallback<number[]>): void
發(fā)送指令到Tag上,使用AsyncCallback方式作為異步方法。
需要權(quán)限: ohos.permission.NFC_TAG
系統(tǒng)能力: SystemCapability.Communication.NFC.Tag
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
data | number[] | 是 | 要發(fā)送的指令。每個(gè)number十六進(jìn)制表示,范圍是0x00~0xFF。 |
callback | AsyncCallback<number[]> | 是 | 回調(diào)函數(shù),返回響應(yīng)數(shù)據(jù)。每個(gè)number十六進(jìn)制表示,范圍是0x00~0xFF。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見NFC錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
3100201 | Tag running state is abnormal in service. |
示例:
- import tag from '@ohos.nfc.tag';
- // see 'tag.TagInfo' at 'js-apis-nfcTag.md', tagInfo is an Object given by nfc service when tag is dispatched.
- // the folowing getXXX, can be one of getIsoDep, getNdef, getMifareClassic, ...
- // connect the tag at first if not connected.
- try {
- if (!tag.getIsoDep(tagInfo).isConnected()) {
- tag.getIsoDep(tagInfo).connect();
- }
- } catch (busiError) {
- console.log("tag connect busiError: " + busiError);
- return;
- }
- let cmdData = [0x01, 0x02, 0x03, 0x04]; // change the raw data to be correct.
- try {
- tag.getIsoDep(tagInfo).transmit(cmdData, (err, response)=> {
- if (err) {
- console.log("tagSession transmit AsyncCallback err: " + err);
- } else {
- console.log("tagSession transmit AsyncCallback response: " + response);
- }
- });
- } catch (busiError) {
- console.log("tag transmit busiError: " + busiError);
- return;
- }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: