微信小程序讀取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