Notice
Recent Posts
Recent Comments
Link
투케이2K
102. (AndroidStudio/android/java) WifiInfo 및 DhcpInfo 사용해 현재 연결된 와이파이 (wifi) 정보 확인 실시 본문
Android
102. (AndroidStudio/android/java) WifiInfo 및 DhcpInfo 사용해 현재 연결된 와이파이 (wifi) 정보 확인 실시
투케이2K 2021. 4. 1. 15:45/* =========================== */
[ 개발 환경 설정 ]
개발 툴 : AndroidStudio
개발 언어 : java
/* =========================== */
/* =========================== */
[소스 코드]
public void getWifiInfo(){
try {
/** [퍼미션 필요]
* <uses-permission android:name="android.permission.INTERNET" />
* <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
* */
//TODO wifi 가 연결 되었는지 확인 실시
ConnectivityManager manager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
boolean wificon = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
if(wificon == false){ //TODO wifi 연걸이 안된 경우
return;
}
//TODO wifi 정보 가져오기
WifiManager wifimanager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifimanager.getConnectionInfo();
DhcpInfo dhcpInfo = wifimanager.getDhcpInfo();
int wIp = dhcpInfo.ipAddress;
int wNetmask = dhcpInfo.netmask;
int wGateway = dhcpInfo.gateway;
int wDns1 = dhcpInfo.dns1;
String ssid = wifiInfo.getSSID();
ssid = ssid.replaceAll("[\"]","");
String ip = String.format("%d.%d.%d.%d", (wIp & 0xff), (wIp >> 8 & 0xff), (wIp >> 16 & 0xff), (wIp >> 24 & 0xff));
String subnet = String.format("%d.%d.%d.%d", (wNetmask & 0xff), (wNetmask >> 8 & 0xff), (wNetmask >> 16 & 0xff), (wNetmask >> 24 & 0xff));
String gateway = String.format("%d.%d.%d.%d", (wGateway & 0xff), (wGateway >> 8 & 0xff), (wGateway >> 16 & 0xff), (wGateway >> 24 & 0xff));
String dns1 = String.format("%d.%d.%d.%d", (wDns1 & 0xff), (wDns1 >> 8 & 0xff), (wDns1 >> 16 & 0xff), (wDns1 >> 24 & 0xff));
Log.d("---","---");
Log.w("//===========//","================================================");
Log.d("","\n"+"[A_Wifi > getWifiInfo() 메소드 : 현재 연결된 와이파이 정보 확인 실시]");
Log.d("","\n"+"[ssid : "+String.valueOf(ssid)+"]");
Log.d("","\n"+"[ip : "+String.valueOf(ip)+"]");
Log.d("","\n"+"[subnet : "+String.valueOf(subnet)+"]");
Log.d("","\n"+"[gateway : "+String.valueOf(gateway)+"]");
Log.d("","\n"+"[dns1 : "+String.valueOf(dns1)+"]");
Log.w("//===========//","================================================");
Log.d("---","---");
}
catch (Exception e){
e.printStackTrace();
}
}
/* =========================== */
/* =========================== */
[결과 출력]
/* =========================== */
반응형
'Android' 카테고리의 다른 글
Comments