Search in sources :

Example 86 with ScanResult

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

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

use of android.net.wifi.ScanResult in project dobby-android by InceptAi.

the class WifiState method updateWithScanResult.

private void updateWithScanResult(List<ScanResult> scanResultList) {
    for (ScanResult scanResult : scanResultList) {
        //Update the channel info stuff
        ChannelInfo channelInfo = channelInfoMap.get(scanResult.frequency);
        if (channelInfo == null) {
            channelInfo = new ChannelInfo(scanResult.frequency);
            channelInfoMap.put(scanResult.frequency, channelInfo);
        }
        channelInfo.numberAPs++;
        if (isAPStrong(scanResult.level)) {
            channelInfo.highestStrengthAps++;
        }
        channelInfo.contentionMetric = (channelInfo.contentionMetric * MOVING_AVERAGE_DECAY_FACTOR + computeContention(scanResult) * (100 - MOVING_AVERAGE_DECAY_FACTOR)) / 100;
        //Update the snr stuff
        String keyForSignalUpdating = scanResult.frequency + "-" + scanResult.BSSID;
        Integer signal = movingSignalAverage.get(keyForSignalUpdating);
        Long timestamp = lastSeenSignalTimestamp.get(keyForSignalUpdating);
        if (signal == null) {
            movingSignalAverage.put(keyForSignalUpdating, scanResult.level);
            lastSeenSignalTimestamp.put(keyForSignalUpdating, System.currentTimeMillis());
        } else {
            long currentTimestamp = System.currentTimeMillis();
            signal = Utils.computeMovingAverageSignal(scanResult.level, signal, currentTimestamp, timestamp, MAX_AGE_FOR_SIGNAL_UPDATING_MS);
            movingSignalAverage.put(keyForSignalUpdating, signal);
            lastSeenSignalTimestamp.put(keyForSignalUpdating, currentTimestamp);
        }
    }
}
Also used : ScanResult(android.net.wifi.ScanResult)

Example 88 with ScanResult

use of android.net.wifi.ScanResult in project dobby-android by InceptAi.

the class FakeWifiAnalyzer method getFakeScanResultForMainAP.

private ScanResult getFakeScanResultForMainAP() {
    String ssid = FAKE_WIFI_SCAN_CONFIG.mainApSSID;
    int level = getLevel(FAKE_WIFI_SCAN_CONFIG.signalZoneMainAp);
    int channelFreq = CHAN_1_FREQ;
    switch(FAKE_WIFI_SCAN_CONFIG.mainApChannelNumber) {
        case 1:
            channelFreq = CHAN_1_FREQ;
            break;
        case 6:
            channelFreq = CHAN_6_FREQ;
            break;
        case 11:
            channelFreq = CHAN_11_FREQ;
            break;
    }
    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;
}
Also used : ScanResult(android.net.wifi.ScanResult)

Example 89 with ScanResult

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

the class AccessPointTest method testOnLevelChanged.

public void testOnLevelChanged() {
    ScanResult result = new ScanResult();
    result.capabilities = "";
    result.SSID = TEST_SSID;
    // Give it a level.
    result.level = WifiTrackerTest.levelToRssi(1);
    mAccessPoint.update(result);
    verifyOnLevelChangedCallback(1);
    // Give it a better level.
    result.level = WifiTrackerTest.levelToRssi(2);
    mAccessPoint.update(result);
    verifyOnLevelChangedCallback(1);
}
Also used : ScanResult(android.net.wifi.ScanResult)

Example 90 with ScanResult

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

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);
            accessPoint.update(connectionConfig, mLastInfo, mLastNetworkInfo);
            if (mIncludeSaved) {
                // If saved network not present in scan result then set its Rssi to MAX_VALUE
                boolean apFound = false;
                for (ScanResult result : results) {
                    if (result.SSID.equals(accessPoint.getSsidStr())) {
                        apFound = true;
                        break;
                    }
                }
                if (!apFound) {
                    accessPoint.setRssi(Integer.MAX_VALUE);
                }
                accessPoints.add(accessPoint);
                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()) {
                    // name).
                    try {
                        WifiConfiguration config = mWifiManager.getMatchingWifiConfig(result);
                        if (config != null) {
                            accessPoint.update(config);
                        }
                    } catch (UnsupportedOperationException e) {
                    // Passpoint not supported on the device.
                    }
                }
                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)119 ArrayList (java.util.ArrayList)42 WifiConfiguration (android.net.wifi.WifiConfiguration)29 Scanner (com.android.settingslib.wifi.WifiTracker.Scanner)20 WifiManager (android.net.wifi.WifiManager)14 Test (org.junit.Test)11 AccessPoint (com.android.settingslib.wifi.AccessPoint)9 WifiInfo (android.net.wifi.WifiInfo)8 HashMap (java.util.HashMap)7 LargeTest (android.test.suitebuilder.annotation.LargeTest)6 Map (java.util.Map)6 Bundle (android.os.Bundle)5 SpannableString (android.text.SpannableString)5 Button (android.widget.Button)4 AlertDialog (androidx.appcompat.app.AlertDialog)4 Parcel (android.os.Parcel)3 InOrder (org.mockito.InOrder)3 SuppressLint (android.annotation.SuppressLint)2 GsmCellLocation (android.telephony.gsm.GsmCellLocation)2 ImageView (android.widget.ImageView)2