본문 바로가기
PhoneGap

[android] 코드로 특정 와이파이에 붙기

by NariNalDa 2013. 4. 15.

static final String[] SECURITY_MODES = { "WEP", "WPA", "WPA2", "WPA_EAP", "IEEE8021X" };


public String getScanResultSecurity(ScanResult scanResult) {

        final String cap = scanResult.capabilities;

        for (int i = SECURITY_MODES.length - 1; i >= 0; i--) {

        Log.i("a","5");

            if (cap.contains(SECURITY_MODES[i])) {

                return SECURITY_MODES[i];

            }

        }

return "Open";

    }


public void connectToWifi(String ssid, String passkey) {

String networkSSID = ssid;

String networkPass = passkey;

Log.i("connect",passkey);

   Log.i("ap", "* connectToWifi " + ssid);

   WifiConfiguration wfc = new WifiConfiguration();

   

   wfc.SSID = "\"" + networkSSID + "\"";

   wfc.status = WifiConfiguration.Status.DISABLED;

   wfc.priority = 40;

  

   for (ScanResult result : scanResultList) {

      if (result.SSID.equals(networkSSID)) {

      String securityMode = getScanResultSecurity(result);

      Log.i("mode",securityMode);

      if (securityMode.equalsIgnoreCase("OPEN")) {

      wfc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

       wfc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);

       wfc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);

       wfc.allowedAuthAlgorithms.clear();

       wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);

       wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);

       wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);

       wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);

       wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);

       System.out.println("나 다됨");

      }

      else if(securityMode.equalsIgnoreCase("WEP")){

      wfc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

       wfc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);

       wfc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);

       wfc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);

       wfc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);

       wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);

       wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);

       wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);

       wfc.wepKeys[0] = "\"".concat(passkey).concat("\"");

       wfc.wepTxKeyIndex = 0;

      }

      else {

       wfc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);

       wfc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);

       wfc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);

       wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);

       wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);

       wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);

       wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);

       wfc.preSharedKey = "\"".concat(passkey).concat("\"");

      }

      }

   }

   WifiManager wfMgr = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);

   int networkId = wfMgr.addNetwork(wfc);

   System.out.println(networkId);

   if (networkId == -1) {

    Log.i("a","error");

    // success, can call wfMgr.enableNetwork(networkId, true) to connect

   }

   wfMgr.setWifiEnabled(true);

   boolean a = wfMgr.enableNetwork(networkId, true);


}