2012-04-04 226 views
5

的高級設置,我打開便攜式WiFi熱點由下面的代碼:如何設置安卓wifihotspot

private void createWifiAccessPoint() { 
    WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE); 
    if(wifiManager.isWifiEnabled()) 
    { 
     wifiManager.setWifiEnabled(false);   
    }  
    Method[] wmMethods = wifiManager.getClass().getDeclaredMethods(); //Get all declared methods in WifiManager class  
    boolean methodFound=false; 
    for(Method method: wmMethods){ 
     if(method.getName().equals("setWifiApEnabled")){ 
      methodFound=true; 
      WifiConfiguration netConfig = new WifiConfiguration(); 
      netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); 
      netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN); 
      netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA); 
      netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 
      netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); 
      netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); 
      netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); 
      netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 
      try { 
       boolean apstatus=(Boolean) method.invoke(wifiManager, netConfig,true);   
       //statusView.setText("Creating a Wi-Fi Network \""+netConfig.SSID+"\""); 
       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); 
           //     netConfig=(WifiConfiguration)method1.invoke(wifi); 
           //statusView.append("\nSSID:"+netConfig.SSID+"\nPassword:"+netConfig.preSharedKey+"\n"); 
          } 
         } 
        } 
       } 
       if(apstatus) 
       { 
        System.out.println("SUCCESSdddd"); 
        //statusView.append("\nAccess Point Created!"); 
        //finish(); 
        //Intent searchSensorsIntent = new Intent(this,SearchSensors.class);    
        //startActivity(searchSensorsIntent); 
       }else 
       { 
        System.out.println("FAILED"); 
        //statusView.append("\nAccess Point Creation failed!"); 
       } 
      } catch (IllegalArgumentException e) { 
       e.printStackTrace(); 
      } catch (IllegalAccessException e) { 
       e.printStackTrace(); 
      } catch (InvocationTargetException e) { 
       e.printStackTrace(); 
      } 
     }  
    } 
    if(!methodFound){ 
     //statusView.setText("Your phone's API does not contain setWifiApEnabled method to configure an access point"); 
    } 
} 

它爲我.......熱點開啓......但也有先進的設置,通過點擊「菜單」按鈕....這裏有一個問題...在LanSettings中DHCP被禁用,而Power Mode只有5分鐘的工作....我希望DHCP tto被啓用和PowerMode - 「永遠在線」...我如何解決它?

+0

我在運行2.3.5的HTC手機上有完全相同的問題。你找到解決方案嗎? – Andy 2012-11-30 23:46:18

回答

1

您可能需要查看Android源代碼才能找到答案。你的代碼是一個能夠連接並獲得IP地址的設備嗎?如果是這種情況,那麼AP的DHCP正在工作。

就我個人而言,我甚至沒有DHCP啓用或禁用選項或ICS上的PowerMode。