Skip to content

Instantly share code, notes, and snippets.

@liu7yong
Last active November 7, 2021 14:45
Show Gist options
  • Save liu7yong/f3765398b2ea5f30c986 to your computer and use it in GitHub Desktop.
Save liu7yong/f3765398b2ea5f30c986 to your computer and use it in GitHub Desktop.
How to connect to a specific wifi network in Android programmatically?
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = String.format("\"%s\"", ssid);
wifiConfig.preSharedKey = String.format("\"%s\"", key);
WifiManager wifiManager = (WifiManager)getSystemService(WIFI_SERVICE);
//remember id
int netId = wifiManager.addNetwork(wifiConfig);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
@YD27
Copy link

YD27 commented Mar 1, 2021

this is not working on android 10

it's deprecated
check this for Android 10 https://developer.android.com/guide/topics/connectivity/wifi-suggest

@rohitsingh2410
Copy link

@YD27 usin https://developer.android.com/guide/topics/connectivity/wifi-suggest i was able to connect to wifi but internet is not working

@Scienticious
Copy link

How to connect to a specific network in android 10 , any working example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment