微信官方文档说明确实太模糊了也没有示例代码,真是研究了半天才摸清楚,话不多说上代码。希望能帮助到大家!

 

// nfc识别

    nfcRead() {

        var that = this;

        const adapter = wx.getNFCAdapter();

        console.log("获取NFC实例", adapter)

        adapter.onDiscovered(res => {

            console.log("读取到卡片了", res);

            let tagId = res.id;

            console.log("获取到tagID:", tagId);

            if (res.techs.includes(adapter.tech.mifareClassic)) { //如果影响你nfc可以去掉

                console.log('发现' + adapter.tech.mifareClassic + '卡');

                let nfcIsoDep = adapter.getIsoDep();

                nfcIsoDep.connect({

                    success: res => {

                        console.log('完成连接', res)

                    }

                })

                let str = '*********************'

                const sendData = new Uint8Array(str.match(/[\da-f]{2}/gi).map(function (h) {

                    return parseInt(h, 16)

                }))

                nfcIsoDep.transceive({

                    data: sendData.buffer,

                    success: function (res) {

                        console.log('发送数据并解密成功, 接收数据如下:', res);

                        let resData = that.ab2str(res.data)

                        console.log('resData', resData);

                    },

                    fail: function (err) {

                        console.log('发送数据失败A', err);

                        wx.showToast({

                            title: 'nfc失败!',

                            icon: 'error'

                        })

                    }

                })

                let str2 = '**************'

                const sendData2 = new Uint8Array(str2.match(/[\da-f]{2}/gi).map(function (h) {

                    return parseInt(h, 16)

                }))

                nfcIsoDep.transceive({

                    data: sendData2.buffer,

                    success: function (res) {

                        console.log('发送数据并解密成功, 接收数据如下:', res);

                        let resData = that.ab2str(res.data)

                        console.log('resData', resData);

                    },

                    fail: function (err) {

                        console.log('发送数据失败A', err);

                        wx.showToast({

                            title: 'nfc失败!',

                            icon: 'error'

                        })

                    }

                })

            } else {

                console.log('没发现卡');

                wx.showToast({

                    title: '卡片类型有误!',

                    icon: 'error'

                })

            }

        })

        adapter.startDiscovery({

            success: function (res) {

                console.log('startDiscovery:', res);

            },

            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;

                }

            }

        })

    },

    ab2str(buffer) { //这里是我这里需要的数据

        const hexArr = Array.prototype.map.call(

            new Uint8Array(buffer),

            function (bit) {

                return ('00' + bit.toString(16)).slice((-2))

            })

        return hexArr.join('')

    },

更多推荐