簡體   English   中英

以編程方式連接到便攜式熱點中的Android設備

[英]Programmatically connect to an android device in Portable hotspot

我已經成功地在我的設備上使用指定的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();
            }

        }           
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM