readNFC() {
let app = this;
// 获取NFC适配器实例
const nfc = wx.getNFCAdapter();
// 检查NFC是否可用
nfc.startDiscovery({
success: (res) => {
console.log('NFC发现成功', res);
// 监听NFC标签发现
nfc.onDiscovered((res) => {
console.log('NFC标签发现', res); // 打印完整的发现结果
if (res.messages && res.messages.length > 0) {
const nfcData = res.messages[0];
console.log('NFC数据', nfcData); // 打印具体的NFC数据
const record = nfcData.records[0];
console.log('record',record);
if (record.tnf === 1) { // NFC Forum Well-Known type
const typeArray = new Uint8Array(record.type);
const type = String.fromCharCode(...typeArray);
console.log('记录类型:', type);
if (type === 'T') { // Text Record
const payloadArray = new Uint8Array(record.payload);
const languageCodeLength = payloadArray[0];
const languageCode = String.fromCharCode(...payloadArray.slice(1, 1 + languageCodeLength));
const text = String.fromCharCode(...payloadArray.slice(1 + languageCodeLength));
console.log('语言代码:', languageCode);
console.log('文本:', text); }
}
} else {
console.error('没有发现NFC标签');
}
});
},
fail: (err) => {
console.error('NFC发现失败', err);
wx.showToast({
title: 'NFC不可用',
icon: 'none'
});
}
});
} readNFC() {
let app = this;
// 获取NFC适配器实例
const nfc = wx.getNFCAdapter();
// 检查NFC是否可用
nfc.startDiscovery({
success: (res) => {
console.log('NFC发现成功', res);
// 监听NFC标签发现
nfc.onDiscovered((res) => {
console.log('NFC标签发现', res); // 打印完整的发现结果
if (res.messages && res.messages.length > 0) {
const nfcData = res.messages[0];
console.log('NFC数据', nfcData); // 打印具体的NFC数据
const record = nfcData.records[0];
console.log('record',record);
if (record.tnf === 1) { // NFC Forum Well-Known type
const typeArray = new Uint8Array(record.type);
const type = String.fromCharCode(...typeArray);
console.log('记录类型:', type);
if (type === 'T') { // Text Record
const payloadArray = new Uint8Array(record.payload);
const languageCodeLength = payloadArray[0];
const languageCode = String.fromCharCode(...payloadArray.slice(1, 1 + languageCodeLength));
const text = String.fromCharCode(...payloadArray.slice(1 + languageCodeLength));
console.log('语言代码:', languageCode);
console.log('文本:', text);
if(text){
app.nfc=text;
}
if(app.nfc==app.deviceDetail.nfc){
wx.showToast({
title: ' NFC识别成功',
icon: 'none'
});
}else {
wx.showToast({
title: 'NFC识别信息与设备信息不匹配',
icon: 'none'
});
}
}else {
}
}
} else {
console.error('没有发现NFC标签');
}
});
},
fail: (err) => {
console.error('NFC发现失败', err);
wx.showToast({
title: 'NFC不可用',
icon: 'none'
});
}
});
}