藍牙總覽
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Android 平台支援藍牙網路堆疊,讓裝置能夠以無線方式與其他藍牙裝置交換資料。應用程式架構可讓您透過藍牙 API 存取藍牙功能。這些 API 可讓應用程式連線至其他藍牙裝置,提供點對點和多點無線功能。
透過 Bluetooth API,應用程式可以執行以下操作:
- 掃描其他藍牙裝置。
- 查詢配對藍牙裝置的本機藍牙轉接器。
- 建立 RFCOMM 管道。
- 透過服務探索功能連結其他裝置。
- 將資料轉移至其他裝置,或從其他裝置轉移資料。
- 管理多個連結。
本主題著重於傳統藍牙。傳統藍牙是最適合執行耗用大量電池的作業的選擇,包括在裝置之間串流及通訊。對於低功耗藍牙裝置,請考慮使用藍牙低功耗連線。
本文件說明不同的藍牙設定檔,並說明如何使用藍牙 API 完成透過藍牙進行通訊的四大工作:
- 正在設定藍牙。
- 尋找所在區域的配對或可用裝置。
- 正在連接裝置。
- 在裝置間轉移資料。
如需藍牙 API 的使用示範,請參閱藍牙 Chat 範例應用程式。
基本資訊
如要讓支援藍牙的裝置互相傳輸資料,必須先使用配對程序建立通訊管道。一部裝置是可偵測的裝置,可用來處理傳入的連線要求。其他裝置會使用服務探索程序尋找可供探索的裝置。可供搜尋的裝置接受配對要求後,兩部裝置就會完成綁定程序,並在過程中交換安全金鑰。裝置會快取這些金鑰供日後使用。配對和綁定程序完成後,兩部裝置就會交換資訊。工作階段結束後,啟動配對要求的裝置會釋出已連結至可搜尋裝置的頻道。但是,這兩部裝置仍會保持綁定狀態,因此只要兩者相鄰,且兩者均未移除繫結,就能在日後的工作階段期間自動重新連線。
如要使用 Bluetooth API,您必須在資訊清單檔案中宣告多項權限。應用程式具備使用藍牙的權限後,應用程式就需要存取 BluetoothAdapter
,並判斷裝置上是否可使用藍牙。如果有可用藍牙,可以透過三個步驟建立連線:
某些裝置會使用特定藍牙設定檔來宣告其提供的資料。
主要類別和介面
android.bluetooth
套件中提供了所有藍牙 API。以下是建立藍牙連線所需的類別和介面:
BluetoothAdapter
- 代表本機藍牙轉接器 (藍牙無線電)。
BluetoothAdapter
是所有藍牙互動的進入點。您可利用這個 API 探索其他藍牙裝置、查詢綁定 (配對) 的裝置清單、使用已知 MAC 位址將 BluetoothDevice
執行個體化,以及建立 BluetoothServerSocket
以監聽其他裝置的通訊。
BluetoothDevice
- 代表遠端藍牙裝置。使用此功能透過
BluetoothSocket
要求與遠端裝置建立連線,或查詢裝置的名稱、位址、類別和連接狀態等。 BluetoothSocket
- 代表藍牙通訊端的介面 (類似 TCP
Socket
)。這個連接點可讓應用程式使用 InputStream
和 OutputStream
與其他藍牙裝置交換資料。
BluetoothServerSocket
- 代表可監聽傳入要求的開放伺服器通訊端 (類似於 TCP
ServerSocket
)。為連接兩部裝置,一部裝置必須透過此類別開啟伺服器通訊端。當遠端藍牙裝置向這部裝置發出連線要求時,裝置會接受該連線,然後傳回已連線的 BluetoothSocket
。
BluetoothClass
- 說明藍牙裝置的一般特性和功能。這是一組定義裝置類別和服務的唯讀屬性。雖然這項資訊可以針對裝置類型提供實用的提示,但該類別的屬性並不一定能說明裝置支援的所有藍牙設定檔和服務。
BluetoothProfile
- 代表藍牙設定檔的介面。藍牙設定檔是用於裝置間的藍牙通訊的無線介面規格。例如「手持式」設定檔。如要進一步瞭解設定檔,請參閱藍牙設定檔。
BluetoothHeadset
- 支援搭配手機使用的藍牙耳機。這包括藍牙耳機設定檔和免持裝置 (v1.5) 設定檔。
BluetoothA2dp
- 定義如何使用進階音訊發布設定檔 (A2DP),透過藍牙連線從某裝置串流高品質的音訊。
BluetoothHealth
- 代表控制藍牙服務的健康裝置設定檔 Proxy。
BluetoothHealthCallback
- 用於實作
BluetoothHealth
回呼的抽象類別。您必須擴充這個類別並實作回呼方法,才能接收應用程式註冊狀態和藍牙頻道狀態變更的最新消息。 BluetoothHealthAppConfiguration
- 代表 Bluetooth Health 第三方應用程式註冊的應用程式設定,以便與遠端藍牙健康裝置通訊。
BluetoothProfile.ServiceListener
- 一個介面會在與執行特定設定檔的內部服務連線或中斷連線時,
BluetoothProfile
通知處理序間通訊 (IPC) 用戶端。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[[["容易理解","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-26 (世界標準時間)。"],[],[],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."]]