Search in sources :

Example 16 with ScanResult

use of android.net.wifi.ScanResult in project platform_frameworks_base by android.

the class WifiTrackerTest method testSavedOnly.

public void testSavedOnly() {
    mWifiTracker = new WifiTracker(mContext, mWifiListener, mLooper, true, false, true, mWifiManager, mMainLooper);
    mWifiTracker.mScanner = mWifiTracker.new Scanner();
    List<WifiConfiguration> wifiConfigs = new ArrayList<WifiConfiguration>();
    List<ScanResult> scanResults = new ArrayList<ScanResult>();
    generateTestNetworks(wifiConfigs, scanResults, true);
    // Tell WifiTracker we are connected now.
    sendConnected();
    // Send all of the configs and scan results to the tracker.
    Mockito.when(mWifiManager.getConfiguredNetworks()).thenReturn(wifiConfigs);
    Mockito.when(mWifiManager.getScanResults()).thenReturn(scanResults);
    sendScanResultsAndProcess(false);
    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());
}
Also used : Scanner(com.android.settingslib.wifi.WifiTracker.Scanner) ScanResult(android.net.wifi.ScanResult) WifiConfiguration(android.net.wifi.WifiConfiguration) ArrayList(java.util.ArrayList)

Example 17 with ScanResult

use of android.net.wifi.ScanResult in project platform_frameworks_base by android.

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;
}
Also used : WifiManager(android.net.wifi.WifiManager) ScanResult(android.net.wifi.ScanResult)

Example 18 with ScanResult

use of android.net.wifi.ScanResult in project android_frameworks_base by DirtyUnicorns.

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));
    }
}
Also used : ScanResult(android.net.wifi.ScanResult) Bundle(android.os.Bundle) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 19 with ScanResult

use of android.net.wifi.ScanResult in project android_frameworks_base by DirtyUnicorns.

the class WifiTracker method fetchScanResults.

private Collection<ScanResult> fetchScanResults() {
    mScanId++;
    final List<ScanResult> newResults = mWifiManager.getScanResults();
    if (newResults == null) {
        return null;
    }
    for (ScanResult newResult : newResults) {
        if (newResult.SSID == null || newResult.SSID.isEmpty()) {
            continue;
        }
        mScanResultCache.put(newResult.BSSID, newResult);
        mSeenBssids.put(newResult.BSSID, mScanId);
    }
    if (mScanId > NUM_SCANS_TO_CONFIRM_AP_LOSS) {
        if (DBG)
            Log.d(TAG, "------ Dumping SSIDs that were expired on this scan ------");
        Integer threshold = mScanId - NUM_SCANS_TO_CONFIRM_AP_LOSS;
        for (Iterator<Map.Entry<String, Integer>> it = mSeenBssids.entrySet().iterator(); it.hasNext(); ) /* nothing */
        {
            Map.Entry<String, Integer> e = it.next();
            if (e.getValue() < threshold) {
                ScanResult result = mScanResultCache.get(e.getKey());
                if (DBG)
                    Log.d(TAG, "Removing " + e.getKey() + ":(" + result.SSID + ")");
                mScanResultCache.remove(e.getKey());
                it.remove();
            }
        }
        if (DBG)
            Log.d(TAG, "---- Done Dumping SSIDs that were expired on this scan ----");
    }
    return mScanResultCache.values();
}
Also used : ScanResult(android.net.wifi.ScanResult) HashMap(java.util.HashMap) Map(java.util.Map)

Example 20 with ScanResult

use of android.net.wifi.ScanResult in project android_frameworks_base by DirtyUnicorns.

the class AccessPointTest method testOnAccessPointChangedCallback.

public void testOnAccessPointChangedCallback() {
    WifiInfo wifiInfo = Mockito.mock(WifiInfo.class);
    Mockito.when(wifiInfo.getNetworkId()).thenReturn(NETWORK_ID);
    mAccessPoint.update(wifiInfo, null);
    verifyOnAccessPointsCallback(1);
    mAccessPoint.update(null, null);
    verifyOnAccessPointsCallback(2);
    ScanResult result = new ScanResult();
    result.capabilities = "";
    result.SSID = TEST_SSID;
    mAccessPoint.update(result);
    verifyOnAccessPointsCallback(3);
}
Also used : ScanResult(android.net.wifi.ScanResult) WifiInfo(android.net.wifi.WifiInfo)

Aggregations

ScanResult (android.net.wifi.ScanResult)75 WifiConfiguration (android.net.wifi.WifiConfiguration)27 ArrayList (java.util.ArrayList)27 Scanner (com.android.settingslib.wifi.WifiTracker.Scanner)20 WifiManager (android.net.wifi.WifiManager)7 WifiInfo (android.net.wifi.WifiInfo)6 LargeTest (android.test.suitebuilder.annotation.LargeTest)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 Bundle (android.os.Bundle)5 SpannableString (android.text.SpannableString)5 DhcpInfo (android.net.DhcpInfo)1 NetworkInfo (android.net.NetworkInfo)1 Parcelable (android.os.Parcelable)1 NeighboringCellInfo (android.telephony.NeighboringCellInfo)1 TelephonyManager (android.telephony.TelephonyManager)1 GsmCellLocation (android.telephony.gsm.GsmCellLocation)1 ImageView (android.widget.ImageView)1 RelativeLayout (android.widget.RelativeLayout)1 TextView (android.widget.TextView)1