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

วิธีตรวจสอบว่า WiFi ปัจจุบันรองรับ 5 GHz หรือไม่ใน Android

ตัวอย่างนี้แสดงวิธีตรวจสอบว่า WiFi ปัจจุบันรองรับ 5 GHz band หรือไม่ โดยใช้ WifiManager.is5GHzBandSupported() (ต้องการ API 24+ / Android 7.0 Nougat)

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

เปิด Android Studio ไปที่ File → New Project

ขั้นตอนที่ 2: เพิ่ม Layout XML

เพิ่มโค้ดใน res/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:gravity="center"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/text"
        android:textSize="30sp"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

ขั้นตอนที่ 3: เพิ่มโค้ด Java

เพิ่มโค้ดใน src/MainActivity.java:

package com.example.myapplication;

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);
        // ตรวจสอบการรองรับ 5 GHz
        boolean is5GHzSupported = wifiManager.is5GHzBandSupported();
        textView.setText("5 GHz Supported: " + is5GHzSupported);
    }
}

ขั้นตอนที่ 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" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
ข้อมูลเพิ่มเติม:
  • is5GHzBandSupported() คืนค่า true หากฮาร์ดแวร์รองรับ 5 GHz
  • ต้องการ ACCESS_WIFI_STATE permission
  • เพื่อตรวจสอบว่ากำลังเชื่อมต่อ 5 GHz อยู่หรือไม่ ใช้ WifiInfo.getFrequency() แทน
  • 5 GHz ให้ความเร็วสูงกว่า แต่ระยะสั้นกว่า 2.4 GHz

การตรวจสอบความถี่ปัจจุบัน (Bonus)

WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int frequency = wifiInfo.getFrequency(); // MHz
boolean is5GHz = frequency > 4900 && frequency < 5900; // ช่วง 5 GHz

การทดสอบ

รันแอปพลิเคชันบนอุปกรณ์จริง จะแสดง "true" หรือ "false"

วิธีตรวจสอบว่า WiFi ปัจจุบันรองรับ 5 GHz หรือไม่ใน Android