7

We have been using reflection to enable the wifi hotspot in Android with our app (as everybody else, as it seems):

WifiManager mWifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE);
Method method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
method.invoke(mWifiManager, wifiConfig, enabled);

Since Android 7.1 this is not working any more. Although it starts the hotspot (I see the hotspot icon in the info bar), the connecting client can't use the internet connection of the phone.

With Oreo (Android 8.0) the situation is getting worse as it does not do anything at all.

I found this solution for a local-only-hotspot: How to turn on/off wifi hotspot programatically in Android 8.0 (Oreo) But we need to share the internet connection, so we can't use this one.

Also having root access to the phone is no solution for us.

Did we miss any official or unofficial way to achieve our goal or is there really no way of doing this any more since Android 7.1?

  • did you find the solution please share the answer....i also having same problem....is there any work around solution also ok for me – Gowthaman M May 25 '18 at 14:14
  • 1
    @GowthamanM No, sadly I found no way to achieve it without root access to the phone. (But I did no new researches the last 4 months about this topic, since our app now is restricted to old Android versions.) –  Jun 04 '18 at 05:49
  • As i research i found solution like we have to redirect to setting page make it hotspot on – Gowthaman M Jun 04 '18 at 07:08
  • I didn't try but this guy seems to found something : https://stackoverflow.com/questions/45984345/how-to-turn-on-off-wifi-hotspot-programmatically-in-android-8-0-oreo – Titou Dec 12 '18 at 11:45

1 Answers1

-1

Although its not a proper solution but u can give it a try if there is no alternative.

First: CHECK IF HOTSPOT ACTIVE

boolean isHotSpotActive(Context cxt)
{
    boolean isSuccess = false;
    WifiManager wifi = (WifiManager)cxt.getSystemService(Context.WIFI_SERVICE);
    Method[] wmMethods = wifi.getClass().getDeclaredMethods();
    for (Method method : wmMethods)
    {
        if (method.getName().equals("isWifiApEnabled"))
        {
            try 
            {
                boolean isWifiApEnabled = (boolean)method.invoke(wifi);
                isSuccess = isWifiApEnabled;
            }catch (IllegalArgumentException e){
                e.printStackTrace();
            }catch (IllegalAccessException e){
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
    return  isSuccess;
}

Second: If hotspot active , turn Wifi ON and then OFF.

WifiManager wifimanager=(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
boolean isWifiOn =  wifimanager.setWifiEnabled(true); // turning wifi on 
//will turn off the hotspot
if(isWifiOn)
{
    wifimanager.setWifiEnabled(false);
}
  • Thanks for your reply. Unfortunately this is no solution to us, since we wanted to turn the hotspot ON if it's not already. No need to do anything, if the hotspot is already turned on. –  Oct 09 '17 at 07:26