用户
 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,登录网站

小程序社区 首页 教程 查看内容

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

Rolan 2021-10-9 00:32

首先通过wx.getNFCAdapter()来获取nfc实例然后调用startDiscovery获取当前手机nfc状态已经打开了nfc的就可以调用监听方法onDiscovered监听到数据后setData把数据展示出来就ok了,方法如下 nfc() { const nfc = wx.ge ...

首先通过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;
                  }
            }
        })
    },
鲜花
鲜花
鸡蛋
鸡蛋
分享至 : QQ空间
收藏
原作者: 淡然是福 来自: 博客园