androidkotlingarbage-collectionandroid-12hotspot

startLocalOnlyHotspot and create hotspot programaticaly


startLocalOnlyHotspot and create hotspot programaticaly

I am trying to create a hotspot programmatically similar to this app my problem is hotspot create and immediately disappear

Here is the code I am using:

   private val wifiManager: WifiManager by lazy {
    (applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager)
}



   startLocalOnlyHotspot(object : WifiManager.LocalOnlyHotspotCallback() {
            override fun onStarted(reservation: WifiManager.LocalOnlyHotspotReservation) {
                super.onStarted(reservation)
            }

            override fun onStopped() {
                super.onStopped()
            }

            override fun onFailed(reason: Int) {
                super.onFailed(reason)
            }
        }, Handler(Looper.getMainLooper()))

I have also tried this code with foreground service and WorkManager but it did not work. When I check my logcat, I see that the hotspot is created but disappears immediately with the following error message:

System resource failed to call close.


Solution

  • keep the reservation object as a field like this :

    private WifiManager.LocalOnlyHotspotReservation reservation