|
@@ -9,7 +9,6 @@ import android.net.*
|
|
|
import android.net.NetworkInfo.DetailedState
|
|
|
import android.net.wifi.*
|
|
|
import android.os.Parcelable
|
|
|
-import android.os.SystemClock
|
|
|
import android.util.Log
|
|
|
import com.develop.base.ext.appGlobalScope
|
|
|
import com.develop.base.ext.getSN
|
|
@@ -24,13 +23,20 @@ class WifiHelp {
|
|
|
private var timeDownUtil: TimeDownUtil? = null
|
|
|
private val mapWifi = mutableMapOf<String, Int>() //存wifi重连次数
|
|
|
private var currWifiSSID = ""
|
|
|
- private var wifiData :WifiData ?=null
|
|
|
- private var isWifiNum = if (getSN().startsWith("011")) 5 else 6
|
|
|
+ private var wifiData: WifiData? = null
|
|
|
+ private var isWifiNum = if (getSN().startsWith("011")) 5 else 6
|
|
|
|
|
|
private var mDISCONNECTEDTime: Long = 0
|
|
|
|
|
|
private var isWifiLoading = false;
|
|
|
|
|
|
+ //第一次按返回键系统的时间戳,默认为0。
|
|
|
+ private var firstTime: Long = 0
|
|
|
+
|
|
|
+ //第一次按返回键系统的时间戳,默认为0。
|
|
|
+ private var initWorkTime: Long = 0
|
|
|
+
|
|
|
+
|
|
|
private fun init(build: Build) {
|
|
|
mWifiManager =
|
|
|
build.mContext?.applicationContext?.getSystemService(Context.WIFI_SERVICE) as WifiManager
|
|
@@ -246,7 +252,7 @@ class WifiHelp {
|
|
|
val wifiConfigurationList = mWifiManager.configuredNetworks
|
|
|
for (item in wifiConfigurationList) {
|
|
|
if (item.SSID == addQuotationMarks(wifiData.ssid)) {
|
|
|
- Log.e("TAG removeSaveWifi:","ssid :"+item.SSID + " item :"+wifiData.ssid)
|
|
|
+ Log.e("TAG removeSaveWifi:", "ssid :" + item.SSID + " item :" + wifiData.ssid)
|
|
|
mWifiManager.disconnect()
|
|
|
mWifiManager.removeNetwork(item.networkId)
|
|
|
mWifiManager.saveConfiguration()
|
|
@@ -254,8 +260,8 @@ class WifiHelp {
|
|
|
}
|
|
|
}
|
|
|
mapWifi.forEach { (s, i) ->
|
|
|
- Log.e("TAG MAP","name :"+s)
|
|
|
- mapWifi[s] = 0
|
|
|
+ Log.e("TAG MAP", "name :" + s)
|
|
|
+ mapWifi[s] = 0
|
|
|
}
|
|
|
currWifiSSID = ""
|
|
|
this.wifiData = null
|
|
@@ -275,8 +281,8 @@ class WifiHelp {
|
|
|
}
|
|
|
}
|
|
|
mapWifi.forEach { (s, i) ->
|
|
|
- Log.e("TAG MAP","name :"+s)
|
|
|
- mapWifi[s] = 0
|
|
|
+ Log.e("TAG MAP", "name :" + s)
|
|
|
+ mapWifi[s] = 0
|
|
|
}
|
|
|
this.wifiData = null
|
|
|
currWifiSSID = ""
|
|
@@ -403,11 +409,19 @@ class WifiHelp {
|
|
|
* 刷新wifi的全部数据(搜索的WiFi与当前连接WiFi数据)
|
|
|
*/
|
|
|
public fun refreshWifiData() {
|
|
|
+ var secondTime = System.currentTimeMillis()
|
|
|
+ //2秒内同时刷新return
|
|
|
+ if (secondTime - firstTime < 100) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ firstTime = secondTime
|
|
|
+
|
|
|
appGlobalScope.launch(Dispatchers.IO) {
|
|
|
val list = getScanDevice()
|
|
|
val connectionWifi = getConnectionWifi()
|
|
|
withContext(Dispatchers.Main) {
|
|
|
timeDownUtil?.cancel()
|
|
|
+
|
|
|
mBuild?.mScanCallback?.invoke(list)
|
|
|
mBuild?.mAlreadyConnectionCallback?.invoke(connectionWifi)
|
|
|
|
|
@@ -606,7 +620,7 @@ class WifiHelp {
|
|
|
//在每次连接成功or连接失败后更新一次数据
|
|
|
|
|
|
//loading 的时候不刷新wifi列表
|
|
|
- if (!isWifiLoading){
|
|
|
+ if (!isWifiLoading) {
|
|
|
refreshWifiData()
|
|
|
}
|
|
|
if (networkInfo.detailedState == NetworkInfo.DetailedState.CONNECTED) {
|
|
@@ -615,7 +629,7 @@ class WifiHelp {
|
|
|
}
|
|
|
|
|
|
//正在进行验证
|
|
|
- if (networkInfo.detailedState == NetworkInfo.DetailedState.AUTHENTICATING){
|
|
|
+ if (networkInfo.detailedState == NetworkInfo.DetailedState.AUTHENTICATING) {
|
|
|
}
|
|
|
if (networkInfo.detailedState == NetworkInfo.DetailedState.DISCONNECTED) {
|
|
|
|
|
@@ -623,22 +637,28 @@ class WifiHelp {
|
|
|
// if (SystemClock.elapsedRealtime() - mDISCONNECTEDTime < 1000) {
|
|
|
// return
|
|
|
// }
|
|
|
- Log.e("TAG WIFI","DISCONNECTED---DISCONNECTED---DISCONNECTED")
|
|
|
+ Log.e("TAG WIFI", "DISCONNECTED---DISCONNECTED---DISCONNECTED")
|
|
|
// mBuild?.mErrorAuthenticating?.invoke("failed to connect")
|
|
|
- if (wifiData!=null&&wifiData?.ssid==currWifiSSID){
|
|
|
- Log.e("TAG WIFI","进来了 ---- currWifiSSID :"+currWifiSSID)
|
|
|
- if (currWifiSSID==""){
|
|
|
+ if (wifiData != null && wifiData?.ssid == currWifiSSID) {
|
|
|
+ Log.e("TAG WIFI", "进来了 ---- currWifiSSID :" + currWifiSSID)
|
|
|
+ if (currWifiSSID == "") {
|
|
|
return
|
|
|
}
|
|
|
if (mapWifi[currWifiSSID] != null) {
|
|
|
- Log.e("TAG WIFI","currWifiSSID :"+currWifiSSID+ " 次数:"+mapWifi[currWifiSSID])
|
|
|
- mapWifi[currWifiSSID] = mapWifi[currWifiSSID]!! +1
|
|
|
- }else{
|
|
|
+ Log.e(
|
|
|
+ "TAG WIFI",
|
|
|
+ "currWifiSSID :" + currWifiSSID + " 次数:" + mapWifi[currWifiSSID]
|
|
|
+ )
|
|
|
+ mapWifi[currWifiSSID] = mapWifi[currWifiSSID]!! + 1
|
|
|
+ } else {
|
|
|
mapWifi[currWifiSSID] = 0
|
|
|
}
|
|
|
//判断重连次数删除wifi
|
|
|
- if (mapWifi[currWifiSSID]!!>isWifiNum){
|
|
|
- Log.e("TAG WIFI","currWifiSSID :"+currWifiSSID+ " removeSaveSSIDWifi")
|
|
|
+ if (mapWifi[currWifiSSID]!! > isWifiNum) {
|
|
|
+ Log.e(
|
|
|
+ "TAG WIFI",
|
|
|
+ "currWifiSSID :" + currWifiSSID + " removeSaveSSIDWifi"
|
|
|
+ )
|
|
|
mapWifi[currWifiSSID] = 0
|
|
|
removeSaveSSIDWifi(currWifiSSID)
|
|
|
return
|
|
@@ -651,7 +671,7 @@ class WifiHelp {
|
|
|
currWifiData?.apply {
|
|
|
if (this.isSavePwd || this.password.isNotEmpty()) {
|
|
|
} else {
|
|
|
- Log.e("TAG WIFI","进来了 ---- removeSaveWifi :"+this.ssid)
|
|
|
+ Log.e("TAG WIFI", "进来了 ---- removeSaveWifi :" + this.ssid)
|
|
|
removeSaveWifi(this)
|
|
|
}
|
|
|
|
|
@@ -701,14 +721,23 @@ class WifiHelp {
|
|
|
|
|
|
@SuppressLint("MissingPermission")
|
|
|
fun initWork() {
|
|
|
- val configurationList = mWifiManager.configuredNetworks //已经保存密码的WiFi
|
|
|
- if (configurationList.isNotEmpty()) {
|
|
|
- configurationList.forEach {
|
|
|
- currWifiSSID = removeQuotationMarks(it.SSID)
|
|
|
- connectSavedWiFi(currWifiSSID)
|
|
|
+ var initTime = System.currentTimeMillis()
|
|
|
+
|
|
|
+ //initWorkTime = 0
|
|
|
+ if (initTime - initWorkTime < 2000) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ Log.e("TAG WIFI","initWork initWork initTime: $initTime initWorkTime:$initWorkTime ")
|
|
|
+ initWorkTime = initTime
|
|
|
+ appGlobalScope.launch(Dispatchers.IO) {
|
|
|
+ val configurationList = mWifiManager.configuredNetworks //已经保存密码的WiFi
|
|
|
+ if (configurationList.isNotEmpty()) {
|
|
|
+ configurationList.forEach {
|
|
|
+ currWifiSSID = removeQuotationMarks(it.SSID)
|
|
|
+ connectSavedWiFi(currWifiSSID)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
fun getDetailedState(): DetailedState? {
|
|
@@ -754,15 +783,20 @@ class WifiHelp {
|
|
|
* @param[ssid] 字符串或一串16进制的数字
|
|
|
*/
|
|
|
fun connectSavedWiFi(ssids: String) {
|
|
|
- val config = getWiFiConfig(ssids)
|
|
|
- config.let {
|
|
|
+ appGlobalScope.launch(Dispatchers.IO) {
|
|
|
+ val config = getWiFiConfig(ssids)
|
|
|
+ config.let {
|
|
|
+ }
|
|
|
+ config?.networkId?.let {
|
|
|
+ mWifiManager.enableNetwork(it, true)
|
|
|
+ }
|
|
|
+ mWifiManager.saveConfiguration()
|
|
|
|
|
|
+// withContext(Dispatchers.Main){
|
|
|
+// refreshWifiData()
|
|
|
+// }
|
|
|
}
|
|
|
- config?.networkId?.let {
|
|
|
|
|
|
- mWifiManager.enableNetwork(it, true)
|
|
|
- }
|
|
|
- mWifiManager.saveConfiguration()
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -789,17 +823,16 @@ class WifiHelp {
|
|
|
}
|
|
|
|
|
|
|
|
|
- fun setIsWifiLoading(isWifiLoading : Boolean){
|
|
|
+ fun setIsWifiLoading(isWifiLoading: Boolean) {
|
|
|
this.isWifiLoading = isWifiLoading
|
|
|
}
|
|
|
- fun getIsWifiLoading() :Boolean{
|
|
|
+
|
|
|
+ fun getIsWifiLoading(): Boolean {
|
|
|
return isWifiLoading
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
/**
|
|
|
* wifi数据
|
|
|
*/
|