微信小程序读取nfc门卡信息
首先通过wx.getNFCAdapter()来获取nfc实例
然后调用startDiscovery获取当前手机nfc状态
已经打开了nfc的就可以调用监听方法onDiscovered
监听到数据后setData把数据展示出来就ok了,方法如下
打开nfc
1 nfc() { 2 const nfc = wx.getNFCAdapter() 3 this.nfc = nfc 4 let _this = this 5 6 function discoverHandler(res) { 7 const data = new Uint8Array(res.id) 8 let str = "" 9 data.forEach(e => { 10 let item = e.toString(16) 11 if (item.length == 1) { 12 item = '0' + item 13 } 14 item = item.toUpperCase() 15 str += item 16 }) 17 _this.setData({ 18 newCardCode: str 19 }) 20 wx.showToast({ 21 title: '读取成功!', 22 icon: 'none' 23 }) 24 } 25 nfc.startDiscovery({ 26 success(res) { 27 wx.showToast({ 28 title: 'NFC读取功能已开启!', 29 icon: 'none' 30 }) 31 nfc.onDiscovered(discoverHandler) 32 }, 33 fail(err) {if(!err.errCode){ 34 wx.showToast({ 35 title: '请检查NFC功能是否正常!', 36 icon: 'none' 37 }) 38 return 39 } 40 switch (err.errCode) { 41 case 13000: 42 wx.showToast({ 43 title: '设备不支持NFC!', 44 icon: 'none' 45 }) 46 break; 47 case 13001: 48 wx.showToast({ 49 title: '系统NFC开关未打开!', 50 icon: 'none' 51 }) 52 break; 53 case 13019: 54 wx.showToast({ 55 title: '用户未授权!', 56 icon: 'none' 57 }) 58 break; 59 case 13010: 60 wx.showToast({ 61 title: '未知错误!', 62 icon: 'none' 63 }) 64 break; 65 } 66 } 67 }) 68 },
读取nfc芯片
1 nfcRead() { 2 console.log('nfc') 3 const nfc = wx.getNFCAdapter() 4 this.nfc = nfc 5 let _this = this 6 7 function discoverHandler(res) { 8 console.log('discoverHandler', res) 9 const data = new Uint8Array(res.id) 10 let str = "" 11 data.forEach(e => { 12 let item = e.toString(16) 13 if (item.length == 1) { 14 item = '0' + item 15 } 16 item = item.toUpperCase() 17 console.log(item) 18 str += item 19 }) 20 _this.setData({ 21 newCardCode: str 22 }) 23 console.log(str) 24 wx.showToast({ 25 title: '读取成功!', 26 icon: 'none' 27 }) 28 } 29 nfc.startDiscovery({ 30 success(res) { 31 console.log(res) 32 wx.showToast({ 33 title: 'NFC读取功能已开启!', 34 icon: 'none' 35 }) 36 nfc.onDiscovered(discoverHandler) 37 }, 38 fail(err) { 39 console.log('failed to discover:', err) 40 if(!err.errCode){ 41 wx.showToast({ 42 title: '请检查NFC功能是否正常!', 43 icon: 'none' 44 }) 45 return 46 } 47 switch (err.errCode) { 48 case 13000: 49 wx.showToast({ 50 title: '设备不支持NFC!', 51 icon: 'none' 52 }) 53 break; 54 case 13001: 55 wx.showToast({ 56 title: '系统NFC开关未打开!', 57 icon: 'none' 58 }) 59 break; 60 case 13019: 61 wx.showToast({ 62 title: '用户未授权!', 63 icon: 'none' 64 }) 65 break; 66 case 13010: 67 wx.showToast({ 68 title: '未知错误!', 69 icon: 'none' 70 }) 71 break; 72 } 73 } 74 }) 75 },
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」