【问题标题】:Find available wifi connections in Android在 Android 中查找可用的 wifi 连接
【发布时间】:2023-03-29 14:51:01
【问题描述】:
我正在尝试创建一个帮助向导以从应用程序中的不良网络连接中恢复。我希望处理的一个测试案例是最终用户关闭 WiFi,WiFi 可用,并且移动网络比 WiFi 网络慢的情况。在这种情况下,我希望能够(1)发现可用的 WiFi 网络,(2)找到 WiFi 网络速度,(3)将其速度与移动网络速度进行比较,(4)消化用户对更快的网络。
为此,我需要知道如何以编程方式获取有关可用连接的信息。这是我们能做的吗?如果是这样,我们如何判断哪些连接可用?提前致谢。
【问题讨论】:
标签:
android
networking
android-wifi
【解决方案1】:
任务 1:发现可用的 WiFi 网络
这可以通过从系统中获取 WifiManager 的实例来完成。
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.getScanResults();
// The above is an async call and will results are available System will broadcast `SCAN_RESULTS_AVAILABLE` intent and you need to set a `BroadCastReceiver` for it.
// And get the results like this
List<ScanResult> results = wifiManager.getScanResults();
Task-2&3:求网速
This链接回答了你关于如何获得wifi和移动网络的网络速度的问题
无线网络:
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
int linkSpeed = wifiManager.getConnectionInfo().getRssi();
如果是移动设备,它应该可以工作:
TelephonyManager telephonyManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
CellInfoGsm cellinfogsm = (CellInfoGsm)telephonyManager.getAllCellInfo().get(0);
CellSignalStrengthGsm cellSignalStrengthGsm = cellinfogsm.getCellSignalStrength();
cellSignalStrengthGsm.getDbm();
Then You should compare this signal levels and if WIFI signal is better keep it turn on, but if mobile is better disconnect wifi
任务 4:切换到更高速度的选项
默认情况下,如果 wifi 已打开并已连接,则您的移动网络将不会被使用。因此,要使用移动数据,您必须断开所有可用的 wifi 网络或关闭 wifi。
我还建议您阅读链接this、this 和this,以获取有关如何获得连接速度的更多信息。