투케이2K

563. (Android/Java) connectivityManager requestNetwork 사용해 실시간 와이파이 (wifi) 연결 상태 감지 본문

Android

563. (Android/Java) connectivityManager requestNetwork 사용해 실시간 와이파이 (wifi) 연결 상태 감지

투케이2K 2023. 5. 21. 18:09

[개발 환경 설정]

개발 툴 : AndroidStudio

개발 언어 : Java

 

[소스 코드]

 

        // -------------------------------------
        // [로직 처리 실시]
        // -------------------------------------
        try {

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {

                NetworkRequest networkRequest = new NetworkRequest.Builder()
                        //.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
                        .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) // TODO [와이파이 연결 감지]
                        //.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
                        //.addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET)
                        //.addTransportType(NetworkCapabilities.TRANSPORT_BLUETOOTH)
                        //.setNetworkSpecifier(wifiNetworkSpecifier)
                        .build();

                ConnectivityManager connectivityManager = (ConnectivityManager)this.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
                connectivityManager.requestNetwork(networkRequest, new ConnectivityManager.NetworkCallback() {
                    @Override
                    public void onAvailable(Network network) {
                        super.onAvailable(network);
                        S_Log._W_("requestNetwork :: onAvailable", null);
                    }

                    @Override
                    public void onUnavailable() {
                        super.onUnavailable();
                        S_Log._E_("requestNetwork :: onUnavailable", null);
                    }

                    @Override
                    public void onLost(Network network) {
                        super.onLost(network);
                        S_Log._E_("requestNetwork :: onLost", null);
                    }

                });

            }

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

반응형
Comments