ภาพรวมบลูทูธ
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
แพลตฟอร์ม Android รองรับสแต็กเครือข่ายบลูทูธ ซึ่ง
ช่วยให้อุปกรณ์แลกเปลี่ยนข้อมูลกับอุปกรณ์บลูทูธอื่นๆ แบบไร้สายได้
เฟรมเวิร์กของแอปช่วยให้เข้าถึงฟังก์ชันการทำงานของบลูทูธผ่าน
API ของบลูทูธได้ API เหล่านี้ช่วยให้แอปเชื่อมต่อกับอุปกรณ์บลูทูธอื่นๆ ได้ ซึ่งจะช่วยให้ใช้ฟีเจอร์ไร้สายแบบจุดต่อจุดและแบบหลายจุดได้
แอปสามารถทำสิ่งต่อไปนี้ได้โดยใช้ Bluetooth API
- สแกนหาอุปกรณ์บลูทูธอื่นๆ
- ค้นหาอุปกรณ์บลูทูธที่จับคู่แล้วในอะแดปเตอร์บลูทูธในเครื่อง
- สร้างแชแนล RFCOMM
- เชื่อมต่อกับอุปกรณ์อื่นๆ ผ่านการค้นพบบริการ
- โอนข้อมูลไปยังและจากอุปกรณ์อื่นๆ
- จัดการการเชื่อมต่อหลายรายการ
หัวข้อนี้มุ่งเน้นไปที่บลูทูธคลาสสิก บลูทูธคลาสสิกเป็นตัวเลือกที่เหมาะสม
สำหรับการทำงานที่ใช้แบตเตอรี่มากกว่า ซึ่งรวมถึงการสตรีมและการสื่อสาร
ระหว่างอุปกรณ์ สำหรับอุปกรณ์บลูทูธที่ต้องการพลังงานต่ำ ให้พิจารณาใช้การเชื่อมต่อบลูทูธพลังงานต่ำ
เอกสารนี้อธิบายโปรไฟล์บลูทูธต่างๆ และอธิบายวิธีใช้ Bluetooth API เพื่อทํางานหลัก 4 อย่างที่จําเป็นต่อการสื่อสารโดยใช้บลูทูธ
- การตั้งค่าบลูทูธ
- ค้นหาอุปกรณ์ที่จับคู่แล้วหรือพร้อมใช้งานในพื้นที่
- เชื่อมต่ออุปกรณ์
- การโอนข้อมูลระหว่างอุปกรณ์
หากต้องการดูตัวอย่างการใช้ Bluetooth API โปรดดูแอปตัวอย่างแชทผ่านบลูทูธ
ข้อมูลเบื้องต้น
หากต้องการให้อุปกรณ์ที่เปิดใช้บลูทูธส่งข้อมูลระหว่างกัน อุปกรณ์จะต้อง
สร้างช่องทางการสื่อสารก่อนโดยใช้กระบวนการจับคู่ อุปกรณ์เครื่องหนึ่งซึ่งเป็น
อุปกรณ์ที่ค้นพบได้จะทำให้ตัวเองพร้อมรับคำขอการเชื่อมต่อขาเข้า
อุปกรณ์อื่นค้นหาอุปกรณ์ที่ค้นพบได้โดยใช้กระบวนการค้นหาบริการ
หลังจากอุปกรณ์ที่ค้นพบได้ยอมรับคำขอจับคู่แล้ว อุปกรณ์ทั้ง 2 เครื่อง
จะดำเนินการผูกอุปกรณ์ให้เสร็จสมบูรณ์ ซึ่งจะมีการแลกเปลี่ยนคีย์ความปลอดภัย อุปกรณ์จะแคชคีย์เหล่านี้เพื่อใช้ในภายหลัง หลังจากกระบวนการจับคู่และการเชื่อมต่อเสร็จสมบูรณ์
อุปกรณ์ทั้ง 2 เครื่องจะแลกเปลี่ยนข้อมูลกัน เมื่อเซสชันเสร็จสมบูรณ์
อุปกรณ์ที่เริ่มคำขอจับคู่จะปล่อยช่องที่ลิงก์
อุปกรณ์นั้นกับอุปกรณ์ที่ค้นพบได้ อย่างไรก็ตาม อุปกรณ์ทั้ง 2 เครื่องจะยังคงจับคู่กันอยู่ ดังนั้น
จึงเชื่อมต่อใหม่ได้โดยอัตโนมัติในเซสชันในอนาคต ตราบใดที่อุปกรณ์ทั้ง 2 เครื่องอยู่
ในระยะกันและกัน และไม่มีอุปกรณ์ใดยกเลิกการจับคู่
การใช้ Bluetooth API ต้องประกาศสิทธิ์หลายรายการในไฟล์ Manifest เมื่อแอปได้รับสิทธิ์ใช้บลูทูธแล้ว แอปจะต้องเข้าถึง BluetoothAdapter
และตรวจสอบว่าอุปกรณ์มีบลูทูธหรือไม่
หากมีบลูทูธ คุณจะเชื่อมต่อได้โดยทำตาม 3 ขั้นตอนต่อไปนี้
อุปกรณ์บางเครื่องใช้โปรไฟล์บลูทูธที่เฉพาะเจาะจงซึ่งประกาศข้อมูล
ที่อุปกรณ์นั้นให้
คลาสและอินเทอร์เฟซที่สำคัญ
API บลูทูธทั้งหมดจะอยู่ในแพ็กเกจ
android.bluetooth
ต่อไปนี้คือคลาสและอินเทอร์เฟซที่คุณต้องใช้เพื่อสร้างการเชื่อมต่อ Bluetooth
BluetoothAdapter
- แสดงถึงอะแดปเตอร์บลูทูธในเครื่อง (วิทยุบลูทูธ)
BluetoothAdapter
คือจุดแรกเข้าสำหรับการโต้ตอบผ่านบลูทูธทั้งหมด เมื่อใช้
ฟีเจอร์นี้ คุณจะค้นพบอุปกรณ์บลูทูธอื่นๆ สอบถามรายการอุปกรณ์ที่เชื่อมต่อ
(จับคู่) สร้างอินสแตนซ์ของ
BluetoothDevice
โดยใช้ที่อยู่ MAC ที่รู้จัก และสร้าง
BluetoothServerSocket
เพื่อฟังการสื่อสารจากอุปกรณ์อื่นๆ ได้
BluetoothDevice
- แสดงถึงอุปกรณ์บลูทูธระยะไกล ใช้เพื่อขอการเชื่อมต่อกับอุปกรณ์ระยะไกลผ่าน
BluetoothSocket
หรือค้นหาข้อมูลเกี่ยวกับอุปกรณ์ เช่น ชื่อ ที่อยู่ คลาส และสถานะการจับคู่
BluetoothSocket
- แสดงอินเทอร์เฟซสำหรับซ็อกเก็ตบลูทูธ (คล้ายกับ TCP
Socket
) ซึ่งเป็นจุดเชื่อมต่อที่
อนุญาตให้แอปแลกเปลี่ยนข้อมูลกับอุปกรณ์บลูทูธอื่นโดยใช้
InputStream
และ
OutputStream
BluetoothServerSocket
- แสดงถึงซ็อกเก็ตเซิร์ฟเวอร์ที่เปิดอยู่ซึ่งรอรับคำขอขาเข้า (คล้ายกับ TCP
ServerSocket
) หากต้องการเชื่อมต่ออุปกรณ์ 2 เครื่อง อุปกรณ์เครื่องหนึ่งต้องเปิดซ็อกเก็ตเซิร์ฟเวอร์ด้วยคลาสนี้ เมื่ออุปกรณ์บลูทูธระยะไกลส่งคำขอเชื่อมต่อมายังอุปกรณ์นี้
อุปกรณ์จะยอมรับการเชื่อมต่อ จากนั้นจะส่ง BluetoothSocket
ที่เชื่อมต่อแล้วกลับไป
BluetoothClass
- อธิบายลักษณะและความสามารถทั่วไปของอุปกรณ์บลูทูธ
นี่คือชุดพร็อพเพอร์ตี้แบบอ่านอย่างเดียวที่กำหนดคลาสและ
บริการของอุปกรณ์ แม้ว่าข้อมูลนี้จะให้คำแนะนำที่เป็นประโยชน์เกี่ยวกับประเภทของอุปกรณ์ แต่แอตทริบิวต์ของคลาสนี้ไม่ได้อธิบายโปรไฟล์และบริการบลูทูธทั้งหมดที่อุปกรณ์รองรับเสมอไป
BluetoothProfile
- อินเทอร์เฟซที่แสดงโปรไฟล์บลูทูธ โปรไฟล์บลูทูธคือ
ข้อกำหนดอินเทอร์เฟซไร้สายสำหรับการสื่อสารที่ใช้บลูทูธระหว่าง
อุปกรณ์ ตัวอย่างเช่น โปรไฟล์แฮนด์ฟรี ดูข้อมูลเพิ่มเติมเกี่ยวกับ
โปรไฟล์ได้ที่โปรไฟล์บลูทูธ
BluetoothHeadset
- รองรับการใช้ชุดหูฟังบลูทูธกับโทรศัพท์มือถือ ซึ่งรวมถึงทั้งโปรไฟล์ชุดหูฟังบลูทูธและโปรไฟล์แฮนด์ฟรี (v1.5)
BluetoothA2dp
- กำหนดวิธีสตรีมเสียงคุณภาพสูงจากอุปกรณ์หนึ่งไปยังอีกอุปกรณ์หนึ่งผ่านการเชื่อมต่อบลูทูธโดยใช้โปรไฟล์การกระจายเสียงขั้นสูง (A2DP)
BluetoothHealth
- แสดงถึงพร็อกซีโปรไฟล์อุปกรณ์สุขภาพที่ควบคุมบริการบลูทูธ
BluetoothHealthCallback
- คลาสแบบนามธรรมที่คุณใช้เพื่อใช้
BluetoothHealth
การเรียกกลับ คุณ
ต้องขยายคลาสนี้และใช้เมธอดเรียกกลับเพื่อรับข้อมูลอัปเดต
เกี่ยวกับการเปลี่ยนแปลงสถานะการลงทะเบียนของแอปและสถานะช่องสัญญาณบลูทูธ
BluetoothHealthAppConfiguration
- แสดงการกำหนดค่าแอปที่แอปสุขภาพบลูทูธของบุคคลที่สาม
ลงทะเบียนเพื่อสื่อสารกับอุปกรณ์สุขภาพบลูทูธระยะไกล
BluetoothProfile.ServiceListener
- อินเทอร์เฟซที่แจ้งเตือนไคลเอ็นต์
BluetoothProfile
การสื่อสารระหว่างกระบวนการ (IPC)
เมื่อเชื่อมต่อหรือยกเลิกการเชื่อมต่อกับบริการภายใน
ที่เรียกใช้โปรไฟล์หนึ่งๆ
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา 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,["# Bluetooth overview\n\nThe Android platform includes support for the Bluetooth network stack, which\nallows a device to wirelessly exchange data with other Bluetooth devices. The\napp framework provides access to the Bluetooth functionality through Bluetooth\nAPIs. These APIs let apps connect to other Bluetooth devices, enabling\npoint-to-point and multipoint wireless features.\n\nUsing the Bluetooth APIs, an app can perform the following:\n\n- Scan for other Bluetooth devices.\n- Query the local Bluetooth adapter for paired Bluetooth devices.\n- Establish RFCOMM channels.\n- Connect to other devices through service discovery.\n- Transfer data to and from other devices.\n- Manage multiple connections.\n\nThis topic focuses on *Classic Bluetooth* . Classic Bluetooth is the right choice\nfor more battery-intensive operations, which include streaming and communicating\nbetween devices. For Bluetooth devices with low power requirements, consider\nusing [Bluetooth Low Energy](/develop/connectivity/bluetooth/ble-overview)\nconnections.\n\nThis documentation describes different Bluetooth\n[profiles](/develop/connectivity/bluetooth/profiles) and explains how to\nuse the Bluetooth APIs to accomplish the four major tasks necessary to\ncommunicate using Bluetooth:\n\n- Setting up Bluetooth.\n- Finding devices that are either paired or available in the local area.\n- Connecting devices.\n- Transferring data between devices.\n\nFor a demonstration of using the Bluetooth APIs, see the [Bluetooth Chat sample\napp](https://github.com/android/connectivity-samples/tree/master/BluetoothChat).\n\nThe basics\n----------\n\nFor Bluetooth-enabled devices to transmit data between each other, they must\nfirst form a channel of communication using a pairing process. One device, a\ndiscoverable device, makes itself available for incoming connection requests.\nAnother device finds the discoverable device using a service discovery process.\nAfter the discoverable device accepts the pairing request, the two devices\ncomplete a bonding process in which they exchange security keys. The devices\ncache these keys for later use. After the pairing and bonding processes are\ncomplete, the two devices exchange information. When the session is complete,\nthe device that initiated the pairing request releases the channel that had\nlinked it to the discoverable device. The two devices remain bonded, however, so\nthey can reconnect automatically during a future session as long as they're in\nrange of each other and neither device has removed the bond.\n\nUse of the Bluetooth APIs requires\n[declaring several permissions](/develop/connectivity/bluetooth/bt-permissions#declare)\nin your manifest file. Once your app has permission to use Bluetooth, your app\nneeds to access the\n[`BluetoothAdapter`](/reference/android/bluetooth/BluetoothAdapter) and\n[determine if Bluetooth is available on the device](/develop/connectivity/bluetooth/setup).\nIf Bluetooth is available, there are three steps to make a connection:\n\n- [Find nearby Bluetooth\n devices](/develop/connectivity/bluetooth/find-bluetooth-devices), either devices that are already paired or new ones.\n- [Connect to a Bluetooth\n device](/develop/connectivity/bluetooth/connect-bluetooth-devices).\n- [Transfer data with the connected\n device](/develop/connectivity/bluetooth/transfer-data).\n\nCertain devices use a specific [Bluetooth\nprofile](/develop/connectivity/bluetooth/profiles) that declares the data\nit provides.\n\nKey classes and interfaces\n--------------------------\n\nAll of the Bluetooth APIs are available in the\n[`android.bluetooth`](/reference/android/bluetooth/package-summary) package.\nThe following are the classes and interfaces you need in order to create\nBluetooth connections:\n\n[`BluetoothAdapter`](/reference/android/bluetooth/BluetoothAdapter)\n: Represents the local Bluetooth adapter (Bluetooth radio). The\n `BluetoothAdapter` is the entry-point for all Bluetooth interaction. Using\n this, you can discover other Bluetooth devices, query a list of bonded\n (paired) devices, instantiate a\n `BluetoothDevice` using a known MAC address, and create a\n `BluetoothServerSocket` to listen for communications from other devices.\n\n[`BluetoothDevice`](/reference/android/bluetooth/BluetoothDevice)\n: Represents a remote Bluetooth device. Use this to request a connection with a\n remote device through a `BluetoothSocket` or query information about the\n device such as its name, address, class, and bonding state.\n\n[`BluetoothSocket`](/reference/android/bluetooth/BluetoothSocket)\n: Represents the interface for a Bluetooth socket (similar to a TCP\n [`Socket`](/reference/java/net/Socket)). This is the connection point that\n allows an app to exchange data with another Bluetooth device using\n [`InputStream`](/reference/java/io/InputStream) and\n [`OutputStream`](/reference/java/io/OutputStream).\n\n[`BluetoothServerSocket`](/reference/android/bluetooth/BluetoothServerSocket)\n: Represents an open server socket that listens for incoming requests (similar\n to a TCP [`ServerSocket`](/reference/java/net/ServerSocket)). In order to\n connect two devices, one device must open a server socket with this\n class. When a remote Bluetooth device makes a connection request to this\n device, the device accepts the connection and then returns a connected\n `BluetoothSocket`.\n\n[`BluetoothClass`](/reference/android/bluetooth/BluetoothClass)\n: Describes the general characteristics and capabilities of a Bluetooth device.\n This is a read-only set of properties that defines the device's classes and\n services. Although this information provides a useful hint regarding a\n device's type, the attributes of this class don't necessarily describe all\n Bluetooth profiles and services that the device supports.\n\n[`BluetoothProfile`](/reference/android/bluetooth/BluetoothProfile)\n: An interface that represents a Bluetooth profile. A Bluetooth profile is a\n wireless interface specification for Bluetooth-based communication between\n devices. An example is the Hands-Free profile. For more discussion of\n profiles, see [Bluetooth profiles](/develop/connectivity/bluetooth/profiles).\n\n[`BluetoothHeadset`](/reference/android/bluetooth/BluetoothHeadset)\n: Provides support for Bluetooth headsets to be used with mobile phones. This\n includes both the Bluetooth Headset profile and the Hands-Free (v1.5) profile.\n\n[`BluetoothA2dp`](/reference/android/bluetooth/BluetoothA2dp)\n: Defines how high-quality audio can be streamed from one device to another over\n a Bluetooth connection using the Advanced Audio Distribution Profile (A2DP).\n\n[`BluetoothHealth`](/reference/android/bluetooth/BluetoothHealth)\n: Represents a Health Device Profile proxy that controls the Bluetooth service.\n\n[`BluetoothHealthCallback`](/reference/android/bluetooth/BluetoothHealthCallback)\n: An abstract class that you use to implement `BluetoothHealth` callbacks. You\n must extend this class and implement the callback methods to receive updates\n about changes in the app's registration state and Bluetooth channel\n state.\n\n[`BluetoothHealthAppConfiguration`](/reference/android/bluetooth/BluetoothHealthAppConfiguration)\n: Represents an app configuration that the Bluetooth Health third-party\n app registers to communicate with a remote Bluetooth health device.\n\n[`BluetoothProfile.ServiceListener`](/reference/android/bluetooth/BluetoothProfile.ServiceListener)\n: An interface that notifies `BluetoothProfile` interprocess communication (IPC)\n clients when they have been connected to or disconnected from the internal\n service that runs a particular profile."]]