Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีดึง BSSID ของ Wi-Fi ปัจจุบันใน Android

บทนำ

ตัวอย่างนี้สาธิตวิธีดึง BSSID (Basic Service Set Identifier) ของ Access Point Wi-Fi ที่เชื่อมต่ออยู่ โดย BSSID คือ MAC Address ของ Router/AP ไม่ใช่ของอุปกรณ์ Android

ขั้นตอนที่ 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();
        
        // ดึง BSSID (MAC Address ของ Access Point)
        textView.setText("" + wifiInfo.getBSSID());
    }

    @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>

ผลลัพธ์

วิธีดึง BSSID ของ Wi-Fi ปัจจุบันใน Android

ความแตกต่างระหว่าง MAC Address และ BSSID

คุณสมบัติ MAC Address (Device) BSSID (Access Point)
เมธอดwifiInfo.getMacAddress()wifiInfo.getBSSID()
หมายถึงที่อยู่ฮาร์ดแวร์ของ Wi-Fi Chip ในโทรศัพท์ที่อยู่ฮาร์ดแวร์ของ Router/AP ที่เชื่อมต่ออยู่
Android 6.0+คืนค่า Randomized/Constantคืนค่า BSSID จริงของ AP (ต้องมี Location Permission)
ประโยชน์ระบุตัวตนอุปกรณ์ (จำกัดแล้ว)ระบุ AP, Location-based Services, Wi-Fi Analytics

ข้อจำกัด Privacy (Android 10+)

  • Android 10 (API 29): ต้องการ ACCESS_FINE_LOCATION หรือ ACCESS_COARSE_LOCATION เพื่ออ่าน BSSID
  • Android 11 (API 30): เพิ่ม ACCESS_BACKGROUND_LOCATION หากเข้าถึงจาก Background
  • Android 12 (API 31): ใช้ WifiManager.getConnectionInfo() ต้องมี Permission ACCESS_FINE_LOCATION และ App ต้องเป็น Foreground

โค้ดปลอดภัย (Runtime Permission Check)

private void fetchBSSID() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        
        WifiManager wifiMgr = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
        if (wifiMgr != null) {
            WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
            if (wifiInfo != null) {
                String bssid = wifiInfo.getBSSID();
                textView.setText(bssid != null ? bssid : "Not connected");
            }
        }
    } else {
        ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1001);
    }
}

กรณีใช้งาน BSSID

  • Indoor Positioning / Wi-Fi Fingerprinting
  • Network Diagnostics (Signal Strength per AP)
  • Auto-login Captive Portal Detection
  • Enterprise Wi-Fi Management