Detect hotspot changes and re-acquire hotspot information

This commit is contained in:
Jtun-coder 2024-12-05 15:10:37 +08:00
parent d43c533399
commit 2f2e04fa3a
3 changed files with 14 additions and 12 deletions

View File

@ -88,7 +88,13 @@ class MainActivity : AppCompatActivity(), EasyPermissions.PermissionCallbacks, L
httpWeb?.startServer()
httpServer = HttpServer()
httpServer?.startServer()
httpServer?.restartCallback = {
//启动一个LocalServer用于管理第三方应用
LocalServer.instance.localServerListener = this
LocalServer.instance.init(Const.ROUTING_TAG)
WifiApControl.getInstance().trafficStats()
WifiApControl.getInstance().initNetSpeed()
WifiApControl.getInstance().activeCallback = {
lifecycleScope.launch {
val config = WifiApControl.getInstance().getSoftApConfig()
val ip = NetworkUtils.getLocalIpv4Address()
@ -97,12 +103,6 @@ class MainActivity : AppCompatActivity(), EasyPermissions.PermissionCallbacks, L
}
}
}
//启动一个LocalServer用于管理第三方应用
LocalServer.instance.localServerListener = this
LocalServer.instance.init(Const.ROUTING_TAG)
WifiApControl.getInstance().trafficStats()
WifiApControl.getInstance().initNetSpeed()
FileHelper.init(this@MainActivity)
startAp()
SystemCtrlUtil.systemSettings(this)

View File

@ -58,6 +58,8 @@ class WifiApControl private constructor() {
private var localOnlyIfaces = emptyList<String>()
private var erroredIfaces = emptyList<String>()
private var ifaceLookup: Map<String, NetworkInterface> = emptyMap()
var activeCallback: (() -> Unit)? = null
private val receiver = broadcastReceiver { _, intent ->
activeIfaces = intent.tetheredIfaces ?: return@broadcastReceiver
localOnlyIfaces = intent.localOnlyTetheredIfaces ?: return@broadcastReceiver
@ -70,6 +72,9 @@ class WifiApControl private constructor() {
emptyMap()
}
KLog.i("activeIfaces $activeIfaces")
if(activeIfaces.isNotEmpty()){
activeCallback?.invoke()
}
KLog.i("ifaceLookup $ifaceLookup")
}
companion object {
@ -370,7 +375,7 @@ class WifiApControl private constructor() {
KLog.i("soft ap : $configuration")
return configuration
}
fun restartTethering(call:(() -> Unit)?){
fun restartTethering(){
GlobalScope.launch(Dispatchers.Main) {
TetheringManager.stopTethering(TetheringManager.TETHERING_WIFI,errorCallback = {
JLog.r("restartTether","restartTethering stop exception : $it")
@ -383,12 +388,10 @@ class WifiApControl private constructor() {
TetheringManager.startTethering(TetheringManager.TETHERING_WIFI,true,object : TetheringManager.StartTetheringCallback{
override fun onTetheringStarted() {
JLog.r("restartTether","restartTethering startTethering success")
call?.invoke()
}
override fun onTetheringFailed(error: Int?) {
JLog.r("restartTether","restartTethering onTetheringFailed error : $error")
call?.invoke()
}
override fun onException(e: Exception) {

View File

@ -48,7 +48,6 @@ import org.json.JSONObject
*/
class HttpServer : NanoHTTPD(Config.HTTP_PORT) {
private val tag = HttpServer::class.java.simpleName
var restartCallback: (() -> Unit)? = null
companion object{
private const val KEY_ADMIN_PASS = "admin_pass_key"
private const val DATA_PLAN = "data_plan"
@ -124,7 +123,7 @@ class HttpServer : NanoHTTPD(Config.HTTP_PORT) {
HttpInterface.SET_AP_CONFIG ->{
val data = jsonObject.getJSONObject("data")
resp = setApConfig(data.toString())
WifiApControl.getInstance().restartTethering(restartCallback)
WifiApControl.getInstance().restartTethering()
}
HttpInterface.SET_ADMIN_PASS ->{
val newPass = jsonObject.getString("newPass")