Giới thiệu
Bài viết này hướng dẫn lấy địa chỉ IP Wi-Fi của thiết bị Android thông qua WifiManager và Formatter.formatIpAddress(). Lưu ý: chỉ lấy được IP khi Wi-Fi đang kết nối.
Các bước thực hiện
Bước 1: Tạo dự án mới
Mở Android Studio, chọn File → New Project và tạo dự án.
Bước 2: Thiết kế giao diện (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:orientation="vertical">
<TextView
android:id="@+id/getIPAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Địa chỉ IP thiết bị"
android:textSize="25sp"
android:textStyle="bold" />
</RelativeLayout>
Bước 3: Viết code logic (src/MainActivity.java)
import android.net.wifi.WifiManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.format.Formatter;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.getIPAddress);
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
int ipAddressInt = wifiManager.getConnectionInfo().getIpAddress();
String ipAddress = Formatter.formatIpAddress(ipAddressInt);
textView.setText("Địa chỉ IP thiết bị: " + ipAddress);
}
}
Bước 4: Cấu hình quyền trong AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<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" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Chạy ứng dụng
Kết nối thiết bị với Wi-Fi, nhấn Run. TextView hiển thị địa chỉ IP (ví dụ: 192.168.1.15). Nếu chưa bật Wi-Fi, sẽ hiển thị 0.0.0.0.
Lấy IP cho cả Wi-Fi và Mobile Data (Cách toàn diện hơn)
WifiManager chỉ cho IP Wi-Fi. Để lấy tất cả interface network:
public String getAllIpAddresses() {
StringBuilder sb = new StringBuilder();
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
sb.append(intf.getDisplayName()).append(": ").append(inetAddress.getHostAddress()).append("\n");
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return sb.toString();
}
Cần quyền: android.permission.INTERNET và android.permission.ACCESS_NETWORK_STATE
Lưu ý
- IP trả về là IP nội bộ (private IP) trong mạng LAN, không phải IP công khai
- Để lấy IP công khai, cần gọi API bên ngoài (ví dụ: https://api.ipify.org)
- Android 10+ hạn chế truy cập MAC address, nhưng IP vẫn lấy được bình thường