Java Code Examples for android.net.wifi.WifiManager#getScanResults()

The following examples show how to use android.net.wifi.WifiManager#getScanResults() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AppNetworkMgr.java    From AndroidWallet with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 过滤扫描结果
 *
 * @param context
 * @param bssid
 * @return
 */
public static ScanResult getScanResultsByBSSID(Context context, String bssid) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    ScanResult scanResult = null;
    boolean f = wifiManager.startScan();
    if (!f) {
        getScanResultsByBSSID(context, bssid);
    }
    List<ScanResult> list = wifiManager.getScanResults();
    if (list != null) {
        for (int i = 0; i < list.size(); i++) {
            scanResult = list.get(i);
            if (scanResult.BSSID.equals(bssid)) {
                break;
            }
        }
    }
    return scanResult;
}
 
Example 2
Source File: WifiInfo.java    From MobileInfo with Apache License 2.0 6 votes vote down vote up
private static void scanSuccess(WifiManager wifiManager, WifiBean wifiBean, long startTime, WifiScanListener wifiScanListener) {
    List<ScanResult> results = wifiManager.getScanResults();
    wifiBean.setWifiScanStatus(results.size() != 0);
    JSONArray scanArray = new JSONArray();
    for (ScanResult scanResult : results) {
        WifiBean.WifiResultBean wifiResultBean = new WifiBean.WifiResultBean();
        wifiResultBean.setBSSID(scanResult.BSSID);
        wifiResultBean.setSSID(scanResult.SSID);
        wifiResultBean.setCapabilities(scanResult.capabilities);
        wifiResultBean.setLevel(scanResult.level);
        scanArray.put(wifiResultBean.toJSONObject());
    }
    wifiBean.setWifiScanResult(scanArray);
    wifiBean.setTime(System.currentTimeMillis() - startTime);
    wifiScanListener.onResult(wifiBean.toJSONObject());

}
 
Example 3
Source File: MacAddressUtils.java    From CacheEmulatorChecker with Apache License 2.0 6 votes vote down vote up
public static String getConnectedWifiMacAddress(Application context) {
    String connectedWifiMacAddress = null;
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    List<ScanResult> wifiList;

    if (wifiManager != null) {
        wifiList = wifiManager.getScanResults();
        WifiInfo info = wifiManager.getConnectionInfo();
        if (wifiList != null && info != null) {
            for (int i = 0; i < wifiList.size(); i++) {
                ScanResult result = wifiList.get(i);
                if (!TextUtils.isEmpty(info.getBSSID()) && info.getBSSID().equals(result.BSSID)) {
                    connectedWifiMacAddress = result.BSSID;
                }
            }
        }
    }
    return connectedWifiMacAddress;
}
 
Example 4
Source File: NetworkHelper.java    From letv with Apache License 2.0 6 votes vote down vote up
public static boolean wifiConnection(Context context, String wifiSSID, String password) {
    WifiManager wifi = (WifiManager) context.getSystemService("wifi");
    String strQuotationSSID = "\"" + wifiSSID + "\"";
    WifiInfo wifiInfo = wifi.getConnectionInfo();
    if (wifiInfo != null && (wifiSSID.equals(wifiInfo.getSSID()) || strQuotationSSID.equals(wifiInfo.getSSID()))) {
        return true;
    }
    List<ScanResult> scanResults = wifi.getScanResults();
    if (scanResults == null || scanResults.size() == 0) {
        return false;
    }
    for (int nAllIndex = scanResults.size() - 1; nAllIndex >= 0; nAllIndex--) {
        String strScanSSID = ((ScanResult) scanResults.get(nAllIndex)).SSID;
        if (wifiSSID.equals(strScanSSID) || strQuotationSSID.equals(strScanSSID)) {
            WifiConfiguration config = new WifiConfiguration();
            config.SSID = strQuotationSSID;
            config.preSharedKey = "\"" + password + "\"";
            config.status = 2;
            return wifi.enableNetwork(wifi.addNetwork(config), false);
        }
    }
    return false;
}
 
Example 5
Source File: WifiAutoConnectManager.java    From gokit-android with MIT License 5 votes vote down vote up
public static WifiCipherType getCipherType(Context context, String ssid) {
	WifiManager wifiManager = (WifiManager) context
			.getSystemService(Context.WIFI_SERVICE);

	List<ScanResult> list = wifiManager.getScanResults();

	for (ScanResult scResult : list) {

		if (!TextUtils.isEmpty(scResult.SSID) && scResult.SSID.equals(ssid)) {
			String capabilities = scResult.capabilities;
			// Log.i("hefeng","capabilities=" + capabilities);

			if (!TextUtils.isEmpty(capabilities)) {

				if (capabilities.contains("WPA")
						|| capabilities.contains("wpa")) {
					Log.i("hefeng", "wpa");
					return WifiCipherType.WIFICIPHER_WPA;
				} else if (capabilities.contains("WEP")
						|| capabilities.contains("wep")) {
					Log.i("hefeng", "wep");
					return WifiCipherType.WIFICIPHER_WEP;
				} else {
					Log.i("hefeng", "no");
					return WifiCipherType.WIFICIPHER_NOPASS;
				}
			}
		}
	}
	return WifiCipherType.WIFICIPHER_INVALID;
}
 
