Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
uni-api
提交
127fbd5f
U
uni-api
项目概览
DCloud
/
uni-api
通知
677
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看板
提交
127fbd5f
编写于
11月 18, 2022
作者:
杜庆泉
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
android wifi connect ing
上级
5a6e3423
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
278 addition
and
13 deletion
+278
-13
pages/index/index.vue
pages/index/index.vue
+9
-1
uni_modules/uni-usercapturescreen/utssdk/app-android/index.uts
...odules/uni-usercapturescreen/utssdk/app-android/index.uts
+1
-1
uni_modules/uni-wifi/utssdk/app-android/WifiConnector.uts
uni_modules/uni-wifi/utssdk/app-android/WifiConnector.uts
+151
-0
uni_modules/uni-wifi/utssdk/app-android/index.uts
uni_modules/uni-wifi/utssdk/app-android/index.uts
+117
-11
未找到文件。
pages/index/index.vue
浏览文件 @
127fbd5f
...
...
@@ -35,7 +35,15 @@
console
.
log
(
res
);
},
testConnnectWifi
(){
uni
.
connectWifi
({});
uni
.
connectWifi
({
partialInfo
:
false
,
SSID
:
"
Xiaomi_20D0
"
,
password
:
"
BBBB
"
,
complete
:(
res
)
=>
{
console
.
log
(
res
);
}
});
},
testGetConnnectWifi
(){
uni
.
getConnectedWifi
({
...
...
uni_modules/uni-usercapturescreen/utssdk/app-android/index.uts
浏览文件 @
127fbd5f
...
...
@@ -45,7 +45,7 @@ class ScreenFileObserver extends FileObserver {
constructor(screenFile: string) {
super(
File(allScreen
))
super(
new File(screenFile
))
this.allScreen = new File(screenFile);
}
...
...
uni_modules/uni-wifi/utssdk/app-android/WifiConnector.uts
0 → 100644
浏览文件 @
127fbd5f
import WifiManager from "android.net.wifi.WifiManager";
import WifiConfiguration from 'android.net.wifi.WifiConfiguration';
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 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;
}
// 查看以前是否也配置过这个网络
function isExsits(SSID:string ,wifiManager:WifiManager):WifiConfiguration | null{
let existingConfigs = wifiManager.getConfiguredNetworks();
for (let existingConfig in existingConfigs) {
if (existingConfig.SSID.equals("\" + SSID + "\")) {
return existingConfig;
}
}
return null;
}
function createWifiInfo(SSID:string ,password:string):WifiConfiguration {
let config = new WifiConfiguration();
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
config.SSID = "\"" + SSID + "\"";
// // nopass
// // if (Type == WifiCipherType.WIFICIPHER_NOPASS) {
// // config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
// // }
// // // wep
// // if (Type == WifiCipherType.WIFICIPHER_WEP) {
// 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 (Type == WifiCipherType.WIFICIPHER_WPA) {
// 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;
}
class ConnectRunnable extends Runnable {
ssid:string = ""
password:string = ""
constructor(ssid:string,password:string) {
this.ssid = ssid
this.password = password
}
override run():void{
try {
// 打开wifi wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING
WifiConfiguration wifiConfig = createWifiInfo(ssid, password,
type);
WifiConfiguration tempConfig = isExsits(ssid);
if (tempConfig != null) {
wifiManager.removeNetwork(tempConfig.networkId);
}
int netID = wifiManager.addNetwork(wifiConfig);
boolean enabled = wifiManager.enableNetwork(netID, true);
boolean connected = wifiManager.reconnect();
} catch (Exception e) {
// TODO: handle exception
sendMsg(e.getMessage());
e.printStackTrace();
}
}
}
class WifiConnector {
wifiManager:WifiManager;
//WIFICIPHER_WEP是WEP ,WIFICIPHER_WPA是WPA,WIFICIPHER_NOPASS没有密码
// enum WifiCipherType {
// WIFICIPHER_WEP, WIFICIPHER_WPA, WIFICIPHER_NOPASS, WIFICIPHER_INVALID
// }
// 构造函数
constructor(wifiManager:WifiManager) {
this.wifiManager = wifiManager;
}
// 提供一个外部接口,传入要连接的无线网
connect(ssid:string ,password:string) {
Thread thread = new Thread(new ConnectRunnable(ssid,password));
thread.start();
}
}
uni_modules/uni-wifi/utssdk/app-android/index.uts
浏览文件 @
127fbd5f
...
...
@@ -12,6 +12,7 @@ import Gson from "com.google.gson.Gson";
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';
/**
* Wifi 函数通用入参封装
...
...
@@ -34,8 +35,26 @@ type WifiConnectOption = {
success?: (res: object) => void;
fail?: (res: object) => void;
complete?: (res: object) => void;
}
function wrapWifiConfiguration(SSID:string ,password:string):WifiConfiguration {
let config = new WifiConfiguration();
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
config.SSID = "\"" + SSID + "\"";
return config;
}
/**
* Wifi信息统一数据结构
*/
...
...
@@ -47,6 +66,9 @@ class UniWifiInfo {
signalStrength: Number = 0;
frequency: Number = 0;
/*下面的字段属于扩展字段*/
securityType:string = ""
constructor(scanResult?: ScanResult) {
if (scanResult != null) {
// 如果是通过扫描列表得到的数据,进行封装
...
...
@@ -64,6 +86,9 @@ class UniWifiInfo {
} else {
this.secure = true;
}
/*扩展字段*/
this.securityType = getSecurityType(scanResult);
}
}
...
...
@@ -120,6 +145,18 @@ class Global {
}
function getSecurityType(result:ScanResult):string {
if (result.capabilities.contains("WEP")) {
return "WEP";
} else if (result.capabilities.contains("PSK")) {
return "WPA";
} else if (result.capabilities.contains("EAP")) {
return "EAP";
}
return "NONE";
}
/**
* 自定义wifi变化广播监听器
*/
...
...
@@ -208,14 +245,12 @@ class CustomBroadcastReceiver extends BroadcastReceiver {
*/
export function getWifiList(option: WifiOption) {
if (Global.mReceiver == null) {
// 还没调用startWifi 提示报错
var result = {
errCode: 12000,
errMsg: "getWifiList:fail:not invoke startWifi",
errSubject:"uni-
wifi
"
errSubject:"uni-
getWifiList
"
}
option.fail?.(result)
option.complete?.(result)
...
...
@@ -259,7 +294,6 @@ export function offWifiConnected(callback: UTSCallback) {
* 注册Wifi列表的监听事件
*/
export function onGetWifiList(callback: UTSCallback) {
Global.getWifiListCallbackList.push(callback)
}
/**
...
...
@@ -272,15 +306,87 @@ export function offGetWifiList(callback: UTSCallback) {
}
}
export function connectWifi(_option: WifiConnectOption) {
// todo
console.log(_option)
if(_option.maunal == true){
// 指定了手动模式
let manunalIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
getUniActivity()!!.startActivity(manunalIntent);
/**
* 链接指定wifi
*/
export function connectWifi(option: WifiConnectOption) {
var result = {
errCode: 12000,
errMsg: "connectWifi:fail:not invoke startWifi",
errSubject:"uni-connectWifi"
}
if (Global.mReceiver == null || Global.scanList.length < 1) {
// 还没调用startWifi 提示报错
option.fail?.(result)
option.complete?.(result)
return
}
// if(option.maunal == true){
// // 指定了手动模式
// let manunalIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
// getUniActivity()!!.startActivity(manunalIntent);
// result.errCode = 0
// result.errMsg = "connectWifi:ok"
// option.success
// ?.(result)
// option.complete?.(result)
// return
// }
// 执行后续的逻辑
let scanWifiInfo:UniWifiInfo|null = null
for (let scanResult in Global.scanList) {
if (scanResult.SSID.equals(option.SSID)) {
scanWifiInfo = scanResult
}
}
if(scanWifiInfo == null){
// 不在扫描列表中返回错误
option.fail?.(result)
option.complete?.(result)
return
}
console.log(scanWifiInfo);
let wifiConfigration = wrapWifiConfiguration(option.SSID,option.password);
let wifiManager: WifiManager =
getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager
let targetExistConfig:WifiConfiguration|null = null
let existingConfigs = wifiManager.getConfiguredNetworks();
for (let existingConfig in existingConfigs) {
if (existingConfig.SSID.equals("\"" + option.SSID + "\"")) {
targetExistConfig = existingConfig
}
}
if (targetExistConfig != null) {
wifiManager.removeNetwork(targetExistConfig.networkId);
}
let netID = wifiManager.addNetwork(wifiConfigration);
let enabled = wifiManager.enableNetwork(netID, true);
let connected = wifiManager.reconnect();
console.log(connected);
}
/**
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录