2016년 5월 17일 화요일

android wifi ap 연결 예제 샘플 소스

public void connectWifi(WifiItem menuItem) { 

/////////////////// 공통 ////////////////////////// 
        WifiConfiguration wfc = new WifiConfiguration(); 
        wfc.SSID = "\"".concat(menuItem.getSsid()).concat("\""); 
        wfc.status = WifiConfiguration.Status.DISABLED; 
        wfc.priority = 40; 
/////////////////// 공통 ////////////////////////// 

//        name: jimmyNet 
//        pw : 12345678 


////////////////// 보안 방식에 따른 설정 ////////////////////// 
        int type = 2; 
        if (type == 0) { 
//        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); 
        } else if (type == 1) { 
//    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] = "12345678";  // <<--이부분중요. \"부분 빼고 그냥 비밀번호만 넣는다 
            wfc.wepTxKeyIndex = 0; 
        } else if (type == 2) { 
//    wpa나 wpa2일때 
            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("12345678").concat("\""); 
        } 

////////////////// 보안 방식에 따른 설정 ////////////////////// 

        int networkId = wifimanager.addNetwork(wfc); 

        if (networkId != -1) { 
            wifimanager.enableNetwork(networkId, true); 
        } 

    }

댓글 없음:

댓글 쓰기