当前位置:   article > 正文

uni小程序NFC获取数据 与 扫二维码获取数据的方法_wx.getnfcadapter(); ondiscovered 重复执行

wx.getnfcadapter(); ondiscovered 重复执行

NFC获取数据

微信小程序官方文档中提供了调用NFC功能,直接在uniapp小程序中直接调用就行
—> wx.getNFCAdapter()


			getNFC(){
				const nfc = wx.getNFCAdapter()
				this.nfc = nfc
				let _this = this
				
				function discoverHandler(res) {
					_this.$refs.popup.close()
					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
					})
					/* str就是我们读取出的UID码 */
					// console.log(str)
					/* ------根据需求 16转10进制------ */
					let parStr = parseInt(str,16)
					
					if (parStr) {
						/*--在这里进行业务逻辑处理--*/
						
					}else {
						uni.showToast({
							title:'获取失败',
							icon:'error'
						})
					}
				}
				nfc.startDiscovery({
					success(res) {
						uni.showToast({
							title: 'NFC读取功能已开启!',
							icon: 'none'
						})
						nfc.onDiscovered(discoverHandler)
					},
					fail(err) {
						if (!err.errCode) {
							uni.showToast({
								title: '请检查NFC功能是否正常!',
								icon: 'none'
							})
							return
						}
					}
				})
			},
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

扫码获取数据

使用uni.scanCode(),调用小程序中的扫码功能。

			getQR(){
				let _sanm = this
				uni.scanCode({
					onlyFromCamera: true,  // 只允许通过相机扫码
					success: function (res) {
						console.log(res)
						// console.log('条码类型:' + res.scanType);
						// console.log('条码内容:' + res.result);
						if(res.result !== '') {
							/*在这里进行业务逻辑处理*/
						}
						
					},
					fail(error) {
						console.log(error)
					}
				});
			},
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

官方参数:
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/243034
推荐阅读
相关标签
  

闽ICP备14008679号