Android 2.2 wifi热点API

发布于 2024-09-05 06:14:33 字数 75 浏览 8 评论 0原文

我需要在 Android 2.2 (Froyo) 中进行什么 API 调用来创建 Wifi 热点(如网络共享和便携式热点设置项中所示)。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

羅雙樹 2024-09-12 06:14:33

您可以在获取 WifiManager 后使用反射调用

private boolean setWifiApEnabled(WifiConfiguration wifiConfig, booleanenabled);

:) 使用

反射获取 WifiManager声明的方法,查找这个方法名setWifiApEnabled并通过WifiManager对象调用它。

这些API被标记为@hide,所以目前你不能直接使用它们,但它们出现在WifiManager 的 AIDL,以便可以访问它们!

一个例子是:

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

它可以工作,但我无法用自己的配置更改当前配置,并且获取活动 AP 的当前 WifiConfiguration 会将我带到空配置。为什么?

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?

只想待在家 2024-09-12 06:14:33

这适用于 API 8 及更高版本。我使用的版本与下面(或上面)的版本截然不同,并且遇到了与 markov00 遇到的相同问题;无法加载便携式 Wi-Fi AP 的默认 WifiConfiguration。我在其他地方找到了解决方案。

如果您喜欢这个解决方案,如果它被接受作为答案,那就太好了

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

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();
        }
    }
}
梦里兽 2024-09-12 06:14:33

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文