บทนำ
ตัวอย่างนี้สาธิตวิธีดึง MAC Address ของอินเทอร์เฟซ Wi-Fi ที่เชื่อมต่ออยู่ โดยใช้ WifiManager และ WifiInfo
ขั้นตอนที่ 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.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();
// ดึง MAC Address
textView.setText("" + wifiInfo.getMacAddress());
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onResume() {
super.onResume();
}
}
ขั้นตอนที่ 4: AndroidManifest.xml - Permission
<?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>
ผลลัพธ์

⚠️ ข้อจำกัดสำคัญ (Privacy Changes)
| Android Version | พฤติกรรม |
|---|---|
| Android 5.1 (API 22) และก่อน | คืนค่า MAC Address จริงของอุปกรณ์ |
| Android 6.0 - 9 (API 23-28) | คืนค่า 02:00:00:00:00:00 (ค่าคงที่) สำหรับแอปที่ไม่ใช่ System/Privileged |
| Android 10 (API 29) ขึ้นไป | ใช้ Randomized MAC ต่อ SSID แต่ละอัน ไม่สามารถดึง MAC จริงได้ |
ทางเลือกแทน MAC Address
- Instance ID / Firebase Installation ID - ระบุตัวตนการติดตั้งแอป
- Advertising ID (GAID) - สำหรับโฆษณา/Analytics (ผู้ใช้ปิดได้)
- Settings.Secure.ANDROID_ID - คงที่ต่อการติดตั้ง (Reset เมื่อ Factory Reset)
- Attestation / SafetyNet / Play Integrity - ยืนยันความถูกต้องของอุปกรณ์
โค้ดที่ปลอดภัยกว่า (Null Check)
WifiManager wifiMgr = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
if (wifiMgr != null) {
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
if (wifiInfo != null) {
String mac = wifiInfo.getMacAddress();
textView.setText(mac != null ? mac : "N/A");
}
}