|
@@ -9,6 +9,7 @@ import android.net.*
|
|
|
import android.net.NetworkInfo.DetailedState
|
|
|
import android.net.wifi.*
|
|
|
import android.os.Parcelable
|
|
|
+import android.util.Log
|
|
|
import com.develop.base.ext.appGlobalScope
|
|
|
import com.develop.base.util.TimeDownUtil
|
|
|
import kotlinx.coroutines.*
|
|
@@ -21,8 +22,10 @@ class WifiHelp {
|
|
|
private var timeDownUtil: TimeDownUtil? = null
|
|
|
private val mapWifi = mutableMapOf<String, Int>() //存wifi重连次数
|
|
|
private var currWifiSSID = ""
|
|
|
+ private var wifiData :WifiData ?=null
|
|
|
private fun init(build: Build) {
|
|
|
- mWifiManager = build.mContext?.applicationContext?.getSystemService(Context.WIFI_SERVICE) as WifiManager
|
|
|
+ mWifiManager =
|
|
|
+ build.mContext?.applicationContext?.getSystemService(Context.WIFI_SERVICE) as WifiManager
|
|
|
mBuild = build
|
|
|
build.mContext?.let {
|
|
|
registerReceiver(it)
|
|
@@ -71,7 +74,7 @@ class WifiHelp {
|
|
|
|
|
|
fun connect(wifiData: WifiData, isSaveType: Boolean) {
|
|
|
countTime()
|
|
|
- connectNew(wifiData, "",isSaveType)
|
|
|
+ connectNew(wifiData, "", isSaveType)
|
|
|
}
|
|
|
|
|
|
@SuppressLint("MissingPermission")
|
|
@@ -98,6 +101,7 @@ class WifiHelp {
|
|
|
WiFiPwdType.ESS -> {
|
|
|
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE)
|
|
|
}
|
|
|
+
|
|
|
WiFiPwdType.WAP -> {
|
|
|
wifiConfiguration.preSharedKey = addQuotationMarks(password)
|
|
|
wifiConfiguration.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN)
|
|
@@ -110,12 +114,14 @@ class WifiHelp {
|
|
|
wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA)
|
|
|
wifiConfiguration.status = WifiConfiguration.Status.ENABLED
|
|
|
}
|
|
|
+
|
|
|
WiFiPwdType.WEP -> {
|
|
|
wifiConfiguration.wepKeys[0] = addQuotationMarks(password)
|
|
|
wifiConfiguration.wepTxKeyIndex = 0;
|
|
|
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
|
|
|
wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
|
|
|
}
|
|
|
+
|
|
|
WiFiPwdType.WPA2_EAP -> {
|
|
|
//不支持企业WiFi密码
|
|
|
}
|
|
@@ -186,7 +192,6 @@ class WifiHelp {
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 断开当前连接的网络
|
|
|
*/
|
|
@@ -240,11 +245,15 @@ class WifiHelp {
|
|
|
// mWifiManager.reconnect()
|
|
|
}
|
|
|
}
|
|
|
+ mapWifi.forEach { (s, i) ->
|
|
|
+ mapWifi[s] = 0
|
|
|
+ }
|
|
|
+
|
|
|
+ mBuild?.mNetworkStateChangedListener?.invoke(NetworkInfo.DetailedState.FAILED)
|
|
|
}
|
|
|
|
|
|
fun removeSaveSSIDWifi(ssid: String) {
|
|
|
val wifiConfigurationList = mWifiManager.configuredNetworks
|
|
|
- mBuild?.mNetworkStateChangedListener?.invoke(NetworkInfo.DetailedState.FAILED)
|
|
|
for (item in wifiConfigurationList) {
|
|
|
if (item.SSID == addQuotationMarks(ssid)) {
|
|
|
mWifiManager.disconnect()
|
|
@@ -253,6 +262,11 @@ class WifiHelp {
|
|
|
// mWifiManager.reconnect()
|
|
|
}
|
|
|
}
|
|
|
+ mapWifi.forEach { (s, i) ->
|
|
|
+ mapWifi[s] = 0
|
|
|
+ }
|
|
|
+ mBuild?.mNetworkStateChangedListener?.invoke(NetworkInfo.DetailedState.FAILED)
|
|
|
+
|
|
|
}
|
|
|
|
|
|
@SuppressLint("MissingPermission")
|
|
@@ -352,7 +366,7 @@ class WifiHelp {
|
|
|
//在频繁的切换WiFi与重连WiFi底层会返回 unknown ssid, 这里将其排除掉
|
|
|
return null
|
|
|
}
|
|
|
- if (getStringId(wifiInfo.ipAddress)!="0.0.0.0"){
|
|
|
+ if (getStringId(wifiInfo.ipAddress) != "0.0.0.0") {
|
|
|
//处理重连问题有时候会重连 已成功num会往上涨 估要重新设置为0
|
|
|
mapWifi[removeQuotationMarks(wifiInfo.ssid)] = 0
|
|
|
}
|
|
@@ -582,26 +596,29 @@ class WifiHelp {
|
|
|
mBuild?.mErrorAuthenticating?.invoke("failed to connect")
|
|
|
|
|
|
// println("failed to connect :"+)
|
|
|
- if (currWifiSSID==""){
|
|
|
- return
|
|
|
- }
|
|
|
- if (mapWifi[currWifiSSID] != null) {
|
|
|
- mapWifi[currWifiSSID] = mapWifi[currWifiSSID]!! +1
|
|
|
- }else{
|
|
|
- mapWifi[currWifiSSID] = 0
|
|
|
- }
|
|
|
- //判断重连次数删除wifi
|
|
|
- if (mapWifi[currWifiSSID]!!>8){
|
|
|
- mapWifi[currWifiSSID] = 0
|
|
|
- removeSaveSSIDWifi(currWifiSSID)
|
|
|
- return
|
|
|
+ if (wifiData!=null&&wifiData?.ssid==currWifiSSID){
|
|
|
+ if (currWifiSSID==""){
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if (mapWifi[currWifiSSID] != null) {
|
|
|
+ Log.e("TAG WIFI","currWifiSSID :"+currWifiSSID+ " 次数:"+mapWifi[currWifiSSID])
|
|
|
+ mapWifi[currWifiSSID] = mapWifi[currWifiSSID]!! +1
|
|
|
+ }else{
|
|
|
+ mapWifi[currWifiSSID] = 0
|
|
|
+ }
|
|
|
+ //判断重连次数删除wifi
|
|
|
+ if (mapWifi[currWifiSSID]!!>6){
|
|
|
+ mapWifi[currWifiSSID] = 0
|
|
|
+ removeSaveSSIDWifi(currWifiSSID)
|
|
|
+ return
|
|
|
+ }
|
|
|
}
|
|
|
/**
|
|
|
* 之前由于save连接也会走这个回调,固然添加isSavePwd判断不走删除WiFi,
|
|
|
* 当第一次连接的时候,会导致密码正确,但isSavaPwd 返回false,固加多个password 不为空的情况下,也不清除
|
|
|
* */
|
|
|
currWifiData?.apply {
|
|
|
- if (this.isSavePwd||this.password.isNotEmpty()) {
|
|
|
+ if (this.isSavePwd || this.password.isNotEmpty()) {
|
|
|
} else {
|
|
|
removeSaveWifi(this)
|
|
|
}
|
|
@@ -609,7 +626,7 @@ class WifiHelp {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if (networkInfo.detailedState == NetworkInfo.DetailedState.BLOCKED){
|
|
|
+ if (networkInfo.detailedState == NetworkInfo.DetailedState.BLOCKED) {
|
|
|
}
|
|
|
if (networkInfo.detailedState == NetworkInfo.DetailedState.FAILED) {
|
|
|
mBuild?.mErrorAuthenticating?.invoke("failed to connect")
|
|
@@ -663,10 +680,10 @@ class WifiHelp {
|
|
|
} else null
|
|
|
}
|
|
|
|
|
|
- fun isNetworkConnected (context: Context) :Boolean{
|
|
|
+ fun isNetworkConnected(context: Context): Boolean {
|
|
|
var connectivityManager =
|
|
|
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
|
|
- var activeNetworkInfo = connectivityManager.activeNetworkInfo;
|
|
|
+ var activeNetworkInfo = connectivityManager.activeNetworkInfo;
|
|
|
return activeNetworkInfo != null && activeNetworkInfo.isConnected;
|
|
|
}
|
|
|
|
|
@@ -725,9 +742,20 @@ class WifiHelp {
|
|
|
}
|
|
|
return null
|
|
|
}
|
|
|
+
|
|
|
+ fun setConnectWifiDate(wifiData: WifiData) {
|
|
|
+ this.wifiData = wifiData
|
|
|
+ }
|
|
|
+
|
|
|
+ fun getConnectWifiDate(): WifiData? {
|
|
|
+ return this.wifiData
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* wifi数据
|
|
|
*/
|