0

i need Connect to a WiFi (esp8266 nodeMCU) in Android programmatically. SSID of this network is Hide and security of this is WPA2-PSK. also it is not connected to internet, it is a Local network.

i try this Code, but i cant Connect to HotSpot. "enableNetworkBoolean" is "false". but i can connect to hotspot in settings.

public class ConnWifi  {

public void Connect(Context context){
    WifiConfiguration config = new WifiConfiguration();
    config.SSID = ""reyhan112233"";
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        config.SSID = "reyhan112233";
    }
    config.preSharedKey = ""reyhan112233"";
    config.hiddenSSID = true;
    config.status = WifiConfiguration.Status.ENABLED;
    config.allowedGroupCiphers.set(WifiConfiguration.G  roupCipher.TKIP);
    config.allowedGroupCiphers.set(WifiConfiguration.G  roupCipher.CCMP);
    config.allowedKeyManagement.set(WifiConfiguration.  KeyMgmt.WPA_PSK);
    config.allowedPairwiseCiphers.set(WifiConfiguratio  n.PairwiseCipher.TKIP);
    config.allowedPairwiseCiphers.set(WifiConfiguratio  n.PairwiseCipher.CCMP);
    config.allowedProtocols.set(WifiConfiguration.Prot  ocol.RSN);
    config.allowedProtocols.set(WifiConfiguration.Prot  ocol.WPA);

    WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI  _SERVICE);

    int networkId = wifiManager.addNetwork(config);
    if(networkId != -1){
        connectWifi(config, wifiManager);
    }
}

private void connectWifi(WifiConfiguration config , WifiManager wifiManager) {
    wifiManager.disconnect();
    wifiManager.setWifiEnabled(true);
    boolean enableNetworkBoolean = wifiManager.enableNetwork(config.networkId, true);

    boolean reconnectBoolean = wifiManager.reconnect();
    boolean changeHappen = wifiManager.saveConfiguration();
    if(enableNetworkBoolean && reconnectBoolean && changeHappen){
    }
    else{

    }
}
radin
  • 251
  • 3
  • 16
  • `but i cant Connect to HotSpot.` what is a problem? what happens instead? – Vladyslav Matviienko May 01 '18 at 19:25
  • I suggest using a debugger and looking at the logs. Try to see if there are any errors in the logs you could fix. If it still doesn't help, you could post the relevant portion of the logs/errors here with what you tried to fix it. – Yashovardhan99 May 01 '18 at 19:56
  • @VladyslavMatviienko: device cant connect to hotspot, There are no errors. but "enableNetworkBoolean" is "false". i can connect to hotspot in settings. – radin May 02 '18 at 05:14
  • @Yashovardhan There are no errors. but "enableNetworkBoolean" is "false". but i can connect to hotspot in settings. – radin May 02 '18 at 05:16

0 Answers0