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

วิธีตรวจสอบว่า Bluetooth รองรับ LE 2M PHY ใน Android

ตัวอย่างนี้แสดงวิธีตรวจสอบว่า Bluetooth ปัจจุบันรองรับ LE 2M PHY (LE 2M PHY) หรือไม่ ซึ่งเป็นคุณสมบัติของ Bluetooth 5.0 ที่เพิ่มความเร็วการส่งข้อมูลเป็น 2 เท่า โดยใช้ BluetoothAdapter.isLe2MPhySupported() (ต้องการ API 26+ / Android 8.0 Oreo)

ขั้นตอนที่ 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.bluetooth.BluetoothManager;
import android.content.Context;
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);

        final BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
        
        // ตรวจสอบ LE 2M PHY Support (ต้องการ API 26+)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            boolean isLe2MPhySupported = manager.getAdapter().isLe2MPhySupported();
            textView.setText("LE 2M PHY Supported: " + isLe2MPhySupported);
        } else {
            textView.setText("Requires Android 8.0+ (API 26)");
        }
    }
}

ขั้นตอนที่ 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.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <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>
เกี่ยวกับ LE 2M PHY (Bluetooth 5.0):
  • 2M PHY - เร็ว 2 เท่า (2 Mbps) แตระยะสั้นกว่า
  • 1M PHY - มาตรฐานเดิม (1 Mbps) สมดุลระหว่างความเร็วและระยะ
  • Coded PHY (Long Range) - ช้ากว่า (125/500 Kbps) แต่ไกล 4 เท่า
  • ทั้งสองอุปกรณ์ต้องรองรับ Bluetooth 5.0 จึงจะใช้ 2M PHY ได้

การทดสอบ

รันแอปพลิเคชันบนอุปกรณ์ที่รองรับ Bluetooth 5.0+ จะแสดง "true"

วิธีตรวจสอบว่า Bluetooth รองรับ LE 2M PHY ใน Android