wifi.vue 2.2 KB
Newer Older
打打卡夫卡's avatar
打打卡夫卡 已提交
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
<template>
	<view class="content">
		<button @tap="testStartWifi" style="width:100%">初始化wifi模块</button>
		<button @tap="testGetWifiList" style="width:100%">获取当前wifi列表</button>
		<button @tap="testGetConnnectWifi" style="width:100%">获取当前连接的wifi</button>
		<button @tap="testStopWifi" style="width:100%">关闭wifi模块</button>
	</view>
</template>

<script>

	export default {
		data() {
			return {
			}
		},
		onLoad() {

		},
		methods: {
			testConnnectWifi(){

				uni.connectWifi({
					maunal:false,
					SSID:"Xiaomi_20D0",
					password:"BBBB",
					complete:(res)=>{
						console.log(res);
					}
				});
			},
			testGetConnnectWifi(){
				uni.getConnectedWifi({
					partialInfo:false,
					complete:(res)=>{
						console.log(res);
						uni.showToast({
							icon:'none',
							title:res.wifi.SSID
						})
					}
				});
			},
			testStartWifi(){
				uni.startWifi({
					success:(res)=> {
						console.log("success: " + JSON.stringify(res));
						// wifi 开启成功后,注册wifi链接状态监听和wifi列表获取监听
						uni.onGetWifiList(function(res){
							console.log("onGetWifiList");
							console.log(res);
						});
						uni.onWifiConnected(function(res){
							console.log("onWifiConnected");
							console.log(res);
						});
						
					},fail:(res)=>{
						console.log("fail: " + JSON.stringify(res));
					},complete:(res)=>{
						console.log("complete: " + JSON.stringify(res));
					}
				})
			},
			testStopWifi() {
				uni.stopWifi({
					success:(res)=> {
						console.log("success: " + JSON.stringify(res));
					},fail:(res)=>{
						console.log("fail: " + JSON.stringify(res));
					},complete:(res)=>{
						console.log("complete: " + JSON.stringify(res));
					}
				})
				
			},
			testGetWifiList() {
				uni.getWifiList({
					success:(res)=> {
						console.log("success: " + JSON.stringify(res));
					},fail:(res)=>{
						console.log("fail: " + JSON.stringify(res));
					},complete:(res)=>{
						console.log("complete: " + JSON.stringify(res));
					}
				})
			},
			
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

</style>