Notice
Recent Posts
Recent Comments
Link
투케이2K
551. (Android/Java) [유틸 파일] observableWifiScanList : Wifi 와이파이 스캔 리스트 목록 결과 반환 본문
Android
551. (Android/Java) [유틸 파일] observableWifiScanList : Wifi 와이파이 스캔 리스트 목록 결과 반환
투케이2K 2023. 4. 17. 23:59[개발 환경 설정]
개발 툴 : AndroidStudio
개발 언어 : Java
[소스 코드]
// -----------------------------------------------------------------------------------------
// TODO [SEARCH FAST] : [Observable] : [Wifi 스캔 리스트 목록 결과 반환]
// -----------------------------------------------------------------------------------------
// TODO [호출 방법 소스 코드]
// -----------------------------------------------------------------------------------------
/*
try {
C_App.observableWifiScanList(A_Intro.this)
.subscribeOn(AndroidSchedulers.mainThread()) // [Observable (생성자) 로직을 IO 스레드에서 실행 : 백그라운드]
.observeOn(Schedulers.io()) // [Observer (관찰자) 로직을 메인 스레드에서 실행]
.subscribe(new Observer<List<ScanResult>>() { // [Observable.create 타입 지정]
@Override
public void onSubscribe(@NonNull Disposable d) {
}
@Override
public void onNext(@NonNull List<ScanResult> value) {
S_Log.ltw("================================================");
S_Log.cnt("["+ACTIVITY_NAME+" >> WIFI SCAN RESULT :: onNext]");
S_Log.cnt("-----------------------------------------");
S_Log.cnt("[SCAN COUNT :: "+String.valueOf(value.size())+"]");
S_Log.cnt("-----------------------------------------");
S_Log.cnt("[SCAN LIST :: "+String.valueOf(value.toString())+"]");
S_Log.lbw("================================================");
}
@Override
public void onError(@NonNull Throwable e) {
S_Log.lte("================================================");
S_Log.cnt("["+ACTIVITY_NAME+" >> WIFI SCAN RESULT :: onError]");
S_Log.cnt("-----------------------------------------");
S_Log.cnt("[ERROR :: "+String.valueOf(e.getMessage())+"]");
S_Log.lbe("================================================");
}
@Override
public void onComplete() {
}
});
} catch (Exception e) {
e.printStackTrace();
}
*/
// -----------------------------------------------------------------------------------------
static String WIFI_M_LOG = "";
static int WIFI_COUNT = 0;
static List<HashMap<String, Object>> WIFI_LIST = null;
public static Observable<List<ScanResult>> observableWifiScanList(Context mContext){
// [로직 처리 실시]
return Observable.create(subscriber -> {
// [변수 초기화]
WIFI_M_LOG = "";
WIFI_COUNT = 0;
List<HashMap<String, Object>> WIFI_LIST = new ArrayList<>();
try {
S_Log.ltd("================================================");
S_Log.cnt("["+ACTIVITY_NAME+" >> observableWifiScanList :: 와이파이 리스트 스캔 시작]");
S_Log.lbd("================================================");
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
// [퍼미션 권한 체크 실시]
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_WIFI_STATE) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CHANGE_WIFI_STATE) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED) {
// [퍼미션이 부여되어있지 않은 경우 종료]
WIFI_M_LOG = "[ERROR] : [observableWifiScanList] : Wifi Scan Permission not Granted (와이파이 스캔에 필요한 권한을 확인해주세요. : GPS , Location)";
S_Log.lte("================================================");
S_Log.cnt("["+ACTIVITY_NAME+" >> observableWifiScanList :: ERROR]");
S_Log.cnt("-----------------------------------------");
S_Log.cnt("[MESSAGE :: "+String.valueOf(WIFI_M_LOG)+"]");
S_Log.lbe("================================================");
// [리턴 반환 실시]
subscriber.onError(new Throwable(WIFI_M_LOG));
subscriber.onComplete();
}
else {
// [와이파이 리스트 스캔 실시]
WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
List<ScanResult> scanResultList = wifiManager.getScanResults();
for (ScanResult result : scanResultList) {
HashMap<String, Object> map = new HashMap<>();
map.put("SSID", result.SSID);
map.put("BSSID", result.BSSID);
//map.put("capabilities", result.capabilities);
WIFI_LIST.add(map);
}
WIFI_COUNT = WIFI_LIST.size();
// [로그 출력]
S_Log.ltw("================================================");
S_Log.cnt("["+ACTIVITY_NAME+" >> observableWifiScanList :: SUCCESS]");
S_Log.cnt("-----------------------------------------");
S_Log.cnt("[SCAN COUNT :: "+String.valueOf(WIFI_COUNT)+"]");
S_Log.cnt("-----------------------------------------");
S_Log.cnt("[SCAN LIST :: "+String.valueOf(WIFI_LIST)+"]");
S_Log.lbw("================================================");
// ------------------------------------------------------
// TODO [리턴 데이터 반환]
// ------------------------------------------------------
try {
// [메시지 삽입]
WIFI_M_LOG = "SUCCESS";
// [리턴 반환 실시]
subscriber.onNext(scanResultList);
subscriber.onComplete();
}
catch (Exception ex){
ex.printStackTrace();
}
}
}
}, 0);
} catch (final Exception e){
e.printStackTrace();
S_Log.lte("================================================");
S_Log.cnt("["+ACTIVITY_NAME+" >> observableWifiScanList :: ERROR]");
S_Log.cnt("-----------------------------------------");
S_Log.cnt("[EXCEPTION :: "+String.valueOf(e.getMessage())+"]");
S_Log.lbe("================================================");
// ------------------------------------------------------
// TODO [리턴 데이터 반환]
// ------------------------------------------------------
try {
// [에러 메시지 삽입]
WIFI_M_LOG = "[EXCEPTION] : [observableWifiScanList] : " + String.valueOf(e.getMessage());
// [리턴 반환 실시]
subscriber.onError(new Throwable(WIFI_M_LOG));
subscriber.onComplete();
}
catch (Exception ex){
ex.printStackTrace();
}
}
});
}
[결과 출력]
W///===========//: ================================================
I/: [C_App >> observableWifiScanList :: SUCCESS]
I/: -----------------------------------------
I/: [SCAN COUNT :: 43]
I/: -----------------------------------------
I/: [SCAN LIST :: [{BSSID=08:5d:dd:65:74:90, SSID=olleh_GiGA_WiFi_748D} ... ]]
W///===========//: ================================================
반응형
'Android' 카테고리의 다른 글
Comments