Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
fxy060608
uni-api
提交
9d4c5adb
U
uni-api
项目概览
fxy060608
/
uni-api
与 Fork 源项目一致
Fork自
DCloud / uni-api
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-api
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
9d4c5adb
编写于
12月 03, 2022
作者:
杜庆泉
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
connect wifi 功能实现
上级
fac7d96b
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
204 addition
and
149 deletion
+204
-149
pages/index/index.vue
pages/index/index.vue
+9
-1
uni_modules/uni-wifi/utssdk/app-android/index.uts
uni_modules/uni-wifi/utssdk/app-android/index.uts
+195
-148
未找到文件。
pages/index/index.vue
浏览文件 @
9d4c5adb
...
...
@@ -36,10 +36,18 @@
},
testConnnectWifi
(){
// uni.connectWifi({
// maunal:false,
// SSID:"Xiaomi_20D0",
// password:"BBBB",
// complete:(res)=>{
// console.log(res);
// }
// });
uni
.
connectWifi
({
maunal
:
false
,
SSID
:
"
Xiaomi_20D0
"
,
password
:
"
BBBB
"
,
password
:
"
streamApp!2016
"
,
complete
:(
res
)
=>
{
console
.
log
(
res
);
}
...
...
uni_modules/uni-wifi/utssdk/app-android/index.uts
浏览文件 @
9d4c5adb
...
...
@@ -27,6 +27,15 @@ type WifiOption = {
};
/**
* 获取当前链接的wifi信息
*/
type GetConnectedWifiOptions = {
partialInfo?: boolean
success?: (res: UTSJSONObject) => void
fail?: (res: UTSJSONObject) => void
complete?: (res: UTSJSONObject) => void
}
/**
* Wifi 链接参数封装
...
...
@@ -42,6 +51,24 @@ type WifiConnectOption = {
complete?: (res: object) => void;
}
/**
* 全局数据储存
*/
class Global {
static mReceiver: CustomBroadcastReceiver|null = null;
static WIFI_AUTH_OPEN: string = "";
static WIFI_AUTH_ROAM: String = "[ESS]";
// 扫描wifi结果
static scanList: UniWifiInfo[] = []
// 获取wifi列表监听
static getWifiListCallbackList: UTSCallback[] = []
// wifi链接监听
static onWifiConnectCallbackList: UTSCallback[] = []
}
/**
* 是否是标准的16进制字符
*/
...
...
@@ -71,53 +98,7 @@ function isHexWepKey(wepKey:string):boolean {
return isHex(wepKey);
}
@Suppress("DEPRECATION")
function wrapWifiConfiguration(SSID:string ,password:string,passwordType: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 ("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] = "\"".concat(password).concat("\"");
}
}
config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
config.allowedKeyManagement.set(KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
}
// wpa
if ("WPA".equals(passwordType)) {
config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.preSharedKey = "\"".concat(password).concat("\"");
}
return config;
}
/**
* Wifi信息统一数据结构
*/
...
...
@@ -180,35 +161,63 @@ class UniWifiInfo {
}
}
/**
*
获取当前链接的wifi信息
*
连接wifi时使用,根据用户输入内容包装为系统需要的wifi配置对象
*/
type GetConnectedWifiOptions = {
partialInfo?: boolean
success?: (res: UTSJSONObject) => void
fail?: (res: UTSJSONObject) => void
complete?: (res: UTSJSONObject) => void
@Suppress("DEPRECATION")
function wrapWifiConfiguration(SSID:string ,password:string,passwordType: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 ("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] = "\"".concat(password).concat("\"");
}
}
config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
config.allowedKeyManagement.set(KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
}
// wpa
if ("WPA".equals(passwordType)) {
config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.preSharedKey = "\"".concat(password).concat("\"");
}
return config;
}
/**
*
全局数据储存
*
判断当前wifi的加密类型
*/
class Global {
static mReceiver: CustomBroadcastReceiver|null = null;
static WIFI_AUTH_OPEN: string = "";
static WIFI_AUTH_ROAM: String = "[ESS]";
static scanList: UniWifiInfo[] = []
static getWifiListCallbackList: UTSCallback[] = []
static onWifiConnectCallbackList: UTSCallback[] = []
}
function getSecurityType(result:ScanResult):string {
if (result.capabilities.contains("WEP")) {
return "WEP";
...
...
@@ -246,7 +255,6 @@ class CustomBroadcastReceiver extends BroadcastReceiver {
//做一些异步操作
setTimeout(function() {
// BroadcastReceiver 中不能执行耗时任务,需要使用setTimeout
console.log(Thread.currentThread().getName())
// @ts-ignore
let winfo = this.mWifiManager!.getConnectionInfo();
while (winfo.bssid == null) {
...
...
@@ -305,19 +313,98 @@ class CustomBroadcastReceiver extends BroadcastReceiver {
/************************* 下面是对外提供的函数 *************************/
/**
* 开启wifi
*/
@Suppress("DEPRECATION")
export function startWifi(option: WifiOption) {
// 需要先开启wifi,才能使用后续的功能
let requestCode = 1001;
let permissionWifi = arrayOf("android.permission.ACCESS_FINE_LOCATION");
var result = {
errCode: 12001,
errMsg: "startWifi:premission loss",
errSubject:"uni-startWifi"
}
// 检查权限
if (ActivityCompat.checkSelfPermission(UTSAndroid.getUniActivity()!, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(UTSAndroid.getUniActivity()!, permissionWifi, requestCode)
// 尚不具备权限,返回错误
option.fail?.(result)
option.complete?.(result)
return;
}
// 具备了权限,继续前进
let wifiManager: WifiManager =
UTSAndroid.getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager
// 用户没有开启wifi 总开关
if(!wifiManager.isWifiEnabled()){
// wifi 没开启
result.errCode = 12005;
result.errMsg = "wifi not turned on";
option.fail?.(result);
option.complete?.(result);
return;
}
// 初始化wifi 状态广播监听,后续所有的api,均基于此
if (Global.mReceiver == null) {
Global.mReceiver = new CustomBroadcastReceiver(wifiManager)
}
let filter = new IntentFilter()
filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)
// @ts-ignore
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION)
// @ts-ignore
filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
UTSAndroid.getUniActivity()!.registerReceiver(Global.mReceiver, filter)
/**
* activity 被销毁时,取消注册
*/
UTSAndroid.onAppActivityDestroy(function(){
if(Global.mReceiver!= null){
UTSAndroid.getUniActivity()!.unregisterReceiver(Global.mReceiver)
Global.mReceiver = null
Global.scanList = []
Global.getWifiListCallbackList = []
Global.onWifiConnectCallbackList = []
}
});
// 开始扫描
wifiManager.startScan()
result.errCode = 0
result.errMsg = "startWifi:ok"
option.success?.(result)
option.complete?.(result)
}
/**
* 获取wifi列表
*/
@Suppress("DEPRECATION")
export function getWifiList(option: WifiOption) {
var result = {
errCode: 12000,
errMsg: "getWifiList:fail:not invoke startWifi",
errSubject:"uni-getWifiList"
}
if (Global.mReceiver == null) {
// 还没调用startWifi 提示报错
var result = {
errCode: 12000,
errMsg: "getWifiList:fail:not invoke startWifi",
errSubject:"uni-getWifiList"
}
option.fail?.(result)
option.complete?.(result)
...
...
@@ -326,6 +413,7 @@ export function getWifiList(option: WifiOption) {
let wifiManager: WifiManager =
UTSAndroid.getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager
wifiManager.startScan()
let ret = {
...
...
@@ -428,6 +516,7 @@ export function connectWifi(option: WifiConnectOption) {
let wifiConfigration = wrapWifiConfiguration(option.SSID,option.password,scanWifiInfo.securityType);
console.log("wifiConfigration == " + wifiConfigration);
let wifiManager: WifiManager =
UTSAndroid.getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager
...
...
@@ -435,109 +524,67 @@ export function connectWifi(option: WifiConnectOption) {
let targetExistConfig:WifiConfiguration|null = null
let existingConfigs = wifiManager.getConfiguredNetworks();
for (let existingConfig in existingConfigs) {
if (existingConfig.SSID.equals("\"" + option.SSID + "\"")) {
targetExistConfig = existingConfig
}
}
// 如果wifi已经保存了当前ssid的配置,可能是别的应用添加的。android系统要求,需要删除掉重新添加
console.log("targetExistConfig == " + targetExistConfig);
if (targetExistConfig != null) {
wifiManager.removeNetwork(targetExistConfig.networkId);
}
let connected = false;
try {
console.log(wifiConfigration);
let currentConnect = wifiManager.getConnectionInfo()
if(currentConnect.networkId >= 0){
wifiManager.disableNetwork(currentConnect.networkId)
}
wifiManager.disconnect()
console.log("wifiConfigration" + wifiConfigration);
let netID = wifiManager.addNetwork(wifiConfigration);
// 如果-1 说明没添加上,报错即可
console.log("netID === " + netID);
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);
console.log("enabled === " + enabled);
let
connected = wifiManager.reconnect();
connected = wifiManager.reconnect();
console.log("connected === " + connected);
} catch (e) {
connected = false;
// TODO: handle exception
console.log(e);
// e.printStackTrace();
}
result.errCode = 0
result.errMsg = "connectWifi:ok"
option.success?.(result)
option.complete?.(result)
}
/**
* 开启wifi
*/
@Suppress("DEPRECATION")
export function startWifi(option: WifiOption) {
// 需要先开启wifi,才能使用后续的功能
let requestCode = 1001;
let permissionWifi = arrayOf("android.permission.ACCESS_FINE_LOCATION");
var result = {
errCode: 12001,
errMsg: "startWifi:premission loss",
errSubject:"uni-startWifi"
if(!connected){
// 出错了,返回错误
}
// 检查权限
if (ActivityCompat.checkSelfPermission(UTSAndroid.getUniActivity()!, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(UTSAndroid.getUniActivity()!, permissionWifi, requestCode)
// 尚不具备权限,返回错误
option.fail?.(result)
option.complete?.(result)
return;
}
// 具备了权限,继续前进
let wifiManager: WifiManager =
UTSAndroid.getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager
if (Global.mReceiver == null) {
Global.mReceiver = new CustomBroadcastReceiver(wifiManager)
}
let filter = new IntentFilter()
filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)
// @ts-ignore
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION)
// @ts-ignore
filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
UTSAndroid.getUniActivity()!.registerReceiver(Global.mReceiver, filter)
/**
* activity 被销毁时,取消注册
*/
UTSAndroid.onAppActivityDestroy(function(){
if(Global.mReceiver!= null){
UTSAndroid.getUniActivity()!.unregisterReceiver(Global.mReceiver)
Global.mReceiver = null
Global.scanList = []
Global.getWifiListCallbackList = []
Global.onWifiConnectCallbackList = []
console.log("UTSAndroid.getUniActivity() == " + UTSAndroid.getUniActivity());
console.log("Global.mReceiver == " + Global.mReceiver);
}
});
wifiManager.startScan()
result.errCode = 0
result.errMsg = "
star
tWifi:ok"
result.errMsg = "
connec
tWifi:ok"
option.success?.(result)
option.complete?.(result)
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录