Example 6
Source File: PlatformNetworksManager.java    From 365browser with Apache License 2.0 5 votes vote down vote up
static Set<VisibleWifi> getAllVisibleWifis(Context context, WifiManager wifiManager) {
    if (!hasLocationAndWifiPermission(context)) {
        return Collections.emptySet();
    }
    Set<VisibleWifi> visibleWifis = new HashSet<>();
    // Do not trigger a scan, but use current visible networks from latest scan.
    List<ScanResult> scanResults = wifiManager.getScanResults();
    if (scanResults == null) {
        return visibleWifis;
    }
    long elapsedTime = sTimeProvider.getElapsedRealtime();
    long currentTime = sTimeProvider.getCurrentTime();
    for (int i = 0; i < scanResults.size(); i++) {
        ScanResult scanResult = scanResults.get(i);
        String bssid = scanResult.BSSID;
        if (bssid == null) continue;
        Long scanResultTimestamp = scanResultTimestamp(scanResult);
        Long wifiTimestamp = null;
        if (scanResultTimestamp != null) {
            long ageMs = elapsedTime - TimeUnit.MICROSECONDS.toMillis(scanResultTimestamp);
            wifiTimestamp = currentTime - ageMs;
        }
        visibleWifis.add(
                VisibleWifi.create(scanResult.SSID, bssid, scanResult.level, wifiTimestamp));
    }
    return visibleWifis;
}
 
Example 7
Source File: NetUtil.java    From ESeal with Apache License 2.0 5 votes vote down vote up
/**
 * 获取所有搜索的WiFi的SSID
 */
public static List<String> getScanWifiSSIDList(Activity activity) {
    WifiManager wifiManager = (WifiManager) activity.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    List<String> result = new ArrayList<>();
    for (ScanResult scanResult :
            wifiManager.getScanResults()) {
        result.add(scanResult.SSID);
    }
    return result;
}
 
Example 8
Source File: NetUtil.java    From ESeal with Apache License 2.0 5 votes vote down vote up
/**
 * 获取所有搜索的WiFi的SSID
 */
public static List<String> getScanWifiSSIDList(Activity activity) {
    WifiManager wifiManager = (WifiManager) activity.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    List<String> result = new ArrayList<>();
    for (ScanResult scanResult :
            wifiManager.getScanResults()) {
        result.add(scanResult.SSID);
    }
    return result;
}
 
Example 9
Source File: WifiManagerModule.java    From react-native-wifi-manager with MIT License 5 votes vote down vote up
@ReactMethod
public void connect(String ssid, String password) {
  WifiManager mWifiManager = (WifiManager) getReactApplicationContext().getSystemService(Context.WIFI_SERVICE);
  List < ScanResult > results = mWifiManager.getScanResults();
  for (ScanResult result: results) {
    if (ssid.equals(result.SSID)) {
      connectTo(result, password, ssid);
    }
  }
}
 
Example 10
Source File: WifiReceiver.java    From ownmdm with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onReceive (Context context, Intent intent) {

	ArrayList<String> al = new ArrayList<String>();
	
       long now = 0;
       long timeDiff = 0;		

       Time t = new Time();
       t.setToNow();
       now = t.toMillis(false);

       timeDiff = now - Util.lastLocation;
       if (timeDiff > 60000) { // solo en 60 segundos
       	return;
       }
       
	try {
		al.clear();
        WifiManager w = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        List<ScanResult> l = w.getScanResults();
        for (ScanResult r : l) {
            al.add(r.level + " : " + r.SSID);
        }
        Collections.sort(al);
       	Util.logDebug("Wifi: " + al.toString());
		log(context, al.toString());
		
		// dejar wifi como estaba
		if (Util.wifiApagado)
			w.setWifiEnabled(false);
		
	} catch (Exception e) {
		Util.logDebug("Error al enviar desde update receiver: " + e.getMessage());
	}  
}
 
