如何使用蓝牙 HIDL?

How to use Bluetooth HIDL?

我正在开发一个必须向蓝牙芯片组发送 HCI 命令的 AOSP 应用程序。

我发现我可以使用这个接口:https://source.android.com/reference/hidl/android/hardware/bluetooth/1.0/IBluetoothHci

为了使用它,我试着关注这个页面:https://source.android.com/devices/architecture/hidl-java/index.html

如果我理解得很好,我必须创建一个 Android.mk 文件并把

LOCAL_STATIC_JAVA_LIBRARIES +=  android.hardware.bluetooth@1.0

但我不明白怎么办?我还是 AOSP 开发的新手,我该如何使用这个库?

如果您是应用程序开发人员

您找到的 IBluetoothHci 定义了蓝牙硬件抽象层 (HAL) 的接口。 HAL 接口不能直接从应用程序访问,但由框架服务使用,框架服务提供可供应用程序使用的接口。 我建议查看 Android SDK:https://developer.android.com/guide/topics/connectivity/bluetooth

如果您是平台开发人员

如果您打算编写具有更多权限的服务(您自己构建 AOSP 并刷新整个设备),那么您是对的。IBluetoothHci 是要使用的接口。您可能希望从使用 Android.mk 切换到 Android.bp,因为 Android.mk 文件已弃用。 在您的代码中,我希望看到这样的内容:

import android.hardware.bluetooth.V1_0.IBluetoothHci;
...
// retry to wait until the service starts up if it is in the manifest
IBluetoothHci bluetooth = IBluetoothHci.getService(true /* retry */); // throws NoSuchElementException if not available
bluetooth.initialize();

关于如何使用 HAL 接口的提示也可以在相应的 VTS 测试中找到(尽管它们是用 C++ 编写的):https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/master/bluetooth/1.0/vts/functional/VtsHalBluetoothV1_0TargetTest.cpp