質問

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?

役に立ちましたか?

解決 3

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)

他のヒント

try

Intent intent = new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK);       
startActivity(intent);

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;
            }
        }
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top