如何设置android wifi热点的高级设置

时间:2012-04-04 12:54:51

标签: android android-wifi

我正在通过以下代码打开便携式wifi热点:

 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");
    }
}  

它对我有用.......热点开启......但是点击“菜单”按钮也有高级设置....这里有一个问题...... DHCP被禁用在LanSettings和Power Mode中工作只需要5分钟......我希望DHCP能够启用和PowerMode - “永远在线”...我该如何解决?

1 个答案:

答案 0 :(得分:1)

您可能需要查看Android源代码才能找到答案。您的代码是一个能够连接并获取IP地址的设备吗?如果是这种情况,那么AP的DHCP正在运行。

就个人而言,我甚至没有启用DHCP或禁用选项或ICS上的PowerMode。