2013-03-05 11 views
6

ich einen mobilen Hotspot erfolgreich programmatisch mit einer bestimmten SSID auf meinem Gerät erstellt haben. Jetzt möchte ich von einem anderen Gerät aus verbinden! Ich verwende diesen Code:programmatisch zu einem Android-Gerät in Mobiler Hotspot verbinden

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

Aber nichts passiert. Wo ist der Fehler? Dank

Antwort

8

So fand Jungs ich das Problem! Die SSID war falsch wegen der Anführungszeichen "". Also, wenn Sie einen offenen mobilen Hotspot mit dem folgenden Code erstellen (Ich habe es irgendwo im Netz):

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

Sie müssen es die Verbindung mit:

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

     }   
    } 
+0

dies fügt Anführungszeichen zweimal wie '„“Tinybox„“', dann ist dies die richtige Methode ist –

+0

es bei „für (WifiConfiguration i: Liste)“ bricht immer .. was das Problem sein könnte? (ein Newbe) –

+0

@phcaze ist es möglich, Passwort zu geben? – Roster

Verwandte Themen