ตัวอย่างนี้แสดงวิธีการ เปิด Bluetooth ใน Android โดยใช้ BluetoothAdapter.enable() หมายเหตุ: เมธอดนี้ต้องการสิทธิ์ BLUETOOTH_ADMIN และใน Android 12+ (API 31) ต้องใช้ BluetoothAdapter.enable() ผ่าน system dialog แทน
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
เปิด Android Studio ไปที่ File → New Project
ขั้นตอนที่ 2: เพิ่ม Layout XML
เพิ่มโค้ดใน res/layout/activity_main.xml ใช้ TextView เป็นปุ่มกด:
<?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.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
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);
textView.setText("Tap to Enable Bluetooth");
final BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// วิธีเก่า (Deprecated ใน API 31+)
// manager.getAdapter().enable();
// วิธีใหม่สำหรับ Android 12+ (API 31+)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
// ใช้ Intent เพื่อขอเปิด Bluetooth ผ่าน System Dialog
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
} else {
// สำหรับ Android เก่ากว่า API 31
manager.getAdapter().enable();
}
}
});
}
private static final int REQUEST_ENABLE_BT = 1;
}
ขั้นตอนที่ 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" />
<!-- สำหรับ Android 12+ ต้องเพิ่ม permission ใหม่ -->
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<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>
การเปลี่ยนแปลงใน Android 12 (API 31+):
BluetoothAdapter.enable()และdisable()ถูก Deprecated- ต้องใช้
BluetoothAdapter.ACTION_REQUEST_ENABLEIntent แทน - ต้องเพิ่ม permission
BLUETOOTH_CONNECT(normal permission) - ต้องขอ Runtime Permission สำหรับ
BLUETOOTH_CONNECTและBLUETOOTH_SCAN
โค้ดสมบูรณ์สำหรับ Android 12+ (Runtime Permission)
// ตรวจสอบและขอสิทธิ์ก่อนเปิด Bluetooth
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (checkSelfPermission(Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.BLUETOOTH_CONNECT}, REQUEST_BT_PERMISSION);
} else {
enableBluetooth();
}
} else {
enableBluetooth();
}
private void enableBluetooth() {
BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
BluetoothAdapter adapter = manager.getAdapter();
if (adapter != null && !adapter.isEnabled()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
} else {
adapter.enable();
}
}
}
การทดสอบ
รันแอปพลิเคชัน กดที่ TextView จะแสดง Dialog ขออนุญาตเปิด Bluetooth
