nfc读写功能,在我取消监听和关闭nfc对象后,在次进入功能页面后就会有
errMsg: "connect:fail:Tech already connected", errCode: 13022, errno: 1504204这个错误,读写功能正常。
代码片段:https://developers.weixin.qq.com/s/WHHMEmmF7Pv0
import { routerFilter } from '../../../router/filter'
routerFilter({
/**
* 页面的初始数据
*/
data: {
id: '',
type: '',
name: '',
titel: '',
NFCAdapter: null,
aid: '',
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function () {
let _this = this
//初始化 NFC 模块。
const nfc = wx.getNFCAdapter()
_this.setData({
NFCAdapter: nfc,
})
// 获取NfcA实例,实例支持NFC-A (ISO 14443-3A)标准的读写
function discoverHandler(res) {
let starTtime = Date.now();
let aid = parseInt(ab2hex(res.id), 16);
var ttid = '';
var type = '';
var name = '';
_this.setData({
aid: aid,
})
console.log('aid:', aid)
if (res.messages) {
let cordsArray = res.messages[0].records;
cordsArray.find(item => {
ttid = String.fromCharCode.apply(null, new Uint16Array(item.id));
type = String.fromCharCode.apply(null, new Uint16Array(item.type));
name = String.fromCharCode.apply(null, new Uint16Array(item.payload));
console.log('id:', ttid)
console.log('type:', type)
console.log('name:', name)
_this.setData({
name: name,
id: ttid,
type: type,
})
});
}
// 获取NDEF对象
const NFCTab = nfc.getNdef()
// 连接标签
NFCTab.connect({
success: res => {
_this.setData({
title: '连接设备成功'
})
console.log(res);
// 准备写入的数据
let time = Date.now();
console.log(time); //1523619204809
const records = [{
id: str2ab(time + ''),
payload: str2ab('liguohong'),
type: str2ab('周意豪洪'),
tnf: 2
}];
// 注:tnf这个字段我参照别踩过的坑看到的,具体详情自行百度
// 执行写入
NFCTab.writeNdefMessage({
records: records,
success: res => {
_this.setData({
title: '数据写入成功'
})
console.log(res);
let endTime = Date.now();
console.log('写入用时:', starTtime-endTime);
var msg = '数据写入成功!!!' + ttid + ':::写入用时:' + (starTtime-endTime);
console.log('msg', msg);
wx.showToast({
title: msg,
icon: 'none',
duration: 5000
})
},
fail: error => {
console.error('数据写入失败' + error);
},
complete: res => {
_this.closeConnect(NFCTab);
_this.setData({
title: '请将设备放入识别区NFC'
})
console.log(res);
}
});
},
fail: error => {
console.log('连接设备失败', error);
},
complete: res => {
console.log(res);
}
});
}
// 字符串转为ArrayBuffer对象,参数为字符串
function str2ab(str) {
var buf = new ArrayBuffer(str.length * 2); // 每个字符占用2个字节
var bufView = new Uint16Array(buf);
for (var i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
};
/**
* 格式化得到aid值
* @param {Object} buffer
*/
const ab2hex = function (buffer) {
var hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function (bit) {
return ('00' + bit.toString(16)).slice(-2);
}
);
return hexArr.join('');
};
// 监听 NFC Tag
nfc.onDiscovered(discoverHandler)
nfc.startDiscovery({
success: res => {
_this.setData({
title: '请将设备放入识别区NFC'
})
console.log(res);
},
fail: error => {
_this.setData({
title: '查看是否已经打开NFC功能,刷新重试'
})
console.error(error);
},
complete: res => {
console.log(res);
}
})
},
// 关闭 连接
closeConnect(NFCTab) {
let _this = this
NFCTab.close({
success: res => {
_this.setData({
title: '清除标签连接成功'
})
console.log('清除标签连接成功', res);
},
fail: error => {
_this.setData({
title: '清除标签连接失败'
})
console.error(res);
},
complete: res => {
console.log(res);
}
});
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
this.closeNFC();
},
closeNFC() {
var NFCAdapter = this.data.NFCAdapter
NFCAdapter.offDiscovered(callback => {
console.log('取消监听',callback);
});
NFCAdapter.stopDiscovery({
success: res => {
console.log('关闭nfc对象', res);
},
fail: error => {
console.error(res);
},
complete: res => {
console.log(res);
}
});
console.log('关闭nfc对象');
},
})
你好,麻烦在手机微信那里上传下日志: 我->设置->帮助与反馈右上角有个上报日志的入口,麻烦提供一下微信号,时间点