2012-08-23 46 views
1

你好,我試圖從我的應用程序創建一個接入點。我可以站在AP上,但不能使用我設置的配置。我使用的是HTC感覺XEAndroid 4.0.3創建wifi熱點API

WifiManager wifiManager = (WifiManager) rc.getSystemService(Context.WIFI_SERVICE); 

if(wifiManager.isWifiEnabled()) 
{ 
    wifiManager.setWifiEnabled(false); 
} 
Method[] wmMethods = wifiManager.getClass().getDeclaredMethods(); 
boolean methodFound=false; 

WifiConfiguration netConfig = new WifiConfiguration(); 

netConfig.SSID = "MyWifiAP"; 
netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); 
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN); 
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA); 
netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 
for(Method method: wmMethods){ 
if(method.getName().equals("setWifiApEnabled")){     
    methodFound=true; 
    try { 
    boolean apstatus=(Boolean) method.invoke(wifiManager, netConfig,true); 
    for (Method isWifiApEnabledmethod: wmMethods){ 
     if(isWifiApEnabledmethod.getName().equals("isWifiApEnabled")){ 
      while(!(Boolean)isWifiApEnabledmethod.invoke(wifiManager)){ 
      }; 
      for(Method method1: wmMethods){ 
       if(method1.getName().equals("getWifiApState")){ 
       int apstate; 
       apstate=(Integer)method1.invoke(wifiManager); 
       for(Method method2: wmMethods){ 
        if(method2.getName().equals("getWifiApConfiguration")){ 
         try { 
         netConfig=(WifiConfiguration)method2.invoke(wifiManager); 
         } catch (IllegalArgumentException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } catch (IllegalAccessException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } catch (InvocationTargetException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
         Log.e("CLIENT", "\nSSID:"+netConfig.SSID+"\nPassword:"+netConfig.preSharedKey+"\n");           

         if (apstate==0) { 
          Log.d("basura", "apstate es: "+apstate); 
         } 
        } 
       } 
       } 
      } 
     } catch (IllegalArgumentException e) { 
      e.printStackTrace(); 
     } catch (IllegalAccessException e) { 
      e.printStackTrace(); 
     } catch (InvocationTargetException e) { 
      e.printStackTrace(); 
     } 
    }  
} 

當我打印:

Log.e( 「客戶」, 「\ nSSID:」 + netConfig.SSID + 「\ n密碼:」 + netConfig.preSharedKey +「\ N「);

我得到:

nSSID:空 n密碼:空

回答

2

我有我的HTC Desire和Android 2.2.2同樣的問題。看來許多(或全部)HTC設備不能這樣配置。 HTC可能已經重寫了一些代碼並限制了一些隱藏功能。你正在使用的方法,它不是通過反射API(例如setWifiApEnabled)。在我的情況下,熱點將被創建,但使用默認配置。可能你有同樣的問題。它將適用於其他一些設備,如