Android检测相同SSID下的wifi热点并切换到信号更强的

WifiManager mWifiManager = (WifiManager) context
        .getSystemService(Context.WIFI_SERVICE);

WifiInfo wifiInfo = mWifiManager.getConnectionInfo();

if (wifiInfo != null) {


    String curentSSID = wifiInfo.getSSID();

    List sameSSIDList = new ArrayList();
    List list = mWifiManager.getScanResults();

    if (list != null) {
        for (ScanResult rt : list) {

            if (curentSSID.replace("\"", "").equals(rt.SSID)) {
                sameSSIDList.add(rt);
            }
        }
    }

    if (sameSSIDList.size() >= 2) {
        ScanResult strongestRssi = sameSSIDList.get(0);
        Log.e("lihaile", "multiple ap");
        for (int i = 1; i <= (sameSSIDList.size() - 1); i++) {
            if (sameSSIDList.get(i).level > strongestRssi.level) {
                strongestRssi = sameSSIDList.get(i);
            }
        }

        if ((strongestRssi.level - wifiInfo.getRssi()) > 10) {
            Log.e("lihaile", "change ap");
            // MainActivity.appInfo("有更好的wifi");
            mWifiManager.disconnect();
            //  MainActivity.appInfo("正在切换");
            mWifiManager.reconnect();
        }
    }
}

你可能感兴趣的:(java,android,wifi,ssid)