Bluetooth API 支持使用蓝牙配置文件。蓝牙配置文件是适用于设备间蓝牙通信的无线接口规范,例如免触摸配置文件。如需让移动设备连接到无线耳机,两台设备都必须支持免触摸配置文件。
Bluetooth API 提供了以下蓝牙配置文件的实现:
- 耳机。耳机配置文件支持蓝牙耳机,以便与手机配合使用。Android 提供了
BluetoothHeadset
类,它是用于控制蓝牙耳机服务的代理。这包括蓝牙耳机和免提 (v1.5) 配置文件。BluetoothHeadset
类包含对 AT 命令的支持。如需详细了解此主题,请参阅特定于供应商的 AT 命令。 - A2DP。高级音频分发配置文件 (A2DP) 配置文件定义了如何通过蓝牙连接将高质量音频从一个设备流式传输到另一个设备。Android 提供了
BluetoothA2dp
类,它是用于控制蓝牙 A2DP 服务的代理。 - 健康设备。Android 支持蓝牙健康设备配置文件 (HDP)。这样,您就可以创建使用蓝牙与支持蓝牙功能的健康设备(例如心率监测仪、血糖仪、温度计、体重秤等)进行通信的应用。如需查看受支持设备及其对应的设备数据专业化代码的列表,请参阅蓝牙的 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)。Bluetooth Health API 包含 BluetoothHealth
、BluetoothHealthCallback
和 BluetoothHealthAppConfiguration
类,关键类和接口中对这些类进行了介绍。
使用 Bluetooth Health API 时,了解以下关键 HDP 概念会很有帮助:
- 信息来源
- 用于将医疗数据传输到 Android 手机或平板电脑等智能设备的健康设备,如体重秤、血糖仪或温度计。
- 接收器
- 接收医疗数据的智能设备。在 HDP 应用中,接收器由
BluetoothHealthAppConfiguration
对象表示。 - 注册
- 用于注册接收器以与特定健康设备进行通信的过程。
- 情感联系
- 用于打开健康设备(源)和智能设备(接收器)之间的通道的过程。
创建 HDP 应用
以下是创建 HDP 应用所涉及的基本步骤:
获取对
BluetoothHealth
代理对象的引用。与常规耳机和采用 A2DP 配置文件的设备一样,您必须使用BluetoothProfile.ServiceListener
和HEALTH
配置文件类型调用getProfileProxy()
,以便与配置文件代理对象建立连接。创建
BluetoothHealthCallback
并注册充当健康接收器的应用配置 (BluetoothHealthAppConfiguration
)。建立与健康设备的连接。
成功连接到健康设备后,使用文件描述符对健康设备执行读写操作。接收的数据需要使用实现 IEEE 11073 规范的运行状况管理器进行解读。
完成后,关闭运行状况通道并取消注册应用。当长期闲置时,该通道也会关闭。