问题
I'm trying to run a hotspot with a new name and open accessibility.
wifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wifiConfig.SSID = "\"MySSID\"";
wifiConfig.networkId = 1;
methodNum = getMethodNumber("setWifiApEnabled");
try {
wmMethods[methodNum].invoke(wifiManager, wifiConfig, true);
} catch (IllegalArgumentException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InvocationTargetException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
I get the right method and it seems like it starts the hotspot on the phone, but the configuration doesn't change.
I tried to get the current configuration data with getWifiApConfiguration and i get nothing with it, no ssid and not the current encryption.
I'm using HTC Evo 3d for the debugging.
回答1:
Some htc phones seems to use a class of type HotspotProfile to keep its configuration. So, before calling setWifiApEnabled, you need set the ssid in htc's way:
if (isHtc) setHTCSSID(config);
methodNum = getMethodNumber("setWifiApEnabled");
try {
wmMethods[methodNum].invoke(wifiManager, wifiConfig, true);
...
isHtc can be calculated like:
try { isHtc = null!=WifiConfiguration.class. getDeclaredField("mWifiApProfile"); }
catch (java.lang.NoSuchFieldException e) { isHtc = false }
and setHTCSSID would be:
public void setHTCSSID(WifiConfiguration config) {
try {
Field mWifiApProfileField = WifiConfiguration.class.getDeclaredField("mWifiApProfile");
mWifiApProfileField.setAccessible(true);
Object hotSpotProfile = mWifiApProfileField.get(config);
mWifiApProfileField.setAccessible(false);
if(hotSpotProfile!=null){
Field ssidField = hotSpotProfile.getClass().getDeclaredField("SSID");
ssidField.setAccessible(true);
ssidField.set(hotSpotProfile, config.SSID);
ssidField.setAccessible(false);
}
} catch(Exception e) {
e.printStackTrace();
}
}
I found this information in some chinese blogs: http://xiaxingwork.iteye.com/blog/1727722 and http://blog.sina.com.cn/s/blog_53dd443a010109i8.html
回答2:
It seems like its a problem with HTC. Some of my friends tried similar code on HTC and other devices. Didnt work on HTC, worked on the others.
来源:https://stackoverflow.com/questions/12823083/change-configuration-of-mobile-hotspot