以编程方式连接到便携式热点Android设备 [英] Programmatically connect to an android device in Portable hotspot

查看:240
本文介绍了以编程方式连接到便携式热点Android设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地创建了一个便携式热点编程设备上通过指定SSID。现在,我想从另一台设备连接到它!我使用这个code:

I have succesfully created a portable hotspot programmatically on my device with a specified SSID. Now I want to connect to it from another device! I'm using this code:

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

但没有任何反应。哪里错了吗?谢谢

But nothing happens. Where is the mistake? Thanks

推荐答案

所以,球员我发现这个问题!该SSID是因为引号的错误。所以,如果你创建一个开放的便携式热点有以下code(我把它放在网上):

So guys I found the problem! The SSID was wrong because of the quotes "". So if you create an open portable hotspot with the following code (I took it somewhere on the net):

    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设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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