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

android wifi connect ing

上级 5a6e3423
...@@ -35,7 +35,15 @@ ...@@ -35,7 +35,15 @@
console.log(res); console.log(res);
}, },
testConnnectWifi(){ testConnnectWifi(){
uni.connectWifi({});
uni.connectWifi({
partialInfo:false,
SSID:"Xiaomi_20D0",
password:"BBBB",
complete:(res)=>{
console.log(res);
}
});
}, },
testGetConnnectWifi(){ testGetConnnectWifi(){
uni.getConnectedWifi({ uni.getConnectedWifi({
......
...@@ -45,7 +45,7 @@ class ScreenFileObserver extends FileObserver { ...@@ -45,7 +45,7 @@ class ScreenFileObserver extends FileObserver {
constructor(screenFile: string) { constructor(screenFile: string) {
super(File(allScreen)) super(new File(screenFile))
this.allScreen = new File(screenFile); this.allScreen = new File(screenFile);
} }
......
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();
}
}
...@@ -12,6 +12,7 @@ import Gson from "com.google.gson.Gson"; ...@@ -12,6 +12,7 @@ import Gson from "com.google.gson.Gson";
import JSONObject from "com.alibaba.fastjson.JSONObject"; import JSONObject from "com.alibaba.fastjson.JSONObject";
import Intent from "android.content.Intent"; import Intent from "android.content.Intent";
import Thread from "java.lang.Thread"; import Thread from "java.lang.Thread";
import WifiConfiguration from 'android.net.wifi.WifiConfiguration';
/** /**
* Wifi 函数通用入参封装 * Wifi 函数通用入参封装
...@@ -34,8 +35,26 @@ type WifiConnectOption = { ...@@ -34,8 +35,26 @@ type WifiConnectOption = {
success?: (res: object) => void; success?: (res: object) => void;
fail?: (res: object) => void; fail?: (res: object) => void;
complete?: (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信息统一数据结构 * Wifi信息统一数据结构
*/ */
...@@ -46,6 +65,9 @@ class UniWifiInfo { ...@@ -46,6 +65,9 @@ class UniWifiInfo {
secure: boolean = false; secure: boolean = false;
signalStrength: Number = 0; signalStrength: Number = 0;
frequency: Number = 0; frequency: Number = 0;
/*下面的字段属于扩展字段*/
securityType:string = ""
constructor(scanResult?: ScanResult) { constructor(scanResult?: ScanResult) {
if (scanResult != null) { if (scanResult != null) {
...@@ -64,6 +86,9 @@ class UniWifiInfo { ...@@ -64,6 +86,9 @@ class UniWifiInfo {
} else { } else {
this.secure = true; this.secure = true;
} }
/*扩展字段*/
this.securityType = getSecurityType(scanResult);
} }
} }
...@@ -120,6 +145,18 @@ class Global { ...@@ -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变化广播监听器 * 自定义wifi变化广播监听器
*/ */
...@@ -208,14 +245,12 @@ class CustomBroadcastReceiver extends BroadcastReceiver { ...@@ -208,14 +245,12 @@ class CustomBroadcastReceiver extends BroadcastReceiver {
*/ */
export function getWifiList(option: WifiOption) { export function getWifiList(option: WifiOption) {
if (Global.mReceiver == null) { if (Global.mReceiver == null) {
// 还没调用startWifi 提示报错 // 还没调用startWifi 提示报错
var result = { var result = {
errCode: 12000, errCode: 12000,
errMsg: "getWifiList:fail:not invoke startWifi", errMsg: "getWifiList:fail:not invoke startWifi",
errSubject:"uni-wifi" errSubject:"uni-getWifiList"
} }
option.fail?.(result) option.fail?.(result)
option.complete?.(result) option.complete?.(result)
...@@ -259,7 +294,6 @@ export function offWifiConnected(callback: UTSCallback) { ...@@ -259,7 +294,6 @@ export function offWifiConnected(callback: UTSCallback) {
* 注册Wifi列表的监听事件 * 注册Wifi列表的监听事件
*/ */
export function onGetWifiList(callback: UTSCallback) { export function onGetWifiList(callback: UTSCallback) {
Global.getWifiListCallbackList.push(callback) Global.getWifiListCallbackList.push(callback)
} }
/** /**
...@@ -272,15 +306,87 @@ export function offGetWifiList(callback: UTSCallback) { ...@@ -272,15 +306,87 @@ export function offGetWifiList(callback: UTSCallback) {
} }
} }
export function connectWifi(_option: WifiConnectOption) {
// todo /**
console.log(_option) * 链接指定wifi
if(_option.maunal == true){ */
// 指定了手动模式 export function connectWifi(option: WifiConnectOption) {
let manunalIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
getUniActivity()!!.startActivity(manunalIntent);
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.
先完成此消息的编辑!
想要评论请 注册