투케이2K

527. (Android/Java) [유틸 파일] LocalHotspotOnOff : startLocalOnlyHotspot 로컬 핫스팟 활성 수행 실시 본문

Android

527. (Android/Java) [유틸 파일] LocalHotspotOnOff : startLocalOnlyHotspot 로컬 핫스팟 활성 수행 실시

투케이2K 2023. 3. 31. 22:39

[개발 환경 설정]

개발 툴 : AndroidStudio

 

[소스 코드]

    // -----------------------------------------------------------------------------------------
    // TODO [SEARCH FAST] : [RETURN] myHotSpotON : 핫스팟 ON 수행
    // -----------------------------------------------------------------------------------------
    public static WifiManager.LocalOnlyHotspotReservation mReservation = null;
    public static WifiConfiguration wifiConfiguration = null;
    public static SoftApConfiguration softApConfiguration = null;
    public static void LocalHotspotOnOff(Context mContext, int type) {

        /**
         * // -----------------------------------------
         * [LocalHotspotOnOff 메소드 설명]
         * // -----------------------------------------
         * 1. LocalHotspotOnOff : 핫스팟 ON 수행
         * // -----------------------------------------
         * 2. 호출 방식 :
         *
         *   C_App.LocalHotspotOnOff(A_Intro.this, 0); // [off]
         *
         *   C_App.LocalHotspotOnOff(A_Intro.this, 1); // [on]
         *
         * // -----------------------------------------
         * 3. 필요 퍼미션 :
         *
         * <uses-permission android:name="android.permission.INTERNET"/>
         * <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
         * <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
         * <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
         * <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
         *
         * <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
         * <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
         * // -----------------------------------------
         * 4. 참고 사이트 :
         *
         * https://developer.android.com/reference/android/net/wifi/WifiManager.LocalOnlyHotspotReservation
         * // -----------------------------------------
         * */


        // [로그 출력 변수 선언]
        String M_LOG = "";


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

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // TODO [오레오 버전 이상]

                if (C_StateCheck.getWifiConnect(mContext) == false){ // [현재 와이파이 연결이 안된 경우]
                    
                    // [타입 별 분기 처리]
                    if (type == 0){ // TODO [OFF]

                        if (mReservation != null){
                            mReservation.close();
                            mReservation = null;
                            M_LOG = "[STATE] : HotSpot Off";
                            S_Log.lte("================================================");
                            S_Log.cnt("[" + ACTIVITY_NAME + " >> LocalHotspotOnOff :: 핫스팟 OFF 수행]");
                            S_Log.lbe("================================================");
                        }
                        else {
                            M_LOG = "[STATE] : mReservation Is Null";
                        }

                    }
                    else { // TODO [ON]
                        if (mReservation != null){ // [이미 활성 중]
                            M_LOG = "[STATE] : HotSpot Current Proceeding";
                        }
                        else {
                            // [권한 체크 실시]
                            if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                                S_Log.lte("================================================");
                                S_Log.cnt("["+ACTIVITY_NAME+" >> LocalHotspotOnOff :: 핫스팟 ON 수행]");
                                S_Log.cnt("-----------------------------------------");
                                S_Log.cnt("[RESULT :: ACCESS_FINE_LOCATION Is Not Granted]");
                                S_Log.lbe("================================================");
                                return;
                            }

                            // [Hotspot On 수행]
                            M_LOG = "[STATE] : HotSpot On";
                            WifiManager manager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
                            manager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {
                                @Override
                                public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
                                    super.onStarted(reservation);
                                    mReservation = reservation;
                                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                                        softApConfiguration = mReservation.getSoftApConfiguration();
                                        S_Log.ltw("================================================");
                                        S_Log.cnt("[" + ACTIVITY_NAME + " >> LocalHotspotOnOff :: 핫스팟 ON 수행]");
                                        S_Log.cnt("-----------------------------------------");
                                        S_Log.cnt("[RESULT :: [SoftApConfiguration] Hotspot Start]");
                                        S_Log.cnt("-----------------------------------------");
                                        S_Log.cnt("[SSID :: "+softApConfiguration.getSsid()+"]");
                                        S_Log.cnt("-----------------------------------------");
                                        S_Log.cnt("[PASSWORD :: "+softApConfiguration.getPassphrase()+"]");
                                        S_Log.lbw("================================================");
                                    }
                                    else {
                                        wifiConfiguration = mReservation.getWifiConfiguration();
                                        S_Log.ltw("================================================");
                                        S_Log.cnt("[" + ACTIVITY_NAME + " >> LocalHotspotOnOff :: 핫스팟 ON 수행]");
                                        S_Log.cnt("-----------------------------------------");
                                        S_Log.cnt("[RESULT :: [WifiConfiguration] Hotspot Start]");
                                        S_Log.cnt("-----------------------------------------");
                                        S_Log.cnt("[SSID :: "+wifiConfiguration.SSID.toString()+"]");
                                        S_Log.cnt("-----------------------------------------");
                                        S_Log.cnt("[PASSWORD :: "+wifiConfiguration.preSharedKey.toString()+"]");
                                        S_Log.lbw("================================================");
                                    }

                                }

                                @Override
                                public void onStopped() {
                                    super.onStopped();
                                    S_Log.lte("================================================");
                                    S_Log.cnt("[" + ACTIVITY_NAME + " >> LocalHotspotOnOff :: 핫스팟 ON 수행]");
                                    S_Log.cnt("-----------------------------------------");
                                    S_Log.cnt("[RESULT :: Hotspot Stop]");
                                    S_Log.lbe("================================================");
                                    if (mReservation != null){
                                        mReservation.close();
                                        mReservation = null;
                                    }
                                }

                                @Override
                                public void onFailed(int reason) {
                                    super.onFailed(reason);
                                    S_Log.lte("================================================");
                                    S_Log.cnt("[" + ACTIVITY_NAME + " >> LocalHotspotOnOff :: 핫스팟 ON 수행]");
                                    S_Log.cnt("-----------------------------------------");
                                    S_Log.cnt("[RESULT :: Hotspot Fail]");
                                    S_Log.cnt("-----------------------------------------");
                                    S_Log.cnt("[REASON :: "+String.valueOf(reason)+"]");
                                    S_Log.lbe("================================================");
                                    if (mReservation != null){
                                        mReservation.close();
                                        mReservation = null;
                                    }
                                }
                            }, new Handler());
                        }
                    }
                }
                else {
                    M_LOG = "[ERROR] : Wifi Connect State";
                }
            }
            else {
                M_LOG = "[ERROR] : Mobile Version Oreo Under";
            }

            // [로그 출력]
            S_Log.ltd("================================================");
            S_Log.cnt("["+ACTIVITY_NAME+" >> LocalHotspotOnOff :: 핫스팟 ON 수행]");
            S_Log.cnt("-----------------------------------------");
            S_Log.cnt("[M_LOG :: "+String.valueOf(M_LOG)+"]");
            S_Log.lbd("================================================");

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

[결과 출력]

 

W///===========//: ================================================
I/: [C_App >> LocalHotspotOnOff :: 핫스팟 ON 수행]
I/: -----------------------------------------
I/: [RESULT :: [SoftApConfiguration] Hotspot Start]
I/: -----------------------------------------
I/: [SSID :: AndroidShare_3902]
I/: -----------------------------------------
I/: [PASSWORD :: mhjxzaz448ja8yt]
W///===========//: ================================================

 

반응형
Comments