[author] yjj
[desc]安全检测仪4G模块调试 1、接触wifiHelper和SimHelper之间的耦合关联 2、新增NetWorkHelper,统一管理wifi和4G网络相关 3、修改http请求服务器ip地址 4、增加用于测试的log显示view 5、4G流量使用情况参数修改未mobilemain
							parent
							
								
									2e5b1c5676
								
							
						
					
					
						commit
						c58d15f777
					
				@ -0,0 +1,8 @@
 | 
			
		||||
package com.yinuo.safetywatcher.watcher.interfaces
 | 
			
		||||
 | 
			
		||||
interface NetworkStateCallback {
 | 
			
		||||
 | 
			
		||||
    fun onEnable(enable: Boolean)
 | 
			
		||||
    fun onLevel(level: Int)
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,5 @@
 | 
			
		||||
package com.yinuo.safetywatcher.watcher.log
 | 
			
		||||
 | 
			
		||||
interface LogPrintListener {
 | 
			
		||||
    fun log(msg: String?)
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,74 @@
 | 
			
		||||
package com.yinuo.safetywatcher.watcher.utils
 | 
			
		||||
 | 
			
		||||
import android.content.Context
 | 
			
		||||
import com.common.commonlib.CommonApplication
 | 
			
		||||
import com.yinuo.safetywatcher.watcher.interfaces.NetworkStateCallback
 | 
			
		||||
import com.yinuo.safetywatcher.watcher.log.LogPrintListener
 | 
			
		||||
import kotlinx.coroutines.Dispatchers
 | 
			
		||||
import kotlinx.coroutines.GlobalScope
 | 
			
		||||
import kotlinx.coroutines.launch
 | 
			
		||||
 | 
			
		||||
object NetworkHelper {
 | 
			
		||||
 | 
			
		||||
    private var lastNetworkState : Boolean = false
 | 
			
		||||
 | 
			
		||||
    private var logPrinter : LogPrintListener? = null
 | 
			
		||||
 | 
			
		||||
    fun init(context: Context){
 | 
			
		||||
        WifiHelper.init(context)
 | 
			
		||||
        SimHelper.init(context)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun setLogPrinter(logPrinter: LogPrintListener?){
 | 
			
		||||
        this.logPrinter = logPrinter
 | 
			
		||||
        SimHelper.logListener = logPrinter
 | 
			
		||||
        WifiHelper.logListener = logPrinter
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun release(context: Context){
 | 
			
		||||
        WifiHelper.release(context)
 | 
			
		||||
        SimHelper.release(context)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun watch(wifiCallback: NetworkStateCallback, simCallback: NetworkStateCallback){
 | 
			
		||||
        WifiHelper.addCallBack(wifiCallback)
 | 
			
		||||
        SimHelper.addCallBack(simCallback)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun removeNetworkCallback(wifiCallback: NetworkStateCallback, simCallback: NetworkStateCallback){
 | 
			
		||||
        SimHelper.removeCallback(simCallback)
 | 
			
		||||
        WifiHelper.removeCallback(wifiCallback)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 网络是否可用
 | 
			
		||||
     * **/
 | 
			
		||||
    fun isNetworkAvailable() : Boolean{
 | 
			
		||||
        return WifiHelper.isWifiAvailable() || SimHelper.is4GAvailable()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 通知网络变化
 | 
			
		||||
     */
 | 
			
		||||
    @Synchronized
 | 
			
		||||
    fun notifyNetworkChange(){
 | 
			
		||||
        // 当前网络状态
 | 
			
		||||
        val currentNetworkState = isNetworkAvailable()
 | 
			
		||||
        logPrinter?.log("网络发生变化 curr = $currentNetworkState   last = $lastNetworkState")
 | 
			
		||||
        if (currentNetworkState == lastNetworkState) {
 | 
			
		||||
            // 网络状态无变化
 | 
			
		||||
            return
 | 
			
		||||
        }
 | 
			
		||||
        // 网络状态有变化
 | 
			
		||||
        lastNetworkState = currentNetworkState
 | 
			
		||||
        val str = if (!currentNetworkState) "网络链接断开!" else "网络已连接!"
 | 
			
		||||
        GlobalScope.launch(Dispatchers.Main) {
 | 
			
		||||
            CommonApplication.getContext()?.showToast(str)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun printLog(msg : String){
 | 
			
		||||
        logPrinter?.log(msg)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -1,2 +1,3 @@
 | 
			
		||||
host=http://192.168.1.102:8080
 | 
			
		||||
host=http://121.41.168.18:8080
 | 
			
		||||
#host=http://192.168.1.102:8080
 | 
			
		||||
#host=http://192.168.51.123:8080
 | 
			
		||||
 | 
			
		||||
					Loading…
					
					
				
		Reference in New Issue