微信小程序读取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     },

 

posted @ 2021-09-22 09:49  Pavetr  阅读(3038)  评论(0编辑  收藏  举报