Android에서 현재 Wi-Fi IP 주소 가져오기
이 예제에서는 Android 앱에서 현재 연결된 Wi-Fi 네트워크의 IP 주소를 가져와 화면에 표시하는 방법을 단계별로 알아봅니다. WifiManager와 Formatter 클래스를 활용하면 몇 줄의 코드만으로 간단하게 구현할 수 있습니다.
1단계: 새 프로젝트 생성
Android Studio를 실행하고 File → New Project 메뉴로 이동한 후, 새 프로젝트 생성에 필요한 모든 세부 정보를 입력하여 프로젝트를 만듭니다.
2단계: 레이아웃 파일 작성 (res/layout/activity_main.xml)
아래 코드를 activity_main.xml에 추가합니다. 이 레이아웃은 Wi-Fi IP 주소를 표시할 TextView 하나로 구성되어 있습니다.
<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android"
xmlns:app = "https://schemas.android.com/apk/res-auto"
xmlns:tools = "https://schemas.android.com/tools"
android:layout_width = "match_parent"
android:gravity = "center"
android:layout_height = "match_parent"
tools:context = ".MainActivity">
<TextView
android:id = "@+id/text"
android:textSize = "30sp"
android:layout_width = "match_parent"
android:layout_height = "match_parent" />
</LinearLayout>위 코드에서는 Wi-Fi IP 주소를 화면에 보여주기 위한 TextView를 배치했습니다.
3단계: MainActivity 작성 (src/MainActivity.java)
다음으로 MainActivity.java에 아래 코드를 추가합니다. 핵심 로직은 WifiManager를 통해 연결 정보를 얻고, Formatter.formatIpAddress() 메서드로 사람이 읽을 수 있는 IP 주소 문자열로 변환하는 것입니다.
package com.example.myapplication;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.text.format.Formatter;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text);
WifiManager wifiMgr = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
String ipAddress = Formatter.formatIpAddress(wifiInfo.getIpAddress());
textView.setText("" + ipAddress);
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onResume() {
super.onResume();
}
}코드 설명:
getSystemService(WIFI_SERVICE): 시스템 서비스로부터 WifiManager 인스턴스를 가져옵니다.wifiMgr.getConnectionInfo(): 현재 Wi-Fi 연결 정보(WifiInfo)를 반환합니다.wifiInfo.getIpAddress(): IP 주소를 정수(int) 형태로 반환합니다.Formatter.formatIpAddress(): 정수형 IP 주소를 "192.168.0.10"과 같은 일반적인 문자열 형식으로 변환합니다.
4단계: 매니페스트 권한 설정 (androidManifest.xml)
Wi-Fi 상태에 접근하려면 ACCESS_WIFI_STATE 권한이 필요합니다. 아래와 같이 AndroidManifest.xml에 권한을 추가합니다.
<?xml version = "1.0" encoding = "utf-8"?>
<manifest xmlns:android = "https://schemas.android.com/apk/res/android"
package = "com.example.myapplication">
<uses-permission android:name = "android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup = "true"
android:icon = "@mipmap/ic_launcher"
android:label = "@string/app_name"
android:roundIcon = "@mipmap/ic_launcher_round"
android:supportsRtl = "true"
android:theme = "@style/AppTheme">
<activity android:name = ".MainActivity">
<intent-filter>
<action android:name = "android.intent.action.MAIN" />
<action android:name = "android.net.conn.CONNECTIVITY_CHANGE" />
<category android:name = "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>앱 실행 및 결과 확인
이제 애플리케이션을 실행해 보겠습니다. 실제 Android 기기를 컴퓨터에 연결했다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 후, 툴바의 Run(실행) 아이콘을 클릭하세요. 실행 옵션 목록에서 본인의 모바일 기기를 선택하면, 기기 화면에 현재 Wi-Fi IP 주소가 표시됩니다.

참고 사항
Formatter.formatIpAddress()는 IPv4 주소만 지원하며, IPv6 환경에서는 다른 방법을 고려해야 합니다.- API 31(Android 12) 이상에서는 개인 정보 보호 정책에 따라 MAC 주소 등 일부 네트워크 정보 접근이 제한될 수 있습니다.
- Wi-Fi에 연결되어 있지 않으면
getIpAddress()가 0을 반환하므로, 실행 전 반드시 Wi-Fi 연결 상태를 확인하세요.