HTC Android Hotspot 热点配置 之DHCP(包括如何让其它手机连接上去)

最近在研究手机热点的建立。HTC手机比较坑爹,首先一个,是不能按常规的办法修改 SSID, BSSID,

在网上搜了下,有篇博客把这个问题解决了,请参考:http://blog.csdn.net/mengweiqi33/article/details/8494441


然后就发现HTC手机热点是建好了,但是其他手机去连它,怎么也连不上,后来发现,使用静态IP能解决这个问题,但太麻烦了。

研究了下通过手动配置HTC的WiFi热点程序 com.htc.WiFiRouter 里面有一个Local DHCP的配置,把它勾上就能自动获取IP。

但要怎么样用代码实现呢这下坑爹了。


研究了半天,还是只有把HTC独有的类给它全部dump出来,代码如下


[java]  view plain copy
  1. public void dumpHTCWifiFunction2(){  
  2.          Log.e(TAG, "dumpHTCWifiFunction2++");  
  3. //      Method[] classMethods = null;  
  4.         Field localField1;  
  5.           
  6.         Field[] fieldlist;  
  7.         WifiConfiguration apConfig = new WifiConfiguration();  
  8.         try {  
  9.             localField1 = WifiConfiguration.class.getDeclaredField("mWifiApProfile");  
  10.               localField1.setAccessible(true);  
  11.   
  12.                 Object localObject2 = localField1.get(apConfig);  
  13.   
  14.                 localField1.setAccessible(false);  
  15.                   
  16.                 if(localObject2!=null){  
  17.                     fieldlist = localObject2.getClass().getDeclaredFields();  
  18.                       
  19.                     for (int i = 0;i < fieldlist.length; i++)  
  20.   
  21.                     {  
  22.   
  23.                         Log.e(TAG, "found api: "+ fieldlist[i].getName());  
  24.   
  25.                     }  
  26.                 }  
  27.                   
  28.                   
  29.         } catch (NoSuchFieldException e) {  
  30.             // TODO Auto-generated catch block  
  31.             e.printStackTrace();  
  32.         } catch (IllegalArgumentException e) {  
  33.             // TODO Auto-generated catch block  
  34.             e.printStackTrace();  
  35.         } catch (IllegalAccessException e) {  
  36.             // TODO Auto-generated catch block  
  37.             e.printStackTrace();  
  38.         }  
  39.   
  40.         Log.e(TAG, "dumpHTCWifiFunction2--");  
  41.     }  


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]  view plain copy
  1. private boolean setWifiSSIDForHTC(WifiConfiguration apConfig) {  
  2.         boolean successed = true;  
  3.           
  4. //      WifiConfiguration mnetConfig = new WifiConfiguration();  
  5.         Field localField1;  
  6.   
  7.                         try {  
  8.   
  9.                             localField1 = WifiConfiguration.class.getDeclaredField("mWifiApProfile");  
  10.   
  11.                             localField1.setAccessible(true);  
  12.   
  13.                             Object localObject2 = localField1.get(apConfig);  
  14.   
  15.                             localField1.setAccessible(false);  
  16.                             if(localObject2!=null){  
  17.   
  18.                                 Field localField5 = localObject2.getClass().getDeclaredField("SSID");  
  19.   
  20.                                 localField5.setAccessible(true);  
  21.   
  22.                                 localField5.set(localObject2, apConfig.SSID);// netConfig.SSID);  
  23.   
  24.                                 localField5.setAccessible(false);  
  25.   
  26.                                 Field localField4 = localObject2.getClass().getDeclaredField("BSSID");  
  27.   
  28.                                 localField4.setAccessible(true);  
  29.   
  30.                                 localField4.set(localObject2, apConfig.BSSID);//netConfig.BSSID);  
  31.   
  32.                                 localField4.setAccessible(false);  
  33.                                   
  34.                                 Field localField6 = localObject2.getClass().getDeclaredField("dhcpEnable");  
  35.   
  36.                                 localField6.setAccessible(true);  
  37.   
  38. //                                localField6.set(localObject2, "true");//netConfig.BSSID);  
  39.                                 localField6.setInt(localObject2, 1);  
  40.   
  41.                                 localField6.setAccessible(false);  
  42.   
  43.                             }  
  44.   
  45.                         } catch(Exception e) {  
  46.   
  47.                             e.printStackTrace();  
  48.   
  49.                         }  
  50.         return successed;  
  51.     }  
转自: http://blog.csdn.net/wchinaw/article/details/8446443

你可能感兴趣的:(HTC Android Hotspot 热点配置 之DHCP(包括如何让其它手机连接上去))