• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Android 5+ Enable Hotspot in code

Sajmoon

Lurker
Hi Everyone...
I wonder if it is possible to enable hotspot from java code.
I got all devices running android 6.0+ and can't test current code on older androids.
Tried a few different examples and nothing works.

Example1 :
Java:
 public boolean isHotspotON(){
        WifiManager wifiMgr = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        try{
            Method method = wifiMgr.getClass().getDeclaredMethod("isWifiApEnabled");
            method.setAccessible(true);
            return (Boolean) method.invoke(wifiMgr);
        }
        catch(Throwable ignoreException)
        {
            return false;
        }
    }

    public void EnableHotspot()
    {
        if(!this.isHotspotON())
        {
            WifiManager wifiMgr = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
            WifiConfiguration wifiConfig = new WifiConfiguration();
            try{

                Method method = wifiMgr.getClass().getMethod("setWifiApEnabled",WifiConfiguration.class,boolean.class);
                method.invoke(wifiMgr,wifiConfig,!isHotspotON());

                }
            catch(Exception e) {
                    e.getStackTrace();
            }

        }

    }

Example 2:
Java:
 public void EnableHotspov2()
    {
        if(!this.isHotspotON())
        {
            WifiManager wifiMgr = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
            WifiConfiguration wifiConfig = new WifiConfiguration();
            wifiConfig.SSID = "Example AP";
            wifiConfig.preSharedKey = "TestPassword";
            wifiConfig.hiddenSSID = false;
            wifiConfig.allowedKeyManagement.set((WifiConfiguration.KeyMgmt.WPA_PSK));
            wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
            wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
            try{
                Method method = wifiMgr.getClass().getMethod("setWifiApEnabled",WifiConfiguration.class,boolean.class);
                method.invoke(wifiMgr,wifiConfig,false);
                wifiMgr.saveConfiguration();
            }
            catch (Exception e)
            {
                e.getMessage();
            }



        }

    }
Example 3:
Java:
    public void EnableHotspot3()
    {
        try {
            WifiManager mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);

                mWifiManager.setWifiEnabled(false);

            WifiConfiguration conf = getWifiApConfiguration();
            mWifiManager.addNetwork(conf);

             mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class).invoke(mWifiManager, conf, true);
        } catch (Exception e) {
            e.printStackTrace();

        }

    }
    public static WifiConfiguration getWifiApConfiguration() {
        WifiConfiguration conf = new WifiConfiguration();
        conf.SSID =  "DupaHotspot";
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        return conf;
    }



Is there anything I'm missing?
btw...targeted api are 16-25.
Just in case...I added all required permissions to manifest
 
After hours of researching, I finally was able to start hotspot...

THIS SOLUTION WORKS ON ANDROID 6.0.1 AND 7.0.

Not tested on older versions because all devices I've got at home got andro 6+.


Here is my code:
Java:
public boolean ToggleHotspot(boolean ON_OFF)
{



    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    {


        if (!Settings.System.canWrite(mContext))
        {
            Intent writeSettingIntent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);

          // Works when calling directly from MainActivity.java,but not from Android Library.WHY??
          // writeSettingIntent.setData(Uri.parse("package: " + mContext.getPackageName()));
            writeSettingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(writeSettingIntent);
        }
    }
        //TODO -> Check if hotspot enabled.If not start it...
         WifiManager wifiMgr = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
                try{

                    if(this.isWiFiEnabled())
                    {
                        this.DisableWiFi();
                    }

                    Method invokeMethod = wifiMgr.getClass().getMethod("setWifiApEnabled",WifiConfiguration.class,boolean.class);
                    return (Boolean)invokeMethod.invoke(wifiMgr,initHotspotConfig(),ON_OFF);
                }
                catch(Throwable ignoreException)
                {
                    return false;
                }


}




private WifiConfiguration initHotspotConfig(){

     WifiConfiguration wifiConfig = new WifiConfiguration();

      wifiConfig.SSID = "Test Hotspot";
     // must be 8 length
      wifiConfig.preSharedKey = "abcd1234";

      wifiConfig.hiddenSSID = true;


     wifiConfig.status = WifiConfiguration.Status.ENABLED;
     wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
     wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
     wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
     wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
     wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
     wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);


    return wifiConfig;
}
 
Back
Top Bottom