安卓4.0.3创建WiFi热点API [英] Android 4.0.3 Create wifi hotspot API

查看:129
本文介绍了安卓4.0.3创建WiFi热点API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想创建我的应用程序的接入点。我可以站起来的AP,但不与我设置的配置。我使用的是HTC感觉XE

Hello I'm trying to create an access point from my app. I can stand up the AP but not with the configuration that I set. I am using an htc Sensation XE

WifiManager wifiManager = (WifiManager) rc.getSystemService(Context.WIFI_SERVICE);

if(wifiManager.isWifiEnabled())
{
    wifiManager.setWifiEnabled(false); 
}
Method[] wmMethods = wifiManager.getClass().getDeclaredMethods();
boolean methodFound=false;

WifiConfiguration netConfig = new WifiConfiguration();

netConfig.SSID = "MyWifiAP";
netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
for(Method method: wmMethods){
if(method.getName().equals("setWifiApEnabled")){                   
  methodFound=true;
  try {
  boolean apstatus=(Boolean) method.invoke(wifiManager, netConfig,true);
  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);
                for(Method method2: wmMethods){
                   if(method2.getName().equals("getWifiApConfiguration")){
                      try {
                         netConfig=(WifiConfiguration)method2.invoke(wifiManager);
                      } catch (IllegalArgumentException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      } catch (IllegalAccessException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      } catch (InvocationTargetException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
                      Log.e("CLIENT", "\nSSID:"+netConfig.SSID+"\nPassword:"+netConfig.preSharedKey+"\n");                                          

                      if (apstate==0) {
                          Log.d("basura", "apstate es: "+apstate);
                      }
                   }
                }   
              }   
           }   
        } catch (IllegalArgumentException e) {
           e.printStackTrace();
        } catch (IllegalAccessException e) {
           e.printStackTrace();
        } catch (InvocationTargetException e) {
           e.printStackTrace();
        }
    }      
}

当我打印:

Log.e(客户,\ nSSID:+ netConfig.SSID +\ n密码:+ netconfig的preSharedKey +\ N);

Log.e("CLIENT", "\nSSID:"+netConfig.SSID+"\nPassword:"+netConfig.preSharedKey+"\n");

我得到:

nSSID:空 n密码:空

nSSID:null nPassword:null

推荐答案

我有同样的问题,我的HTC Desire和Android 2.2.2。似乎很多(或全部)HTC设备不能被这种方式配置。 HTC可能已经改写了一些code和限制一些隐藏的功能。您正在使用的方法,这是不offically通过反射API(如setWifiApEnabled)研究。在我的情况下,热点将被创建,但与默认配置。也许你有同样的问题。这将适用于其他一些设备认为

I have the same problem with my HTC Desire and Android 2.2.2. It seems that many(or all) HTC devices can't be configured this way. HTC has probably rewrote some of the code and restrict some of the hidden features. You are using methods, which are not offically in the API (eg setWifiApEnabled) through reflection. In my case the hotspot will be created, but with the default configuration. Probably you have the same issue. It will work for some other devices thought

这篇关于安卓4.0.3创建WiFi热点API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