Java Android 2.2 wifi热点API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3023226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:40:52  来源:igfitidea点击:

Android 2.2 wifi hotspot API

javaandroidwifihotspot

提问by Thomas

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) 中调用什么 API 来创建 Wifi 热点(如网络共享和便携式热点设置项中所示)。

采纳答案by markov00

You can call

你可以打电话

private boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled);

private boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled);

using reflection :)

使用反射:)

after getting the WifiManageruse the reflection to get the WifiManagerdeclared methods, look for this method name setWifiApEnabledand invoke it through the WifiManagerobject

获取后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 会将我驱动为空配置。为什么?

回答by CommonsWare

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

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

回答by slinden77

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