如何在android中获取wifi配置文件位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12564423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the wifi configuration file location in android
提问by Mahmoud Jorban
I'm developing an app that backup the wifi configuration from any android device (rooted) so I want to know how to get the file location in the android device so can I deal with it.
我正在开发一个应用程序,它可以从任何 android 设备(root 用户)备份 wifi 配置,所以我想知道如何在 android 设备中获取文件位置,以便我可以处理它。
I know there is a lot of location depending on your ROM or device
我知道有很多位置取决于您的 ROM 或设备
like /data/wifi/bcm_supp.conf
or /data/misc/wifi/wpa_supplicant.conf
喜欢/data/wifi/bcm_supp.conf
或/data/misc/wifi/wpa_supplicant.conf
but I want to get it dynamically .
但我想动态获取它。
采纳答案by sachin10
You need to create a WifiConfiguration
instance like this:
您需要创建一个WifiConfiguration
这样的实例:
String networkSSID = "test";
String networkPass = "pass";
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\""; //
Then, for WEP network you need to do this:
然后,对于 WEP 网络,您需要这样做:
conf.wepKeys[0] = "\"" + networkPass + "\"";
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
For WPA network you need to add passphrase like this:
对于 WPA 网络,您需要像这样添加密码:
conf.preSharedKey = "\""+ networkPass +"\"";
For Open network you need to do this:
对于开放网络,您需要执行以下操作:
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
Then, you need to add it to Android Wi-Fi manager settings:
然后,您需要将其添加到 Android Wi-Fi 管理器设置中:
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.add(conf);
And finally, you might need to enable it, so Android connects to it:
最后,您可能需要启用它,以便 Android 连接到它:
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for (WifiConfiguration i : list) {
if (i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
wm.disconnect();
wm.enableNetwork(i.networkId, true);
wm.reconnect();
break;
}
}
In case of WEP, if your password is in hex, you do not need to surround it with quotation marks.
在 WEP 的情况下,如果您的密码是十六进制的,则不需要用引号将其括起来。