onLoad: function () {
this.NFCReadCard()
},
NFCReadCard: function () {
let that = this
let adapter = wx.getNFCAdapter()
console.log(adapter)
adapter.startDiscovery({
success: (e) => {
console.log('开始监听贴卡', e)
adapter.onDiscovered(function (res) {
console.log('读到卡片了', res);
let arrbuf = that.strToArrayBuffer('GET DATA')
console.log(arrbuf,'arrbuf')
if (res.techs.includes(adapter.tech.nfcB)) {
console.log('发现' + adapter.tech.nfcB + '卡');
let nfcB = adapter.getNfcB();
app.nfcB = nfcB;
nfcB.connect({
success: function (res) {
console.log('设备已连接', res)
nfcB.transceive({
data: arrbuf,
success: function (res) {
console.log('发送数据成功, 接收数据如下:', res);
},
fail: function (err) {
console.log('发送数据失败', err);
}
})
},
fail: function (err) {
console.log('设备联接错误', err);
}
});
}
})
}
})
},
strToArrayBuffer: function (str) {
// 首先将字符串转为16进制
let val = ""
for (let i = 0; i < str.length; i++) {
if (val === '') {
val = str.charCodeAt(i).toString(16)
} else {
val += ',' + str.charCodeAt(i).toString(16)
}
}
// 将16进制转化为ArrayBuffer
return new Uint8Array(val.match(/[\da-f]{2}/gi).map(function (h) {
return parseInt(h, 16)
})).buffer
},