List of usage examples for android.net.wifi WifiManager enableNetwork
public boolean enableNetwork(int netId, boolean attemptConnect)
From source file:tf.nox.wifisetup.WifiSetup.java
private void saveWifiConfig() { WifiManager wifiManager = (WifiManager) this.getApplicationContext().getSystemService(WIFI_SERVICE); wifiManager.setWifiEnabled(true);/* w w w .ja v a 2 s.c o m*/ WifiConfiguration currentConfig = new WifiConfiguration(); List<WifiConfiguration> configs = null; for (int i = 0; i < 10 && configs == null; i++) { configs = wifiManager.getConfiguredNetworks(); try { Thread.sleep(1); } catch (InterruptedException e) { continue; } } if (check5g.isChecked()) { ssid = "34C3"; } else { ssid = "34C3-legacy"; } subject_match = "/CN=radius.c3noc.net"; altsubject_match = "DNS:radius.c3noc.net"; s_username = username.getText().toString(); s_password = password.getText().toString(); realm = ""; if (s_username.equals("") && s_password.equals("")) { s_username = "34c3"; s_password = "34c3"; } else { if (s_username.indexOf("@") >= 0) { int idx = s_username.indexOf("@"); realm = s_username.substring(idx); } } // Use the existing eduroam profile if it exists. boolean ssidExists = false; if (configs != null) { for (WifiConfiguration config : configs) { if (config.SSID.equals(surroundWithQuotes(ssid))) { currentConfig = config; ssidExists = true; break; } } } currentConfig.SSID = surroundWithQuotes(ssid); currentConfig.hiddenSSID = false; currentConfig.priority = 40; currentConfig.status = WifiConfiguration.Status.DISABLED; currentConfig.allowedKeyManagement.clear(); currentConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP); // GroupCiphers (Allow most ciphers) currentConfig.allowedGroupCiphers.clear(); currentConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); currentConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); currentConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104); // PairwiseCiphers (CCMP = WPA2 only) currentConfig.allowedPairwiseCiphers.clear(); currentConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); // Authentication Algorithms (OPEN) currentConfig.allowedAuthAlgorithms.clear(); currentConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); // Protocols (RSN/WPA2 only) currentConfig.allowedProtocols.clear(); currentConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN); // Enterprise Settings HashMap<String, String> configMap = new HashMap<String, String>(); configMap.put(INT_SUBJECT_MATCH, subject_match); configMap.put(INT_ALTSUBJECT_MATCH, altsubject_match); configMap.put(INT_ANONYMOUS_IDENTITY, "anonymous" + realm); configMap.put(INT_IDENTITY, s_username); configMap.put(INT_PASSWORD, s_password); configMap.put(INT_EAP, "TTLS"); configMap.put(INT_PHASE2, "auth=PAP"); configMap.put(INT_ENGINE, "0"); // configMap.put(INT_CA_CERT, INT_CA_PREFIX + ca_name); applyAndroid43EnterpriseSettings(currentConfig, configMap); if (!ssidExists) { int networkId = wifiManager.addNetwork(currentConfig); wifiManager.enableNetwork(networkId, false); } else { wifiManager.updateNetwork(currentConfig); wifiManager.enableNetwork(currentConfig.networkId, false); } wifiManager.saveConfiguration(); }
From source file:com.mozilla.SUTAgentAndroid.SUTAgentAndroid.java
public boolean setUpNetwork(String sIniFile) { boolean bRet = false; int lcv = 0;//from w w w . ja v a2 s. c o m int lcv2 = 0; WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiConfiguration wc = new WifiConfiguration(); DoCommand tmpdc = new DoCommand(getApplication()); String ssid = tmpdc.GetIniData("Network Settings", "SSID", sIniFile); String auth = tmpdc.GetIniData("Network Settings", "AUTH", sIniFile); String encr = tmpdc.GetIniData("Network Settings", "ENCR", sIniFile); String key = tmpdc.GetIniData("Network Settings", "KEY", sIniFile); String eap = tmpdc.GetIniData("Network Settings", "EAP", sIniFile); String adhoc = tmpdc.GetIniData("Network Settings", "ADHOC", sIniFile); Toast.makeText(getApplication().getApplicationContext(), "Starting and configuring network", Toast.LENGTH_LONG).show(); /* ContentResolver cr = getContentResolver(); int nRet; try { nRet = Settings.System.getInt(cr, Settings.System.WIFI_USE_STATIC_IP); String foo2 = "" + nRet; } catch (SettingNotFoundException e1) { e1.printStackTrace(); } */ /* wc.SSID = "\"Mozilla-Build\""; wc.preSharedKey = "\"MozillaBuildQA500\""; wc.hiddenSSID = true; wc.status = WifiConfiguration.Status.ENABLED; wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); */ wc.SSID = "\"" + ssid + "\""; // wc.SSID = "\"Mozilla-G\""; // wc.SSID = "\"Mozilla\""; if (auth.contentEquals("wpa2")) { wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); wc.preSharedKey = null; } if (encr.contentEquals("aes")) { wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); } if (eap.contentEquals("peap")) { wc.eap.setValue("PEAP"); wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP); wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X); } wc.status = WifiConfiguration.Status.ENABLED; if (!wifi.isWifiEnabled()) wifi.setWifiEnabled(true); while (wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLED) { Thread.yield(); if (++lcv > 10000) return (bRet); } wl = wifi.createWifiLock(WifiManager.WIFI_MODE_FULL, "SUTAgent"); if (wl != null) wl.acquire(); WifiConfiguration foo = null; int nNetworkID = -1; List<WifiConfiguration> connsLst = wifi.getConfiguredNetworks(); int nConns = connsLst.size(); for (int i = 0; i < nConns; i++) { foo = connsLst.get(i); if (foo.SSID.equalsIgnoreCase(wc.SSID)) { nNetworkID = foo.networkId; wc.networkId = foo.networkId; break; } } int res; if (nNetworkID != -1) { res = wifi.updateNetwork(wc); } else { res = wifi.addNetwork(wc); } Log.d("WifiPreference", "add Network returned " + res); boolean b = wifi.enableNetwork(res, true); Log.d("WifiPreference", "enableNetwork returned " + b); wifi.saveConfiguration(); WifiInfo wi = wifi.getConnectionInfo(); SupplicantState ss = wi.getSupplicantState(); lcv = 0; lcv2 = 0; while (ss.compareTo(SupplicantState.COMPLETED) != 0) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } if (wi != null) wi = null; if (ss != null) ss = null; wi = wifi.getConnectionInfo(); ss = wi.getSupplicantState(); if (++lcv > 60) { if (++lcv2 > 5) { Toast.makeText(getApplication().getApplicationContext(), "Unable to start and configure network", Toast.LENGTH_LONG).show(); return (bRet); } else { Toast.makeText(getApplication().getApplicationContext(), "Resetting wifi interface", Toast.LENGTH_LONG).show(); if (wl != null) wl.release(); wifi.setWifiEnabled(false); while (wifi.getWifiState() != WifiManager.WIFI_STATE_DISABLED) { Thread.yield(); } wifi.setWifiEnabled(true); while (wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLED) { Thread.yield(); } b = wifi.enableNetwork(res, true); Log.d("WifiPreference", "enableNetwork returned " + b); if (wl != null) wl.acquire(); lcv = 0; } } } lcv = 0; while (getLocalIpAddress() == null) { if (++lcv > 10000) return (bRet); } Toast.makeText(getApplication().getApplicationContext(), "Network started and configured", Toast.LENGTH_LONG).show(); bRet = true; return (bRet); }
From source file:org.sipdroid.sipua.ui.Receiver.java
@Override public void onReceive(Context context, Intent intent) { String intentAction = intent.getAction(); if (!Sipdroid.on(context)) return;//from w ww. ja v a 2 s . c o m if (!Sipdroid.release) Log.i("SipUA:", intentAction); if (mContext == null) mContext = context; if (intentAction.equals(Intent.ACTION_BOOT_COMPLETED)) { on_vpn(false); engine(context).register(); } else if (intentAction.equals(ConnectivityManager.CONNECTIVITY_ACTION) || intentAction.equals(ACTION_EXTERNAL_APPLICATIONS_AVAILABLE) || intentAction.equals(ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE) || intentAction.equals(Intent.ACTION_PACKAGE_REPLACED)) { engine(context).register(); } else if (intentAction.equals(ACTION_VPN_CONNECTIVITY) && intent.hasExtra("connection_state")) { String state = intent.getSerializableExtra("connection_state").toString(); if (state != null && on_vpn() != state.equals("CONNECTED")) { on_vpn(state.equals("CONNECTED")); for (SipProvider sip_provider : engine(context).sip_providers) if (sip_provider != null) sip_provider.haltConnections(); engine(context).register(); } } else if (intentAction.equals(ACTION_DATA_STATE_CHANGED)) { engine(context).registerMore(); } else if (intentAction.equals(ACTION_PHONE_STATE_CHANGED) && !intent.getBooleanExtra(context.getString(R.string.app_name), false)) { stopRingtone(); pstn_state = intent.getStringExtra("state"); pstn_time = SystemClock.elapsedRealtime(); if (pstn_state.equals("IDLE") && call_state != UserAgent.UA_STATE_IDLE) broadcastCallStateChanged(null, null); if (!pstn_state.equals("IDLE") && call_state == UserAgent.UA_STATE_INCALL) mHandler.sendEmptyMessageDelayed(MSG_HOLD, 5000); else if (!pstn_state.equals("IDLE") && (call_state == UserAgent.UA_STATE_INCOMING_CALL || call_state == UserAgent.UA_STATE_OUTGOING_CALL)) mHandler.sendEmptyMessageDelayed(MSG_HANGUP, 5000); else if (pstn_state.equals("IDLE")) { mHandler.removeMessages(MSG_HOLD); mHandler.removeMessages(MSG_HANGUP); if (call_state == UserAgent.UA_STATE_HOLD) mHandler.sendEmptyMessageDelayed(MSG_HOLD, 1000); } } else if (intentAction.equals(ACTION_DOCK_EVENT)) { docked = intent.getIntExtra(EXTRA_DOCK_STATE, -1) & 7; if (call_state == UserAgent.UA_STATE_INCALL) engine(mContext).speaker(speakermode()); } else if (intentAction.equals(ACTION_SCO_AUDIO_STATE_CHANGED)) { bluetooth = intent.getIntExtra(EXTRA_SCO_AUDIO_STATE, -1); progress(); RtpStreamSender.changed = true; } else if (intentAction.equals(Intent.ACTION_HEADSET_PLUG)) { headset = intent.getIntExtra("state", -1); if (call_state == UserAgent.UA_STATE_INCALL) engine(mContext).speaker(speakermode()); } else if (intentAction.equals(Intent.ACTION_SCREEN_ON)) { if (!PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean( org.sipdroid.sipua.ui.Settings.PREF_OWNWIFI, org.sipdroid.sipua.ui.Settings.DEFAULT_OWNWIFI)) alarm(0, OwnWifi.class); } else if (intentAction.equals(Intent.ACTION_USER_PRESENT)) { mHandler.sendEmptyMessageDelayed(MSG_ENABLE, 3000); } else if (intentAction.equals(Intent.ACTION_SCREEN_OFF)) { if (PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean( org.sipdroid.sipua.ui.Settings.PREF_OWNWIFI, org.sipdroid.sipua.ui.Settings.DEFAULT_OWNWIFI)) { WifiManager wm = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); WifiInfo wi = wm.getConnectionInfo(); if (wm.getWifiState() != WifiManager.WIFI_STATE_ENABLED || wi == null || wi.getSupplicantState() != SupplicantState.COMPLETED || wi.getIpAddress() == 0) alarm(2 * 60, OwnWifi.class); else alarm(15 * 60, OwnWifi.class); } if (SipdroidEngine.pwl != null) for (PowerManager.WakeLock pwl : SipdroidEngine.pwl) if (pwl != null && pwl.isHeld()) { pwl.release(); pwl.acquire(); } } else if (intentAction.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { if (PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean( org.sipdroid.sipua.ui.Settings.PREF_SELECTWIFI, org.sipdroid.sipua.ui.Settings.DEFAULT_SELECTWIFI)) mHandler.sendEmptyMessageDelayed(MSG_SCAN, 3000); } else if (intentAction.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { if (SystemClock.uptimeMillis() > lastscan + 45000 && PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean( org.sipdroid.sipua.ui.Settings.PREF_SELECTWIFI, org.sipdroid.sipua.ui.Settings.DEFAULT_SELECTWIFI)) { WifiManager wm = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); WifiInfo wi = wm.getConnectionInfo(); String activeSSID = null; if (wi != null) activeSSID = wi.getSSID(); if (activeSSID != null && (activeSSID.length() == 0 || activeSSID.equals("0x"))) activeSSID = null; List<ScanResult> mScanResults = wm.getScanResults(); List<WifiConfiguration> configurations = wm.getConfiguredNetworks(); if (configurations != null) { WifiConfiguration bestconfig = null, maxconfig = null, activeconfig = null; for (final WifiConfiguration config : configurations) { if (maxconfig == null || config.priority > maxconfig.priority) { maxconfig = config; } if (config.SSID != null && (config.SSID.equals("\"" + activeSSID + "\"") || config.SSID.equals(activeSSID))) activeconfig = config; } ScanResult bestscan = null, activescan = null; if (mScanResults != null) for (final ScanResult scan : mScanResults) { for (final WifiConfiguration config : configurations) { if (config.SSID != null && config.SSID.equals("\"" + scan.SSID + "\"")) { if (bestscan == null || scan.level > bestscan.level) { bestscan = scan; bestconfig = config; } if (config == activeconfig) activescan = scan; } } } if (activescan != null) System.out.println("debug wifi asu(active)" + asu(activescan)); if (bestconfig != null && (activeconfig == null || bestconfig.priority != activeconfig.priority) && asu(bestscan) > asu(activescan) * 1.5 && /* (activeSSID == null || activescan != null) && */ asu(bestscan) > 22) { if (!Sipdroid.release) Log.i("SipUA:", "changing to " + bestconfig.SSID); if (activeSSID == null || !activeSSID.equals(bestscan.SSID)) wm.disconnect(); bestconfig.priority = maxconfig.priority + 1; wm.updateNetwork(bestconfig); wm.enableNetwork(bestconfig.networkId, true); wm.saveConfiguration(); if (activeSSID == null || !activeSSID.equals(bestscan.SSID)) wm.reconnect(); lastscan = SystemClock.uptimeMillis(); } else if (activescan != null && asu(activescan) < 15) { wm.disconnect(); wm.disableNetwork(activeconfig.networkId); wm.saveConfiguration(); } } } } }