I am using android 4.1.1 ... I am making an application that allows the user to make his own network using Wifi Hotspot and then the clients can connect to it and share data. I have successfully created the Wifi hotspot in android but I cannot configure it for the purpose. Is there any way to configure Wifi Hotspot on android through coding ??
            Asked
            
        
        
            Active
            
        
            Viewed 1.6k times
        
    8
            
            
        - 
                    ,... check out my response for you. – gumuruh Jul 25 '14 at 18:57
2 Answers
16
            
            
        This answer maybe outdated!
if(wifiManager.isWifiEnabled())
{
    wifiManager.setWifiEnabled(false); 
}
WifiConfiguration netConfig = new WifiConfiguration();
netConfig.SSID = "MyAP";
netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
try{
    Method setWifiApMethod = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
    boolean apstatus=(Boolean) setWifiApMethod.invoke(wifiManager, netConfig,true);
    Method isWifiApEnabledmethod = wifiManager.getClass().getMethod("isWifiApEnabled"); 
    while(!(Boolean)isWifiApEnabledmethod.invoke(wifiManager)){};
    Method getWifiApStateMethod = wifiManager.getClass().getMethod("getWifiApState"); 
    int apstate=(Integer)getWifiApStateMethod.invoke(wifiManager);
    Method getWifiApConfigurationMethod = wifiManager.getClass().getMethod("getWifiApConfiguration");
    netConfig=(WifiConfiguration)getWifiApConfigurationMethod.invoke(wifiManager);
    Log.e("CLIENT", "\nSSID:"+netConfig.SSID+"\nPassword:"+netConfig.preSharedKey+"\n");
} catch (Exception e) {
    Log.e(this.getClass().toString(), "", e);
}
 
    
    
        mizdler
        
- 621
- 4
- 8
- 
                    Is there any `setWifiApConfiguration` for this Wifi HotSpot so I can connect to custom wifi access point using code. – zionpi Jun 28 '16 at 03:48
- 
                    Doesn't work on Android 6 and 5.1 above. I have added the ACTION_MANAGE_WRITE_SETTINGS permission but still getting error as `Exception in softap start java.lang.IllegalStateException: command '2707 softap set wlan0 ....` – AkshayT Jan 30 '17 at 09:46
- 
                    @AkshayTaru No surprise! at the time I answered the latest version of android was 5.0 , I hope you can update the answer. – mizdler Jan 31 '17 at 20:31
0
            
            
        Here is a possible solution:
private WifiManager.LocalOnlyHotspotReservation mReservation;
 WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {
        @Override
        public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
            super.onStarted(reservation);
            mReservation = reservation;
        }
        @Override
        public void onStopped() {
            super.onStopped();
        }
        @Override
        public void onFailed(int reason) {
            super.onFailed(reason);
        }
    }, new Handler());
 
    
    
        double-beep
        
- 5,031
- 17
- 33
- 41
 
    
    
        tushar
        
- 36
- 4
- 
                    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – double-beep Dec 29 '18 at 17:30
 
    