收藏
回答

NFC功能 执行nfcA.transceive后小程序会再次执行onDiscovered的监听函数?

const app = getApp()

Page({
  nfcnull,
  onLoadfunction () {
    const nfc = wx.getNFCAdapter()
    this.nfc = nfc
    function discoverHandler(res{
      if (res.techs.includes(nfc.tech.ndef)) {
        console.log(res.messages)
        //const ndef = nfc.getNdef()
        //ndef.writeNdefMessage({
        //  uris: [''],
        //  complete(res) {
        //    console.log('res:', res)
        //  }
        //})
        //return
      }

      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 = [0x470x11]
        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信息

回答关注问题邀请回答
收藏
登录 后发表内容