Android: Programmatically Turn on WiFi hotspot

前端 未结 3 611
梦如初夏
梦如初夏 2020-12-18 04:02

I am trying to turn on the portable Wifi hotspot ON, by referring this link:
how to set advanced settings of android wifihotspot

This is working well on Samsung

相关标签:
3条回答
  • 2020-12-18 04:10

    If you are using WPA_PSK authentication, you have to specify preSharedKey.

            netConfig.preSharedKey = "testPassword";
            netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    

    Otherwise it will crash and restart the device.

    0 讨论(0)
  • 2020-12-18 04:22

    Try this code .I test in android 5.0.1 :

          public boolean setHotSpot(String SSID,String passWord){
            Method[] mMethods = mWifiManager.getClass().getDeclaredMethods();
    
            for(Method mMethod: mMethods){
    
                if(mMethod.getName().equals("setWifiApEnabled")) {
                    WifiConfiguration netConfig = new WifiConfiguration();
                    if(passWord==""){
                        netConfig.SSID = SSID;
                        netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
                        netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                        netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
                        netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);     
                    }else{
                    netConfig.SSID = SSID ;
                    netConfig.preSharedKey = passWord;
                    netConfig.hiddenSSID = true;
                    netConfig.status = WifiConfiguration.Status.ENABLED;
                    netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                    netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                    netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                    netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                    netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                    netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                    netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
                    }
                    try {              
                        mMethod.invoke(mWifiManager, netConfig,false);
                        mWifiManager.saveConfiguration();
                        return true;
    
                    } catch (Exception e) {
                      e.getMessage();
                    }
                }
            }
            return false; 
      }
    

    Insert this permission in AndroidManifest:

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    
    0 讨论(0)
  • 2020-12-18 04:22

    I had the same issue. You must to erase the following code line:

    netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);    
    

    That works for me in versions 4.1.1, 4.2 and 4.3.

    0 讨论(0)
提交回复
热议问题