2013-03-05 39 views
6

Tôi đã tạo thành công điểm phát sóng di động theo cách lập trình trên thiết bị của mình bằng SSID được chỉ định. Bây giờ tôi muốn kết nối với nó từ một thiết bị khác! Tôi đang sử dụng mã này:Lập trình kết nối với thiết bị Android trong Điểm phát sóng di động

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

Nhưng không có gì xảy ra. Sai lầm ở đâu? Cảm ơn

Trả lời

8

Vì vậy, tôi đã tìm thấy vấn đề! SSID đã sai vì dấu ngoặc kép "". Vì vậy, nếu bạn tạo một điểm phát sóng di mở với đoạn mã sau (tôi lấy nó ở đâu đó trên 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(); 
      } 
     }  
    } 

Bạn cần phải kết nối với nó bằng cách sử:

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

này sẽ thêm dấu ngoặc kép hai lần như '" "Tinybox" "', đây có phải là phương thức đúng –

+0

nó luôn bị ngắt tại "for (WifiConfiguration i: list)" .. điều gì có thể là vấn đề? (a newbe) –

+0

@phcaze có thể cung cấp mật khẩu không? – Roster

Các vấn đề liên quan