const app = getApp()
Page({
nfc: null,
onLoad: function () {
const nfc = wx.getNFCAdapter()
this.nfc = nfc
function discoverHandler(res) {
if (res.techs.includes(nfc.tech.ndef)) {
console.log(res.messages)
}
if (res.techs.includes(nfc.tech.nfcA)) {
const nfcA = nfc.getNfcA()
nfcA.connect({
success() {
console.log("discoverHandler connect success")
},
fail() {
console.log("discoverHandler connect fail")
}
})
let array = [0x47, 0x11]
var arrayBuffer = new Uint8Array(array).buffer;
nfcA.transceive({
data: arrayBuffer,
complete(res) {
console.log('res:', res)
}
})
return
}
}
nfc.onDiscovered(discoverHandler)
nfc.startDiscovery({
fail(err) {
console.log('failed to discover:', err)
}
})
},
onHide() {
if (this.nfc) {
this.nfc.stopDiscovery()
}
}
})
使用以上代码测试,程序会循环执行discoverHandler->transceive->discoverHandler->transceive,见附件图片log信息