Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách lấy tốc độ Wi-Fi tối đa hỗ trợ trên Android

Giới thiệu

Đọc WifiInfo.getLinkSpeed() để biết tốc độ liên kết hiện tại (Mbps). Đây là tốc độ PHY tối đa của chuẩn Wi-Fi đang kết nối.

Code (MainActivity.java)

public class MainActivity extends AppCompatActivity {

    private TextView textView;

    @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) {
            int linkSpeed = wifiInfo.getLinkSpeed(); // Mbps
            textView.setText("Tốc độ liên kết: " + linkSpeed + " Mbps");
        } else {
            textView.setText("Chưa kết nối Wi-Fi");
        }
    }
}

Quyền

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

Bảng tốc độ phổ biến theo chuẩn

ChuẩnTốc độ PHY tối đa (Mbps)
802.11b11
802.11g54
802.11n (Wi-Fi 4)72 - 600 (tùy antenna, channel width)
802.11ac (Wi-Fi 5)433 - 3466
802.11ax (Wi-Fi 6/6E)600 - 9608

Lưu ý

  • Tốc độ thực tế (throughput) thường chỉ 50-70% tốc độ PHY do overhead, nhiễu, khoảng cách.
  • Để biết chuẩn Wi-Fi (n/ac/ax), cần parse WifiInfo.getFrequency() hoặc dùng WifiManager.getConfiguredNetworks().