บทนำ
ตัวอย่างนี้สาธิตวิธีดึงที่อยู่ IP Address (IPv4) ของอินเทอร์เฟซ Wi-Fi ที่เชื่อมต่ออยู่ โดยใช้ WifiManager, WifiInfo และ Formatter.formatIpAddress()
ขั้นตอนที่ 1: สร้างโปรเจกต์
Android Studio → File → New Project → Empty Activity
ขั้นตอนที่ 2: Layout (activity_main.xml)
<?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:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp" />
</LinearLayout>
ขั้นตอนที่ 3: โค้ดหลัก (MainActivity.java)
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();
// getIpAddress() คืนค่า int (IPv4 แบบ packed) ต้องแปลงด้วย Formatter
int ipAddressInt = wifiInfo.getIpAddress();
String ipAddress = Formatter.formatIpAddress(ipAddressInt);
textView.setText("" + ipAddress);
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onResume() {
super.onResume();
}
}
ขั้นตอนที่ 4: 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>
ผลลัพธ์

คำอธิบายการทำงาน
wifiInfo.getIpAddress()คืนค่าintที่เป็น IPv4 แบบ Packed (Little Endian)Formatter.formatIpAddress(int)แปลง int เป็น String รูปแบบ "xxx.xxx.xxx.xxx"- ตัวอย่าง: int 3232235777 → "192.168.1.1"
ข้อจำกัดและ Privacy (Android 10+)
| Version | พฤติกรรม |
|---|---|
| Android 9 (API 28) และก่อน | ดึง IP ได้ปกติด้วย ACCESS_WIFI_STATE |
| Android 10 (API 29) - 11 (API 30) | ต้องการ ACCESS_FINE_LOCATION หรือ ACCESS_COARSE_LOCATION |
| Android 12 (API 31) ขึ้นไป | จำกัดเพิ่มเติม อาจคืนค่า 0.0.0.0 หากไม่มี Permission ที่เหมาะสม |
ทางเลือกสมัยใหม่ (Android 10+)
// ใช้ NetworkCallback และ LinkProperties
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkRequest request = new NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.build();
cm.registerNetworkCallback(request, new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(@NonNull Network network) {
NetworkCapabilities nc = cm.getNetworkCapabilities(network);
if (nc != null) {
LinkProperties lp = nc.getLinkProperties();
for (LinkAddress addr : lp.getLinkAddresses()) {
Log.d("IP", addr.getAddress().getHostAddress());
}
}
}
});
โค้ดปลอดภัย (Null Check)
WifiManager wifiMgr = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
if (wifiMgr != null) {
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
if (wifiInfo != null) {
int ipInt = wifiInfo.getIpAddress();
String ip = (ipInt != 0) ? Formatter.formatIpAddress(ipInt) : "Not connected";
textView.setText(ip);
}
}