Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
uni-api
提交
b20d5872
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看板
提交
b20d5872
编写于
12月 03, 2022
作者:
杜庆泉
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
connect wifi done
上级
58a58d67
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
84 addition
and
28 deletion
+84
-28
manifest.json
manifest.json
+1
-1
pages/index/index.vue
pages/index/index.vue
+1
-0
uni_modules/uni-wifi/utssdk/app-android/index.uts
uni_modules/uni-wifi/utssdk/app-android/index.uts
+82
-27
未找到文件。
manifest.json
浏览文件 @
b20d5872
...
@@ -68,5 +68,5 @@
...
@@ -68,5 +68,5 @@
"uniStatistics"
:
{
"uniStatistics"
:
{
"enable"
:
false
"enable"
:
false
},
},
"vueVersion"
:
"
3
"
"vueVersion"
:
"
2
"
}
}
pages/index/index.vue
浏览文件 @
b20d5872
...
@@ -46,6 +46,7 @@
...
@@ -46,6 +46,7 @@
// });
// });
uni
.
connectWifi
({
uni
.
connectWifi
({
maunal
:
false
,
maunal
:
false
,
partialInfo
:
false
,
SSID
:
"
Xiaomi_20D0
"
,
SSID
:
"
Xiaomi_20D0
"
,
password
:
"
streamApp!2016
"
,
password
:
"
streamApp!2016
"
,
complete
:(
res
)
=>
{
complete
:(
res
)
=>
{
...
...
uni_modules/uni-wifi/utssdk/app-android/index.uts
浏览文件 @
b20d5872
...
@@ -45,6 +45,7 @@ type WifiConnectOption = {
...
@@ -45,6 +45,7 @@ type WifiConnectOption = {
BSSID:string;
BSSID:string;
password:string;
password:string;
maunal:boolean;
maunal:boolean;
// 只返回ssid
partialInfo:boolean;
partialInfo:boolean;
success?: (res: object) => void;
success?: (res: object) => void;
fail?: (res: object) => void;
fail?: (res: object) => void;
...
@@ -157,10 +158,28 @@ class UniWifiInfo {
...
@@ -157,10 +158,28 @@ class UniWifiInfo {
//Android返回的值是-100~0,而微信API规范是0~100,值越大信号越好,需要+100拉齐
//Android返回的值是-100~0,而微信API规范是0~100,值越大信号越好,需要+100拉齐
this.signalStrength = connectInfo.getRssi() + 100;
this.signalStrength = connectInfo.getRssi() + 100;
this.frequency = connectInfo.getFrequency();
this.frequency = connectInfo.getFrequency();
}
}
// toUTSJSON(partialInfo:boolean):UTSJSONObject{
// if(partialInfo){
// var retWifi = {
// SSID:this.SSID
// }
// return retWifi
// }else{
// var retWifi = {
// SSID:this.SSID,
// BSSID:this.BSSID,
// secure:this.secure,
// signalStrength:this.signalStrength,
// frequency:this.frequency
// }
// return retWifi
// }
// }
}
}
/**
/**
...
@@ -170,13 +189,13 @@ class UniWifiInfo {
...
@@ -170,13 +189,13 @@ class UniWifiInfo {
function wrapWifiConfiguration(SSID:string ,password:string,passwordType:string):WifiConfiguration {
function wrapWifiConfiguration(SSID:string ,password:string,passwordType:string):WifiConfiguration {
let config = new WifiConfiguration();
let config = new WifiConfiguration();
config.status = WifiConfiguration.Status.ENABLED;
config.allowedAuthAlgorithms.clear();
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
config.allowedProtocols.clear();
config.SSID = "\""
+ SSID + "\""
;
config.SSID = "\""
.concat(SSID).concat("\"")
;
// nopass
// nopass
...
@@ -503,7 +522,7 @@ export function connectWifi(option: WifiConnectOption) {
...
@@ -503,7 +522,7 @@ export function connectWifi(option: WifiConnectOption) {
var result = {
var result = {
errCode: 12000,
errCode: 12000,
errMsg: "connectWifi:fail:not invoke startWifi",
errMsg: "connectWifi:fail:not invoke startWifi",
errSubject:"uni-connectWifi"
errSubject:"uni-connectWifi"
,
}
}
if (Global.mReceiver == null || Global.scanList.length < 1) {
if (Global.mReceiver == null || Global.scanList.length < 1) {
...
@@ -540,15 +559,13 @@ export function connectWifi(option: WifiConnectOption) {
...
@@ -540,15 +559,13 @@ export function connectWifi(option: WifiConnectOption) {
// 不在扫描列表中返回错误
// 不在扫描列表中返回错误
option.fail?.(result)
option.fail?.(result)
option.complete?.(result)
option.complete?.(result)
return
return
}
}
console.log(JSON.stringify(scanWifiInfo.securityType));
let wifiConfigration = wrapWifiConfiguration(scanWifiInfo.SSID,option.password,scanWifiInfo.securityType);
wifiConfigration.BSSID = scanWifiInfo.BSSID
let wifiConfigration = wrapWifiConfiguration(option.SSID,option.password,scanWifiInfo.securityType);
console.log("option.password === " + option.password);
console.log("wifiConfigration.BSSID === " + wifiConfigration.BSSID);
console.log("wifiConfigration == " + wifiConfigration);
let wifiManager: WifiManager =
let wifiManager: WifiManager =
UTSAndroid.getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager
UTSAndroid.getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager
...
@@ -556,35 +573,45 @@ export function connectWifi(option: WifiConnectOption) {
...
@@ -556,35 +573,45 @@ export function connectWifi(option: WifiConnectOption) {
let targetExistConfig:WifiConfiguration|null = null
let targetExistConfig:WifiConfiguration|null = null
let existingConfigs = wifiManager.getConfiguredNetworks();
let existingConfigs = wifiManager.getConfiguredNetworks();
for (let existingConfig in existingConfigs) {
for (let existingConfig in existingConfigs) {
if (existingConfig.SSID.equals("\"" + option.SSID + "\"")) {
if (existingConfig.SSID.equals("\"" + option.SSID + "\"")) {
targetExistConfig = existingConfig
targetExistConfig = existingConfig
}
}
}
}
// 如果wifi已经保存了当前ssid的配置,可能是别的应用添加的。android系统要求,需要删除掉重新添加
// 如果wifi已经保存了当前ssid的配置,可能是别的应用添加的。android系统要求,需要删除掉重新添加
console.log("targetExistConfig == " + targetExistConfig);
if (targetExistConfig != null) {
if (targetExistConfig != null) {
wifiManager.removeNetwork(targetExistConfig.networkId);
console.log("targetExistConfig.networkId === " + targetExistConfig.networkId);
let removeRet = wifiManager.removeNetwork(targetExistConfig.networkId);
if(!removeRet){
// 移除之前的配置失败了,返回错误,需要用户手动取消保存一下
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)
}else{
wifiManager.removeNetwork(currentConnect.networkId)
}
}
wifiManager.disconnect()
let connected = false;
let connected = false;
try {
try {
let currentConnect = wifiManager.getConnectionInfo()
if(currentConnect.networkId >= 0){
wifiManager.disableNetwork(currentConnect.networkId)
}
wifiManager.disconnect()
console.log("wifiConfigration" + wifiConfigration);
let netID = wifiManager.addNetwork(wifiConfigration);
let netID = wifiManager.addNetwork(wifiConfigration);
// 如果-1 说明没添加上,报错即可
// 如果-1 说明没添加上,报错即可
console.log("netID === " + netID);
if(netID < 0){
if(netID < 0){
result.errCode = 12002
result.errCode = 12002
result.errMsg = "connectWifi:password error Wi-Fi"
result.errMsg = "connectWifi:password error Wi-Fi"
option.fail?.(result)
option.fail?.(result)
option.complete?.(result)
option.complete?.(result)
...
@@ -593,16 +620,23 @@ export function connectWifi(option: WifiConnectOption) {
...
@@ -593,16 +620,23 @@ export function connectWifi(option: WifiConnectOption) {
let enabled = wifiManager.enableNetwork(netID, true);
let enabled = wifiManager.enableNetwork(netID, true);
console.log("enabled === " + enabled);
if(!enabled){
result.errCode = 12007
result.errMsg = "connectWifi:user denied"
option.fail?.(result)
option.complete?.(result)
return
}
connected = wifiManager.reconnect();
connected = wifiManager.reconnect();
console.log("connected === " + connected);
} catch (e) {
} catch (e) {
connected = false;
connected = false;
console.log(e);
console.log(e);
}
}
if(!connected){
if(!connected){
// 出错了,返回错误
// 出错了,返回错误
// 兜底的报错
// 兜底的报错
...
@@ -614,13 +648,23 @@ export function connectWifi(option: WifiConnectOption) {
...
@@ -614,13 +648,23 @@ export function connectWifi(option: WifiConnectOption) {
return
return
}
}
wifiManager.saveConfiguration()
//scanWifiInfo 根据 partialInfo 填充给返回字段
if(option.partialInfo){
result['wifi'] = {
SSID:scanWifiInfo.SSID
}
}else{
result['wifi'] = scanWifiInfo
}
// result.wifi = scanWifiInfo.toUTSJSON(option.partialInfo)
result.errCode = 0
result.errCode = 0
result.errMsg = "connectWifi:ok"
result.errMsg = "connectWifi:ok"
option.success?.(result)
option.success?.(result)
option.complete?.(result)
option.complete?.(result)
}
}
...
@@ -700,9 +744,20 @@ export function getConnectedWifi(option: GetConnectedWifiOptions) {
...
@@ -700,9 +744,20 @@ export function getConnectedWifi(option: GetConnectedWifiOptions) {
// 测试android 12上可以使用
// 测试android 12上可以使用
//@ts-ignore
//@ts-ignore
const winfo = wm.getConnectionInfo();
const winfo = wm.getConnectionInfo();
// 判断一下是否wifi 关闭了
// 封装成数据对象
// 封装成数据对象
wifiInfo.wrapConnectInfo(winfo);
wifiInfo.wrapConnectInfo(winfo);
if(wifiInfo.BSSID == null || zeroCountNum(wifiInfo.BSSID) > 3){
res.errCode = 12005
res.errMsg = "getConnectedWifi:fail:wifi is disable"
res.wifi = wifiInfo;
option.fail?.(res)
option.complete?.(res)
return
}
res.errCode = 0
res.errCode = 0
res.errMsg = "getConnectedWifi:ok"
res.errMsg = "getConnectedWifi:ok"
res.wifi = wifiInfo;
res.wifi = wifiInfo;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录