Android 2.2 wifi热点API

时间:2022-04-16 14:33:20

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).

我需要在Android 2.2(Froyo)中创建一个用于创建Wifi热点的API调用(如Tethering和Portable Hotspot设置项中所示)。

3 个解决方案

#1


42  

You can call

你可以打电话

private boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled);

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

获取WifiManager后使用反射获取WifiManager声明的方法,查找此方法名称setWifiApEnabled并通过WifiManager对象调用它

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!

这些API标记为@hide,因此目前您无法直接使用它们,但它们出现在WifiManager的AIDL上,因此可以访问它们!

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?

它工作但我不能用我自己的配置改变当前的配置,并获得当前的活动AP的WifiConfiguration驱动我的空配置。为什么?

#2


4  

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.

这适用于API 8及更高版本。我使用了一个完全不同的版本然后在下面(或上面),并遇到了同样的问题markov00遇到;无法加载便携式Wi-Fi AP的默认WifiConfiguration。我在别处找到了解决方案。

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

#3


2  

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

似乎没有用于创建WiFi热点的API调用 - 抱歉!

#1


42  

You can call

你可以打电话

private boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled);

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

获取WifiManager后使用反射获取WifiManager声明的方法,查找此方法名称setWifiApEnabled并通过WifiManager对象调用它

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!

这些API标记为@hide,因此目前您无法直接使用它们,但它们出现在WifiManager的AIDL上,因此可以访问它们!

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?

它工作但我不能用我自己的配置改变当前的配置,并获得当前的活动AP的WifiConfiguration驱动我的空配置。为什么?

#2


4  

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.

这适用于API 8及更高版本。我使用了一个完全不同的版本然后在下面(或上面),并遇到了同样的问题markov00遇到;无法加载便携式Wi-Fi AP的默认WifiConfiguration。我在别处找到了解决方案。

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

#3


2  

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

似乎没有用于创建WiFi热点的API调用 - 抱歉!