|
@@ -77,7 +77,8 @@
|
|
wifiListCache: [], // 缓存 Wi-Fi 列表
|
|
wifiListCache: [], // 缓存 Wi-Fi 列表
|
|
initTimeout: null, // 初始化超时定时器
|
|
initTimeout: null, // 初始化超时定时器
|
|
wifiListFetchTimeout: null, // 获取Wi-Fi列表超时定时器
|
|
wifiListFetchTimeout: null, // 获取Wi-Fi列表超时定时器
|
|
- connectTimeout: null, // 配网超时定时器
|
|
|
|
|
|
+ connectTimeout: null, // 配网超时定时器
|
|
|
|
+ phoneType:''
|
|
}
|
|
}
|
|
},
|
|
},
|
|
onLoad: function(options) {
|
|
onLoad: function(options) {
|
|
@@ -88,7 +89,7 @@
|
|
|
|
|
|
xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent);
|
|
xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent);
|
|
|
|
|
|
- wx.showLoading({title:'设备初始化...'})
|
|
|
|
|
|
+ // wx.showLoading({title:'设备初始化...'})
|
|
|
|
|
|
// 初始化超时处理
|
|
// 初始化超时处理
|
|
this.initTimeout = setTimeout(() => {
|
|
this.initTimeout = setTimeout(() => {
|
|
@@ -102,6 +103,14 @@
|
|
|
|
|
|
xBlufi.notifyInitBleEsp32({
|
|
xBlufi.notifyInitBleEsp32({
|
|
deviceId: options.deviceId,
|
|
deviceId: options.deviceId,
|
|
|
|
+ })
|
|
|
|
+ uni.getSystemInfo({
|
|
|
|
+ success:res=>{
|
|
|
|
+ this.phoneType = res.platform;
|
|
|
|
+ if(res.platform === 'android'){
|
|
|
|
+ this.startSearchWifi();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
})
|
|
})
|
|
},
|
|
},
|
|
onUnload() {
|
|
onUnload() {
|
|
@@ -144,56 +153,60 @@
|
|
break;
|
|
break;
|
|
case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_CUSTON_DATA:
|
|
case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_CUSTON_DATA:
|
|
break;
|
|
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;
|
|
break;
|
|
case xBlufi.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT:
|
|
case xBlufi.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT:
|
|
if (this.initTimeout) {
|
|
if (this.initTimeout) {
|
|
clearTimeout(this.initTimeout);
|
|
clearTimeout(this.initTimeout);
|
|
this.initTimeout = null;
|
|
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 {
|
|
} 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;
|
|
break;
|
|
}
|
|
}
|
|
@@ -203,28 +216,32 @@
|
|
},
|
|
},
|
|
initWiFi(){
|
|
initWiFi(){
|
|
// 如果有缓存直接使用
|
|
// 如果有缓存直接使用
|
|
- if (this.wifiListCache.length > 0) {
|
|
|
|
|
|
+ if (this.wifiListCache?.length > 0) {
|
|
this.wifiList = [...this.wifiListCache];
|
|
this.wifiList = [...this.wifiListCache];
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
this.sxWiFi();
|
|
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(){
|
|
connectWifi(){
|
|
let that = this;
|
|
let that = this;
|
|
@@ -252,19 +269,65 @@
|
|
},
|
|
},
|
|
toSelectWifi(){
|
|
toSelectWifi(){
|
|
this.show = true;
|
|
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){
|
|
selectWifi(item){
|
|
this.wifiSSID = item;
|
|
this.wifiSSID = item;
|
|
this.wifiPWD = uni.getStorageSync(item) || '';
|
|
this.wifiPWD = uni.getStorageSync(item) || '';
|
|
this.show = false;
|
|
this.show = false;
|
|
this.isInitOK = true
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|