微信小程序如何实现进入小程序自动连WiFi功能

先上代码

<template>
	<view class="home">
		<button class="home-but" @click="authorizeLocation">扫码连WIFI</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {}
		},
		onLoad() {

		},
		methods: {
			authorizeLocation() {
				uni.authorize({
					scope: 'scope.userLocation',
					success: () => {
						// 用户授权成功后,开始连接WiFi
						this.startWifi();
					},
					fail: () => {
						uni.showToast({
							title: '位置信息授权失败',
							icon: 'none'
						});
					}
				});
			},
			// 开启WiFi模块
			startWifi() {
				uni.startWifi({
					success: (success) => {
						console.log("success", success)
						this.detectionWifi();
					},
					fail: (err) => {
						console.log("开启WiFi模块失败", err);
						// 开启WiFi模块失败后的处理逻辑
					}
				});
			},
			// 检测当前是否链接该网络
			detectionWifi() {
				wx.getConnectedWifi({
					success: (res) => {
						console.log(res);
						if(res.wifi.SSID === 'dlbsx_3'){
							uni.showToast({
								title: '请勿重复连接'
							})
							return;
						}else{
							this.linkWifi();
						}
					},
					fail: (err) => {
						console.log(err);
						this.linkWifi();
					}
				})
			},
			// 连接wifi
			linkWifi(){
				uni.showLoading({
					title: '连接中...'
				})
				uni.connectWifi({
					SSID: '',  //wifi名称
					//BSSID:'你的设备唯一值',//Wi-Fi 设备 BSSID 
					password: '', // 这里需要用户输入真实的WiFi密码
					success: () => {
						// console.log("WiFi连接成功");
						// 连接成功后的处理逻辑
						uni.showToast({
							title: 'WiFi连接成功'
						})
					},
					fail: (err) => {
						// console.log("WiFi连接失败", err);
						// 连接失败后的处理逻辑
						uni.showToast({
							title: 'WiFi连接失败'
						})
					}
				});
			}
		}
	}
</script>

<style>
	.home {
		width: 100%;
	}

	.home-but {
		width: 50%;
		height: 80rpx;
		margin-top: 100rpx;
		background-color: #1989FA;
		color: #FFFFFF;
		letter-spacing: 5rpx;
		line-height: 80rpx;
		outline: none;
		box-shadow: 2px 2px 5px #eeeeee;
	}
</style>

原创作者:吴小糖

创作时间:2024.3.25 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小吴吴吴呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值