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

วิธีตรวจสอบความเร็วสูงสุดที่ Wi-Fi รองรับใน Android

บทนำ

ตัวอย่างนี้แสดงวิธีดึง Link Speed ของการเชื่อมต่อ Wi-Fi ปัจจุบัน ซึ่งแสดงความเร็วสูงสุดทฤษฎี (PHY Rate) ที่อุปกรณ์และ Access Point เนโกเฉชได้ (หน่วย Mbps)

ขั้นตอนที่ 1: สร้างโปรเจกต์

Android Studio → 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 wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();

        if (wifiInfo != null) {
            Integer linkSpeed = wifiInfo.getLinkSpeed(); // หน่วย: WifiInfo.LINK_SPEED_UNITS (Mbps)
            textView.setText(linkSpeed + " Mbps");
        }
    }
}

ขั้นตอนที่ 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" />
    <uses-permission android:name="android.permission.CHANGE_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>

ผลลัพธ์

วิธีตรวจสอบความเร็วสูงสุดที่ Wi-Fi รองรับใน Android

ค่าความเร็วที่พบบ่อยตามมาตรฐาน Wi-Fi

มาตรฐาน Band Stream Channel Width Link Speed (Mbps)
802.11b2.4 GHz120 MHz1, 2, 5.5, 11
802.11g2.4 GHz120 MHz6, 9, 12, 18, 24, 36, 48, 54
802.11n (Wi-Fi 4)2.4/5 GHz1-420/40 MHz65 - 600
802.11ac (Wi-Fi 5)5 GHz1-820/40/80/160 MHz433 - 6933
802.11ax (Wi-Fi 6/6E)2.4/5/6 GHz1-820/40/80/160 MHz600 - 9608
802.11be (Wi-Fi 7)2.4/5/6 GHz1-16320 MHzUp to 46,120

ข้อสังเกตสำคัญ

  • Link Speed ≠ Throughput: ค่าที่ได้คือ PHY Rate (Physical Layer Rate) ทฤษฎี สูงสุด
  • Throughput จริง: ประมาณ 40-60% ของ Link Speed เนื่องจาก Overhead, Retransmission, Interference
  • Dynamic: ค่าเปลี่ยนไปตามสัญญาณ ระยะทาง Interference และ Capability ของทั้ง Client และ AP
  • -1: ไม่ได้เชื่อมต่อ Wi-Fi

ข้อมูลเพิ่มเติมที่ดึงได้จาก WifiInfo

WifiInfo wifiInfo = wifiManager.getConnectionInfo();

String ssid = wifiInfo.getSSID();           // ชื่อเครือข่าย (มี quotes "SSID")
String bssid = wifiInfo.getBSSID();         // MAC ของ AP
int rssi = wifiInfo.getRssi();              // Signal Strength (dBm, ติดลบ เช่น -45)
int frequency = wifiInfo.getFrequency();    // Frequency (MHz) 2412, 5180, etc.
int linkSpeed = wifiInfo.getLinkSpeed();    // Mbps
String macAddress = wifiInfo.getMacAddress(); // Device MAC (Randomized Android 6+)
int ipAddress = wifiInfo.getIpAddress();    // IPv4 (int)
int networkId = wifiInfo.getNetworkId();    // Network ID ในระบบ

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

  • ต้องการ ACCESS_FINE_LOCATION หรือ ACCESS_COARSE_LOCATION
  • Android 12+ ต้องเป็น Foreground App
  • ควรใช้ ConnectivityManager.NetworkCallback ร่วมกับ WifiManager สำหรับ Real-time Updates