提交 b20d5872 编写于 作者: 杜庆泉's avatar 杜庆泉

connect wifi done

上级 58a58d67
...@@ -68,5 +68,5 @@ ...@@ -68,5 +68,5 @@
"uniStatistics" : { "uniStatistics" : {
"enable" : false "enable" : false
}, },
"vueVersion" : "3" "vueVersion" : "2"
} }
...@@ -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)=>{
......
...@@ -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,31 +573,41 @@ export function connectWifi(option: WifiConnectOption) { ...@@ -556,31 +573,41 @@ 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 connected = false; }
try {
let currentConnect = wifiManager.getConnectionInfo() let currentConnect = wifiManager.getConnectionInfo()
if(currentConnect.networkId >= 0){ if(currentConnect.networkId >= 0){
wifiManager.disableNetwork(currentConnect.networkId) wifiManager.disableNetwork(currentConnect.networkId)
}else{
wifiManager.removeNetwork(currentConnect.networkId)
} }
wifiManager.disconnect() wifiManager.disconnect()
console.log("wifiConfigration" + wifiConfigration); let connected = false;
try {
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"
...@@ -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.
先完成此消息的编辑!
想要评论请 注册