|
@@ -9,7 +9,6 @@ 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.*
|
|
@@ -71,7 +70,7 @@ class WifiHelp {
|
|
|
|
|
|
fun connect(wifiData: WifiData, isSaveType: Boolean) {
|
|
|
countTime()
|
|
|
- connectNew(wifiData, "", isSaveType)
|
|
|
+ connectNew(wifiData, "",isSaveType)
|
|
|
}
|
|
|
|
|
|
@SuppressLint("MissingPermission")
|
|
@@ -98,7 +97,6 @@ class WifiHelp {
|
|
|
WiFiPwdType.ESS -> {
|
|
|
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE)
|
|
|
}
|
|
|
-
|
|
|
WiFiPwdType.WAP -> {
|
|
|
wifiConfiguration.preSharedKey = addQuotationMarks(password)
|
|
|
wifiConfiguration.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN)
|
|
@@ -111,14 +109,12 @@ 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密码
|
|
|
}
|
|
@@ -132,7 +128,8 @@ class WifiHelp {
|
|
|
@SuppressLint("MissingPermission")
|
|
|
fun connectNew(wifiData: WifiData, password: String, isSaveType: Boolean = false) {
|
|
|
// removeAllWifi()
|
|
|
- mWifiManager.disconnect()
|
|
|
+// mWifiManager.disconnect()
|
|
|
+ disconnect()
|
|
|
val configurationList = mWifiManager.configuredNetworks //已经保存密码的WiFi
|
|
|
val isSave = configurationList.any {
|
|
|
removeQuotationMarks(it.SSID) == wifiData.ssid
|
|
@@ -180,7 +177,6 @@ class WifiHelp {
|
|
|
}
|
|
|
}
|
|
|
var netId = mWifiManager.addNetwork(wifiConfiguration)
|
|
|
-// mWifiManager.enableNetwork(netId, true)
|
|
|
mWifiManager.enableNetwork(netId, true)
|
|
|
// mWifiManager.reconnect()
|
|
|
}
|
|
@@ -188,6 +184,7 @@ class WifiHelp {
|
|
|
}
|
|
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 断开当前连接的网络
|
|
|
*/
|
|
@@ -255,6 +252,7 @@ class WifiHelp {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ @SuppressLint("UnspecifiedRegisterReceiverFlag")
|
|
|
private fun registerReceiver(context: Context) {
|
|
|
val intentFilter = IntentFilter()
|
|
|
//当前连接WiFi状态的变化,这个监听是指当前已经连接WiFi的断开与重连的状态
|
|
@@ -311,7 +309,7 @@ class WifiHelp {
|
|
|
formatLevel(scanResult.level),
|
|
|
scanResult.BSSID,
|
|
|
"",
|
|
|
- securityType(scanResult.capabilities), configuration != null, isConnected = isConnected(scanResult.SSID)
|
|
|
+ securityType(scanResult.capabilities), configuration != null
|
|
|
)
|
|
|
wifiList.add(wifiData)
|
|
|
}
|
|
@@ -330,19 +328,6 @@ class WifiHelp {
|
|
|
return wifiList
|
|
|
}
|
|
|
|
|
|
- open fun isConnected(ssid: String): Boolean {
|
|
|
-
|
|
|
- val wifiInfo = mWifiManager.connectionInfo ?: return false
|
|
|
- return when (wifiInfo.supplicantState) {
|
|
|
- SupplicantState.AUTHENTICATING, SupplicantState.ASSOCIATING, SupplicantState.ASSOCIATED, SupplicantState.FOUR_WAY_HANDSHAKE, SupplicantState.GROUP_HANDSHAKE, SupplicantState.COMPLETED -> wifiInfo.ssid.replace(
|
|
|
- "\"",
|
|
|
- ""
|
|
|
- ) == ssid
|
|
|
-
|
|
|
- else -> false
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 获取当前连接的WiFi网络
|
|
|
*/
|
|
@@ -370,15 +355,11 @@ class WifiHelp {
|
|
|
appGlobalScope.launch(Dispatchers.IO) {
|
|
|
val list = getScanDevice()
|
|
|
val connectionWifi = getConnectionWifi()
|
|
|
-
|
|
|
-
|
|
|
withContext(Dispatchers.Main) {
|
|
|
timeDownUtil?.cancel()
|
|
|
mBuild?.mScanCallback?.invoke(list)
|
|
|
mBuild?.mAlreadyConnectionCallback?.invoke(connectionWifi)
|
|
|
|
|
|
-
|
|
|
-
|
|
|
//重连机制
|
|
|
var isWifiConnected = false
|
|
|
list.forEach {
|
|
@@ -389,7 +370,7 @@ class WifiHelp {
|
|
|
|
|
|
|
|
|
var reconnectWifi =
|
|
|
- if (!isWifiConnected) list.filter { filter -> filter.isConnected && filter.isSavePwd } else null
|
|
|
+ if (!isWifiConnected) list.filter { filter -> !filter.isConnected && filter.isSavePwd } else null
|
|
|
|
|
|
if (reconnectWifi!!.isNotEmpty()) {
|
|
|
initWork()
|
|
@@ -578,13 +559,14 @@ class WifiHelp {
|
|
|
}
|
|
|
if (networkInfo.detailedState == NetworkInfo.DetailedState.DISCONNECTED) {
|
|
|
mBuild?.mErrorAuthenticating?.invoke("failed to connect")
|
|
|
-
|
|
|
+ println("WifiHelp DetailedState.DISCONNECTED: ")
|
|
|
/**
|
|
|
* 之前由于save连接也会走这个回调,固然添加isSavePwd判断不走删除WiFi,
|
|
|
* 当第一次连接的时候,会导致密码正确,但isSavaPwd 返回false,固加多个password 不为空的情况下,也不清除
|
|
|
* */
|
|
|
currWifiData?.apply {
|
|
|
- if (this.isSavePwd || this.password.isNotEmpty()) {
|
|
|
+ println("WifiHelp DetailedState.DISCONNECTED: isSavePwd-->"+this.isSavePwd+" password:"+this.password)
|
|
|
+ if (this.isSavePwd||this.password.isNotEmpty()) {
|
|
|
} else {
|
|
|
removeSaveWifi(this)
|
|
|
}
|
|
@@ -592,7 +574,7 @@ class WifiHelp {
|
|
|
}
|
|
|
if (networkInfo.detailedState == NetworkInfo.DetailedState.FAILED) {
|
|
|
mBuild?.mErrorAuthenticating?.invoke("failed to connect")
|
|
|
-
|
|
|
+ println("WifiHelp DetailedState.FAILED: FAILED FAILED FAILED-->")
|
|
|
currWifiData?.apply {
|
|
|
removeSaveWifi(this)
|
|
|
}
|
|
@@ -610,6 +592,7 @@ class WifiHelp {
|
|
|
if (intent.action == WifiManager.SUPPLICANT_STATE_CHANGED_ACTION) {
|
|
|
val linkWifiResult = intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1)
|
|
|
if (linkWifiResult == WifiManager.ERROR_AUTHENTICATING) {
|
|
|
+
|
|
|
/**
|
|
|
* 密码错误的回调方法
|
|
|
* */
|
|
@@ -640,7 +623,6 @@ class WifiHelp {
|
|
|
getActiveNetworkInfo()?.detailedState
|
|
|
} else null
|
|
|
}
|
|
|
-
|
|
|
/**
|
|
|
* 获取当前网络信息
|
|
|
*/
|