public static void installConferenceWiFi(final Context context) { // Create config WifiConfiguration config = new WifiConfiguration(); // Must be in double quotes to tell system this is an ASCII SSID and passphrase. config.SSID = String.format("\"%s\"", Config.WIFI_SSID); config.preSharedKey = String.format("\"%s\"", Config.WIFI_PASSPHRASE); // Store config final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); int netId = wifiManager.addNetwork(config); if (netId != -1) { wifiManager.enableNetwork(netId, false); boolean result = wifiManager.saveConfiguration(); if (!result) { Log.e(TAG, "Unknown error while calling WiFiManager.saveConfiguration()"); Toast.makeText( context, context.getResources().getString(R.string.wifi_install_error_message), Toast.LENGTH_SHORT) .show(); } } else { Log.e(TAG, "Unknown error while calling WiFiManager.addNetwork()"); Toast.makeText( context, context.getResources().getString(R.string.wifi_install_error_message), Toast.LENGTH_SHORT) .show(); } }
/** * Configure a network, and connect to it. * * @param wifiMgr * @param scanResult * @param password Password for secure network or is ignored. * @return */ public boolean connectToNewNetwork( ScanResult scanResult, String password, int numOpenNetworksKept) { final String security = ConfigSec.getScanResultSecurity(scanResult); if (ConfigSec.isOpenNetwork(security)) { checkForExcessOpenNetworkAndSave(numOpenNetworksKept); } WifiConfiguration config = new WifiConfiguration(); config.SSID = convertToQuotedString(scanResult.SSID); config.BSSID = scanResult.BSSID; ConfigSec.setupSecurity(config, security, password); int id = -1; try { id = mWifiManager.addNetwork(config); } catch (NullPointerException e) { Log.e(TAG, "Weird!! Really!! What's wrong??", e); } if (id == -1) { return false; } if (!mWifiManager.saveConfiguration()) { return false; } config = getWifiConfiguration(config, security); if (config == null) { return false; } return connectToConfiguredNetwork(config, true); }
/** * Update the network: either create a new network or modify an existing network * * @param config the new network configuration * @param disableOthers true if other networks must be disabled * @return network ID of the connected network. */ private int updateNetwork(WifiConfiguration config, boolean disableOthers) { WifiConfiguration found = findNetworkInExistingConfig(config.SSID); wifiManager.disconnect(); if (found == null) { statusView.setText(R.string.wifi_creating_network); } else { statusView.setText(R.string.wifi_modifying_network); Log.d(TAG, "Removing network " + found.networkId); wifiManager.removeNetwork(found.networkId); wifiManager.saveConfiguration(); } networkId = wifiManager.addNetwork(config); Log.d(TAG, "Inserted/Modified network " + networkId); if (networkId < 0) { return FAILURE_NO_NETWORK_ID; } // Try to disable the current network and start a new one. if (!wifiManager.enableNetwork(networkId, disableOthers)) { networkId = FAILURE_NO_NETWORK_ID; return FAILURE_NO_NETWORK_ID; } errorCount = 0; wifiManager.reassociate(); return networkId; }
/** * 添加到网络 * * @param wcg */ public boolean addNetwork(WifiConfiguration wcg) { if (wcg == null) { return false; } int wcgID = mWifiManager.addNetwork(wcg); boolean b = mWifiManager.enableNetwork(wcgID, true); mWifiManager.saveConfiguration(); return b; }
/** * Update the network: either create a new network or modify an existing network * * @param config the new network configuration * @return network ID of the connected network. */ private static void updateNetwork(WifiManager wifiManager, WifiConfiguration config) { Integer foundNetworkID = findNetworkInExistingConfig(wifiManager, config.SSID); if (foundNetworkID != null) { Log.i(TAG, "Removing old configuration for network " + config.SSID); wifiManager.removeNetwork(foundNetworkID); wifiManager.saveConfiguration(); } int networkId = wifiManager.addNetwork(config); if (networkId >= 0) { // Try to disable the current network and start a new one. if (wifiManager.enableNetwork(networkId, true)) { Log.i(TAG, "Associating to network " + config.SSID); wifiManager.saveConfiguration(); } else { Log.w(TAG, "Failed to enable network " + config.SSID); } } else { Log.w(TAG, "Unable to add network " + config.SSID); } }
private int shiftPriorityAndSave() { final List<WifiConfiguration> configurations = mWifiManager.getConfiguredNetworks(); sortByPriority(configurations); final int size = configurations.size(); for (int i = 0; i < size; i++) { final WifiConfiguration config = configurations.get(i); config.priority = i; mWifiManager.updateNetwork(config); } mWifiManager.saveConfiguration(); return size; }
/** * Wifiネットワークに接続する * * @return boolean */ public boolean connect(String ssid) { int networkId = 0; WifiConfiguration config = getWifiConfig(ssid); networkId = mWifiManager.addNetwork(config); if (networkId == -1) { return false; } mWifiManager.saveConfiguration(); mWifiManager.updateNetwork(config); config.networkId = networkId; return connect(config); }
/** * Connect to a configured network. * * @param wifiManager * @param config * @param numOpenNetworksKept Settings.Secure.WIFI_NUM_OPEN_NETWORKS_KEPT * @return */ public boolean connectToConfiguredNetwork(WifiConfiguration config, boolean reassociate) { final String security = ConfigSec.getWifiConfigurationSecurity(config); int oldPri = config.priority; // Make it the highest priority. int newPri = getMaxPriority() + 1; if (newPri > MAX_PRIORITY) { newPri = shiftPriorityAndSave(); config = getWifiConfiguration(config, security); if (config == null) { return false; } } // Set highest priority to this configured network config.priority = newPri; int networkId = mWifiManager.updateNetwork(config); if (networkId == -1) { return false; } // Do not disable others if (!mWifiManager.enableNetwork(networkId, false)) { config.priority = oldPri; return false; } if (!mWifiManager.saveConfiguration()) { config.priority = oldPri; return false; } // We have to retrieve the WifiConfiguration after save. config = getWifiConfiguration(config, security); if (config == null) { return false; } // ReenableAllApsWhenNetworkStateChanged.schedule(mContext); // Disable others, but do not save. // Just to force the WifiManager to connect to it. if (!mWifiManager.enableNetwork(config.networkId, true)) { return false; } final boolean connect = reassociate ? mWifiManager.reassociate() : mWifiManager.reconnect(); if (!connect) { return false; } return true; }
/** * Wifiネットワークに接続する * * @return boolean */ public boolean connect(String ssid, String password) { int networkId = 0; Security security = getWifiSecurity(ssid); WifiConfiguration config = createWifiConfig(security, ssid, password); networkId = mWifiManager.addNetwork(config); if (networkId == -1) { return false; } mWifiManager.saveConfiguration(); mWifiManager.updateNetwork(config); config.networkId = networkId; return connect(config); }
/** * Ensure no more than numOpenNetworksKept open networks in configuration list. * * @param wifiMgr * @param numOpenNetworksKept * @return Operation succeed or not. */ private boolean checkForExcessOpenNetworkAndSave(int numOpenNetworksKept) { final List<WifiConfiguration> configurations = mWifiManager.getConfiguredNetworks(); sortByPriority(configurations); boolean modified = false; int tempCount = 0; for (int i = configurations.size() - 1; i >= 0; i--) { final WifiConfiguration config = configurations.get(i); if (ConfigSec.isOpenNetwork(ConfigSec.getWifiConfigurationSecurity(config))) { tempCount++; if (tempCount >= numOpenNetworksKept) { modified = true; mWifiManager.removeNetwork(config.networkId); } } } if (modified) { return mWifiManager.saveConfiguration(); } return true; }
public void connectToAP(String ssid, String passkey, Context ctx) { // Log.i(TAG, "* connectToAP"); WifiConfiguration wifiConfiguration = new WifiConfiguration(); WifiManager wifiManager = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE); String networkSSID = ssid; String networkPass = passkey; // Log.d(TAG, "# password " + networkPass); if (results != null && results.size() > 0) { for (ScanResult result : results) { if (result.SSID.equals(networkSSID)) { String securityMode = getScanResultSecurity(result); if (securityMode.equalsIgnoreCase("OPEN")) { wifiConfiguration.SSID = "\"" + networkSSID + "\""; wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); int res = wifiManager.addNetwork(wifiConfiguration); // Log.d(TAG, "# add Network returned " + res); wifiManager.enableNetwork(res, true); // Log.d(TAG, "# enableNetwork returned " + b); wifiManager.setWifiEnabled(true); } else if (securityMode.equalsIgnoreCase("WEP")) { wifiConfiguration.SSID = "\"" + networkSSID + "\""; wifiConfiguration.wepKeys[0] = "\"" + networkPass + "\""; wifiConfiguration.wepTxKeyIndex = 0; wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); int res = wifiManager.addNetwork(wifiConfiguration); // Log.d(TAG, "### 1 ### add Network returned " + res); wifiManager.enableNetwork(res, true); // Log.d(TAG, "# enableNetwork returned " + b); wifiManager.setWifiEnabled(true); } wifiConfiguration.SSID = "\"" + networkSSID + "\""; wifiConfiguration.preSharedKey = "\"" + networkPass + "\""; wifiConfiguration.hiddenSSID = true; wifiConfiguration.status = WifiConfiguration.Status.ENABLED; wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN); wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA); int res = wifiManager.addNetwork(wifiConfiguration); wifiManager.enableNetwork(res, true); boolean changeHappen = wifiManager.saveConfiguration(); wifiManager.setWifiEnabled(true); if (res != -1 && changeHappen) { if (!checkNetwrk()) Toast.makeText( ctx, "wifi password is set but no internet access. Restart the router", Toast.LENGTH_LONG) .show(); else Toast.makeText(ctx, "wifi is connected", Toast.LENGTH_LONG).show(); try { Thread.sleep(1000); } catch (InterruptedException e) { } Intent i = new Intent(ctx, InternetConnectionSetting.class); startActivity(i); finish(); } } } } else { getWifiList(); } }
/** Tell the supplicant to save the current linked access point. */ public void saveConfiguaration() { mWifiManager.saveConfiguration(); }