蓝牙概览
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Android 平台支持蓝牙网络堆栈,可让设备通过无线方式与其他蓝牙设备交换数据。应用框架提供通过蓝牙 API 访问蓝牙功能的权限。这些 API 允许应用连接到其他蓝牙设备,从而实现点对点和多点无线功能。
使用 Bluetooth API,应用可以执行以下操作:
- 扫描其他蓝牙设备。
- 查询本地蓝牙适配器,获取已配对的蓝牙设备。
- 建立 RFCOMM 通道。
- 通过服务发现连接到其他设备。
- 与其他设备相互传输数据。
- 管理多个连接。
本主题重点介绍传统蓝牙。传统蓝牙非常适合电池消耗较大的操作,包括设备间流式传输和通信。对于具有低功耗要求的蓝牙设备,请考虑使用低功耗蓝牙连接。
本文档介绍了不同的蓝牙配置文件,并说明了如何使用 Bluetooth API 完成使用蓝牙进行通信的四项主要任务:
- 正在设置蓝牙。
- 正在查找已配对或在本地区域可用的设备。
- 正在连接设备。
- 在设备之间传输数据。
如需查看使用蓝牙 API 的演示,请参阅蓝牙聊天示例应用。
基础知识
为了让支持蓝牙的设备在彼此之间传输数据,它们必须先通过配对过程形成通信通道。其中一台设备(可检测到的设备)使其自身可用于传入的连接请求。其他设备会使用服务发现流程找到可检测到的设备。
在可检测到的设备接受配对请求后,两台设备会完成绑定过程,在该过程中它们会交换安全密钥。设备会缓存这些键以供日后使用。配对和绑定过程完成后,两台设备会交换信息。会话完成后,发起配对请求的设备会释放已将其关联到可检测到的设备的渠道。不过,这两台设备仍保持绑定状态,因此,只要它们在彼此的范围内且两个设备均未解除绑定,它们就可以在未来的会话期间自动重新连接。
若要使用 Bluetooth API,您需要在清单文件中声明多项权限。应用获得使用蓝牙的权限后,需要访问 BluetoothAdapter
并确定设备是否可使用蓝牙。如果蓝牙可用,则可以通过以下三个步骤建立连接:
某些设备会使用特定的蓝牙配置文件来声明设备提供的数据。
关键类和接口
android.bluetooth
软件包中提供了所有蓝牙 API。以下是创建蓝牙连接所需的类和接口:
BluetoothAdapter
- 表示本地蓝牙适配器(蓝牙无线装置)。
BluetoothAdapter
是所有蓝牙交互的入口点。借助此功能,您可以发现其他蓝牙设备、查询已绑定(配对)设备的列表、使用已知的 MAC 地址实例化 BluetoothDevice
,以及创建 BluetoothServerSocket
来监听其他设备的通信。
BluetoothDevice
- 表示远程蓝牙设备。使用此方法可以通过
BluetoothSocket
请求与远程设备建立连接,或查询有关设备的信息,例如设备的名称、地址、类和绑定状态。
BluetoothSocket
- 表示蓝牙套接字的接口(类似于 TCP
Socket
)。这是允许应用使用 InputStream
和 OutputStream
与其他蓝牙设备交换数据的连接点。
BluetoothServerSocket
- 表示用于监听传入请求的开放服务器套接字(类似于 TCP
ServerSocket
)。为了连接两台设备,一台设备必须使用此类打开服务器套接字。当远程蓝牙设备向此设备发出连接请求时,该设备会接受连接,然后返回已连接的 BluetoothSocket
。
BluetoothClass
- 描述蓝牙设备的一般特征和功能。这是一组只读属性,用于定义设备的类和服务。虽然这些信息可以提供有关设备类型的有用提示,但该类的属性不一定要描述设备支持的所有蓝牙配置文件和服务。
BluetoothProfile
- 表示蓝牙配置文件的接口。蓝牙配置文件是适用于设备间蓝牙通信的无线接口规范。例如,免触摸配置文件。如需详细了解配置文件,请参阅蓝牙配置文件。
BluetoothHeadset
- 支持蓝牙耳机,以便与手机配合使用。这包括蓝牙耳机配置文件和免提 (v1.5) 配置文件。
BluetoothA2dp
- 定义如何使用高级音频分发配置文件 (A2DP) 通过蓝牙连接将高品质音频从一台设备流式传输到另一台设备。
BluetoothHealth
- 表示用于控制蓝牙服务的健康设备配置文件代理。
BluetoothHealthCallback
- 用于实现
BluetoothHealth
回调的抽象类。您必须扩展此类并实现回调方法,以接收关于应用注册状态和蓝牙通道状态变化的更新。
BluetoothHealthAppConfiguration
- 表示蓝牙运行状况第三方应用注册的应用配置,该配置用于与远程蓝牙健康设备通信。
BluetoothProfile.ServiceListener
- 一个接口,用于在
BluetoothProfile
进程间通信 (IPC) 客户端连接到运行特定配置文件的内部服务或断开与这类服务的连接时通知这些客户端。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):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."]]