반응형
블로그 이미지
sense.45

공지사항

최근에 올라온 글

최근에 달린 댓글

글 보관함

calendar

1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28

안드로이드 WIFI 연결 체크

2019. 2. 12. 11:21 | Posted by sense.45
반응형

WIFI 연결 체크




private boolean checkWifiOnAndConnected() {
    WifiManager wifiMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE);

    if (wifiMgr.isWifiEnabled()) { // Wi-Fi adapter is ON

        WifiInfo wifiInfo = wifiMgr.getConnectionInfo();

        if( wifiInfo.getNetworkId() == -1 ){
            return false; // Not connected to an access point
        }
        return true; // Connected to an access point
    }
    else {
        return false; // Wi-Fi adapter is OFF
    }
}


<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

출처 : https://stackoverflow.com/questions/3841317/how-do-i-see-if-wi-fi-is-connected-on-android





public static String getNetworkType(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();
    return info.getTypeName();
}


아래 함수는 약간만 손보면 사용하기 좋겠슴다.

public static boolean isInternetIsConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null) { // connected to the internet
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
            // connected to wifi
            return true;

        } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
            // connected to the mobile provider's data plan
            return true;
        }
    } else {
        // not connected to the internet
        return false;
    }
    return false;
}


출처 : https://stackoverflow.com/questions/48077755/how-to-check-programmatically-that-wifi-is-connected-or-data-pack-is-enabled-but


반응형

'Android' 카테고리의 다른 글

안드로이드 Calendar.getInstance API level24  (0) 2018.07.10
NFC NTAG 213  (1) 2018.03.26
How to Change the Text Color of a Substring  (0) 2018.03.20