บทนำ
ตัวอย่างนี้แสดงวิธีดึงความเร็วลิงก์ (Link Speed) ของการเชื่อมต่อ Wi-Fi ปัจจุบัน โดยใช้ WifiInfo.getLinkSpeed() หน่วยเป็น Mbps
ขั้นตอนที่ 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();
// ดึง Link Speed (หน่วย Mbps)
textView.setText("" + wifiInfo.getLinkSpeed());
}
@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>
ผลลัพธ์

ข้อมูลเพิ่มเติมเกี่ยวกับ Link Speed
- หน่วย: Mbps (Megabits per second)
- ค่าที่พบบ่อย: 54, 72, 144, 300, 433, 600, 866, 1200, 2400 Mbps
- ค่า -1: หมายถึงไม่ได้เชื่อมต่อ Wi-Fi หรือไม่รองรับ
- ค่าคงที่:
WifiInfo.LINK_SPEED_UNITS= "Mbps"
ข้อจำกัดและข้อควรระวัง
- Link Speed = อัตราสัญญาณสูงสุดทฤษฎี (PHY Rate) ไม่ใช่ Throughput จริง
- Throughput จริงมักประมาณ 40-60% ของ Link Speed
- Android 10+ จำกัดการเข้าถึง WifiInfo ต้องการ Permission
ACCESS_FINE_LOCATIONหรือACCESS_COARSE_LOCATION - สำหรับข้อมูลละเอียดกว่า (Channel Width, MIMO, 802.11ax) ใช้
WifiManager.getConnectionInfo()ร่วมกับScanResult
ตัวอย่างโค้ดปลอดภัย (Null Check)
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
if (wifiManager != null) {
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo != null) {
int linkSpeed = wifiInfo.getLinkSpeed(); // Mbps
if (linkSpeed != -1) {
textView.setText(linkSpeed + " Mbps");
} else {
textView.setText("Not connected");
}
}
}