微信小程序读取nfc门卡信息


首先通过wx.getNFCAdapter()来获取nfc实例

然后调用startDiscovery获取当前手机nfc状态

已经打开了nfc的就可以调用监听方法onDiscovered

监听到数据后setData把数据展示出来就ok了,方法如下

    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;
                  }
            }
        })
    },

 

 

     nfcRead ()  {
         console . log( ' nfc ')
         const  nfc  =  wx . getNFCAdapter()
         this . nfc  =  nfc
         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 '
             })
         }
         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;
                   }
             }
         })
     },


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM