微信小程序讀取nfc門卡信息

首先通過wx.getNFCAdapter()來獲取nfc實例

然后調(diào)用startDiscovery獲取當前手機nfc狀態(tài)

已經(jīng)打開了nfc的就可以調(diào)用監(jiān)聽方法onDiscovered

監(jiān)聽到數(shù)據(jù)后setData把數(shù)據(jù)展示出來就ok了,方法如下

 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: '系統(tǒng)NFC開關(guān)未打開!',

                        icon: 'none'

                      })

                      break;

                    case 13019:

                      wx.showToast({

                        title: '用戶未授權(quán)!',

                        icon: 'none'

                      })

                      break;

                    case 13010:

                      wx.showToast({

                        title: '未知錯誤!',

                        icon: 'none'

                      })

                      break;

                  }

            }

        })

    },
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容