get wifi SSID List Contains Some - Android Wifi

Android examples for Wifi:Wifi SSID

Description

get wifi SSID List Contains Some

Demo Code


import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import java.util.ArrayList;
import java.util.List;

public class Main{
    public static boolean getSSIDListContainsSome(final Context context,
            ArrayList<String> ssids) {
        if (ssids == null)
            return false;
        for (String ssid : ssids) {
            if (getSSIDListContainsKey(context, ssid))
                return true;
        }/*from   w w w.j av a2 s . co  m*/
        return false;
    }
    
    public static boolean getSSIDListContainsKey(final Context context,
            String targetSSID) {
        if (targetSSID == null)
            return false;
        List<ScanResult> apList = WifiUtils.getScanResults(context);
        if (apList == null)
            return false;
        for (ScanResult ap : apList) {
            if (ap.SSID.equals(targetSSID)) {
                return true;
            }
        }
        return false;
    }
    
    public static List<ScanResult> getScanResults(final Context context) {
        WifiManager manager = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        if (manager.getWifiState() != WifiManager.WIFI_STATE_ENABLED)
            return null;
        // AP?
        context.enforceCallingOrSelfPermission(
                android.Manifest.permission.CHANGE_WIFI_STATE,
                "need permission: CHANGE_WIFI_STATE");
        manager.startScan();
        // 
        return manager.getScanResults();
    }
}

Related Tutorials