Enabling Wi-Fi Software

Is it possible to programmatically enable the Wi-Fi hotspot to enable the binding? I tried the code here and here . Both examples run without exception, but when I look at the โ€œModem and portable hotspotโ€ section in the Wi-Fi settings, the binding is still disabled. Is this only possible for internal Google applications?

EDIT . I am using Android 5.1 and I am trying to do this without starting the phone.

+8
java android android-wifi
source share
1 answer

Try using the code below to programmatically turn on your Wi-Fi modem. I tested and worked in my application.

public class WifiAccessManager { private static final String SSID = "1234567890abcdef"; public static boolean setWifiApState(Context context, boolean enabled) { //config = Preconditions.checkNotNull(config); try { WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); if (enabled) { mWifiManager.setWifiEnabled(false); } WifiConfiguration conf = getWifiApConfiguration(); mWifiManager.addNetwork(conf); return (Boolean) mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class).invoke(mWifiManager, conf, enabled); } catch (Exception e) { e.printStackTrace(); return false; } } public static WifiConfiguration getWifiApConfiguration() { WifiConfiguration conf = new WifiConfiguration(); conf.SSID = SSID; conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); return conf; } } 

Using:

 WifiAccessManager.setWifiApState(context, true); 

Permission Required:

 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> 
+7
source share

All Articles