Notice
Recent Posts
Recent Comments
Link
투케이2K
272. (kotlin/코틀린) [유틸 파일] observableWifiScanList : Wifi 와이파이 스캔 리스트 목록 결과 반환 본문
Kotlin
272. (kotlin/코틀린) [유틸 파일] observableWifiScanList : Wifi 와이파이 스캔 리스트 목록 결과 반환
투케이2K 2023. 4. 18. 00:04[개발 환경 설정]
개발 툴 : AndroidStudio
개발 언어 : Kotlin
[소스 코드]
// -----------------------------------------------------------------------------------------
// TODO [SEARCH FAST] : [Observable] : [Wifi 스캔 리스트 목록 결과 반환]
// -----------------------------------------------------------------------------------------
// TODO [호출 방법 소스 코드]
// -----------------------------------------------------------------------------------------
/*
try {
C_App.observableWifiScanList(A_Intro@this)
.subscribeOn(AndroidSchedulers.mainThread()) // [Observable (생성자) 로직을 IO 스레드에서 실행 : 백그라운드]
.observeOn(Schedulers.io()) // [Observer (관찰자) 로직을 메인 스레드에서 실행]
.subscribe(
{ value ->
S_Log.ltw("================================================")
S_Log.cnt("[" + ACTIVITY_NAME + " >> WIFI SCAN RESULT :: onNext]")
S_Log.cnt("-----------------------------------------")
S_Log.cnt("[VALUE :: $value]")
S_Log.lbw("================================================")
},
{ error ->
S_Log.lte("================================================")
S_Log.cnt("[" + ACTIVITY_NAME + " >> WIFI SCAN RESULT :: onError]")
S_Log.cnt("-----------------------------------------")
S_Log.cnt("[ERROR :: " + error.message.toString() + "]")
S_Log.lbe("================================================")
}
)
{
}
}
catch (e : Exception) {
e.printStackTrace()
}
// */
// -----------------------------------------------------------------------------------------
var WIFI_M_LOG = ""
var WIFI_COUNT = 0
var WIFI_LIST: List<HashMap<String, Any>>? = null
fun observableWifiScanList(mContext: Context): Observable<List<ScanResult>> {
// [로직 처리 실시]
return Observable.create { subscriber: ObservableEmitter<List<ScanResult>> ->
// [변수 초기화]
WIFI_M_LOG = ""
WIFI_COUNT = 0
val WIFI_LIST: MutableList<HashMap<String, Any>> = ArrayList()
try {
S_Log.ltd("================================================")
S_Log.cnt("[" + ACTIVITY_NAME + " >> observableWifiScanList :: 와이파이 리스트 스캔 시작]")
S_Log.lbd("================================================")
Handler(Looper.getMainLooper()).postDelayed({
// [퍼미션 권한 체크 실시]
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 :: $WIFI_M_LOG]")
S_Log.lbe("================================================")
// [리턴 반환 실시]
subscriber.onError(Throwable(WIFI_M_LOG))
subscriber.onComplete()
}
else {
// [와이파이 리스트 스캔 실시]
val wifiManager = mContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
val scanResultList = wifiManager.scanResults
for (result in scanResultList) {
val map = HashMap<String, Any>()
map["SSID"] = result.SSID
map["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 :: $WIFI_COUNT]")
S_Log.cnt("-----------------------------------------")
S_Log.cnt("[SCAN LIST :: $WIFI_LIST]")
S_Log.lbw("================================================")
// ------------------------------------------------------
// TODO [리턴 데이터 반환]
// ------------------------------------------------------
try {
// [메시지 삽입]
WIFI_M_LOG = "SUCCESS"
// [리턴 반환 실시]
subscriber.onNext(scanResultList)
subscriber.onComplete()
} catch (ex: Exception) {
ex.printStackTrace()
}
}
}, 0)
} catch (e: Exception) {
e.printStackTrace()
S_Log.lte("================================================")
S_Log.cnt("[" + ACTIVITY_NAME + " >> observableWifiScanList :: ERROR]")
S_Log.cnt("-----------------------------------------")
S_Log.cnt("[EXCEPTION :: " + e.message.toString() + "]")
S_Log.lbe("================================================")
// ------------------------------------------------------
// TODO [리턴 데이터 반환]
// ------------------------------------------------------
try {
// [에러 메시지 삽입]
WIFI_M_LOG = "[EXCEPTION] : [observableWifiScanList] : " + e.message.toString()
// [리턴 반환 실시]
subscriber.onError(Throwable(WIFI_M_LOG))
subscriber.onComplete()
} catch (ex: Exception) {
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///===========//: ================================================
반응형
'Kotlin' 카테고리의 다른 글
Comments