以编程方式连接到便携式热点的安卓设备



我已经用指定的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();
            }
        }           
    }

最新更新