Преглед на файлове

优化:安卓获取手机wifi,苹果获取设备wifi;后期设备获取wifi再优化

htc преди 1 ден
родител
ревизия
48c8bba579
променени са 5 файла, в които са добавени 226 реда и са изтрити 69 реда
  1. 13 2
      components/CusHeader/index.vue
  2. 81 0
      mixins/xBlufiInit.js
  3. 2 0
      pagesMy/deviceAdd.vue
  4. 129 66
      pagesMy/wifiSet.vue
  5. 1 1
      pagesMy/wifiSuccess.vue

+ 13 - 2
components/CusHeader/index.vue

@@ -39,7 +39,11 @@
 					fontWeight: "bold",
 					color: '#111111'
 				}
-			},
+			},
+			backAlert:{
+				typeof:Boolean,
+				default:false
+			}
 		},
 		data() {
 			return {
@@ -53,7 +57,14 @@
 			}
 		},
 		methods: {
-			toBack(url) {
+			toBack(url) {
+				if(this.backAlert){
+					uni.reLaunch({
+						url: '/pages/home'
+					})
+				}else this.dealBack(url)
+			},
+			dealBack(url) {
 				if (!url) {
 					if (uni.getStorageSync('options')) {
 						uni.redirectTo(JSON.parse(decodeURIComponent(uni.getStorageSync('options'))));

+ 81 - 0
mixins/xBlufiInit.js

@@ -0,0 +1,81 @@
+var _this = null;
+var xBlufi = require("@/utils/blufi/xBlufi.js");
+export default {	
+	data(){
+		return {
+			sbList:[],
+			wxList:[],
+			blooth:null
+		}
+	},
+	onLoad(){
+		_this = this;
+		this.Search();
+		xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent);
+	},
+	onUnload(){
+		xBlufi.listenDeviceMsgEvent(false, this.funListenDeviceMsgEvent);
+	},
+	methods:{
+		funListenDeviceMsgEvent: function(options) {
+			let ssid_arry = this.wxList;
+			switch (options.type) {
+				case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS:
+					if (options.result && options.data.length > 0) {
+						_this.sbList = options.data;
+						let ly = _this.sbList.find(d => d.name.indexOf('cx-') > -1);
+						if (!ly) ly = _this.sbList.find(d => d.name.indexOf('BLUFI_DEVICE') > -1);
+		
+						if (ly) {
+							xBlufi.notifyStartDiscoverBle({
+								'isStart': false
+							});
+							_this.blooth = ly;
+							_this.connectWiFi();
+						}
+					}
+					break;
+				case xBlufi.XBLUFI_TYPE.TYPE_CONNECTED:
+					if (options.result) {
+						xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent);
+						xBlufi.notifyInitBleEsp32({
+							deviceId: _this.blooth.deviceId,
+						})
+					}
+					break;
+				case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START:
+					if (!options.result) {
+						console.log('蓝牙未开启')
+					}
+					break;
+				case xBlufi.XBLUFI_TYPE.TYPE_CONNECT_NEAR_ROUTER_LISTS:
+					if ('' === options.data.SSID) break;
+					if(!ssid_arry.includes(options.data.SSID)) ssid_arry.push(options.data.SSID)
+					_this.wxList = ssid_arry;
+					let wifiSSID = _this.wxList[0];
+					let wifiPWD = uni.getStorageSync(wifiSSID)||'';
+					uni.setStorageSync('lanyaWiFiList',JSON.stringify(_this.wxList))
+					uni.setStorageSync('lanyaWiFiSSID',wifiSSID)
+					console.log(_this.wxList,'_this.wxList')
+					console.log(wifiSSID,'wifiSSID')
+					break;
+				case xBlufi.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT:
+					xBlufi.notifySendGetNearRouterSsid()
+					break;
+			}
+		},
+		// 开始或停止搜索设备
+		Search: function() {
+			xBlufi.notifyStartDiscoverBle({
+				'isStart': true
+			});
+		},
+		connectWiFi: function() {
+			xBlufi.notifyConnectBle({
+				isStart: true,
+				deviceId: _this.blooth.deviceId,
+				name: _this.blooth.name
+			});
+		}
+	}
+}

+ 2 - 0
pagesMy/deviceAdd.vue

@@ -49,7 +49,9 @@
 </template>
 
 <script>
+	// import xBlufiInit from '@/mixins/xBlufiInit'
 	export default {
+		// mixins:[xBlufiInit],
 		data(){
 			return {
 				

+ 129 - 66
pagesMy/wifiSet.vue

@@ -77,7 +77,8 @@
 				wifiListCache: [], // 缓存 Wi-Fi 列表
 				initTimeout: null, // 初始化超时定时器
 				wifiListFetchTimeout: null, // 获取Wi-Fi列表超时定时器
-				connectTimeout: null, // 配网超时定时器
+				connectTimeout: null, // 配网超时定时器
+				phoneType:''
 			}
 		},
 		onLoad: function(options) {
@@ -88,7 +89,7 @@
 			
 			xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent);
 			
-			wx.showLoading({title:'设备初始化...'})
+			// wx.showLoading({title:'设备初始化...'})
 			
 			// 初始化超时处理
 			this.initTimeout = setTimeout(() => {
@@ -102,6 +103,14 @@
 
 			xBlufi.notifyInitBleEsp32({
 				deviceId: options.deviceId,
+			})
+			uni.getSystemInfo({
+				success:res=>{
+					this.phoneType = res.platform;
+					if(res.platform === 'android'){
+						this.startSearchWifi();
+					}
+				}
 			})
 		},
 		onUnload() {
@@ -144,56 +153,60 @@
 						break;
 					case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_CUSTON_DATA:
 						break;
-					case xBlufi.XBLUFI_TYPE.TYPE_CONNECT_NEAR_ROUTER_LISTS:
-						if (this.wifiListFetchTimeout) {
-							clearTimeout(this.wifiListFetchTimeout);
-							this.wifiListFetchTimeout = null;
+					case xBlufi.XBLUFI_TYPE.TYPE_CONNECT_NEAR_ROUTER_LISTS:
+						if(this.phoneType==='ios'){
+							if (this.wifiListFetchTimeout) {
+								clearTimeout(this.wifiListFetchTimeout);
+								this.wifiListFetchTimeout = null;
+							}
+							wx.hideLoading();
+							if ('' === options.data.SSID) break;
+							if(!ssid_arry.includes(options.data.SSID)) ssid_arry.push(options.data.SSID)
+							that.wifiList = ssid_arry;
+							that.wifiListCache = [...ssid_arry]; // 缓存
+							that.wifiSSID = that.wifiList[0];
+							that.wifiPWD = uni.getStorageSync(that.wifiSSID)||'';
+							that.isInitOK = true
 						}
-						wx.hideLoading();
-						if ('' === options.data.SSID) break;
-						if(!ssid_arry.includes(options.data.SSID)) ssid_arry.push(options.data.SSID)
-						that.wifiList = ssid_arry;
-						that.wifiListCache = [...ssid_arry]; // 缓存
-						that.wifiSSID = that.wifiList[0];
-						that.wifiPWD = uni.getStorageSync(that.wifiSSID)||'';
-						that.isInitOK = true
 						break;
 					case xBlufi.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT:
 						if (this.initTimeout) {
 							clearTimeout(this.initTimeout);
 							this.initTimeout = null;
 						}
-						wx.hideLoading();
-						if (options.result) {
-							// 检查是否有缓存的Wi-Fi列表
-							if (this.wifiListCache.length > 0) {
-								this.wifiList = [...this.wifiListCache];
-								this.wifiSSID = this.wifiList[0];
-								this.wifiPWD = uni.getStorageSync(this.wifiSSID) || '';
-								this.isInitOK = true;
+						wx.hideLoading();
+						if(this.phoneType==='ios'){
+							if (options.result) {
+								// 检查是否有缓存的Wi-Fi列表
+								if (this.wifiListCache.length > 0) {
+									this.wifiList = [...this.wifiListCache];
+									this.wifiSSID = this.wifiList[0];
+									this.wifiPWD = uni.getStorageSync(this.wifiSSID) || '';
+									this.isInitOK = true;
+								} else {
+									xBlufi.notifySendGetNearRouterSsid()
+									wx.showLoading({
+										title: '获取周围WiFi列表...',
+									})
+									
+									// 获取Wi-Fi列表超时处理
+									this.wifiListFetchTimeout = setTimeout(() => {
+										wx.hideLoading();
+										wx.showModal({
+											title: '提示',
+											content: '获取Wi-Fi列表超时,请重试',
+											showCancel: false
+										});
+									}, 10000);
+								}
 							} else {
-								xBlufi.notifySendGetNearRouterSsid()
-								wx.showLoading({
-									title: '获取周围WiFi列表...',
+								that.connected = false
+								wx.showModal({
+									title: '温馨提示',
+									content: `设备初始化失败`,
+									showCancel: false, //是否显示取消按钮
 								})
-								
-								// 获取Wi-Fi列表超时处理
-								this.wifiListFetchTimeout = setTimeout(() => {
-									wx.hideLoading();
-									wx.showModal({
-										title: '提示',
-										content: '获取Wi-Fi列表超时,请重试',
-										showCancel: false
-									});
-								}, 10000);
-							}
-						} else {
-							that.connected = false
-							wx.showModal({
-								title: '温馨提示',
-								content: `设备初始化失败`,
-								showCancel: false, //是否显示取消按钮
-							})
+							}
 						}
 						break;
 				}
@@ -203,28 +216,32 @@
 			},
 			initWiFi(){
 				// 如果有缓存直接使用
-				if (this.wifiListCache.length > 0) {
+				if (this.wifiListCache?.length > 0) {
 					this.wifiList = [...this.wifiListCache];
 					return;
 				}
 				
 				this.sxWiFi();
 			},
-			sxWiFi(){
-				xBlufi.notifySendGetNearRouterSsid()
-				wx.showLoading({
-					title: '获取周围WiFi列表...',
-				})
-				
-				// 获取Wi-Fi列表超时处理
-				this.wifiListFetchTimeout = setTimeout(() => {
-					wx.hideLoading();
-					wx.showModal({
-						title: '提示',
-						content: '获取Wi-Fi列表超时,请重试',
-						showCancel: false
-					});
-				}, 10000);
+			sxWiFi(){
+				if(this.phoneType==='ios'){
+					xBlufi.notifySendGetNearRouterSsid()
+					wx.showLoading({
+						title: '获取周围WiFi列表...',
+					})
+					
+					// 获取Wi-Fi列表超时处理
+					this.wifiListFetchTimeout = setTimeout(() => {
+						wx.hideLoading();
+						wx.showModal({
+							title: '提示',
+							content: '获取Wi-Fi列表超时,请重试',
+							showCancel: false
+						});
+					}, 10000);
+				}else{
+					this.startSearchWifi();
+				}
 			},
 			connectWifi(){
 				let that = this;
@@ -252,19 +269,65 @@
 			},
 			toSelectWifi(){
 				this.show = true;
-				// 打开弹窗时自动获取Wi-Fi列表(如果没有缓存)
-				if (this.wifiList.length === 0 && this.wifiListCache.length === 0) {
-					this.initWiFi();
-				} else if (this.wifiListCache.length > 0 && this.wifiList.length === 0) {
-					// 使用缓存
-					this.wifiList = [...this.wifiListCache];
-				}
+				// // 打开弹窗时自动获取Wi-Fi列表(如果没有缓存)
+				// if (this.wifiList.length === 0 && this.wifiListCache.length === 0) {
+				// 	this.initWiFi();
+				// } else if (this.wifiListCache.length > 0 && this.wifiList.length === 0) {
+				// 	// 使用缓存
+				// 	this.wifiList = [...this.wifiListCache];
+				// }
 			},
 			selectWifi(item){
 				this.wifiSSID = item;
 				this.wifiPWD = uni.getStorageSync(item) || '';
 				this.show = false;
 				this.isInitOK = true
+			},
+			startSearchWifi() {
+				wx.showLoading({title:'设备初始化获取WiFi...'})
+				this.wifiList = [];
+				uni.startWifi({
+					success: (res) => {
+						console.log('初始化WiFi模块成功', res);
+						this.getWifiList();
+					},
+					fail: (err) => {
+						console.error('startWifi fail:', err);
+					}
+				});
+			},
+			getWifiList() {
+				let that = this;
+				// 步骤 2: 监听获取到 WiFi 列表数据的事件
+				uni.onGetWifiList((res) => {
+					let list = that.processWifiList(res.wifiList);
+					that.wifiList = list.map(l=>l.SSID)?.filter(l=>l.indexOf('5G')<0);
+					that.wifiListCache = [...that.wifiList];
+					that.wifiSSID = that.wifiList[0];
+					that.wifiPWD = uni.getStorageSync(that.wifiSSID)||'';
+					that.isInitOK = true
+					uni.hideLoading();
+				});
+				uni.getWifiList({
+					success: () => {
+						console.log('获取WiFi列表请求成功');
+					},
+					fail: (err) => {
+						console.error('getWifiList fail:', err);
+					}
+				});
+			},
+			processWifiList(list) {
+				const uniqueList = [];
+				const ssidMap = new Map();
+				list.forEach(item => {
+					if (item.SSID && !ssidMap.has(item.SSID)) {
+						ssidMap.set(item.SSID, true);
+						uniqueList.push(item);
+					}
+				});
+				// 按信号强度降序排序
+				return uniqueList.sort((a, b) => b.signalStrength - a.signalStrength);
 			}
 		}
 	}

+ 1 - 1
pagesMy/wifiSuccess.vue

@@ -1,6 +1,6 @@
 <template>
 	<view class="page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
-		<cus-header title='配置网络'></cus-header>
+		<cus-header title='配置网络' :backAlert="true"></cus-header>
 		<image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/06/03/e6540154-b4da-4ddd-ab9b-8f219d5c18a6.png"></image>
 		<template v-if="type=='jpw'">
 			<p>网络配置成功,快去体验吧~</p>