研究了下通過手動配置HTC的WiFi熱點程序 com.htc.WiFiRouter 裡面有一個Local DHCP的配置,把它勾上就能自動獲取IP。
但要怎麼樣用代碼實現呢這下坑爹了。
研究了半天,還是只有把HTC獨有的類給它全部dump出來,代碼如下
[java]
public void dumpHTCWifiFunction2(){
Log.e(TAG, "dumpHTCWifiFunction2++");
// Method[] classMethods = null;
Field localField1;
Field[] fieldlist;
WifiConfiguration apConfig = new WifiConfiguration();
try {
localField1 = WifiConfiguration.class.getDeclaredField("mWifiApProfile");
localField1.setAccessible(true);
Object localObject2 = localField1.get(apConfig);
localField1.setAccessible(false);
if(localObject2!=null){
fieldlist = localObject2.getClass().getDeclaredFields();
for (int i = 0;i < fieldlist.length; i++)
{
Log.e(TAG, "found api: "+ fieldlist[i].getName());
}
}
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.e(TAG, "dumpHTCWifiFunction2--");
}
12-28 11:06:29.956: E/MainActivity(3253): dumpHTCWifiFunction2++
12-28 11:06:29.966: E/MainActivity(3253): found api: DEFAULT_DHCP_MAX_AP_CONNECTION
12-28 11:06:29.966: E/MainActivity(3253): found api: DEFAULT_MAX_AP_CONNECTION
12-28 11:06:29.976: E/MainActivity(3253): found api: DHCP_MAX_AP_CONNECTION
12-28 11:06:29.976: E/MainActivity(3253): found api: MAX_AP_CONNECTION
12-28 11:06:29.976: E/MainActivity(3253): found api: OPEN
12-28 11:06:29.976: E/MainActivity(3253): found api: WEP
12-28 11:06:29.986: E/MainActivity(3253): found api: WPA
12-28 11:06:29.986: E/MainActivity(3253): found api: WPA2
12-28 11:06:29.986: E/MainActivity(3253): found api: BSSID
12-28 11:06:29.996: E/MainActivity(3253): found api: SSID
12-28 11:06:29.996: E/MainActivity(3253): found api: blocklist
12-28 11:06:29.996: E/MainActivity(3253): found api: whitelist
12-28 11:06:29.996: E/MainActivity(3253): found api: this$0
12-28 11:06:30.006: E/MainActivity(3253): found api: startingIP
12-28 11:06:30.006: E/MainActivity(3253): found api: dhcpSubnetMask
12-28 11:06:30.016: E/MainActivity(3253): found api: dnsIPAddr1
12-28 11:06:30.016: E/MainActivity(3253): found api: dnsIPAddr2
12-28 11:06:30.016: E/MainActivity(3253): found api: secureType
12-28 11:06:30.016: E/MainActivity(3253): found api: key
12-28 11:06:30.016: E/MainActivity(3253): found api: ipAddress
12-28 11:06:30.016: E/MainActivity(3253): found api: hiddenSSID
12-28 11:06:30.016: E/MainActivity(3253): found api: maxConns
12-28 11:06:30.016: E/MainActivity(3253): found api: maxDhcpClients
12-28 11:06:30.026: E/MainActivity(3253): found api: enableMacFilter
12-28 11:06:30.026: E/MainActivity(3253): found api: sleepPolicy
12-28 11:06:30.026: E/MainActivity(3253): found api: dhcpEnable
12-28 11:06:30.026: E/MainActivity(3253): found api: connectionArray
12-28 11:06:30.026: E/MainActivity(3253): found api: channel
12-28 11:06:30.026: E/MainActivity(3253): dumpHTCWifiFunction2--
發現了 dhcpEnable 這個成員變量,剛開始猜它是boolean變量,但錯了,在設置的時候會發生錯誤。
干脆把它的類型也打印出來吧,打印語句改為:
Log.e(TAG, "found api: "+ fieldlist[i].getName()+" type:"+fieldlist[i].toString());
found api: dhcpEnable type:public int android.net.wifi.WifiConfiguration$HotspotProfile.dhcpEnable
原來它是int型變量,估且猜它是1為enable. 設置之後,果然可以。
完整代碼如下:
[java]
private boolean setWifiSSIDForHTC(WifiConfiguration apConfig) {
boolean successed = true;
// WifiConfiguration mnetConfig = new WifiConfiguration();
Field localField1;
try {
localField1 = WifiConfiguration.class.getDeclaredField("mWifiApProfile");
localField1.setAccessible(true);
Object localObject2 = localField1.get(apConfig);
localField1.setAccessible(false);
if(localObject2!=null){
Field localField5 = localObject2.getClass().getDeclaredField("SSID");
localField5.setAccessible(true);
localField5.set(localObject2, apConfig.SSID);// netConfig.SSID);
localField5.setAccessible(false);
Field localField4 = localObject2.getClass().getDeclaredField("BSSID");
localField4.setAccessible(true);
localField4.set(localObject2, apConfig.BSSID);//netConfig.BSSID);
localField4.setAccessible(false);
Field localField6 = localObject2.getClass().getDeclaredField("dhcpEnable");
localField6.setAccessible(true);
// localField6.set(localObject2, "true");//netConfig.BSSID);
localField6.setInt(localObject2, 1);
localField6.setAccessible(false);
}
} catch(Exception e) {
e.printStackTrace();
}
return successed;
}