2012-04-04 23 views
5

Tôi quay cầm tay wifi hotspot ON bởi đoạn mã sau:làm thế nào để thiết lập cài đặt nâng cao của android 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"); 
    } 
} 

Nó làm việc cho tôi ....... Hotspot bật ...... Nhưng cũng có cài đặt nâng cao bằng cách nhấp vào nút 'menu' .... Và đây là một vấn đề ... DHCP bị vô hiệu hóa trong LanSettings và Power Mode chỉ 5 phút làm việc trên .... Tôi muốn DHCP được ENABLED và PowerMode - "luôn luôn" ... Làm thế nào tôi có thể giải quyết nó?

+0

Tôi gặp sự cố tương tự trên điện thoại HTC chạy 2.3.5. Bạn đã tìm thấy một giải pháp? – Andy

Trả lời

1

Bạn có thể phải xem qua mã nguồn Android để tìm câu trả lời cho điều này. Với mã của bạn là một thiết bị có thể kết nối và nhận được một địa chỉ IP? Nếu đúng như vậy thì DHCP cho AP đang hoạt động.

Cá nhân tôi thậm chí không có tùy chọn bật hoặc tắt DHCP hoặc PowerMode trên ICS.

Các vấn đề liên quan