2

I reference the code to turn on hotspot in Android 8.0, it is work. But I have no idea about how to disable it

@RequiresApi(api = Build.VERSION_CODES.O)
private void turnOnHotspot(){
    WifiManager manager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

    manager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback(){

        @Override
        public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
            super.onStarted(reservation);
            Log.d(TAG, "Wifi Hotspot is on now");
        }

        @Override
        public void onStopped() {
            super.onStopped();
            Log.d(TAG, "onStopped: ");
        }

        @Override
        public void onFailed(int reason) {
            super.onFailed(reason);
            Log.d(TAG, "onFailed: ");
        }
    },new Handler());
}

I want to use close() method from LocalOnlyHotspotReservation , but how to get the reservation instance from outside, like reservation.close();

Or any way can disable hotspot in Android 8.0

[Update] I found a way can disable hotspot

wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);                    
Method method = wifiManager.getClass().getDeclaredMethod("cancelLocalOnlyHotspotRequest");
method.invoke(wifiManager);

But still, have no idea about how to use close.

Ramesh R
  • 7,009
  • 4
  • 25
  • 38
Lee
  • 21
  • 1
  • 5
  • Possible duplicate of [Android turn On/Off WiFi HotSpot programmatically](https://stackoverflow.com/questions/6394599/android-turn-on-off-wifi-hotspot-programmatically) – Saneesh Oct 20 '17 at 07:13
  • But for Android O , it is not support setWifiApEnabled anymore , it provide LocalOnlyHotspotCallback and LocalOnlyHotspotReservation to enable hotspot. – Lee Oct 20 '17 at 08:15
  • @Lee Please check this answer https://stackoverflow.com/questions/45984345/how-to-turn-on-off-wifi-hotspot-programmatically-in-android-8-0-oreo/45996578#45996578 – Chandrakanth Nov 30 '17 at 11:20

1 Answers1

1

In order to disable it, you need to create a global reference for the WifiManager.LocalOnlyHotspotReservation, assign it in the onSatrted() callback and then close it as follows

private WifiManager.LocalOnlyHotspotReservation mReservation;

private void turnOffHotspot() {
 if (mReservation != null) {
  mReservation.close();
 }
}

You may refer to the following link as follows, it worked for me: How to turn on/off wifi hotspot programmatically in Android 8.0 (Oreo)

Arpita
  • 11
  • 2