diff --git a/autotest.utils.js b/autotest.utils.js deleted file mode 100644 index 16223450c1d92a0bf61004845f15944bfc8dd568..0000000000000000000000000000000000000000 --- a/autotest.utils.js +++ /dev/null @@ -1 +0,0 @@ -"use strict";function t(t,r){const s=t.split("."),e=r.split("."),n=Math.max(s.length,e.length);for(let t=0;tn)return 1;if(r=":return t(r,s.substring(2))>=0;case"<=":return t(r,s.substring(2))<=0;case"!=":return 0!==t(r,s.substring(2))}switch(s.charAt(0)){case">":return t(r,s.substring(1))>0;case"<":return t(r,s.substring(1))<0;case"=":return 0===t(r,s.substring(1));case"^":return parseInt(r.split(".")[0])===parseInt(s.substring(1).split(".")[0])&&t(r,s.substring(1))>=0;case"~":return t(r,(s=s.substring(1).trim())+".0")>=0&&t(r,`${s[0]}.${parseInt(s[2])+1}`)<0;default:return-1!==t(r,s)}}exports.invokeApi=function(t,r,s,...e){if(r||function(t){return!("[object Object]"!==Object.prototype.toString.call(t)||!["success","fail"].find((r=>"function"==typeof t[r])))}(s)){const{success:r,fail:n,...i}=s;return new Promise(((s,u)=>{t({...i,success(t){r&&r(t),s(t)},fail(t){n&&n(t),u(t)}},...e)}))}return t(s,...e)},exports.platformMatch=function(t,s,e){let n=!0;e&&(n=r(s.uniRuntimeVersion,e));const i=t[s.uniPlatform];if(n&&i){if(i[s.osName]){const t=i[s.osName];return r(s[t.versionKey]||"*",t.version||"*")}return r(s[i.versionKey]||"*",i.version||"*")}return!1},exports.satisfies=r; diff --git a/env.js b/env.js deleted file mode 100644 index 206a6644be1a71812183e0705d1d35fa42b14b38..0000000000000000000000000000000000000000 --- a/env.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = {"compile":true,"h5":{"options":{"headless":true},"executablePath":""},"mp-weixin":{"port":9420,"account":"","args":"","cwd":"","launch":true,"teardown":"disconnect","remote":false,"executablePath":""},"app-plus":{"android":{"id":"WDHVB20804034055","executablePath":"D:\\Do\\tools\\HX\\lastest_dev\\HBuilderX.3.7.7.20230315-alpha\\HBuilderX\\plugins\\launcher\\base\\android_base.apk"},"version":"D:\\Do\\tools\\HX\\lastest_dev\\HBuilderX.3.7.7.20230315-alpha\\HBuilderX\\plugins\\launcher\\base\\version.txt","ios":{"id":"","executablePath":""}}} \ No newline at end of file diff --git a/pages/autotest/uni-wifi/uni-wifi.test.js b/pages/autotest/uni-wifi/uni-wifi.test.js deleted file mode 100644 index afbda75cca4be9f5733e0e5de60f3155a576e92d..0000000000000000000000000000000000000000 --- a/pages/autotest/uni-wifi/uni-wifi.test.js +++ /dev/null @@ -1,245 +0,0 @@ -describe('uni-wifi', () => { - let page; - beforeAll(async () => { - page = await program.reLaunch('/pages/autotest/uni-wifi/uni-wifi'); - await page.waitFor(3000); - }); - it('startWifi0', async () => { - const result0 = await page.callMethod('startWifi00'); - expect(result0.res).not.toHaveProperty('_errMsg'); - expect(result0.res.errCode).toEqual(expect.any(Number)); - expect(result0.res.errSubject).toEqual(expect.any(String)); - expect(result0.res.errMsg).toEqual(expect.any(String)); - const result1 = await page.callMethod('startWifi01'); - expect(result1.res).not.toHaveProperty('_errMsg'); - expect(result1.res.errCode).toEqual(expect.any(Number)); - expect(result1.res.errSubject).toEqual(expect.any(String)); - expect(result1.res.errMsg).toEqual(expect.any(String)); - const result2 = await page.callMethod('startWifi02'); - expect(result2.res).not.toHaveProperty('_errMsg'); - expect(result2.res.errCode).toEqual(expect.any(Number)); - expect(result2.res.errSubject).toEqual(expect.any(String)); - expect(result2.res.errMsg).toEqual(expect.any(String)); - const result3 = await page.callMethod('startWifi03'); - expect(result3.res).not.toHaveProperty('_errMsg'); - expect(result3.res.errCode).toEqual(expect.any(Number)); - expect(result3.res.errSubject).toEqual(expect.any(String)); - expect(result3.res.errMsg).toEqual(expect.any(String)); - const result4 = await page.callMethod('startWifi04'); - expect(result4.res).not.toHaveProperty('_errMsg'); - expect(result4.res.errCode).toEqual(expect.any(Number)); - expect(result4.res.errSubject).toEqual(expect.any(String)); - expect(result4.res.errMsg).toEqual(expect.any(String)); - const result5 = await page.callMethod('startWifi05'); - expect(result5.res).not.toHaveProperty('_errMsg'); - expect(result5.res.errCode).toEqual(expect.any(Number)); - expect(result5.res.errSubject).toEqual(expect.any(String)); - expect(result5.res.errMsg).toEqual(expect.any(String)); - const result6 = await page.callMethod('startWifi06'); - expect(result6.res).not.toHaveProperty('_errMsg'); - expect(result6.res.errCode).toEqual(expect.any(Number)); - expect(result6.res.errSubject).toEqual(expect.any(String)); - expect(result6.res.errMsg).toEqual(expect.any(String)); - const result7 = await page.callMethod('startWifi07'); - expect(result7.res).not.toHaveProperty('_errMsg'); - expect(result7.res.errCode).toEqual(expect.any(Number)); - expect(result7.res.errSubject).toEqual(expect.any(String)); - expect(result7.res.errMsg).toEqual(expect.any(String)); - const result8 = await page.callMethod('startWifi08'); - expect(result8.res).not.toHaveProperty('_errMsg'); - expect(result8.res.errCode).toEqual(expect.any(Number)); - expect(result8.res.errSubject).toEqual(expect.any(String)); - expect(result8.res.errMsg).toEqual(expect.any(String)); - const result9 = await page.callMethod('startWifi09'); - expect(result9.res).not.toHaveProperty('_errMsg'); - expect(result9.res.errCode).toEqual(expect.any(Number)); - expect(result9.res.errSubject).toEqual(expect.any(String)); - expect(result9.res.errMsg).toEqual(expect.any(String)); - const result10 = await page.callMethod('startWifi010'); - expect(result10.res).not.toHaveProperty('_errMsg'); - expect(result10.res.errCode).toEqual(expect.any(Number)); - expect(result10.res.errSubject).toEqual(expect.any(String)); - expect(result10.res.errMsg).toEqual(expect.any(String)); - const result11 = await page.callMethod('startWifi011'); - expect(result11.res).not.toHaveProperty('_errMsg'); - expect(result11.res.errCode).toEqual(expect.any(Number)); - expect(result11.res.errSubject).toEqual(expect.any(String)); - expect(result11.res.errMsg).toEqual(expect.any(String)); - const result12 = await page.callMethod('startWifi012'); - expect(result12.res).not.toHaveProperty('_errMsg'); - expect(result12.res.errCode).toEqual(expect.any(Number)); - expect(result12.res.errSubject).toEqual(expect.any(String)); - expect(result12.res.errMsg).toEqual(expect.any(String)); - const result13 = await page.callMethod('startWifi013'); - expect(result13.res).not.toHaveProperty('_errMsg'); - expect(result13.res.errCode).toEqual(expect.any(Number)); - expect(result13.res.errSubject).toEqual(expect.any(String)); - expect(result13.res.errMsg).toEqual(expect.any(String)); - const result14 = await page.callMethod('startWifi014'); - expect(result14.res).not.toHaveProperty('_errMsg'); - expect(result14.res.errCode).toEqual(expect.any(Number)); - expect(result14.res.errSubject).toEqual(expect.any(String)); - expect(result14.res.errMsg).toEqual(expect.any(String)); - const result15 = await page.callMethod('startWifi015'); - expect(result15.res).not.toHaveProperty('_errMsg'); - expect(result15.res.errCode).toEqual(expect.any(Number)); - expect(result15.res.errSubject).toEqual(expect.any(String)); - expect(result15.res.errMsg).toEqual(expect.any(String)); - const result16 = await page.callMethod('startWifi016'); - expect(result16.res).not.toHaveProperty('_errMsg'); - expect(result16.res.errCode).toEqual(expect.any(Number)); - expect(result16.res.errSubject).toEqual(expect.any(String)); - expect(result16.res.errMsg).toEqual(expect.any(String)); - const result17 = await page.callMethod('startWifi017'); - expect(result17.res).not.toHaveProperty('_errMsg'); - expect(result17.res.errCode).toEqual(expect.any(Number)); - expect(result17.res.errSubject).toEqual(expect.any(String)); - expect(result17.res.errMsg).toEqual(expect.any(String)); - const result18 = await page.callMethod('startWifi018'); - expect(result18.res).not.toHaveProperty('_errMsg'); - expect(result18.res.errCode).toEqual(expect.any(Number)); - expect(result18.res.errSubject).toEqual(expect.any(String)); - expect(result18.res.errMsg).toEqual(expect.any(String)); - const result19 = await page.callMethod('startWifi019'); - expect(result19.res).not.toHaveProperty('_errMsg'); - expect(result19.res.errCode).toEqual(expect.any(Number)); - expect(result19.res.errSubject).toEqual(expect.any(String)); - expect(result19.res.errMsg).toEqual(expect.any(String)); - const result20 = await page.callMethod('startWifi020'); - expect(result20.res).not.toHaveProperty('_errMsg'); - expect(result20.res.errCode).toEqual(expect.any(Number)); - expect(result20.res.errSubject).toEqual(expect.any(String)); - expect(result20.res.errMsg).toEqual(expect.any(String)); - const result21 = await page.callMethod('startWifi021'); - expect(result21.res).not.toHaveProperty('_errMsg'); - expect(result21.res.errCode).toEqual(expect.any(Number)); - expect(result21.res.errSubject).toEqual(expect.any(String)); - expect(result21.res.errMsg).toEqual(expect.any(String)); - }); - it('stopWifi1', async () => { - const result0 = await page.callMethod('stopWifi10'); - expect(result0.res).not.toHaveProperty('_errMsg'); - expect(result0.res.errCode).toEqual(expect.any(Number)); - expect(result0.res.errSubject).toEqual(expect.any(String)); - expect(result0.res.errMsg).toEqual(expect.any(String)); - const result1 = await page.callMethod('stopWifi11'); - expect(result1.res).not.toHaveProperty('_errMsg'); - expect(result1.res.errCode).toEqual(expect.any(Number)); - expect(result1.res.errSubject).toEqual(expect.any(String)); - expect(result1.res.errMsg).toEqual(expect.any(String)); - const result2 = await page.callMethod('stopWifi12'); - expect(result2.res).not.toHaveProperty('_errMsg'); - expect(result2.res.errCode).toEqual(expect.any(Number)); - expect(result2.res.errSubject).toEqual(expect.any(String)); - expect(result2.res.errMsg).toEqual(expect.any(String)); - const result3 = await page.callMethod('stopWifi13'); - expect(result3.res).not.toHaveProperty('_errMsg'); - expect(result3.res.errCode).toEqual(expect.any(Number)); - expect(result3.res.errSubject).toEqual(expect.any(String)); - expect(result3.res.errMsg).toEqual(expect.any(String)); - const result4 = await page.callMethod('stopWifi14'); - expect(result4.res).not.toHaveProperty('_errMsg'); - expect(result4.res.errCode).toEqual(expect.any(Number)); - expect(result4.res.errSubject).toEqual(expect.any(String)); - expect(result4.res.errMsg).toEqual(expect.any(String)); - const result5 = await page.callMethod('stopWifi15'); - expect(result5.res).not.toHaveProperty('_errMsg'); - expect(result5.res.errCode).toEqual(expect.any(Number)); - expect(result5.res.errSubject).toEqual(expect.any(String)); - expect(result5.res.errMsg).toEqual(expect.any(String)); - const result6 = await page.callMethod('stopWifi16'); - expect(result6.res).not.toHaveProperty('_errMsg'); - expect(result6.res.errCode).toEqual(expect.any(Number)); - expect(result6.res.errSubject).toEqual(expect.any(String)); - expect(result6.res.errMsg).toEqual(expect.any(String)); - const result7 = await page.callMethod('stopWifi17'); - expect(result7.res).not.toHaveProperty('_errMsg'); - expect(result7.res.errCode).toEqual(expect.any(Number)); - expect(result7.res.errSubject).toEqual(expect.any(String)); - expect(result7.res.errMsg).toEqual(expect.any(String)); - const result8 = await page.callMethod('stopWifi18'); - expect(result8.res).not.toHaveProperty('_errMsg'); - expect(result8.res.errCode).toEqual(expect.any(Number)); - expect(result8.res.errSubject).toEqual(expect.any(String)); - expect(result8.res.errMsg).toEqual(expect.any(String)); - const result9 = await page.callMethod('stopWifi19'); - expect(result9.res).not.toHaveProperty('_errMsg'); - expect(result9.res.errCode).toEqual(expect.any(Number)); - expect(result9.res.errSubject).toEqual(expect.any(String)); - expect(result9.res.errMsg).toEqual(expect.any(String)); - const result10 = await page.callMethod('stopWifi110'); - expect(result10.res).not.toHaveProperty('_errMsg'); - expect(result10.res.errCode).toEqual(expect.any(Number)); - expect(result10.res.errSubject).toEqual(expect.any(String)); - expect(result10.res.errMsg).toEqual(expect.any(String)); - const result11 = await page.callMethod('stopWifi111'); - expect(result11.res).not.toHaveProperty('_errMsg'); - expect(result11.res.errCode).toEqual(expect.any(Number)); - expect(result11.res.errSubject).toEqual(expect.any(String)); - expect(result11.res.errMsg).toEqual(expect.any(String)); - const result12 = await page.callMethod('stopWifi112'); - expect(result12.res).not.toHaveProperty('_errMsg'); - expect(result12.res.errCode).toEqual(expect.any(Number)); - expect(result12.res.errSubject).toEqual(expect.any(String)); - expect(result12.res.errMsg).toEqual(expect.any(String)); - const result13 = await page.callMethod('stopWifi113'); - expect(result13.res).not.toHaveProperty('_errMsg'); - expect(result13.res.errCode).toEqual(expect.any(Number)); - expect(result13.res.errSubject).toEqual(expect.any(String)); - expect(result13.res.errMsg).toEqual(expect.any(String)); - const result14 = await page.callMethod('stopWifi114'); - expect(result14.res).not.toHaveProperty('_errMsg'); - expect(result14.res.errCode).toEqual(expect.any(Number)); - expect(result14.res.errSubject).toEqual(expect.any(String)); - expect(result14.res.errMsg).toEqual(expect.any(String)); - const result15 = await page.callMethod('stopWifi115'); - expect(result15.res).not.toHaveProperty('_errMsg'); - expect(result15.res.errCode).toEqual(expect.any(Number)); - expect(result15.res.errSubject).toEqual(expect.any(String)); - expect(result15.res.errMsg).toEqual(expect.any(String)); - const result16 = await page.callMethod('stopWifi116'); - expect(result16.res).not.toHaveProperty('_errMsg'); - expect(result16.res.errCode).toEqual(expect.any(Number)); - expect(result16.res.errSubject).toEqual(expect.any(String)); - expect(result16.res.errMsg).toEqual(expect.any(String)); - const result17 = await page.callMethod('stopWifi117'); - expect(result17.res).not.toHaveProperty('_errMsg'); - expect(result17.res.errCode).toEqual(expect.any(Number)); - expect(result17.res.errSubject).toEqual(expect.any(String)); - expect(result17.res.errMsg).toEqual(expect.any(String)); - const result18 = await page.callMethod('stopWifi118'); - expect(result18.res).not.toHaveProperty('_errMsg'); - expect(result18.res.errCode).toEqual(expect.any(Number)); - expect(result18.res.errSubject).toEqual(expect.any(String)); - expect(result18.res.errMsg).toEqual(expect.any(String)); - const result19 = await page.callMethod('stopWifi119'); - expect(result19.res).not.toHaveProperty('_errMsg'); - expect(result19.res.errCode).toEqual(expect.any(Number)); - expect(result19.res.errSubject).toEqual(expect.any(String)); - expect(result19.res.errMsg).toEqual(expect.any(String)); - const result20 = await page.callMethod('stopWifi120'); - expect(result20.res).not.toHaveProperty('_errMsg'); - expect(result20.res.errCode).toEqual(expect.any(Number)); - expect(result20.res.errSubject).toEqual(expect.any(String)); - expect(result20.res.errMsg).toEqual(expect.any(String)); - const result21 = await page.callMethod('stopWifi121'); - expect(result21.res).not.toHaveProperty('_errMsg'); - expect(result21.res.errCode).toEqual(expect.any(Number)); - expect(result21.res.errSubject).toEqual(expect.any(String)); - expect(result21.res.errMsg).toEqual(expect.any(String)); - }); - it('connectWifi2', async () => { - const result_assert0 = await page.callMethod('connectWifi2_assert0'); - expect(result_assert0.res).not.toHaveProperty('_errMsg'); - expect(result_assert0.res.errCode).toEqual(expect.any(Number)); - expect(result_assert0.res.errSubject).toEqual(expect.any(String)); - expect(result_assert0.res.errMsg).toEqual(expect.any(String)); - }); - it('getWifiList3', async () => { - const result_assert0 = await page.callMethod('getWifiList3_assert0'); - expect(result_assert0.res).not.toHaveProperty('_errMsg'); - expect(result_assert0.res.errCode).toEqual(expect.any(Number)); - expect(result_assert0.res.errSubject).toEqual(expect.any(String)); - expect(result_assert0.res.errMsg).toEqual(expect.any(String)); - }); -}); \ No newline at end of file diff --git a/pages/autotest/uni-wifi/uni-wifi.vue b/pages/autotest/uni-wifi/uni-wifi.vue deleted file mode 100644 index e4750bfa5be460f47defe312156580403606effe..0000000000000000000000000000000000000000 --- a/pages/autotest/uni-wifi/uni-wifi.vue +++ /dev/null @@ -1,724 +0,0 @@ - - - \ No newline at end of file diff --git a/pages/index/index.vue b/pages/index/index.vue index b3337dfbb036bd78392b46550fcbb3ff816500b0..bcd1af2049e4fc5ed18cc4409f57935cb77c1a85 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -14,6 +14,7 @@ + @@ -39,15 +40,26 @@ console.log(res); }, testConnnectWifi(){ - - uni.connectWifi({ - maunal:true, - SSID:"Xiaomi_20D0", - password:"BBB111", - complete:(res)=>{ - console.log(res); + + uni.startWifi({ + success:(res)=> { + console.log("success: " + JSON.stringify(res)); + uni.connectWifi({ + maunal:false, + SSID:"Xiaomi_20D0", + password:"BBB111", + complete:(res)=>{ + console.log(res); + } + }); + },fail:(res)=>{ + console.log("fail: " + JSON.stringify(res)); + },complete:(res)=>{ + console.log("complete: " + JSON.stringify(res)); } - }); + }) + + }, testGetConnnectWifi(){ @@ -96,6 +108,9 @@ }) }, testStopWifi() { + uni.offWifiConnected() + uni.offWifiConnectedWithPartialInfo() + uni.stopWifi({ success:(res)=> { console.log("success: " + JSON.stringify(res)); @@ -119,6 +134,13 @@ }) }, + + testOffGetWifiList(){ + uni.offGetWifiList() + }, + + + testonMemoryWarning() { uni.onMemoryWarning(this.onMemoryWarning) uni.showToast({ diff --git a/uni_modules/uni-wifi/utssdk/app-android/index.uts b/uni_modules/uni-wifi/utssdk/app-android/index.uts index 048a77b4c6dd06d0f953d8cf3549116b120dcbda..0859c56e208b29bf1ba1b8df374255dc24e1739c 100644 --- a/uni_modules/uni-wifi/utssdk/app-android/index.uts +++ b/uni_modules/uni-wifi/utssdk/app-android/index.uts @@ -16,45 +16,9 @@ import WifiConfiguration from 'android.net.wifi.WifiConfiguration'; import AuthAlgorithm from 'android.net.wifi.WifiConfiguration.AuthAlgorithm'; import KeyMgmt from 'android.net.wifi.WifiConfiguration.KeyMgmt'; import TextUtils from 'android.text.TextUtils'; +import Build from 'android.os.Build'; -import { UniWifiResult, GetConnectedWifiOptions, WifiConnectOption, WifiOption, UniWifiInfo, UniWifiPartialInfo, UniWifiCallback, StartWifi, StopWifi, GetWifiList, GetConnectedWifi, OnGetWifiList, OnWifiConnectedWithPartialInfo, OnOffWifiConnectedWithPartialInfo, OnWifiConnected, ConnectWifi, OffGetWifiList, OffWifiConnected, SetWifiList } from "../interface.uts" - - - -// /** -// * Wifi 函数通用入参封装 -// */ -// type WifiOption = { -// success?: (res: object) => void; -// fail?: (res: object) => void; -// complete?: (res: object) => void; -// }; - - -// /** -// * 获取当前链接的wifi信息 -// */ -// type GetConnectedWifiOptions = { -// partialInfo?: boolean -// success?: (res: UTSJSONObject) => void -// fail?: (res: UTSJSONObject) => void -// complete?: (res: UTSJSONObject) => void -// } - -// /** -// * Wifi 链接参数封装 -// */ -// type WifiConnectOption = { -// SSID:string; -// BSSID?:string; -// password:string; -// maunal:boolean; -// // 只返回ssid -// partialInfo:boolean; -// success?: (res: object) => void; -// fail?: (res: object) => void; -// complete?: (res: object) => void; -// } +import { UniWifiResult, GetConnectedWifiOptions, WifiConnectOption, WifiOption, UniWifiInfo } from "../interface.uts" @@ -103,7 +67,9 @@ function isHexWepKey(wepKey : string) : boolean { return isHex(wepKey); } - +/** + * android 平台特有的Wifi信息对象,主要是加了加密信息这个字段 + */ export type AndroidUniWifiInfo = { SSID : string; BSSID ?: string; @@ -123,7 +89,9 @@ function wrapUniWifiInfoFromAndroid(androidInfo : AndroidUniWifiInfo) : UniWifiI } return ret } - +/** + * 从扫描结果中提取统一的wifi数据结构 + */ function wrapUniWifiInfoFromScan(scanResult : ScanResult) : AndroidUniWifiInfo { let ret : AndroidUniWifiInfo = { @@ -156,7 +124,9 @@ function wrapUniWifiInfoFromScan(scanResult : ScanResult) : AndroidUniWifiInfo { return ret } - +/** + * 从链接信息中提取统一的wifi数据结构 + */ function wrapUniWifiInfoFromConnectInfo(connectInfo : WifiInfo) : UniWifiInfo { let ret : UniWifiInfo = { SSID: "", @@ -185,73 +155,9 @@ function wrapUniWifiInfoFromConnectInfo(connectInfo : WifiInfo) : UniWifiInfo { } -// /** -// * Wifi信息统一数据结构 -// */ -// class UniWifiInfo { - -// SSID: string = ""; -// BSSID: string = ""; -// secure: boolean = false; -// signalStrength: Number = 0; -// frequency: Number = 0; - -// /*下面的字段属于扩展字段*/ -// securityType:string = "" - -// constructor(scanResult: ScanResult|null = null) { -// if (scanResult != null) { -// // 如果是通过扫描列表得到的数据,进行封装 -// this.BSSID = scanResult.BSSID; -// this.SSID = scanResult.SSID; - -// this.signalStrength = scanResult.level; -// this.frequency = scanResult.frequency; - -// // 是否安全,微信的标准是是否需要密码。 来源:https://developers.weixin.qq.com/community/develop/doc/00064cf1790458db19cddf9925ac00?highLine=WifiInfo -// this.secure = false; -// let capabilities = scanResult.capabilities.trim(); -// if ((capabilities.equals(Global.WIFI_AUTH_OPEN) || capabilities.equals(Global.WIFI_AUTH_ROAM))) { -// this.secure = false; -// } else { -// this.secure = true; -// } - -// /*扩展字段*/ -// this.securityType = getSecurityType(scanResult); -// } -// } - -// /** -// * 根据connectInfo 链接信息对wifi数据结构进行初始化 -// */ -// wrapConnectInfo(connectInfo: WifiInfo): void { - -// if (connectInfo.getBSSID() == null) { -// return -// } -// if (connectInfo.getSSID() != null) { -// let s = connectInfo.getSSID(); -// // 微信不带,这里需要去掉引号 -// if (s.length > 2 && s.charAt(0) == '"' && s.charAt(s.length - 1) == '"') { -// s = s.substring(1, s.length - 1); -// } -// this.SSID = s; -// } - -// this.BSSID = connectInfo.getBSSID(); -// //Android返回的值是-100~0,而微信API规范是0~100,值越大信号越好,需要+100拉齐 -// this.signalStrength = connectInfo.getRssi() + 100; -// this.frequency = connectInfo.getFrequency(); - -// } - - -// } - /** - * 连接wifi时使用,根据用户输入内容包装为系统需要的wifi配置对象 - */ + * 连接wifi时使用,根据用户输入内容包装为系统需要的wifi配置对象 + **/ @Suppress("DEPRECATION") function wrapWifiConfiguration(SSID : string, password ?: string, passwordType : string) : WifiConfiguration { @@ -396,6 +302,7 @@ class CustomBroadcastReceiver extends BroadcastReceiver { if (intent.action == WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) { + startWifiScaning = false; // wifi 扫描结果回调 let results = this.mWifiManager!.scanResults; @@ -427,10 +334,14 @@ class CustomBroadcastReceiver extends BroadcastReceiver { /************************* 下面是对外提供的函数 *************************/ +/** + * start wifi是否正在扫描 + */ +var startWifiScaning = false /** - * 开启wifi - */ + * 开启wifi + */ @Suppress("DEPRECATION") export function startWifi(option : WifiOption) { @@ -452,7 +363,7 @@ export function startWifi(option : WifiOption) { return; } - + // 具备了权限,继续前进 let wifiManager : WifiManager = UTSAndroid.getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager @@ -496,6 +407,7 @@ export function startWifi(option : WifiOption) { } }); + startWifiScaning = true // 开始扫描 wifiManager.startScan() @@ -558,18 +470,33 @@ export function onWifiConnectedWithPartialInfo(callback : UTSCallback) { /** - * wifi 链接成功的回调取消注册 - */ -export function offWifiConnected(callback : UTSCallback) { + * wifi 链接成功的回调取消注册 + */ +export function offWifiConnected(callback? : UTSCallback) { + + if(callback == null){ + Global.onWifiConnectCallbackList = [] + return + } + let callbackIndex = Global.onWifiConnectCallbackList.indexOf(callback) - if (callbackIndex > 0) { + if (callbackIndex >= 0) { Global.onWifiConnectCallbackList.splice(callbackIndex, 1); } } -export function offWifiConnectedWithPartialInfo(callback : UTSCallback) { +/** + * 不具备详细信息的wifi 反注册 + */ +export function offWifiConnectedWithPartialInfo(callback? : UTSCallback) { + + if(callback == null){ + Global.onWifiConnectWithPartialInfoCallbackList = [] + return + } + let callbackIndex = Global.onWifiConnectWithPartialInfoCallbackList.indexOf(callback) - if (callbackIndex > 0) { + if (callbackIndex >= 0) { Global.onWifiConnectWithPartialInfoCallbackList.splice(callbackIndex, 1); } } @@ -580,53 +507,34 @@ export function offWifiConnectedWithPartialInfo(callback : UTSCallback) { export function onGetWifiList(callback : UTSCallback) { Global.getWifiListCallbackList.push(callback) } + /** - * 取消注册Wifi列表的监听事件 - */ -export function offGetWifiList(callback : UTSCallback) { + * 取消注册Wifi列表的监听事件 + */ +export function offGetWifiList(callback? : UTSCallback) { + if(callback == null){ + Global.getWifiListCallbackList = [] + return + } let callbackIndex = Global.getWifiListCallbackList.indexOf(callback) - if (callbackIndex > 0) { + if (callbackIndex >= 0) { Global.getWifiListCallbackList.splice(callbackIndex, 1); } } /** - * 链接指定wifi - */ -@Suppress("UNUSED_PARAMETER", "DEPRECATION") -export function connectWifi(option : WifiConnectOption) { - - - if (option.maunal == true) { - // 指定了手动模式 - let manunalIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS); - UTSAndroid.getUniActivity()!.startActivity(manunalIntent); - - result.errCode = 0 - result.errMsg = "connectWifi:ok" - - option.success?.(result) - option.complete?.(result) - return - } - - var result : UniWifiResult = { - errCode: 12000, - errMsg: "connectWifi:fail:not invoke startWifi", - errSubject: "uni-connectWifi", - } - + * 真正执行wifi链接逻辑 + */ +function realWifiConnect(option : WifiConnectOption,result : UniWifiResult){ + if (Global.mReceiver == null || Global.scanList.length < 1) { - // 还没调用startWifi 提示报错 option.fail?.(result) option.complete?.(result) - + return } - - - + // 执行后续的逻辑 let scanWifiInfo : AndroidUniWifiInfo | null = null for (let scanResult in Global.scanList) { @@ -634,19 +542,19 @@ export function connectWifi(option : WifiConnectOption) { scanWifiInfo = scanResult } } - + if (scanWifiInfo == null) { // 不在扫描列表中返回错误 option.fail?.(result) option.complete?.(result) return } - + let wifiConfigration = wrapWifiConfiguration(scanWifiInfo.SSID, option.password, scanWifiInfo.securityType); wifiConfigration.BSSID = scanWifiInfo.BSSID let wifiManager : WifiManager = UTSAndroid.getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager - + // 如果已经存在了指定wifi 配置,移除之 let targetExistConfig : WifiConfiguration | null = null let existingConfigs = wifiManager.getConfiguredNetworks(); @@ -658,20 +566,28 @@ export function connectWifi(option : WifiConnectOption) { // 如果wifi已经保存了当前ssid的配置,可能是别的应用添加的。android系统要求,需要删除掉重新添加 if (targetExistConfig != null) { let removeRet = wifiManager.removeNetwork(targetExistConfig.networkId); - + if (!removeRet) { - // 移除之前的配置失败了,返回错误,需要用户手动取消保存一下 - result.errCode = 12013 - result.errMsg = "connectWifi:wifi config may be expired" - + + // add since 2023-03-28,如果当前系统大于等于android10, 则明确当前系统不支持 + if(Build.VERSION.SDK_INT > 28){ + // 系统大于android 9 + result.errCode = 12001 + result.errMsg = "connectWifi:system not support" + }else{ + // 移除之前的配置失败了,返回错误,需要用户手动取消保存一下 + result.errCode = 12013 + result.errMsg = "connectWifi:wifi config may be expired" + } + option.fail?.(result) option.complete?.(result) - + return } - + } - + let currentConnect = wifiManager.getConnectionInfo() if (currentConnect.networkId >= 0) { wifiManager.disableNetwork(currentConnect.networkId) @@ -679,52 +595,52 @@ export function connectWifi(option : WifiConnectOption) { wifiManager.removeNetwork(currentConnect.networkId) } wifiManager.disconnect() - + let connected = false; try { - + let netID = wifiManager.addNetwork(wifiConfigration); // 如果-1 说明没添加上,报错即可 if (netID < 0) { result.errCode = 12002 result.errMsg = "connectWifi:password error Wi-Fi" - + option.fail?.(result) option.complete?.(result) - + return } - - + + let enabled = wifiManager.enableNetwork(netID, true); if (!enabled) { - + result.errCode = 12007 result.errMsg = "connectWifi:user denied" - + option.fail?.(result) option.complete?.(result) - + return } connected = wifiManager.reconnect(); - + } catch (e) { connected = false; console.log(e); } - + if (!connected) { // 出错了,返回错误 // 兜底的报错 result.errCode = 12010 result.errMsg = "connectWifi:fail:unknown error" - + option.fail?.(result) option.complete?.(result) return } - + wifiManager.saveConfiguration() //scanWifiInfo 根据 partialInfo 填充给返回字段 if (option.partialInfo != null && option.partialInfo == true) { @@ -735,13 +651,65 @@ export function connectWifi(option : WifiConnectOption) { } else { result.wifi = wrapUniWifiInfoFromAndroid(scanWifiInfo) } - + // result.wifi = scanWifiInfo.toUTSJSON(option.partialInfo) result.errCode = 0 result.errMsg = "connectWifi:ok" - + option.success?.(result) option.complete?.(result) +} + +/** + * 链接指定wifi + */ +@Suppress("UNUSED_PARAMETER", "DEPRECATION") +export function connectWifi(option : WifiConnectOption) { + + + var result : UniWifiResult = { + errCode: 12000, + errMsg: "connectWifi:fail:not invoke startWifi", + errSubject: "uni-connectWifi", + } + + if (option.maunal == true) { + // 指定了手动模式 + let manunalIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS); + UTSAndroid.getUniActivity()!.startActivity(manunalIntent); + + result.errCode = 0 + result.errMsg = "connectWifi:ok" + + option.success?.(result) + option.complete?.(result) + return + } + + + + // add since 2022-03-28 ,增加逻辑,如果正在扫描中,则可以等待5s + if(startWifiScaning){ + let taskCount = 0 + let taskId:number = 0 + taskId = setInterval(function(){ + + taskCount += 1; + if(taskCount >= 5 || startWifiScaning == false){ + // 超过10s了。或者扫描过程结束了 + clearInterval(taskId) + realWifiConnect(option,result) + } + },2000) + + UTSAndroid.onAppActivityDestroy(function () { + clearInterval(taskId) + }); + }else{ + realWifiConnect(option,result) + } + + } @@ -768,8 +736,12 @@ export function stopWifi(option : WifiOption) { // 多次调用 //TODO handle the exception } + + Global.getWifiListCallbackList = [] + Global.onWifiConnectWithPartialInfoCallbackList = [] + Global.onWifiConnectCallbackList = [] - var result : UniWifiResult = { + let result : UniWifiResult = { errCode: 0, errSubject: "uni-stopWifi", errMsg: "stopWifi:ok" @@ -780,8 +752,8 @@ export function stopWifi(option : WifiOption) { } /** - * 获取当前连接中的wifi信息 - */ + * 获取当前连接中的wifi信息 + */ @Suppress("DEPRECATION") export function getConnectedWifi(option : GetConnectedWifiOptions) { @@ -803,6 +775,7 @@ export function getConnectedWifi(option : GetConnectedWifiOptions) { return } + // 需要先校验权限,没有位置权限无法获取wifi if (ActivityCompat.checkSelfPermission(UTSAndroid.getUniActivity()!, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // 尚不具备权限,返回错误 res.errCode = 12001 @@ -813,8 +786,6 @@ export function getConnectedWifi(option : GetConnectedWifiOptions) { return; } - // TODO 应该try catch一下,把系统的错误码给返回来,然后和微信的错误码拉齐 - // 需要先校验权限,没有位置权限无法获取wifi const context = UTSAndroid.getAppContext(); if (context != null) { const wm = context.getSystemService( @@ -826,7 +797,7 @@ export function getConnectedWifi(option : GetConnectedWifiOptions) { wifiInfo = wrapUniWifiInfoFromConnectInfo(winfo); // 判断一下是否wifi 关闭了 - if (option.partialInfo!) { + if (option.partialInfo!= null) { let ret : UniWifiInfo = { SSID: wifiInfo.SSID } diff --git a/uni_modules/uni-wifi/utssdk/interface.uts b/uni_modules/uni-wifi/utssdk/interface.uts index c9006003c7ad21ee46f866843d2150de8e989667..8c54c2eb81598df04520c2a466916bcac3824f93 100644 --- a/uni_modules/uni-wifi/utssdk/interface.uts +++ b/uni_modules/uni-wifi/utssdk/interface.uts @@ -83,6 +83,7 @@ interface Uni { /** * @autotest { generated: false, + pollution: false, cases:[ { before: 'startWifi', @@ -90,8 +91,12 @@ interface Uni { input: [{ maunal:false, SSID:"Xiaomi_20D0", - password:"BBB111" - }] + password:"streamApp!2016", + }], + output:{ + callbackType: 'success', + value: { errCode: 12013 ,errMsg: "connectWifi:wifi config may be expired",errSubject: "uni-connectWifi"} + } } ] }