以编程方式连接到便携式热点中的Android设备

以编程方式连接到便携式热点中的Android设备,第1张

概述我已经成功地在我的设备上使用指定的SSID创建了一个可移植的热点.现在我想从另一台设备连接到它!我正在使用此代码:WifiConfigurationconf=newWifiConfiguration();conf.SSID="\""+"TinyBox"+"\"";conf.allowedKeyManagement.set(WifiConfiguration.KeyMgm

我已经成功地在我的设备上使用指定的SSID创建了一个可移植的热点.现在我想从另一台设备连接到它!我正在使用此代码:

    WifiConfiguration conf = new WifiConfiguration();    conf.SSID = "\"" + "TinyBox" + "\"";    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);    WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);     wifiManager.addNetwork(conf);    List<WifiConfiguration> List = wifiManager.getConfigurednetworks();    for( WifiConfiguration i : List ) {        if(i.SSID != null && i.SSID.equals("\"" + "TinyBox" + "\"")) {             wifiManager.disconnect();             wifiManager.enableNetwork(i.networkID, true);             wifiManager.reconnect();                            break;        }               }

但没有任何反应.哪里出错了?谢谢

解决方法:

所以我发现了这个问题!由于引号“”,SSID错误.因此,如果您使用以下代码创建一个打开的可移植热点(我把它带到网上的某个地方):

    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);    if(wifiManager.isWifIEnabled())    {        wifiManager.setWifIEnabled(false);              }           Method[] wmMethods = wifiManager.getClass().getDeclaredMethods();   //Get all declared methods in WifiManager class         boolean methodFound=false;    for(Method method: wmMethods){        if(method.getname().equals("setWifiApEnabled")){            methodFound=true;            WifiConfiguration netConfig = new WifiConfiguration();            netConfig.SSID = "\""+"TinyBox"+"\"";            netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);            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);                            }                        }                    }                }                if(apstatus)                {                    System.out.println("SUCCESSdddd");                  }else                {                    System.out.println("Failed");                   }            } catch (IllegalArgumentException e) {                e.printstacktrace();            } catch (illegalaccessexception e) {                e.printstacktrace();            } catch (InvocationTargetException e) {                e.printstacktrace();            }        }          }

您需要使用以下方式连接到它:

    WifiConfiguration conf = new WifiConfiguration();    conf.SSID = "\"\"" + "TinyBox" + "\"\"";    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);    WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);     wifiManager.addNetwork(conf);    List<WifiConfiguration> List = wifiManager.getConfigurednetworks();    for( WifiConfiguration i : List ) {        if(i.SSID != null && i.SSID.equals("\"\"" + "TinyBox" + "\"\"")) {            try {                wifiManager.disconnect();                wifiManager.enableNetwork(i.networkID, true);                System.out.print("i.networkID " + i.networkID + "\n");                wifiManager.reconnect();                               break;            }            catch (Exception e) {                e.printstacktrace();            }        }               }
总结

以上是内存溢出为你收集整理的以编程方式连接到便携式热点中的Android设备全部内容,希望文章能够帮你解决以编程方式连接到便携式热点中的Android设备所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1098418.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存