Android 2.2 wifi hotspot API

后端 未结 3 1319
梦毁少年i
梦毁少年i 2020-11-29 16:32

What is the API call I need to make in Android 2.2 (Froyo) to create a Wifi hotspot (as seen in the Tethering and Portable Hotspot settings item).

相关标签:
3条回答
  • 2020-11-29 17:18

    There does not appear to be an API call to create a WiFi hotspot -- sorry!

    0 讨论(0)
  • 2020-11-29 17:25

    You can call

    private boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled);

    using reflection :)

    after getting the WifiManager use the reflection to get the WifiManager declared methods, look for this method name setWifiApEnabled and invoke it through the WifiManager object

    These API are marked as @hide, so currently you cannot use them directly, but they appear on the AIDL for the WifiManager so their are accessible!

    An example can be:

    WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    Method[] wmMethods = wifi.getClass().getDeclaredMethods();
    for(Method method: wmMethods){
      if(method.getName().equals("setWifiApEnabled")){
        WifiConfiguration netConfig = new WifiConfiguration();
        netConfig.SSID = "\"PROVAAP\"";
        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 {
          method.invoke(wifi, netConfig,true);
        } catch (IllegalArgumentException e) {
          e.printStackTrace();
        } catch (IllegalAccessException e) {
          e.printStackTrace();
        } catch (InvocationTargetException e) {
          e.printStackTrace();
        }
      }
    }
    

    It works but I cannot change the current configuration with my own, and getting the current WifiConfiguration of an active AP drive me to an empty configuration.Why?

    0 讨论(0)
  • 2020-11-29 17:30

    this works on API 8 and above. I use a heavily different version then this below (or above), and was running into the same issue markov00 ran into; not being able to load the default WifiConfiguration for the portable Wi-Fi AP. I found a solution elsewhere.

    If you like the solution, it would be nice if this was accepted as an answer

    WifiManager wifi    = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    Method[] wmMethods  = wifi.getClass().getDeclaredMethods();
    
    for (Method method: wmMethods){
        if (method.getName().equals("setWifiApEnabled")){
            try {
                // just nullify WifiConfiguration to load the default configuration ;)
                method.invoke(wifi, null, true);
            } catch (IllegalArgumentException e){
                e.printStackTrace();
            } catch (IllegalAccessException e){
                e.printStackTrace();
            } catch (InvocationTargetException e){
                e.printStackTrace();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题