Search in sources :

Example 51 with ScanResult

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

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 52 with ScanResult

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

the class RecommendationRequestTest method setUp.

@Override
public void setUp() throws Exception {
    mScanResults = new ScanResult[2];
    mScanResults[0] = new ScanResult();
    mScanResults[1] = new ScanResult("ssid", "bssid", 0L, /*hessid*/
    1, /*anqpDominId*/
    "caps", 2, /*level*/
    3, /*frequency*/
    4L, /*tsf*/
    5, /*distCm*/
    6, /*distSdCm*/
    7, /*channelWidth*/
    8, /*centerFreq0*/
    9, /*centerFreq1*/
    false);
    mDefaultConfig = new WifiConfiguration();
    mDefaultConfig.SSID = "default_config";
    mConnectedConfig = new WifiConfiguration();
    mConnectedConfig.SSID = "connected_config";
    mConnectableConfigs = new WifiConfiguration[] { mDefaultConfig, mConnectedConfig };
    mLastSelectedNetworkId = 5;
    mLastSelectedNetworkTimestamp = SystemClock.elapsedRealtime();
}
Also used : ScanResult(android.net.wifi.ScanResult) WifiConfiguration(android.net.wifi.WifiConfiguration)

Example 53 with ScanResult

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

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 54 with ScanResult

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

the class WifiTracker method fetchScanResults.

private Collection<ScanResult> fetchScanResults() {
    mScanId++;
    final List<ScanResult> newResults = mWifiManager.getScanResults();
    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 55 with ScanResult

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

the class WifiTracker method updateAccessPoints.

private void updateAccessPoints() {
    // Swap the current access points into a cached list.
    List<AccessPoint> cachedAccessPoints = getAccessPoints();
    ArrayList<AccessPoint> accessPoints = new ArrayList<>();
    // Clear out the configs so we don't think something is saved when it isn't.
    for (AccessPoint accessPoint : cachedAccessPoints) {
        accessPoint.clearConfig();
    }
    /** Lookup table to more quickly update AccessPoints by only considering objects with the
         * correct SSID.  Maps SSID -> List of AccessPoints with the given SSID.  */
    Multimap<String, AccessPoint> apMap = new Multimap<String, AccessPoint>();
    WifiConfiguration connectionConfig = null;
    if (mLastInfo != null) {
        connectionConfig = getWifiConfigurationForNetworkId(mLastInfo.getNetworkId());
    }
    final Collection<ScanResult> results = fetchScanResults();
    final List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
    if (configs != null) {
        mSavedNetworksExist = configs.size() != 0;
        for (WifiConfiguration config : configs) {
            if (config.selfAdded && config.numAssociation == 0) {
                continue;
            }
            AccessPoint accessPoint = getCachedOrCreate(config, cachedAccessPoints);
            if (mLastInfo != null && mLastNetworkInfo != null) {
                if (config.isPasspoint() == false) {
                    accessPoint.update(connectionConfig, mLastInfo, mLastNetworkInfo);
                }
            }
            if (mIncludeSaved) {
                if (!config.isPasspoint() || mIncludePasspoints) {
                    // If saved network not present in scan result then set its Rssi to MAX_VALUE
                    boolean apFound = false;
                    for (ScanResult result : results) {
                        if (accessPoint.matches(result)) {
                            apFound = true;
                            break;
                        }
                    }
                    if (!apFound) {
                        accessPoint.setRssi(Integer.MAX_VALUE);
                    }
                    accessPoints.add(accessPoint);
                }
                if (config.isPasspoint() == false) {
                    apMap.put(accessPoint.getSsidStr(), accessPoint);
                }
            } else {
                // If we aren't using saved networks, drop them into the cache so that
                // we have access to their saved info.
                cachedAccessPoints.add(accessPoint);
            }
        }
    }
    if (results != null) {
        for (ScanResult result : results) {
            // Ignore hidden and ad-hoc networks.
            if (result.SSID == null || result.SSID.length() == 0 || result.capabilities.contains("[IBSS]")) {
                continue;
            }
            boolean found = false;
            for (AccessPoint accessPoint : apMap.getAll(result.SSID)) {
                if (accessPoint.update(result)) {
                    found = true;
                    break;
                }
            }
            if (!found && mIncludeScans) {
                AccessPoint accessPoint = getCachedOrCreate(result, cachedAccessPoints);
                if (mLastInfo != null && mLastNetworkInfo != null) {
                    accessPoint.update(connectionConfig, mLastInfo, mLastNetworkInfo);
                }
                if (result.isPasspointNetwork()) {
                    WifiConfiguration config = mWifiManager.getMatchingWifiConfig(result);
                    if (config != null && config.SSID.equals(result.SSID)) {
                        accessPoint.update(config);
                    }
                }
                if (mLastInfo != null && mLastInfo.getBSSID() != null && mLastInfo.getBSSID().equals(result.BSSID) && connectionConfig != null && connectionConfig.isPasspoint()) {
                    /* This network is connected via this passpoint config */
                    /* SSID match is not going to work for it; so update explicitly */
                    accessPoint.update(connectionConfig);
                }
                accessPoints.add(accessPoint);
                apMap.put(accessPoint.getSsidStr(), accessPoint);
            }
        }
    }
    // Pre-sort accessPoints to speed preference insertion
    Collections.sort(accessPoints);
    // Log accesspoints that were deleted
    if (DBG)
        Log.d(TAG, "------ Dumping SSIDs that were not seen on this scan ------");
    for (AccessPoint prevAccessPoint : mAccessPoints) {
        if (prevAccessPoint.getSsid() == null)
            continue;
        String prevSsid = prevAccessPoint.getSsidStr();
        boolean found = false;
        for (AccessPoint newAccessPoint : accessPoints) {
            if (newAccessPoint.getSsid() != null && newAccessPoint.getSsid().equals(prevSsid)) {
                found = true;
                break;
            }
        }
        if (!found)
            if (DBG)
                Log.d(TAG, "Did not find " + prevSsid + " in this scan");
    }
    if (DBG)
        Log.d(TAG, "---- Done dumping SSIDs that were not seen on this scan ----");
    mAccessPoints = accessPoints;
    mMainHandler.sendEmptyMessage(MainHandler.MSG_ACCESS_POINT_CHANGED);
}
Also used : ScanResult(android.net.wifi.ScanResult) WifiConfiguration(android.net.wifi.WifiConfiguration) ArrayList(java.util.ArrayList)

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