use of android.net.wifi.ScanResult in project android_frameworks_base by crdroidandroid.
the class ConnectionUtil method connectToWifiWithConfiguration.
/**
* Connect to Wi-Fi with the given configuration.
* @param config
* @return true if we are connected to a given AP.
*/
public boolean connectToWifiWithConfiguration(WifiConfiguration config) {
// The SSID in the configuration is a pure string, need to convert it to a quoted string.
String ssid = config.SSID;
config.SSID = convertToQuotedString(ssid);
// If wifi is not enabled, enable it
if (!mWifiManager.isWifiEnabled()) {
Log.v(LOG_TAG, "Wifi is not enabled, enable it");
mWifiManager.setWifiEnabled(true);
// wait for the wifi state change before start scanning.
if (!waitForWifiState(WifiManager.WIFI_STATE_ENABLED, 2 * SHORT_TIMEOUT)) {
Log.v(LOG_TAG, "Wait for WIFI_STATE_ENABLED failed");
return false;
}
}
boolean foundApInScanResults = false;
for (int retry = 0; retry < 5; retry++) {
List<ScanResult> netList = mWifiManager.getScanResults();
if (netList != null) {
Log.v(LOG_TAG, "size of scan result list: " + netList.size());
for (int i = 0; i < netList.size(); i++) {
ScanResult sr = netList.get(i);
if (sr.SSID.equals(ssid)) {
Log.v(LOG_TAG, "Found " + ssid + " in the scan result list.");
Log.v(LOG_TAG, "Retry: " + retry);
foundApInScanResults = true;
mWifiManager.connect(config, new WifiManager.ActionListener() {
public void onSuccess() {
}
public void onFailure(int reason) {
Log.e(LOG_TAG, "connect failed " + reason);
}
});
break;
}
}
}
if (foundApInScanResults) {
return true;
} else {
// Start an active scan
mWifiManager.startScan();
mScanResultIsAvailable = false;
long startTime = System.currentTimeMillis();
while (!mScanResultIsAvailable) {
if ((System.currentTimeMillis() - startTime) > WIFI_SCAN_TIMEOUT) {
Log.v(LOG_TAG, "wait for scan results timeout");
return false;
}
// wait for the scan results to be available
synchronized (this) {
// wait for the scan result to be available
try {
this.wait(WAIT_FOR_SCAN_RESULT);
} catch (InterruptedException e) {
e.printStackTrace();
}
if ((mWifiManager.getScanResults() == null) || (mWifiManager.getScanResults().size() <= 0)) {
continue;
}
mScanResultIsAvailable = true;
}
}
}
}
return false;
}
use of android.net.wifi.ScanResult in project android_frameworks_base by crdroidandroid.
the class WifiStressTest method testWifiScanning.
/**
* Stress Wifi Scanning
* TODO: test the scanning quality for each frequency band
*/
@LargeTest
public void testWifiScanning() {
long scanTimeSum = 0, i, averageScanTime = -1;
// count times of given ssid appear in scan results.
int ssidAppearInScanResultsCount = 0;
for (i = 1; i <= mScanIterations; i++) {
logv("testWifiScanning: iteration: " + i);
averageScanTime = scanTimeSum / i;
writeOutput(String.format("iteration %d out of %d", i, mScanIterations));
writeOutput(String.format("average scanning time is %d", averageScanTime));
writeOutput(String.format("ssid appear %d out of %d scan iterations", ssidAppearInScanResultsCount, i));
List<ScanResult> scanResultLocal = null;
// wait for a scan result
long start = 0;
synchronized (mWifiScanResultLock) {
start = SystemClock.uptimeMillis();
assertTrue("start scan failed", mWifiManager.startScan());
try {
mWifiScanResultLock.wait(WAIT_FOR_SCAN_RESULT);
} catch (InterruptedException e) {
// ignore
}
scanTimeSum += SystemClock.uptimeMillis() - start;
// save the scan result while in lock
scanResultLocal = mLastScanResult;
}
if (scanResultLocal == null || scanResultLocal.isEmpty()) {
fail("Scan results are empty ");
}
logv("size of scan result list: " + scanResultLocal.size());
for (ScanResult sr : scanResultLocal) {
logv(String.format("scan result: " + sr.toString()));
if (mSsid.equals(sr.SSID)) {
ssidAppearInScanResultsCount += 1;
break;
}
}
}
Bundle result = new Bundle();
result.putLong("actual-iterations", i - 1);
result.putLong("avg-scan-time", averageScanTime);
result.putInt("ap-discovered", ssidAppearInScanResultsCount);
getInstrumentation().sendStatus(Activity.RESULT_FIRST_USER, result);
if (i == mScanIterations + 1) {
writeOutput(String.format("iteration %d out of %d", i - 1, mScanIterations));
writeOutput(String.format("average scanning time is %d", scanTimeSum / (i - 1)));
writeOutput(String.format("ssid appear %d out of %d scan iterations", ssidAppearInScanResultsCount, i - 1));
}
}
use of android.net.wifi.ScanResult in project dobby-android by InceptAi.
the class FakeWifiAnalyzer method getFakeScanResult.
private ScanResult getFakeScanResult(@WifiState.SignalStrengthZones int zone, int channelFreq) {
String ssid = randomBssid();
int level = getLevel(zone);
// ScanResult result =
// ScanResult scanResult = new ScanResult(ssid, ssid, hessid,
// /* int anqpDomainId */ 0,
// /* String caps */ Utils.EMPTY_STRING,
// /* level */ level,
// frequency,
// /* long tsf */ 0L,
// /* int distCm */ 0,
// /* int distSdCm */ 0,
// /* int channelWidth*/ 22,
// /* int centerFreq0*/ centerFreq0,
// /* int centerFreq1 */ centerFreq1,
// /* boolean is80211McRTTResponder */ false);
ScanResult result = null;
try {
result = ScanResult.class.newInstance();
result.BSSID = ssid;
result.SSID = ssid;
result.level = level;
result.frequency = channelFreq;
result.centerFreq0 = channelFreq;
result.centerFreq1 = channelFreq;
result.channelWidth = ScanResult.CHANNEL_WIDTH_20MHZ;
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
DobbyLog.e("Unable to generate fake ScanResult");
}
return result;
}
use of android.net.wifi.ScanResult in project android_frameworks_base by crdroidandroid.
the class AccessPoint method getVisibilityStatus.
/**
* Returns the visibility status of the WifiConfiguration.
*
* @return autojoin debugging information
* TODO: use a string formatter
* ["rssi 5Ghz", "num results on 5GHz" / "rssi 5Ghz", "num results on 5GHz"]
* For instance [-40,5/-30,2]
*/
private String getVisibilityStatus() {
StringBuilder visibility = new StringBuilder();
StringBuilder scans24GHz = null;
StringBuilder scans5GHz = null;
String bssid = null;
long now = System.currentTimeMillis();
if (mInfo != null) {
bssid = mInfo.getBSSID();
if (bssid != null) {
visibility.append(" ").append(bssid);
}
visibility.append(" rssi=").append(mInfo.getRssi());
visibility.append(" ");
visibility.append(" score=").append(mInfo.score);
visibility.append(String.format(" tx=%.1f,", mInfo.txSuccessRate));
visibility.append(String.format("%.1f,", mInfo.txRetriesRate));
visibility.append(String.format("%.1f ", mInfo.txBadRate));
visibility.append(String.format("rx=%.1f", mInfo.rxSuccessRate));
}
int rssi5 = WifiConfiguration.INVALID_RSSI;
int rssi24 = WifiConfiguration.INVALID_RSSI;
int num5 = 0;
int num24 = 0;
int numBlackListed = 0;
// Number scan results we included in the string
int n24 = 0;
// Number scan results we included in the string
int n5 = 0;
Map<String, ScanResult> list = mScanResultCache.snapshot();
// TODO: sort list by RSSI or age
for (ScanResult result : list.values()) {
if (result.frequency >= LOWER_FREQ_5GHZ && result.frequency <= HIGHER_FREQ_5GHZ) {
// Strictly speaking: [4915, 5825]
// number of known BSSID on 5GHz band
num5 = num5 + 1;
} else if (result.frequency >= LOWER_FREQ_24GHZ && result.frequency <= HIGHER_FREQ_24GHZ) {
// Strictly speaking: [2412, 2482]
// number of known BSSID on 2.4Ghz band
num24 = num24 + 1;
}
if (result.frequency >= LOWER_FREQ_5GHZ && result.frequency <= HIGHER_FREQ_5GHZ) {
if (result.level > rssi5) {
rssi5 = result.level;
}
if (n5 < 4) {
if (scans5GHz == null)
scans5GHz = new StringBuilder();
scans5GHz.append(" \n{").append(result.BSSID);
if (bssid != null && result.BSSID.equals(bssid))
scans5GHz.append("*");
scans5GHz.append("=").append(result.frequency);
scans5GHz.append(",").append(result.level);
scans5GHz.append("}");
n5++;
}
} else if (result.frequency >= LOWER_FREQ_24GHZ && result.frequency <= HIGHER_FREQ_24GHZ) {
if (result.level > rssi24) {
rssi24 = result.level;
}
if (n24 < 4) {
if (scans24GHz == null)
scans24GHz = new StringBuilder();
scans24GHz.append(" \n{").append(result.BSSID);
if (bssid != null && result.BSSID.equals(bssid))
scans24GHz.append("*");
scans24GHz.append("=").append(result.frequency);
scans24GHz.append(",").append(result.level);
scans24GHz.append("}");
n24++;
}
}
}
visibility.append(" [");
if (num24 > 0) {
visibility.append("(").append(num24).append(")");
if (n24 <= 4) {
if (scans24GHz != null) {
visibility.append(scans24GHz.toString());
}
} else {
visibility.append("max=").append(rssi24);
if (scans24GHz != null) {
visibility.append(",").append(scans24GHz.toString());
}
}
}
visibility.append(";");
if (num5 > 0) {
visibility.append("(").append(num5).append(")");
if (n5 <= 4) {
if (scans5GHz != null) {
visibility.append(scans5GHz.toString());
}
} else {
visibility.append("max=").append(rssi5);
if (scans5GHz != null) {
visibility.append(",").append(scans5GHz.toString());
}
}
}
if (numBlackListed > 0)
visibility.append("!").append(numBlackListed);
visibility.append("]");
return visibility.toString();
}
use of android.net.wifi.ScanResult in project android_frameworks_base by crdroidandroid.
the class WifiTrackerTest method testSavedOnlyNoLooper.
/**
* This tests the case where Settings runs this on a non-looper thread for indexing.
*/
public void testSavedOnlyNoLooper() {
mWifiTracker = new WifiTracker(mContext, mWifiListener, mLooper, true, false, true, mWifiManager, null);
mWifiTracker.mScanner = mWifiTracker.new Scanner();
List<WifiConfiguration> wifiConfigs = new ArrayList<WifiConfiguration>();
List<ScanResult> scanResults = new ArrayList<ScanResult>();
generateTestNetworks(wifiConfigs, scanResults, true);
// Send all of the configs and scan results to the tracker.
Mockito.when(mWifiManager.getConfiguredNetworks()).thenReturn(wifiConfigs);
Mockito.when(mWifiManager.getScanResults()).thenReturn(scanResults);
mWifiTracker.forceUpdate();
List<AccessPoint> accessPoints = mWifiTracker.getAccessPoints();
// Only expect the first two to come back in the results.
assertEquals("Expected number of results", 2, accessPoints.size());
assertEquals(TEST_SSIDS[1], accessPoints.get(0).getSsid());
assertEquals(TEST_SSIDS[0], accessPoints.get(1).getSsid());
}
Aggregations