Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
uni-api
提交
409284d3
U
uni-api
项目概览
DCloud
/
uni-api
通知
670
Star
23
Fork
12
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
3
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-api
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
3
Issue
3
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
409284d3
编写于
11月 22, 2022
作者:
杜庆泉
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
初步实现 connect wifi 函数原型,等待测试
上级
ca0a8cd6
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
97 addition
and
24 deletion
+97
-24
pages/index/index.vue
pages/index/index.vue
+1
-1
uni_modules/uni-wifi/utssdk/app-android/index.uts
uni_modules/uni-wifi/utssdk/app-android/index.uts
+96
-23
未找到文件。
pages/index/index.vue
浏览文件 @
409284d3
...
...
@@ -37,7 +37,7 @@
testConnnectWifi
(){
uni
.
connectWifi
({
partialInfo
:
false
,
maunal
:
false
,
SSID
:
"
Xiaomi_20D0
"
,
password
:
"
BBBB
"
,
complete
:(
res
)
=>
{
...
...
uni_modules/uni-wifi/utssdk/app-android/index.uts
浏览文件 @
409284d3
...
...
@@ -13,6 +13,9 @@ import JSONObject from "com.alibaba.fastjson.JSONObject";
import Intent from "android.content.Intent";
import Thread from "java.lang.Thread";
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';
/**
* Wifi 函数通用入参封装
...
...
@@ -37,12 +40,36 @@ type WifiConnectOption = {
complete?: (res: object) => void;
}
function wrapWifiConfiguration(SSID:string ,password:string):WifiConfiguration {
function isHex(key:string):boolean {
for (var i = key.length - 1; i >= 0; i--) {
let c = key.charAt(i);
if (!(c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a'
&& c <= 'f')) {
return false;
}
}
return true;
}
/**
* 判断是否是wep格式的key
*/
function isHexWepKey(wepKey:string):boolean {
let len = wepKey.length;
// WEP-40, WEP-104, and some vendors using 256-bit WEP (WEP-232?)
if (len != 10 && len != 26 && len != 58) {
return false;
}
return isHex(wepKey);
}
function wrapWifiConfiguration(SSID:string ,password:string,passwordType:string):WifiConfiguration {
let config = new WifiConfiguration();
...
...
@@ -53,6 +80,43 @@ function wrapWifiConfiguration(SSID:string ,password:string):WifiConfiguration {
config.allowedProtocols.clear();
config.SSID = "\"" + SSID + "\"";
// nopass
if ("NONE".equals(passwordType)) {
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
}
// // wep
if ("WEP".equals(passwordType)) {
if (!TextUtils.isEmpty(password)) {
if (isHexWepKey(password)) {
config.wepKeys[0] = password;
} else {
config.wepKeys[0] = "\"" + password + "\"";
}
}
config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
config.allowedKeyManagement.set(KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
}
// wpa
if ("WPA".equals(passwordType)) {
config.preSharedKey = "\"" + password + "\"";
config.hiddenSSID = true;
config.allowedAuthAlgorithms
.set(WifiConfiguration.AuthAlgorithm.OPEN);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.TKIP);
// 此处需要修改否则不能自动重联
config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.CCMP);
config.status = WifiConfiguration.Status.ENABLED;
}
return config;
}
/**
...
...
@@ -312,8 +376,7 @@ export function offGetWifiList(callback: UTSCallback) {
*/
export function connectWifi(option: WifiConnectOption) {
var result = {
errCode: 12000,
errMsg: "connectWifi:fail:not invoke startWifi",
...
...
@@ -328,20 +391,18 @@ export function connectWifi(option: WifiConnectOption) {
return
}
//
if(option.maunal == true){
//
// 指定了手动模式
//
let manunalIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
//
getUniActivity()!!.startActivity(manunalIntent);
if(option.maunal == true){
// 指定了手动模式
let manunalIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
getUniActivity()!!.startActivity(manunalIntent);
//
result.errCode = 0
//
result.errMsg = "connectWifi:ok"
result.errCode = 0
result.errMsg = "connectWifi:ok"
// option.success
// ?.(result)
// option.complete?.(result)
// return
// }
option.success?.(result)
option.complete?.(result)
return
}
// 执行后续的逻辑
let scanWifiInfo:UniWifiInfo|null = null
...
...
@@ -360,13 +421,14 @@ export function connectWifi(option: WifiConnectOption) {
return
}
console.log(
scanWifiInfo
);
console.log(
JSON.stringify(scanWifiInfo.securityType)
);
let wifiConfigration = wrapWifiConfiguration(option.SSID,option.password);
let wifiConfigration = wrapWifiConfiguration(option.SSID,option.password
,scanWifiInfo.securityType
);
let wifiManager: WifiManager =
getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager
// 如果已经存在了指定wifi 配置,移除之
let targetExistConfig:WifiConfiguration|null = null
let existingConfigs = wifiManager.getConfiguredNetworks();
for (let existingConfig in existingConfigs) {
...
...
@@ -379,12 +441,23 @@ export function connectWifi(option: WifiConnectOption) {
wifiManager.removeNetwork(targetExistConfig.networkId);
}
let netID = wifiManager.addNetwork(wifiConfigration);
let enabled = wifiManager.enableNetwork(netID, true);
let connected = wifiManager.reconnect();
try {
let netID = wifiManager.addNetwork(wifiConfigration);
let enabled = wifiManager.enableNetwork(netID, true);
let connected = wifiManager.reconnect();
console.log(connected);
} catch (e) {
// TODO: handle exception
console.log(e);
// e.printStackTrace();
}
console.log(connected);
result.errCode = 0
result.errMsg = "connectWifi:ok"
option.success?.(result)
option.complete?.(result)
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录