Handle receivers in thread
This commit is contained in:
@@ -88,10 +88,12 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService(), CoroutineScope {
|
|||||||
binder.iface = ""
|
binder.iface = ""
|
||||||
updateNotification() // show invisible foreground notification to avoid being killed
|
updateNotification() // show invisible foreground notification to avoid being killed
|
||||||
try {
|
try {
|
||||||
if (!receiverRegistered) {
|
launch {
|
||||||
receiverRegistered = true
|
if (!receiverRegistered) {
|
||||||
registerReceiver(receiver, IntentFilter(WifiApManager.WIFI_AP_STATE_CHANGED_ACTION))
|
receiverRegistered = true
|
||||||
?.let(this::updateState)
|
registerReceiver(receiver, IntentFilter(WifiApManager.WIFI_AP_STATE_CHANGED_ACTION))
|
||||||
|
?.let(this@LocalOnlyHotspotService::updateState)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Services.wifi.startLocalOnlyHotspot(object : WifiManager.LocalOnlyHotspotCallback() {
|
Services.wifi.startLocalOnlyHotspot(object : WifiManager.LocalOnlyHotspotCallback() {
|
||||||
override fun onStarted(reservation: WifiManager.LocalOnlyHotspotReservation?) {
|
override fun onStarted(reservation: WifiManager.LocalOnlyHotspotReservation?) {
|
||||||
@@ -106,7 +108,7 @@ class LocalOnlyHotspotService : IpNeighbourMonitoringService(), CoroutineScope {
|
|||||||
registerReceiver(null, IntentFilter(WifiApManager.WIFI_AP_STATE_CHANGED_ACTION))
|
registerReceiver(null, IntentFilter(WifiApManager.WIFI_AP_STATE_CHANGED_ACTION))
|
||||||
?.let(this@LocalOnlyHotspotService::updateState) // attempt to update again
|
?.let(this@LocalOnlyHotspotService::updateState) // attempt to update again
|
||||||
val state = lastState
|
val state = lastState
|
||||||
unregisterStateReceiver()
|
launch { unregisterStateReceiver() }
|
||||||
checkNotNull(state) { "Failed to obtain latest AP state" }
|
checkNotNull(state) { "Failed to obtain latest AP state" }
|
||||||
val iface = state.second
|
val iface = state.second
|
||||||
if (state.first != WifiApManager.WIFI_AP_STATE_ENABLED || iface.isNullOrEmpty()) {
|
if (state.first != WifiApManager.WIFI_AP_STATE_ENABLED || iface.isNullOrEmpty()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user