Reputation: 10072
I'm programming a Nexus 7. In my program I reach a point where I want the user to select a Wifi network to use (either preconfigured or the choice to create a new one). How can I bring up that dialog programmatically?
Upvotes: 0
Views: 9382
Reputation: 3257
If you want to create your own dialog:
WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
List<WifiConfiguration> list = wifiMgr.getConfiguredNetworks();
Gives you a list of the networks and for connecting to a concrete SSID:
public void connectToWifi(String ssid) {
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + ssid + "\""; // Please note the quotes.
// String should contain
// ssid in quotes
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiMgr.addNetwork(conf);
Log.d(PluginConstants.LOG_TAG, ssid+" added");
List<WifiConfiguration> list = wifiMgr.getConfiguredNetworks();
for (WifiConfiguration i : list) {
if (i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) {
wifiMgr.disconnect();
wifiMgr.enableNetwork(i.networkId, true);
wifiMgr.reconnect();
Log.d(PluginConstants.LOG_TAG, "conneting to: ssid");
break;
}
}
}
}
Upvotes: 3
Reputation: 6905
try
Intent intent = new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK);
startActivity(intent);
Upvotes: 7
Reputation: 943
In order to access the wi-fi list, you may first need to use the wi-fi permissions. Wi-Fi must be turned on first before the Wi-Fi scan can provide a list of results.
My guess is: ACCESS_WIFI_STATE
I think the easiest would approach would be to then launch the appropriate intent. ACTION_PICK_WIFI_NETWORK http://developer.android.com/reference/android/net/wifi/WifiManager.html#ACTION_PICK_WIFI_NETWORK
(May also wish to see: ACTION_WIFI_SETTINGS)
Upvotes: 2