Example 11
Source File: WifiAutoConnectManager.java    From YiZhi with Apache License 2.0 5 votes vote down vote up
public static WifiCipherType getCipherType(Context context, String ssid) {
    WifiManager wifiManager = (WifiManager) context
            .getSystemService(Context.WIFI_SERVICE);

    List<ScanResult> list = wifiManager.getScanResults();

    for (ScanResult scResult : list) {

        if (!StringUtils.isEmpty(scResult.SSID) && scResult.SSID.equals(ssid)) {
            String capabilities = scResult.capabilities;

            if (!StringUtils.isEmpty(capabilities)) {

                if (capabilities.contains("WPA")
                        || capabilities.contains("wpa")) {
                    LogUtils.i("wpa");
                    return WifiCipherType.WIFICIPHER_WPA;
                } else if (capabilities.contains("WEP")
                        || capabilities.contains("wep")) {
                    LogUtils.i("wep");
                    return WifiCipherType.WIFICIPHER_WEP;
                } else {
                    LogUtils.i("no");
                    return WifiCipherType.WIFICIPHER_NOPASS;
                }
            }
        }
    }
    return WifiCipherType.WIFICIPHER_INVALID;
}
 
Example 12
Source File: WifiApListProvider.java    From PrivacyStreams with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent)
{
    WifiManager wifiMgr = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
    String name = wifiInfo.getSSID();
    for(ScanResult result: wifiMgr.getScanResults()){
        WifiApListProvider.this.output(new WifiAp(result, name.equals(result.SSID)));
    }
    WifiApListProvider.this.finish();
}
 
Example 13
Source File: WifiIntentReceiver.java    From find-client-android with MIT License 4 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {
    client = new FindWiFiImpl(getApplicationContext());

    // Getting all the value passed from previous Fragment
    eventName = intent.getStringExtra("event");
    userName = intent.getStringExtra("userName");
    groupName = intent.getStringExtra("groupName");
    serverName = intent.getStringExtra("serverName");
    locationName = intent.getStringExtra("locationName");
    Long timeStamp = System.currentTimeMillis()/1000;

    // getting all wifi APs and forming data payload
    try {
        mWifiData = new WifiData();
        mWifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
        JSONArray wifiResultsArray = new JSONArray();
        List<ScanResult> mResults = mWifiManager.getScanResults();

        for (ScanResult result : mResults) {
            JSONObject wifiResults = new JSONObject();
            if (shouldLog(result)) {
                wifiResults.put("mac", result.BSSID);
                wifiResults.put("rssi", result.level);
                wifiResultsArray.put(wifiResults);
            }
        }
        wifiFingerprint = new JSONObject();
        wifiFingerprint.put("group", groupName);
        wifiFingerprint.put("username", userName);
        wifiFingerprint.put("location", locationName);
        wifiFingerprint.put("time", timeStamp);
        wifiFingerprint.put("wifi-fingerprint", wifiResultsArray);
        Log.d(TAG, String.valueOf(wifiFingerprint));

    } catch (Exception ex) {
        ex.printStackTrace();
    }

    // Send the packet to server
    sendPayload(eventName, serverName, wifiFingerprint);

}
 
Example 14
Source File: WifiIntentReceiver.java    From find-client-android with MIT License 4 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {
    client = new FindWiFiImpl(getApplicationContext());

    // Getting all the value passed from previous Fragment
    eventName = intent.getStringExtra("event");
    userName = intent.getStringExtra("userName");
    groupName = intent.getStringExtra("groupName");
    serverName = intent.getStringExtra("serverName");
    locationName = intent.getStringExtra("locationName");
    Long timeStamp = System.currentTimeMillis()/1000;

    // getting all wifi APs and forming data payload
    try {
        mWifiData = new WifiData();
        mWifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
        JSONArray wifiResultsArray = new JSONArray();
        List<ScanResult> mResults = mWifiManager.getScanResults();

        for (ScanResult result : mResults) {
            JSONObject wifiResults = new JSONObject();
            if (shouldLog(result)) {
                wifiResults.put("mac", result.BSSID);
                wifiResults.put("rssi", result.level);
                wifiResultsArray.put(wifiResults);
            }
        }
        wifiFingerprint = new JSONObject();
        wifiFingerprint.put("group", groupName);
        wifiFingerprint.put("username", userName);
        wifiFingerprint.put("location", locationName);
        wifiFingerprint.put("time", timeStamp);
        wifiFingerprint.put("wifi-fingerprint", wifiResultsArray);
        Log.d(TAG, String.valueOf(wifiFingerprint));

    } catch (Exception ex) {
        ex.printStackTrace();
    }

    // Send the packet to server
    sendPayload(eventName, serverName, wifiFingerprint);

}
 
