微信小程序中使用nfc

在小程序中调用nfc功能,实现小程序刷卡

直接上代码:

  • 先在小程序的根目录或是pages目录下创建一个nfc文件夹
  • 在nfc文件中创建一个nfc.js的文件
    代码:
// 获取nfc实例
function nfc() {
  const nfc = wx.getNFCAdapter()
  this.nfc = nfc
  let _this = this

  function discoverHandler(res) {
      const data = new Uint8Array(res.id)
      let str = ""
      data.forEach(e => {
          let item = e.toString(16)
          if (item.length == 1) {
              item = '0' + item
          }
          item = item.toUpperCase()
          str += item
      })
      _this.setData({
          newCardCode: str
      })
      wx.showToast({
          title: '读取成功!',
          icon: 'none'
      })
  }
  nfc.startDiscovery({
      success(res) {
          wx.showToast({
              title: 'NFC读取功能已开启!',
              icon: 'none'
          })
          nfc.onDiscovered(discoverHandler)
      },
      fail(err) {if(!err.errCode){
            wx.showToast({
              title: '请检查NFC功能是否正常!',
              icon: 'none'
            })
            return
          }
          switch (err.errCode) {
              case 13000:
                wx.showToast({
                  title: '设备不支持NFC!',
                  icon: 'none'
                })
                break;
              case 13001:
                wx.showToast({
                  title: '系统NFC开关未打开!',
                  icon: 'none'
                })
                break;
              case 13019:
                wx.showToast({
                  title: '用户未授权!',
                  icon: 'none'
                })
                break;
              case 13010:
                wx.showToast({
                  title: '未知错误!',
                  icon: 'none'
                })
                break;
            }
      }
  })
}

module.exports = {
  nfc,
}

在需要使用到的页面中加入:

import fnNfc from '../nfc/nfcopen'// nfc内容引入
onLoad() {
    this.nfcRead()			// onload中进行数据读取
},


// 进行使用nfc功能
  nfcRead() {
    const nfc = wx.getNFCAdapter()
    this.nfc = fnNfc
    let _this = this
    function discoverHandler(res) {
      console.log('discoverHandler', res)    // 发现者
      const data = new Uint8Array(res.id)
      let str = ""
      data.forEach(e => {
        let item = e.toString(16)
        if (item.length == 1) {
          item = '0' + item
        }
        item = item.toUpperCase()
        console.log(item)
        str += item
      })
      _this.setData({
        newCardCode: str
      })
      // console.log(str)
      wx.showToast({
        title: '读取成功!',
        icon: 'none'
      })
      wx.navigateTo({
        url: `../recorcentent/recorcentent?cardid=${str}&mac1=${data[0]}`,
      })
      // console.log(discoverHandler.techs);
      return _this.data.newCardCode
    }
    


    nfc.startDiscovery({
      success(res) {
        console.log(res)
        wx.showToast({
          title: 'NFC读取功能已开启!',
          icon: 'none'
        })
        nfc.onDiscovered(discoverHandler)
      },
      fail(err) {
        console.log('failed to discover:', err)
        if (!err.errCode) {
          wx.showToast({
            title: '请检查NFC功能是否正常!',
            icon: 'none'
          })
          return
        }
        switch (err.errCode) {
          case 13000:
            wx.showToast({
              title: '设备不支持NFC!',
              icon: 'none'
            })
            break;
          case 13001:
            wx.showToast({
              title: '系统NFC开关未打开!',
              icon: 'none'
            })
            break;
          case 13019:
            wx.showToast({
              title: '用户未授权!',
              icon: 'none'
            })
            break;
          case 13010:
            wx.showToast({
              title: '未知错误!',
              icon: 'none'
            })
            break;
        }
      }
    })
  },

版权声明:本文为m0_58729897原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。