Android

[Android] Wifi정보 출력

tjgpwl115 2018. 11. 23. 09:04

< Android Wifi Infomations >

안드로이드 와이파이 연결과 연결된 와이파이의 정보를 가져올 수 있는 코드입니다.

먼저, 필요한 모듈을 import 합니다.

import android.net.wifi.WifiInfo;

import android.net.wifi.WifiManager;


그리고 버튼을 생성한 뒤 onclicklistener로 클릭 이벤트를 만들어주고 그 안에 와이파이 서비스를 가져오고 연결하는 코드를 입력합니다.

WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE)

WifiInfo connectionInfo = wifiManager.getConnectionInfo();  


그 다음은 ip주소, SSID, BSSID, MAC주소, RSSI 값과 RSSI Level을 알 수 있습니다. 그 외에 frequency나 networkId 정보도 알 수 있습니다.


int ipAddress = connectionInfo.getIpAddress(); 

String ipString = String.format("%d.%d.%d.%d", (ipAddress & 0xff),(ipAddress >> 8 & 0xff),(ipAddress >> 16 & 0xff),(ipAddress >> 24 & 0xff));

//화면에 출력되는 정보

String wifiInfo = "";

wifiInfo = "SSID: " + connectionInfo.getSSID() + "\n" +"BSSID: " + connectionInfo.getBSSID() + "\n" +"IP Address: " + ipString + "\n" + "MAC Address: " + connectionInfo.getMacAddress() + "\n" +"Rssi: " + connectionInfo.getRssi() + "dBm " + "\n""Rssi Level: " + WifiManager.calculateSignalLevel(connectionInfo.getRssi(), NumOfRSSILevels) +" of " + NumOfRSSILevels;