WifiManager mWifiManager = (WifiManager) context .getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); if (wifiInfo != null) { String curentSSID = wifiInfo.getSSID(); List<ScanResult> sameSSIDList = new ArrayList<ScanResult>(); List<ScanResult> 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(); } } }
转载于:https://my.oschina.net/u/815801/blog/1830525