Example 15
Source File: WifiAdmin.java    From cordova-plugin-wifi with MIT License 4 votes vote down vote up
private PluginResult executeGetWifiInfo(JSONArray inputs, CallbackContext callbackContext) {
  	Log.w(LOGTAG, "executeGetWifiInfo");
  	
Context context = cordova.getActivity().getApplicationContext();
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();

JSONObject obj = new JSONObject();
try {
	JSONObject activity = new JSONObject();
	activity.put("BSSID", wifiInfo.getBSSID());
	activity.put("HiddenSSID", wifiInfo.getHiddenSSID());
	activity.put("SSID", wifiInfo.getSSID());
	activity.put("MacAddress", wifiInfo.getMacAddress());
	activity.put("IpAddress", wifiInfo.getIpAddress());
	activity.put("NetworkId", wifiInfo.getNetworkId());
	activity.put("RSSI", wifiInfo.getRssi());
	activity.put("LinkSpeed", wifiInfo.getLinkSpeed());
	obj.put("activity", activity); 

	JSONArray available = new JSONArray();
       for (ScanResult scanResult : wifiManager.getScanResults()) {
       	JSONObject ap = new JSONObject();
       	ap.put("BSSID", scanResult.BSSID);
       	ap.put("SSID", scanResult.SSID);
       	ap.put("frequency", scanResult.frequency);
       	ap.put("level", scanResult.level);
       	//netwrok.put("timestamp", String.valueOf(scanResult.timestamp));
       	ap.put("capabilities", scanResult.capabilities);
       	available.put(ap);
       }
       obj.put("available", available); 


} catch (JSONException e) {
	e.printStackTrace();
	callbackContext.error("JSON Exception");
}
callbackContext.success(obj);

  	return null;
  }
 
Example 16
Source File: WifiBase.java    From android-wifi-activity with Creative Commons Zero v1.0 Universal 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    WifiManager wifi = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
    List<ScanResult> scanResultList = wifi.getScanResults();
    boolean found = false;
    String security = null;
    for (ScanResult scanResult : scanResultList) {
        if (scanResult.SSID.equals(getWifiSSID())) {
            security = getScanResultSecurity(scanResult);
            found = true;
            break; // found don't need continue
        }
    }
    if (!found) {
        // if no wifi network with the specified ssid is not found exit
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
        new AlertDialog.Builder(context)
                .setCancelable(false)
                .setMessage(String.format(context.getString(R.string.wifi_not_found), getWifiSSID()))
                .setPositiveButton(context.getString(R.string.exit_app), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        getActivity().unregisterReceiver(ScanReceiver.this);
                        getActivity().finish();
                    }
                })
                .show();
    } else {
        // configure based on security
        final WifiConfiguration conf = new WifiConfiguration();
        conf.SSID = "\"" + getWifiSSID() + "\"";
        switch (security) {
            case WEP:
                conf.wepKeys[0] = "\"" + getWifiPass() + "\"";
                conf.wepTxKeyIndex = 0;
                conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
                break;
            case PSK:
                conf.preSharedKey = "\"" + getWifiPass() + "\"";
                break;
            case OPEN:
                conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                break;
        }
        try {context.unregisterReceiver(connectionReceiver);} catch (Exception e) {} // do nothing

        connectionReceiver = new ConnectionReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
        intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
        context.registerReceiver(connectionReceiver, intentFilter);
        int netId = wifi.addNetwork(conf);
        wifi.disconnect();
        wifi.enableNetwork(netId, true);
        wifi.reconnect();
        context.unregisterReceiver(this);
    }
}
 
Example 17
Source File: WifiSignalStrengthChanged.java    From NetworkEvents with Apache License 2.0 4 votes vote down vote up
public List<ScanResult> getWifiScanResults() {
  WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
  return wifiManager.getScanResults();
}
 
Example 18
Source File: AppNetworkMgr.java    From AndroidWallet with GNU General Public License v3.0 2 votes vote down vote up
/**
 * 获取wifi列表
 *
 * @param context
 * @return
 */
public static List<ScanResult> getWifiScanResults(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    return wifiManager.startScan() ? wifiManager.getScanResults() : null;
}
 
Example 19
Source File: NetUtils.java    From GOpenSource_AppKit_Android_AS with MIT License 2 votes vote down vote up
/**
 * 用来获得手机扫描到的所有wifi的信息.
 *
 * @param c
 *            上下文
 * @return the current wifi scan result
 */
static public List<ScanResult> getCurrentWifiScanResult(Context c) {
	WifiManager wifiManager = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
	wifiManager.startScan();
	return wifiManager.getScanResults();
}
 
Example 20
Source File: NetUtils.java    From Gizwits-SmartBuld_Android with MIT License 2 votes vote down vote up
/**
 * 用来获得手机扫描到的所有wifi的信息.
 *
 * @param c
 *            上下文
 * @return the current wifi scan result
 */
static public List<ScanResult> getCurrentWifiScanResult(Context c) {
	WifiManager wifiManager = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
	wifiManager.startScan();
	return wifiManager.getScanResults();
}