ตั้งค่าบลูทูธ
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ก่อนที่แอปของคุณจะสื่อสารผ่านบลูทูธหรือบลูทูธพลังงานต่ำได้
คุณต้องยืนยันว่าอุปกรณ์รองรับบลูทูธ และหากรองรับ
ให้ตรวจสอบว่าได้เปิดใช้แล้ว โปรดทราบว่าการตรวจสอบนี้จำเป็นเฉพาะในกรณีที่
แอตทริบิวต์ android:required
ในรายการไฟล์ Manifest <uses-feature.../>
คือ
ตั้งค่าเป็น false
หากไม่สนับสนุนบลูทูธ คุณควรปิดใช้งานบลูทูธใดๆ ก็ตามอย่างสุภาพ
ใหม่ๆ หากบลูทูธได้รับการสนับสนุนแต่ปิดการใช้งานอยู่ คุณสามารถร้องขอให้มีการ
ผู้ใช้จะเปิดใช้บลูทูธโดยไม่ต้องออกจากแอปได้
ขั้นตอนแรกคือ
การเพิ่มสิทธิ์การใช้บลูทูธ
ลงในไฟล์ Manifest เพื่อใช้ API ต่อไปนี้
เมื่อมีสิทธิ์เรียบร้อยแล้ว การตั้งค่าบลูทูธจะทำได้ใน 2 ขั้นตอน
โดยใช้ BluetoothAdapter
:
สมัคร BluetoothAdapter
ต้องมี BluetoothAdapter
สำหรับกิจกรรมบลูทูธทั้งหมด
BluetoothAdapter
แสดงถึงอะแดปเตอร์บลูทูธของอุปกรณ์ (
วิทยุบลูทูธ) คุณต้องมี BluetoothAdapter
ก่อนจึงจะสมัครได้
Context
ใช้บริบทนี้เพื่อดู
อินสแตนซ์ของ BluetoothManager
บริการของระบบ กำลังโทรหา BluetoothManager#getAdapter
จะให้ออบเจ็กต์ BluetoothAdapter
แก่คุณ หาก getAdapter()
แสดงผลเป็น Null
แสดงว่าอุปกรณ์ไม่รองรับบลูทูธ
เช่น
Kotlin
val bluetoothManager: BluetoothManager = getSystemService(BluetoothManager::class.java)
val bluetoothAdapter: BluetoothAdapter? = bluetoothManager.getAdapter()
if (bluetoothAdapter == null) {
// Device doesn't support Bluetooth
}
Java
BluetoothManager bluetoothManager = getSystemService(BluetoothManager.class);
BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
if (bluetoothAdapter == null) {
// Device doesn't support Bluetooth
}
เปิดใช้บลูทูธ
ขั้นตอนต่อไป คุณต้องตรวจสอบว่าเปิดใช้บลูทูธอยู่ โทร
isEnabled()
ถึง
ตรวจสอบว่าบลูทูธเปิดอยู่ไหม หากวิธีนี้แสดงค่า "เท็จ"
บลูทูธก็จะปิดใช้งาน โทรเพื่อขอเปิดใช้บลูทูธ
startActivityForResult()
ใน
ACTION_REQUEST_ENABLE
การดำเนินการผ่าน Intent การโทรนี้จะออกคำขอเปิดใช้บลูทูธผ่าน
การตั้งค่าระบบ (โดยไม่ต้องหยุดแอปของคุณ)
เช่น
Kotlin
if (bluetoothAdapter?.isEnabled == false) {
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
}
Java
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
กล่องโต้ตอบจะปรากฏขึ้นเพื่อขอสิทธิ์จากผู้ใช้ในการเปิดใช้บลูทูธ ตามที่แสดงใน
รูปที่ 1 หากผู้ใช้ให้สิทธิ์ ระบบจะเริ่มเปิดใช้บลูทูธ
แล้วโฟกัสจะกลับไปที่แอปของคุณเมื่อกระบวนการเสร็จสมบูรณ์ (หรือล้มเหลว)
รูปที่ 1 กล่องโต้ตอบเปิดใช้บลูทูธ
ค่าคงที่ REQUEST_ENABLE_BT
ที่ส่งไปยัง
startActivityForResult()
คือจำนวนเต็มที่กำหนดในเครื่องซึ่งต้องมากกว่าหรือเท่ากับ 0 ระบบ
จะส่งค่าคงที่นี้กลับมาให้คุณใน
onActivityResult()
เป็นพารามิเตอร์ requestCode
หากเปิดใช้บลูทูธสำเร็จ กิจกรรมของคุณจะได้รับ
รหัสผลลัพธ์ RESULT_OK
ใน
การติดต่อกลับ onActivityResult()
หากไม่ได้เปิดใช้บลูทูธเนื่องจากเกิดข้อผิดพลาด (หรือ
ผู้ใช้ตอบว่า "ปฏิเสธ") รหัสผลลัพธ์คือ
RESULT_CANCELED
นอกจากนี้ แอปยังฟังฟังก์ชัน
ACTION_STATE_CHANGED
ความตั้งใจในการออกอากาศ ซึ่งระบบจะประกาศทุกครั้งที่บลูทูธมีสถานะเป็น
การเปลี่ยนแปลง การออกอากาศนี้มีฟิลด์พิเศษ
EXTRA_STATE
และ
EXTRA_PREVIOUS_STATE
,
ซึ่งมีสถานะบลูทูธใหม่และเก่าตามลำดับ ค่าที่เป็นไปได้สำหรับ
ฟิลด์พิเศษเหล่านี้
STATE_TURNING_ON
STATE_ON
,
STATE_TURNING_OFF
,
และ STATE_OFF
การฟังการออกอากาศนี้มีประโยชน์หากแอปต้องตรวจหารันไทม์
การเปลี่ยนแปลงสถานะของบลูทูธ
เคล็ดลับ: การเปิดใช้การค้นพบได้จะเป็นการเปิดใช้โดยอัตโนมัติ
บลูทูธ หากคุณวางแผนที่จะเปิดใช้การค้นพบอุปกรณ์อย่างสม่ำเสมอก่อนวันที่ดังกล่าว
ใช้งานบลูทูธ คุณสามารถข้ามขั้นตอนที่ 2 ในขั้นตอนก่อนหน้านี้ได้
เมื่อเปิดใช้บลูทูธในอุปกรณ์แล้ว คุณจะใช้ทั้งบลูทูธแบบคลาสสิกและ
บลูทูธพลังงานต่ำ
สำหรับบลูทูธแบบคลาสสิก คุณสามารถค้นหาอุปกรณ์บลูทูธ
และ
เชื่อมต่อกับอุปกรณ์บลูทูธ
สำหรับบลูทูธพลังงานต่ำ คุณจะค้นหาอุปกรณ์ BLE, เชื่อมต่อกับเซิร์ฟเวอร์ GATT และ
โอนข้อมูล BLE
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-07-27 UTC"],[],[],null,["# Set up Bluetooth\n\nBefore your app can communicate over Bluetooth or Bluetooth Low Energy,\nyou need to verify that Bluetooth is supported on the device, and if it is,\nensure that it is enabled. Note that this check is only necessary if the\n`android:required` attribute in the `\u003cuses-feature.../\u003e` manifest file entry is\nset to `false`.\n\nIf Bluetooth isn't supported, then you should gracefully disable any Bluetooth\nfeatures. If Bluetooth is supported, but disabled, then you can request that the\nuser enable Bluetooth without leaving your app.\n\nThe first step is\n[adding the Bluetooth permissions](/develop/connectivity/bluetooth/bt-permissions#declare)\nto your manifest file in order to use the following APIs.\n\nOnce the permissions are in place, Bluetooth setup is accomplished in two steps\nusing the [`BluetoothAdapter`](/reference/android/bluetooth/BluetoothAdapter):\n\n1. Get the `BluetoothAdapter`.\n\n The `BluetoothAdapter` is required for any and all Bluetooth activity. The\n `BluetoothAdapter` represents the device's own Bluetooth adapter (the\n Bluetooth radio). To get a `BluetoothAdapter`, you first need to have a\n [`Context`](/reference/android/content/Context). Use this context to obtain\n an instance of the [`BluetoothManager`](/reference/android/bluetooth/BluetoothManager)\n system service. Calling [`BluetoothManager#getAdapter`](/reference/android/bluetooth/BluetoothManager#getAdapter)\n will give you a `BluetoothAdapter` object. If `getAdapter()` returns null,\n then the device doesn't support Bluetooth.\n\n For example: \n\n ### Kotlin\n\n ```kotlin\n val bluetoothManager: BluetoothManager = getSystemService(BluetoothManager::class.java)\n val bluetoothAdapter: BluetoothAdapter? = bluetoothManager.getAdapter()\n if (bluetoothAdapter == null) {\n // Device doesn't support Bluetooth\n }\n ```\n\n ### Java\n\n ```java\n BluetoothManager bluetoothManager = getSystemService(BluetoothManager.class);\n BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();\n if (bluetoothAdapter == null) {\n // Device doesn't support Bluetooth\n }\n ```\n2. Enable Bluetooth.\n\n Next, you need to ensure that Bluetooth is enabled. Call\n [`isEnabled()`](/reference/android/bluetooth/BluetoothAdapter#isEnabled()) to\n check whether Bluetooth is currently enabled. If this method returns false,\n then Bluetooth is disabled. To request that Bluetooth be enabled, call\n [`startActivityForResult()`](/reference/android/app/Activity#startActivityForResult(android.content.Intent,%20int)),\n passing in an\n [`ACTION_REQUEST_ENABLE`](/reference/android/bluetooth/BluetoothAdapter#ACTION_REQUEST_ENABLE)\n intent action. This call issues a request to enable Bluetooth through the\n system settings (without stopping your app).\n\n For example: \n\n ### Kotlin\n\n ```kotlin\n if (bluetoothAdapter?.isEnabled == false) {\n val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)\n startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)\n }\n ```\n\n ### Java\n\n ```java\n if (!bluetoothAdapter.isEnabled()) {\n Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);\n startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);\n }\n ```\n\n \u003cbr /\u003e\n\nA dialog appears requesting user permission to enable Bluetooth, as shown in\nfigure 1. If the user grants permission, the system begins to enable Bluetooth,\nand focus returns to your app once the process completes (or fails).\n\n\u003cbr /\u003e\n\n\n**Figure 1.** The enabling Bluetooth dialog.\n\nThe `REQUEST_ENABLE_BT` constant passed to\n[`startActivityForResult()`](/reference/android/app/Activity#startActivityForResult(android.content.Intent,%20int))\nis a locally-defined integer that must be greater than or equal to 0. The system\npasses this constant back to you in your\n[`onActivityResult()`](/reference/android/app/Activity#onActivityResult(int,%20int,%20android.content.Intent))\nimplementation as the `requestCode` parameter.\n\nIf enabling Bluetooth succeeds, your activity receives the\n[`RESULT_OK`](/reference/android/app/Activity#RESULT_OK) result code in the\n`onActivityResult()` callback. If Bluetooth was not enabled due to an error (or\nthe user responded \"Deny\") then the result code is\n[`RESULT_CANCELED`](/reference/android/app/Activity#RESULT_CANCELED).\n\nOptionally, your app can also listen for the\n[`ACTION_STATE_CHANGED`](/reference/android/bluetooth/BluetoothAdapter#ACTION_STATE_CHANGED)\nbroadcast intent, which the system broadcasts whenever the Bluetooth state\nchanges. This broadcast contains the extra fields\n[`EXTRA_STATE`](/reference/android/bluetooth/BluetoothAdapter#EXTRA_STATE) and\n[`EXTRA_PREVIOUS_STATE`](/reference/android/bluetooth/BluetoothAdapter#EXTRA_PREVIOUS_STATE),\ncontaining the new and old Bluetooth states, respectively. Possible values for\nthese extra fields are\n[`STATE_TURNING_ON`](/reference/android/bluetooth/BluetoothAdapter#STATE_TURNING_ON),\n[`STATE_ON`](/reference/android/bluetooth/BluetoothAdapter#STATE_ON),\n[`STATE_TURNING_OFF`](/reference/android/bluetooth/BluetoothAdapter#STATE_TURNING_OFF),\nand [`STATE_OFF`](/reference/android/bluetooth/BluetoothAdapter#STATE_OFF).\nListening for this broadcast can be useful if your app needs to detect runtime\nchanges made to the Bluetooth state. \n**Tip:** Enabling discoverability automatically enables Bluetooth. If you plan to consistently enable device discoverability before performing Bluetooth activity, you can skip step 2 in the earlier steps.\n\nOnce Bluetooth is enabled on the device, you can use both Bluetooth classic and\nBluetooth Low Energy.\n\nFor Bluetooth classic, you can [find Bluetooth devices](/develop/connectivity/bluetooth/find-bluetooth-devices)\nand\n[connect to Bluetooth devices](/develop/connectivity/bluetooth/connect-bluetooth-devices).\n\nFor Bluetooth Low Energy, you can [find BLE devices](/develop/connectivity/bluetooth/find-ble-devices), [connect to a GATT server](/develop/connectivity/bluetooth/connect-gatt-server), and\n[transfer BLE data](/develop/connectivity/bluetooth/transfer-ble-data)."]]