1

I need to connect my tablet to a wifi programmaticaly. I have tried a least 20 differents codes, nothing works.

I have all the permissions :

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">

And here is the code :

  WifiConfiguration wifiConfig = new WifiConfiguration();
    wifiConfig.SSID = String.format("\"%s\"", "ssis");
    wifiConfig.preSharedKey = String.format("\"%s\"", "password");


    wifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);

    WifiManager wifiManager = (WifiManager)getApplicationContext().getSystemService(WIFI_SERVICE);

    int netId = wifiManager.addNetwork(wifiConfig);
    wifiManager.disconnect();
    wifiManager.enableNetwork(netId, true);
    wifiManager.reconnect();

The wifi protocol I use is WPA/WPA2

Does soemone has a working code?

ImNeos
  • 527
  • 6
  • 17
  • Does this answer your question? [How do I connect to a specific Wi-Fi network in Android programmatically?](https://stackoverflow.com/questions/8818290/how-do-i-connect-to-a-specific-wi-fi-network-in-android-programmatically) – Ravi Apr 10 '20 at 09:48

1 Answers1

1

Try this code this will work on all device except android 10. Check this https://issuetracker.google.com/issues/128554616

WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = String.format("\"%s\"", networkSSID);
wifiConfig.preSharedKey = String.format("\"%s\"", networkPass);
wifiManager = (WifiManager)                         
getActivity().getApplicationContext().getSystemService(WIFI_SERVICE);
if(!wifiManager.isWifiEnabled())
wifiManager.setWifiEnabled(true);
int netId = wifiManager.addNetwork(wifiConfig);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
matdev
  • 4,115
  • 6
  • 35
  • 56
Waleed
  • 63
  • 1
  • 3