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(BluetoothProfile.HEADSET, 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 规范。
完成后,关闭健康通道并取消注册应用。如果长时间处于不活动状态,通道也会关闭。