简体   繁体   English

Android WiFi连接信息

[英]Android WiFi Connection Info

I'm getting wifi connection info this way: 我通过这种方式获取wifi连接信息:

WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo();

But, could getConnectionInfo method return null in any case? 但是,在任何情况下getConnectionInfo方法都可以返回null吗? Android documentation reference say this: Android文档参考这样说:

Return dynamic information about the current Wi-Fi connection, if any is active. 返回有关当前Wi-Fi连接的动态信息(如果有)。

I guess if the device is not connected to any network it will return null , won't it? 我猜如果该设备未连接到任何网络,它将返回null ,不是吗?

Thanks 谢谢

Yes it will return null , if it is not connected to the wi-fi. 是的,如果未连接到Wi-Fi,它将返回null

But if you are also checking whether internet is available by using above code, then it is not 100% correct way to do so. 但是,如果您还通过使用上面的代码检查互联网是否可用,则不是100%正确的方法。 Cause it may happen that your device is connected to wifi but no internet is available. 原因可能是您的设备已连接到wifi,但没有互联网可用。 so for that you have to ping any live site (such as google , facebook , or any other trusted website) and check if the returned status code is 200. 因此,您必须ping任何实时网站(例如googlefacebook或任何其他受信任的网站),并检查返回的状态码是否为200。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM