微信小程序中使用nfc

本文介绍如何在微信小程序中调用NFC功能,通过创建nfc文件夹和nfc.js文件,实现在小程序中进行刷卡操作。
摘要由CSDN通过智能技术生成

在小程序中调用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(
  • 3
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值