use of android.net.wifi.WifiConfiguration in project android_frameworks_base by DirtyUnicorns.
the class WifiAssociationTest method getConfig.
/**
* Get the {@link WifiConfiguration} based on ssid, security, and password.
*/
private WifiConfiguration getConfig(String ssid, SecurityType securityType, String password) {
logv("Security type is %s", securityType.toString());
WifiConfiguration config = null;
switch(securityType) {
case OPEN:
config = WifiConfigurationHelper.createOpenConfig(ssid);
break;
case WEP64:
assertNotNull("password is empty", password);
// always use hex pair for WEP-40
assertTrue(WifiConfigurationHelper.isHex(password, 10));
config = WifiConfigurationHelper.createWepConfig(ssid, password);
config.allowedGroupCiphers.set(GroupCipher.WEP40);
break;
case WEP128:
assertNotNull("password is empty", password);
// always use hex pair for WEP-104
assertTrue(WifiConfigurationHelper.isHex(password, 26));
config = WifiConfigurationHelper.createWepConfig(ssid, password);
config.allowedGroupCiphers.set(GroupCipher.WEP104);
break;
case WPA_TKIP:
assertNotNull("password is empty", password);
config = WifiConfigurationHelper.createPskConfig(ssid, password);
config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
config.allowedProtocols.set(Protocol.WPA);
config.allowedPairwiseCiphers.set(PairwiseCipher.TKIP);
config.allowedGroupCiphers.set(GroupCipher.TKIP);
break;
case WPA2_AES:
assertNotNull("password is empty", password);
config = WifiConfigurationHelper.createPskConfig(ssid, password);
config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
config.allowedProtocols.set(Protocol.RSN);
config.allowedPairwiseCiphers.set(PairwiseCipher.CCMP);
config.allowedGroupCiphers.set(GroupCipher.CCMP);
break;
default:
fail("Not a valid security type: " + securityType);
break;
}
return config;
}
use of android.net.wifi.WifiConfiguration in project android_frameworks_base by DirtyUnicorns.
the class ConnectivityManagerTestBase method disconnectAP.
/*
* Disconnect from the current AP and remove configured networks.
*/
protected boolean disconnectAP() {
// remove saved networks
if (!mWifiManager.isWifiEnabled()) {
logv("Enabled wifi before remove configured networks");
mWifiManager.setWifiEnabled(true);
SystemClock.sleep(SHORT_TIMEOUT);
}
List<WifiConfiguration> wifiConfigList = mWifiManager.getConfiguredNetworks();
if (wifiConfigList == null) {
logv("no configuration list is null");
return true;
}
logv("size of wifiConfigList: " + wifiConfigList.size());
for (WifiConfiguration wifiConfig : wifiConfigList) {
logv("remove wifi configuration: " + wifiConfig.networkId);
int netId = wifiConfig.networkId;
mWifiManager.forget(netId, new WifiManager.ActionListener() {
@Override
public void onSuccess() {
}
@Override
public void onFailure(int reason) {
logv("Failed to forget " + reason);
}
});
}
return true;
}
use of android.net.wifi.WifiConfiguration in project android by cSploit.
the class WifiScannerFragment method performConnection.
private int performConnection(final ScanResult ap, final String key) {
mWifiManager.disconnect();
mCurrentKey = key;
mCurrentAp = ap;
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (key != null)
mStatusText.setText(Html.fromHtml(getString(R.string.wifi_attempting_to) + " <b>" + ap.SSID + "</b> " + getString(R.string.wifi_with_key) + " <b>" + key + "</b> ..."));
else
mStatusText.setText(Html.fromHtml(getString(R.string.wifi_connecting_to) + " <b>" + ap.SSID + "</b> ..."));
}
});
WifiConfiguration config = new WifiConfiguration();
int network = -1;
config.SSID = "\"" + ap.SSID + "\"";
config.BSSID = ap.BSSID;
/*
* Configure security.
*/
if (ap.capabilities.contains("WEP")) {
config.wepKeys[0] = "\"" + key + "\"";
config.wepTxKeyIndex = 0;
config.status = WifiConfiguration.Status.ENABLED;
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
} else if (ap.capabilities.contains("WPA"))
config.preSharedKey = "\"" + key + "\"";
else
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
network = mWifiManager.addNetwork(config);
if (network != -1) {
if (mWifiManager.saveConfiguration()) {
config = NetworkManager.getWifiConfiguration(mWifiManager, config);
// Make it the highest priority.
network = config.networkId;
int old_priority = config.priority, max_priority = NetworkManager.getMaxPriority(mWifiManager) + 1;
if (max_priority > 9999) {
NetworkManager.shiftPriorityAndSave(mWifiManager);
config = NetworkManager.getWifiConfiguration(mWifiManager, config);
}
// Set highest priority to this configured network
config.priority = max_priority;
network = mWifiManager.updateNetwork(config);
if (network != -1) {
// Do not disable others
if (mWifiManager.enableNetwork(network, false)) {
if (mWifiManager.saveConfiguration()) {
// We have to retrieve the WifiConfiguration after save.
config = NetworkManager.getWifiConfiguration(mWifiManager, config);
if (config != null) {
// Just to force the WifiManager to connect to it.
if (mWifiManager.enableNetwork(config.networkId, true)) {
return mWifiManager.reassociate() ? config.networkId : -1;
}
}
} else
config.priority = old_priority;
} else
config.priority = old_priority;
}
}
}
return network;
}
use of android.net.wifi.WifiConfiguration in project android_frameworks_base by ParanoidAndroid.
the class WifiApStress method testWifiHotSpot.
@LargeTest
public void testWifiHotSpot() {
if (mWifiOnlyFlag) {
Log.v(TAG, this.getName() + " is excluded for wi-fi only test");
return;
}
WifiConfiguration config = new WifiConfiguration();
config.SSID = NETWORK_ID;
config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
config.preSharedKey = PASSWD;
// If Wifi is enabled, disable it
if (mAct.mWifiManager.isWifiEnabled()) {
mAct.disableWifi();
}
int i;
for (i = 0; i < iterations; i++) {
Log.v(TAG, "iteration: " + i);
mLastIteration = i;
// enable Wifi tethering
assertTrue(mAct.mWifiManager.setWifiApEnabled(config, true));
// Wait for wifi ap state to be ENABLED
assertTrue(mAct.waitForWifiAPState(WifiManager.WIFI_AP_STATE_ENABLED, 2 * ConnectivityManagerTestActivity.LONG_TIMEOUT));
// Wait for wifi tethering result
assertEquals(ConnectivityManagerTestActivity.SUCCESS, mAct.waitForTetherStateChange(2 * ConnectivityManagerTestActivity.SHORT_TIMEOUT));
// Allow the wifi tethering to be enabled for 10 seconds
try {
Thread.sleep(2 * ConnectivityManagerTestActivity.SHORT_TIMEOUT);
} catch (Exception e) {
fail("thread in sleep is interrupted");
}
assertTrue("no uplink data connection after Wi-Fi tethering", mAct.pingTest(null));
// Disable soft AP
assertTrue(mAct.mWifiManager.setWifiApEnabled(config, false));
// Wait for 30 seconds until Wi-Fi tethering is stopped
try {
Thread.sleep(30 * 1000);
Log.v(TAG, "wait for Wi-Fi tethering to be disabled.");
} catch (Exception e) {
fail("thread in sleep is interrupted");
}
assertFalse("Wi-Fi AP disable failed", mAct.mWifiManager.isWifiApEnabled());
}
if (i == iterations) {
mLastIteration = iterations;
}
}
use of android.net.wifi.WifiConfiguration in project android_frameworks_base by ParanoidAndroid.
the class WifiStressTest method testWifiReconnectionAfterSleep.
// Stress Wifi reconnection to secure net after sleep
@LargeTest
public void testWifiReconnectionAfterSleep() {
int value = Settings.Global.getInt(mRunner.getContext().getContentResolver(), Settings.Global.WIFI_SLEEP_POLICY, -1);
log("wifi sleep policy is: " + value);
if (value != Settings.Global.WIFI_SLEEP_POLICY_DEFAULT) {
Settings.Global.putInt(mRunner.getContext().getContentResolver(), Settings.Global.WIFI_SLEEP_POLICY, Settings.Global.WIFI_SLEEP_POLICY_DEFAULT);
log("set wifi sleep policy to default value");
}
Settings.Global.putLong(mRunner.getContext().getContentResolver(), Settings.Global.WIFI_IDLE_MS, WIFI_IDLE_MS);
// Connect to a Wi-Fi network
WifiConfiguration config = new WifiConfiguration();
config.SSID = mSsid;
config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
if (mPassword.matches("[0-9A-Fa-f]{64}")) {
config.preSharedKey = mPassword;
} else {
config.preSharedKey = '"' + mPassword + '"';
}
config.ipAssignment = IpAssignment.DHCP;
config.proxySettings = ProxySettings.NONE;
assertTrue("Failed to connect to Wi-Fi network: " + mSsid, mAct.connectToWifiWithConfiguration(config));
assertTrue(mAct.waitForWifiState(WifiManager.WIFI_STATE_ENABLED, ConnectivityManagerTestActivity.SHORT_TIMEOUT));
assertTrue(mAct.waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.CONNECTED, ConnectivityManagerTestActivity.WIFI_CONNECTION_TIMEOUT));
// Run ping test to verify the data connection
assertTrue("Wi-Fi is connected, but no data connection.", mAct.pingTest(null));
int i;
long sum = 0;
for (i = 0; i < mReconnectIterations; i++) {
// 1. Put device into sleep mode
// 2. Wait for the device to sleep for sometime, verify wi-fi is off and mobile is on.
// 3. Maintain the sleep mode for some time,
// 4. Verify the Wi-Fi is still off, and data is on
// 5. Wake up the device, verify Wi-Fi is enabled and connected.
writeOutput(String.format("iteration %d out of %d", i, mReconnectIterations));
log("iteration: " + i);
mAct.turnScreenOff();
PowerManager pm = (PowerManager) mRunner.getContext().getSystemService(Context.POWER_SERVICE);
assertFalse(pm.isScreenOn());
sleep(WIFI_IDLE_MS + WIFI_SHUTDOWN_DELAY, "Interruped while wait for wifi to be idle");
assertTrue("Wait for Wi-Fi to idle timeout", mAct.waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.DISCONNECTED, 6 * ConnectivityManagerTestActivity.SHORT_TIMEOUT));
if (!mWifiOnlyFlag) {
// use long timeout as the pppd startup may take several retries.
assertTrue("Wait for cellular connection timeout", mAct.waitForNetworkState(ConnectivityManager.TYPE_MOBILE, State.CONNECTED, 2 * ConnectivityManagerTestActivity.LONG_TIMEOUT));
}
sleep(mWifiSleepTime, "Interrupted while device is in sleep mode");
// Verify the wi-fi is still off and data connection is on
assertEquals("Wi-Fi is reconnected", State.DISCONNECTED, mAct.mCM.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState());
if (!mWifiOnlyFlag) {
assertEquals("Cellular connection is down", State.CONNECTED, mAct.mCM.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState());
assertTrue("Mobile is connected, but no data connection.", mAct.pingTest(null));
}
// Turn screen on again
mAct.turnScreenOn();
// Wait for 2 seconds for the lock screen
sleep(2 * 1000, "wait 2 seconds for lock screen");
// Disable lock screen by inject menu key event
mRunner.sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
// Measure the time for Wi-Fi to get connected
long startTime = System.currentTimeMillis();
assertTrue("Wait for Wi-Fi enable timeout after wake up", mAct.waitForWifiState(WifiManager.WIFI_STATE_ENABLED, ConnectivityManagerTestActivity.SHORT_TIMEOUT));
assertTrue("Wait for Wi-Fi connection timeout after wake up", mAct.waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.CONNECTED, ConnectivityManagerTestActivity.WIFI_CONNECTION_TIMEOUT));
long connectionTime = System.currentTimeMillis() - startTime;
sum += connectionTime;
log("average reconnection time is: " + sum / (i + 1));
assertTrue("Reconnect to Wi-Fi network, but no data connection.", mAct.pingTest(null));
}
if (i == mReconnectIterations) {
writeOutput(String.format("iteration %d out of %d", i, mReconnectIterations));
}
}
Aggregations