import tag from '@ohos.nfc.tag';
import UIAbility from '@ohos.app.ability.UIAbility';
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import Want from '@ohos.app.ability.Want'
export default class EntryAbility extends UIAbility {
onCreate(want : Want, launchParam: AbilityConstant.LaunchParam) {
// add other code here...
// want is initialized by nfc service, contains tag info for this found tag
let tagInfo : tag.TagInfo | null = null;
try {
tagInfo = tag.getTagInfo(want);
} catch (error) {
console.error("tag.getTagInfo catched error: " + error);
}
if (tagInfo == null || tagInfo == undefined) {
console.log("no TagInfo to be created, ignore it.");
return;
}
// get the supported technologies for this found tag.
let isNfcATag = false;
let isIsoDepTag = false;
for (let i = 0; i < tagInfo.technology.length; i++) {
if (tagInfo.technology[i] == tag.NFC_A) {
isNfcATag = true;
}
if (tagInfo.technology[i] == tag.ISO_DEP) {
isIsoDepTag = true;
}
// also check for technology: tag.NFC_B/NFC_F/NFC_V/NDEF/MIFARE_CLASSIC/MIFARE_ULTRALIGHT/NDEF_FORMATABLE
}
// use NfcA APIs to access the found tag.
if (isNfcATag) {
let nfcA : tag.NfcATag | null = null;
try {
nfcA = tag.getNfcATag(tagInfo);
} catch (error) {
console.error("tag.getNfcATag catched error: " + error);
}
// other code to read or write this found tag.
}
// use getIsoDep APIs to access the found tag.
if (isIsoDepTag) {
let isoDep : tag.IsoDepTag | null = null;
try {
isoDep = tag.getIsoDep(tagInfo);
} catch (error) {
console.error("tag.getIsoDep catched error: " + error);
}
// other code to read or write this found tag.
}
// use the same code to handle for "NfcA/NfcB/NfcF/NfcV/Ndef/MifareClassic/MifareUL/NdefFormatable".
}
}
标准NFC-Tag tag.TagInfo demo: