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

connect wifi 功能实现

上级 fac7d96b
...@@ -36,10 +36,18 @@ ...@@ -36,10 +36,18 @@
}, },
testConnnectWifi(){ testConnnectWifi(){
// uni.connectWifi({
// maunal:false,
// SSID:"Xiaomi_20D0",
// password:"BBBB",
// complete:(res)=>{
// console.log(res);
// }
// });
uni.connectWifi({ uni.connectWifi({
maunal:false, maunal:false,
SSID:"Xiaomi_20D0", SSID:"Xiaomi_20D0",
password:"BBBB", password:"streamApp!2016",
complete:(res)=>{ complete:(res)=>{
console.log(res); console.log(res);
} }
......
...@@ -27,6 +27,15 @@ type WifiOption = { ...@@ -27,6 +27,15 @@ type WifiOption = {
}; };
/**
* 获取当前链接的wifi信息
*/
type GetConnectedWifiOptions = {
partialInfo?: boolean
success?: (res: UTSJSONObject) => void
fail?: (res: UTSJSONObject) => void
complete?: (res: UTSJSONObject) => void
}
/** /**
* Wifi 链接参数封装 * Wifi 链接参数封装
...@@ -42,6 +51,24 @@ type WifiConnectOption = { ...@@ -42,6 +51,24 @@ type WifiConnectOption = {
complete?: (res: object) => void; 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进制字符 * 是否是标准的16进制字符
*/ */
...@@ -71,53 +98,7 @@ function isHexWepKey(wepKey:string):boolean { ...@@ -71,53 +98,7 @@ function isHexWepKey(wepKey:string):boolean {
return isHex(wepKey); 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信息统一数据结构 * Wifi信息统一数据结构
*/ */
...@@ -180,35 +161,63 @@ class UniWifiInfo { ...@@ -180,35 +161,63 @@ class UniWifiInfo {
} }
} }
/** /**
* 获取当前链接的wifi信息 * 连接wifi时使用,根据用户输入内容包装为系统需要的wifi配置对象
*/ */
type GetConnectedWifiOptions = { @Suppress("DEPRECATION")
partialInfo?: boolean function wrapWifiConfiguration(SSID:string ,password:string,passwordType:string):WifiConfiguration {
success?: (res: UTSJSONObject) => void
fail?: (res: UTSJSONObject) => void let config = new WifiConfiguration();
complete?: (res: UTSJSONObject) => void
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 { function getSecurityType(result:ScanResult):string {
if (result.capabilities.contains("WEP")) { if (result.capabilities.contains("WEP")) {
return "WEP"; return "WEP";
...@@ -246,7 +255,6 @@ class CustomBroadcastReceiver extends BroadcastReceiver { ...@@ -246,7 +255,6 @@ class CustomBroadcastReceiver extends BroadcastReceiver {
//做一些异步操作 //做一些异步操作
setTimeout(function() { setTimeout(function() {
// BroadcastReceiver 中不能执行耗时任务,需要使用setTimeout // BroadcastReceiver 中不能执行耗时任务,需要使用setTimeout
console.log(Thread.currentThread().getName())
// @ts-ignore // @ts-ignore
let winfo = this.mWifiManager!.getConnectionInfo(); let winfo = this.mWifiManager!.getConnectionInfo();
while (winfo.bssid == null) { while (winfo.bssid == null) {
...@@ -305,19 +313,98 @@ class CustomBroadcastReceiver extends BroadcastReceiver { ...@@ -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列表 * 获取wifi列表
*/ */
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
export function getWifiList(option: WifiOption) { export function getWifiList(option: WifiOption) {
var result = {
errCode: 12000,
errMsg: "getWifiList:fail:not invoke startWifi",
errSubject:"uni-getWifiList"
}
if (Global.mReceiver == null) { if (Global.mReceiver == null) {
// 还没调用startWifi 提示报错 // 还没调用startWifi 提示报错
var result = {
errCode: 12000,
errMsg: "getWifiList:fail:not invoke startWifi",
errSubject:"uni-getWifiList"
}
option.fail?.(result) option.fail?.(result)
option.complete?.(result) option.complete?.(result)
...@@ -326,6 +413,7 @@ export function getWifiList(option: WifiOption) { ...@@ -326,6 +413,7 @@ export function getWifiList(option: WifiOption) {
let wifiManager: WifiManager = let wifiManager: WifiManager =
UTSAndroid.getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager UTSAndroid.getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager
wifiManager.startScan() wifiManager.startScan()
let ret = { let ret = {
...@@ -428,6 +516,7 @@ export function connectWifi(option: WifiConnectOption) { ...@@ -428,6 +516,7 @@ export function connectWifi(option: WifiConnectOption) {
let wifiConfigration = wrapWifiConfiguration(option.SSID,option.password,scanWifiInfo.securityType); let wifiConfigration = wrapWifiConfiguration(option.SSID,option.password,scanWifiInfo.securityType);
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
...@@ -435,109 +524,67 @@ export function connectWifi(option: WifiConnectOption) { ...@@ -435,109 +524,67 @@ 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系统要求,需要删除掉重新添加
console.log("targetExistConfig == " + targetExistConfig);
if (targetExistConfig != null) { if (targetExistConfig != null) {
wifiManager.removeNetwork(targetExistConfig.networkId); wifiManager.removeNetwork(targetExistConfig.networkId);
} }
let connected = false;
try { 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); let netID = wifiManager.addNetwork(wifiConfigration);
// 如果-1 说明没添加上,报错即可
console.log("netID === " + netID); 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); let enabled = wifiManager.enableNetwork(netID, true);
console.log("enabled === " + enabled); console.log("enabled === " + enabled);
let connected = wifiManager.reconnect(); connected = wifiManager.reconnect();
console.log("connected === " + connected); console.log("connected === " + connected);
} catch (e) { } catch (e) {
connected = false;
// TODO: handle exception // TODO: handle exception
console.log(e); console.log(e);
// e.printStackTrace(); // e.printStackTrace();
} }
result.errCode = 0
result.errMsg = "connectWifi:ok"
option.success?.(result)
option.complete?.(result)
} if(!connected){
// 出错了,返回错误
/**
* 开启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
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.errCode = 0
result.errMsg = "startWifi:ok" result.errMsg = "connectWifi:ok"
option.success?.(result) option.success?.(result)
option.complete?.(result) option.complete?.(result)
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册