1

您好我正在尝试从我的应用程序创建一个接入点。我可以站起来 AP,但不能使用我设置的配置。我正在使用 HTC Sensation XE

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("CLIENT", "\nSSID:"+netConfig.SSID+"\nPassword:"+netConfig.preSharedKey+"\n");

我得到:

nSSID:空 nPassword:空

4

1 回答 1

2

我的 HTC Desire 和 Android 2.2.2 也有同样的问题。似乎很多(或所有)HTC 设备都无法以这种方式进行配置。HTC 可能重写了一些代码并限制了一些隐藏的功能。您正在使用通过反射在 API(例如 setWifiApEnabled)中未正式使用的方法。在我的情况下,将创建热点,但使用默认配置。可能你有同样的问题。它适用于其他一些设备

于 2012-11-29T10:34:31.637 回答