Reason:Error
Error name:Error
Error message:Tag running state is abnormal in service.
Error code:
Stacktrace:
Cannot get SourceMap info, dump raw stack:
at tagOn (entry|entry|1.0.0|src/main/ets/d/d1/e1.ts:0:1)
at startReadCardWithHandler (entry|entry|1.0.0|src/main/ets/d/d1/e1.ts:0:1)
at aboutToAppear (entry|entry|1.0.0|src/main/ets/pages/recharge/NFCRechargePage.ts:0:1)
at anonymous (entry|entry|1.0.0|src/main/ets/pages/recharge/RechargeCenterPage.ts:0:1) 在使用NFC相关能力时,应该先校验设备是否支持,以及状态是否开启。或者在出现异常时捕获处理,提醒用户开启NFC。
import { hilog } from '@kit.PerformanceAnalysisKit';
import { nfcController, tag } from '@kit.ConnectivityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { bundleManager } from '@kit.AbilityKit';
let discTech : number[] = [tag.NFC_A]; // 用前台ability时所需要的技术代替
let elementName : bundleManager.ElementName = {
bundleName: 'com.example.commondemofordfx',
abilityName: 'EntryAbility'
};
function readerModeCb(err : BusinessError, tagInfo : tag.TagInfo) {
if (!err) {
console.log('offCallback: tag found tagInfo = ', JSON.stringify(tagInfo));
} else {
console.error('offCallback err: ' + err.message);
return;
}
// taginfo的其他操作
}
@Entry
@Component
struct CheckNFCAbilityPage {
@State nfcSupport: boolean = false;
@State nfcOpen: boolean = false;
@State nfcRunFailMsg: string = '未运行';
aboutToAppear(): void {
this.checkNfcCapability();
this.checkNfcOpen();
}
// 检查设备是否支持NFC能力
checkNfcCapability() {
if (!canIUse('SystemCapability.Communication.NFC.Core')) {
hilog.error(0x0000, 'testTag', 'nfc unavailable.');
return;
}
this.nfcSupport = true;
}
// 检查Nfc是否开启
checkNfcOpen() {
if (!nfcController.isNfcOpen()) {
hilog.error(0x0000, 'testTag', 'nfc disabled.');
return;
}
this.nfcOpen = true;
}
NfcRun() {
try {
tag.on('readerMode', elementName, discTech, readerModeCb);
this.nfcRunFailMsg = '运行成功';
} catch (e) {
hilog.error(0x0000, 'testTag',`tag.on error: ${(e as BusinessError).message}`);
this.nfcRunFailMsg = `失败信息:${(e as BusinessError).message}`;
// 异常处理,例如提示弹窗。
}
}
build() {
Column({ space: 20 }) {
if (!this.nfcSupport) {
// 设备不支持NFC的情况
Text('当前设备不支持NFC功能')
.fontSize(20)
.fontColor(Color.Red)
} else {
Text('当前设备支持NFC功能')
.fontSize(20)
.fontColor(Color.Red)
}
if (!this.nfcOpen) {
// 设备不支持NFC的情况
Text('NFC功能未开启')
.fontSize(20)
.fontColor(Color.Red)
} else {
Text('NFC功能已开启')
.fontSize(20)
.fontColor(Color.Red)
}
Button('重新检查NFC')
.onClick(() => {
this.checkNfcCapability();
this.checkNfcOpen();
})
Button('点击运行NFC')
.onClick(() => {
this.NfcRun();
})
Text(this.nfcRunFailMsg).fontSize(20)
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center)
}
}