투케이2K

489. (Android/Java) 실시간 wifi 와이파이 연결 상태 확인 및 정보 출력 (IntentFilter WIFI_STATE_CHANGED_ACTION) 본문

Android

489. (Android/Java) 실시간 wifi 와이파이 연결 상태 확인 및 정보 출력 (IntentFilter WIFI_STATE_CHANGED_ACTION)

투케이2K 2023. 2. 19. 20:00

[개발 환경 설정]

개발 툴 : AndroidStudio

 

[소스 코드]

 

[onCreate]
    // -----------------------------------------------------------------------------------------
    // TODO [액티비티 onCreate]
    // -----------------------------------------------------------------------------------------

        // -------------------------------------
        // [로직 처리 실시]
        // -------------------------------------
        try {
            IntentFilter filter = new IntentFilter();
            filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
            filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
            registerReceiver(mReceiver, filter);

        }
        catch (Exception e){
            e.printStackTrace();
        }





[receiver]
    // -----------------------------------------------------------------------------------------
    // TODO [리시버 : 와이파이 및 네트워크 상태 변화 체크]
    // -----------------------------------------------------------------------------------------
    BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch(intent.getAction()){

                // [와이파이 상태변화]
                case WifiManager.WIFI_STATE_CHANGED_ACTION:
                    //와이파이 상태값 가져오기
                    int wifistate = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
                    switch(wifistate){
                        case WifiManager.WIFI_STATE_DISABLING: // [와이파이 비활성화중]
                            S_Log.ltd("================================================");
                            S_Log.cnt("["+ACTIVITY_NAME+" >> "+ "onReceive" +" :: 와이파이 비활성화 중]");
                            S_Log.lbd("================================================");
                            break;
                        case WifiManager.WIFI_STATE_DISABLED:  // [와이파이 비활성화]
                            S_Log.lte("================================================");
                            S_Log.cnt("["+ACTIVITY_NAME+" >> "+ "onReceive" +" :: 와이파이 비활성화]");
                            S_Log.lbe("================================================");
                            break;
                        case WifiManager.WIFI_STATE_ENABLING:  // [와이파이 활성화중]
                            S_Log.ltd("================================================");
                            S_Log.cnt("["+ACTIVITY_NAME+" >> "+ "onReceive" +" :: 와이파이 활성화 중]");
                            S_Log.lbd("================================================");
                            break;
                        case WifiManager.WIFI_STATE_ENABLED:   // [와이파이 활성화]
                            S_Log.ltw("================================================");
                            S_Log.cnt("["+ACTIVITY_NAME+" >> "+ "onReceive" +" :: 와이파이 활성화]");
                            S_Log.lbw("================================================");
                            break;
                        default:
                            S_Log.lte("================================================");
                            S_Log.cnt("["+ACTIVITY_NAME+" >> "+ "onReceive" +" :: 알수없음]");
                            S_Log.lbe("================================================");
                            break;
                    }
                    break;

                // [네트워크 상태변화]
                case WifiManager.NETWORK_STATE_CHANGED_ACTION:

                    NetworkInfo info = (NetworkInfo)intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);

                    // [네트워크 상태값 가져오기]
                    NetworkInfo.DetailedState state = info.getDetailedState();

                    String typename = info.getTypeName();

                    if(state==NetworkInfo.DetailedState.CONNECTED){ // [네트워크 연결]
                        // [와이파이 정보 확인]
                        WifiManager wifimanager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
                        WifiInfo wifiInfo = wifimanager.getConnectionInfo();

                        DhcpInfo dhcpInfo = wifimanager.getDhcpInfo();
                        int wIp = dhcpInfo.ipAddress;

                        String getSsid = wifiInfo.getSSID();
                        getSsid = getSsid.replaceAll("[\"]","");

                        String getIpAddress = String.format("%d.%d.%d.%d", (wIp & 0xff), (wIp >> 8 & 0xff), (wIp >> 16 & 0xff), (wIp >> 24 & 0xff));

                        String getBSSID = wifiInfo.getBSSID();
                        getBSSID = getBSSID.trim();

                        S_Log.ltw("================================================");
                        S_Log.cnt("["+ACTIVITY_NAME+" >> "+ "onReceive" +" :: 네트워크 연결]");
                        S_Log.cnt("-----------------------------------------");
                        S_Log.cnt("[getSsid :: "+String.valueOf(getSsid)+"]");
                        S_Log.cnt("-----------------------------------------");
                        S_Log.cnt("[getIpAddress :: "+String.valueOf(getIpAddress)+"]");
                        S_Log.cnt("-----------------------------------------");
                        S_Log.cnt("[getBSSID :: "+String.valueOf(getBSSID)+"]");
                        S_Log.lbw("================================================");
                    }
                    else if(state==NetworkInfo.DetailedState.DISCONNECTED){ // [네트워크 끊음]
                        S_Log.lte("================================================");
                        S_Log.cnt("["+ACTIVITY_NAME+" >> "+ "onReceive" +" :: 네트워크 끊음]");
                        S_Log.lbe("================================================");
                    }
                    break;
            }
        }
    };





[onDestroy]
    // -----------------------------------------------------------------------------------------
    // TODO [액티비티 onDestroy]
    // -----------------------------------------------------------------------------------------
    @Override
    public void onDestroy(){
        super.onDestroy();
        S_Log.lte("================================================");
        S_Log.cnt("["+ACTIVITY_NAME+" >> "+C_Util.getNowMethod(1)+" :: 액티비티 종료 상태]");
        S_Log.lbe("================================================");

        // [리시버 등록 해제]
        try {
            unregisterReceiver(mReceiver);
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

 

[결과 출력]

 

D///===========//: ================================================
I/: [A_Intro >> onReceive :: 와이파이 활성화 중]
D///===========//: ================================================


W///===========//: ================================================
I/: [A_Intro >> onReceive :: 와이파이 활성화]
W///===========//: ================================================


W///===========//: ================================================
I/: [A_Intro >> onReceive :: 네트워크 연결]
I/: -----------------------------------------
I/: [getSsid :: eung]
I/: -----------------------------------------
I/: [getIpAddress :: 192.168.0.14]
I/: -----------------------------------------
I/: [getBSSID :: 88:36:6c:1f:27:4e]
W///===========//: ================================================

 


반응형
Comments