Find available wifi connections in Android

时间:2015-05-24 21:47:38

标签: android networking android-wifi

I am trying to create a help wizard to recover from bad network connections in the app. One test case I hope to handle is the case where an end user has WiFi turned off, WiFi is available, and the mobile network is slower than the WiFi network. In this event, I want to be able to (1) discover the available WiFi network s, (2) find the WiFi network speed, (3) Compare its speed to the mobile network speed, (4) digest the user changes to the faster network.

For this to work, I need to know how to programmatically get information on available connections. Is that something we can do? If so, how can we tell what connections are available? Thanks in advance.

1 个答案:

答案 0 :(得分:4)

任务-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();

任务-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:切换到速度更快的选项

默认情况下,在Android中,如果打开并连接了wifi,那么您的移动网络将无法使用。因此,要使用移动数据,您必须断开所有可用的wifi网络或关闭wifi。

我还建议您阅读链接thisthisthis,以获取有关如何获得连接速度的更多信息。