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() httpWeb?.startServer()
httpServer = HttpServer() httpServer = HttpServer()
httpServer?.startServer() 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 { lifecycleScope.launch {
val config = WifiApControl.getInstance().getSoftApConfig() val config = WifiApControl.getInstance().getSoftApConfig()
val ip = NetworkUtils.getLocalIpv4Address() 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) FileHelper.init(this@MainActivity)
startAp() startAp()
SystemCtrlUtil.systemSettings(this) SystemCtrlUtil.systemSettings(this)

View File

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

View File

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