蓝牙 API 支持使用蓝牙配置文件。蓝牙配置文件是基于蓝牙的设备间通信的无线接口规范,例如免提配置文件。要使移动设备连接到无线耳机,这两个设备都必须支持免提配置文件。
蓝牙 API 为以下蓝牙配置文件提供了实现
- 耳机配置文件。耳机配置文件提供了用于搭配移动电话使用蓝牙耳机的支持。Android 提供了
BluetoothHeadset
类,它是用于控制蓝牙耳机服务的代理。这包括蓝牙耳机和免提 (v1.5) 配置文件。BluetoothHeadset
类支持 AT 命令。有关此主题的更多信息,请参阅特定于供应商的 AT 命令。 - A2DP。高级音频分发配置文件 (A2DP) 定义了如何通过蓝牙连接将高质量音频从一个设备流式传输到另一个设备。Android 提供了
BluetoothA2dp
类,它是用于控制蓝牙 A2DP 服务的代理。 - 健康设备。Android 支持蓝牙健康设备配置文件 (HDP)。通过该配置文件,您可以创建使用蓝牙与支持蓝牙的健康设备进行通信的应用,例如心率监测器、血糖仪、体温计、体重秤等。如需了解支持的设备及其对应的设备数据专用代码列表,请参阅 Bluetooth 的 HDP 设备数据专用信息。这些值在 ISO/IEEE 11073-20601 [7] 规范中的《命名代码附录》中也以
MDC_DEV_SPEC_PROFILE_*
形式引用。如需了解有关 HDP 的更多信息,请参阅健康设备配置文件。
使用配置文件时,基本步骤如下
- 获取默认适配器,如蓝牙设置中所述。
- 设置
BluetoothProfile.ServiceListener
。此监听器会在BluetoothProfile
客户端连接或断开连接到服务时通知它们。 - 使用
getProfileProxy()
与配置文件关联的配置文件代理对象建立连接。在以下示例中,配置文件代理对象是BluetoothHeadset
的实例。 - 在
onServiceConnected()
中,获取对配置文件代理对象的句柄。 - 获得配置文件代理对象后,使用它监控连接状态并执行与该配置文件相关的其他操作。
以下代码段展示了如何连接到 BluetoothHeadset
代理对象,以便您可以控制耳机配置文件
Kotlin
var bluetoothHeadset: BluetoothHeadset? = null // Get the default adapter val bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter() private val profileListener = object : BluetoothProfile.ServiceListener { override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) { if (profile == BluetoothProfile.HEADSET) { bluetoothHeadset = proxy as BluetoothHeadset } } override fun onServiceDisconnected(profile: Int) { if (profile == BluetoothProfile.HEADSET) { bluetoothHeadset = null } } } // Establish connection to the proxy. bluetoothAdapter?.getProfileProxy(context, profileListener, BluetoothProfile.HEADSET) // ... call functions on bluetoothHeadset // Close proxy connection after use. bluetoothAdapter?.closeProfileProxy(BluetoothProfile.HEADSET, bluetoothHeadset)
Java
BluetoothHeadset bluetoothHeadset; // Get the default adapter BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); private BluetoothProfile.ServiceListener profileListener = new BluetoothProfile.ServiceListener() { public void onServiceConnected(int profile, BluetoothProfile proxy) { if (profile == BluetoothProfile.HEADSET) { bluetoothHeadset = (BluetoothHeadset) proxy; } } public void onServiceDisconnected(int profile) { if (profile == BluetoothProfile.HEADSET) { bluetoothHeadset = null; } } }; // Establish connection to the proxy. bluetoothAdapter.getProfileProxy(context, profileListener, BluetoothProfile.HEADSET); // ... call functions on bluetoothHeadset // Close proxy connection after use. bluetoothAdapter.closeProfileProxy(bluetoothHeadset);
特定于供应商的 AT 命令
应用可以注册接收耳机发送的预定义特定于供应商的 AT 命令的系统广播(例如 Plantronics +XEVENT 命令)。例如,应用可以接收指示连接设备电池电量的广播,并根据需要通知用户或执行其他操作。为 ACTION_VENDOR_SPECIFIC_HEADSET_EVENT
intent 创建广播接收器,以处理耳机的特定于供应商的 AT 命令。
健康设备配置文件
Android 支持蓝牙健康设备配置文件 (HDP)。蓝牙健康 API 包含 BluetoothHealth
、BluetoothHealthCallback
和 BluetoothHealthAppConfiguration
类,这些类在关键类和接口中有所介绍。
使用蓝牙健康 API 时,了解这些关键 HDP 概念会很有帮助
- 源
- 一种健康设备,例如体重秤、血糖仪或体温计,将医疗数据传输到智能设备(例如 Android 手机或平板电脑)。
- 接收器
- 接收医疗数据的智能设备。在 HDP 应用中,接收器由
BluetoothHealthAppConfiguration
对象表示。 - 注册
- 用于注册接收器以便与特定健康设备通信的过程。
- 连接
- 用于在健康设备(源)和智能设备(接收器)之间打开通道的过程。
创建 HDP 应用
创建 HDP 应用的基本步骤如下
获取对
BluetoothHealth
代理对象的引用。与常规耳机和 A2DP 配置文件设备一样,您必须调用getProfileProxy()
,传入BluetoothProfile.ServiceListener
和HEALTH
配置文件类型,以与配置文件代理对象建立连接。创建
BluetoothHealthCallback
并注册充当健康接收器的应用配置 (BluetoothHealthAppConfiguration
)。建立与健康设备的连接。
成功连接到健康设备后,使用文件描述符对健康设备进行读写。接收到的数据需要使用实现 IEEE 11073 规范的健康管理器进行解释。
完成后,关闭健康通道并注销应用。长时间不活动时,通道也会关闭。