HarmonyOS鸿蒙Next官方示例代码NFC_Tag
HarmonyOS鸿蒙Next官方示例代码NFC_Tag
async function readerModeCb(error: BusinessError, tagInfo: tag.TagInfo) {
if (!error) {
if (canIUse('SystemCapability.Communication.NFC.Tag')) {
if (tagInfo === null || tagInfo === undefined) {
hilog.error(0x0000, 'testTag', 'readerModeCb tagInfo is invalid');
return;
}
if (tagInfo.uid === null || tagInfo.uid === undefined) {
hilog.error(0x0000, 'testTag', 'readerModeCb uid is invalid');
return;
}
if (tagInfo.technology === null || tagInfo.technology === undefined || tagInfo.technology.length === 0) {
hilog.error(0x0000, 'testTag', 'readerModeCb technology is invalid');
return;
}
AppStorage.setOrCreate('tagInfo', tagInfo);
let nfcA: tag.NfcATag | null = null;
for (let i = 0; i < tagInfo.technology.length; i++) {
if (tagInfo.technology[i] === tag.NFC_A) {
try {
nfcA = tag.getNfcA(tagInfo); // Get Tag object of NfcA type.
} catch (error) {
hilog.error(0x0000, 'testTag', 'readerModeCb getNfcA error = %{public}s', JSON.stringify(error));
return;
}
}
if (tagInfo.technology[i] === tag.NFC_B) {
try {
tag.getNfcB(tagInfo); // Get Tag object of NfcB type.
} catch (error) {
hilog.error(0x0000, 'testTag', 'readerModeCb getNfcB error = %{public}s', JSON.stringify(error));
return;
}
}
if (tagInfo.technology[i] === tag.NFC_F) {
try {
tag.getNfcF(tagInfo); // Get Tag object of NfcF type.
} catch (error) {
hilog.error(0x0000, 'testTag', 'readerModeCb getNfcF error = %{public}s', JSON.stringify(error));
return;
}
}
if (tagInfo.technology[i] === tag.NFC_V) {
try {
tag.getNfcV(tagInfo); // Get Tag object of NfcV type.
} catch (error) {
hilog.error(0x0000, 'testTag', 'readerModeCb getNfcV error = %{public}s', JSON.stringify(error));
return;
}
}
// Add other technology types.
}
if (nfcA === undefined) {
hilog.error(0x0000, 'testTag', 'readerModeCb getNfcA is invalid');
return;
}
if (nfcA) {
try {
nfcA.connect();
} catch (error) {
hilog.error(0x0000, 'testTag', 'readerModeCb nfcA.connect() error = %{public}s', JSON.stringify(error));
return;
}
if (!nfcA.isConnected()) {
hilog.error(0x0000, 'testTag', 'readerModeCb nfcA.connect() false.');
return;
}
let cmdData = [0x01, 0x02, 0x03, 0x04]; // Please change the raw data to be correct.
try {
nfcA.transmit(cmdData)
.then((response: number[]) => {
hilog.info(0x0000, 'testTag', 'readerModeCb nfcA.transmit() response = %{public}s.',
JSON.stringify(response));
})
.catch((err: BusinessError) => {
hilog.info(0x0000, 'testTag', 'readerModeCb nfcA.transmit() err = %{public}s.',
JSON.stringify(err));
return;
})
} catch (error) {
hilog.info(0x0000, 'testTag', 'readerModeCb nfcA.transmit() error = %{public}s.', JSON.stringify(error));
return;
}
} else {
hilog.info(0x0000, 'testTag', 'readerModeCb error %{public}s.', JSON.stringify(error));
}
}
}
}
debug走到nfcA.connect();报错,添加打印语句console.log("nfcA对象",JSON.stringify(nfcA));,打印出来nfcA对象 {}。报错信息
hilog.error(0x0000, 'testTag', 'readerModeCb nfcA.connect() error = %{public}s', JSON.stringify(error));------>readerModeCb nfcA.connect() error = {"code":3100201}
怎么解决?才能拿到里面的NfcA,NfcB对象?
更多关于HarmonyOS鸿蒙Next官方示例代码NFC_Tag的实战教程也可以访问 https://www.itying.com/category-93-b0.html
3 回复
console.log(“有效的nfcB”, JSON.stringify(nfcB));
为什么打印出来还是有效的nfcB {} ?
虽然nfcB.connect()连接上了的。我该怎么取到对象nfcB,在别的方法中引用nfcB,使用nfcB.transmit()发送数据呢?
更多关于HarmonyOS鸿蒙Next官方示例代码NFC_Tag的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
HarmonyOS鸿蒙Next的NFC_Tag示例代码展示了如何在设备上实现NFC标签的读取和写入功能。该示例通过NfcController
类管理NFC功能,使用Tag
类表示检测到的NFC标签,并通过NfcTagTechnology
接口与标签进行交互。开发者可以调用readNdefMessage()
读取标签数据,或使用writeNdefMessage()
写入数据。示例还演示了如何处理NFC标签的发现事件,确保在标签靠近设备时能够及时响应。