Kể từ ngày 27 tháng 3 năm 2025, bạn nên sử dụng android-latest-release
thay vì aosp-main
để xây dựng và đóng góp cho AOSP. Để biết thêm thông tin, hãy xem phần Thay đổi đối với AOSP.
Sử dụng VHAL với ứng dụng gốc
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
VHAL hỗ trợ Java và ứng dụng gốc. Dịch vụ ô tô là ứng dụng Java duy nhất cho VHAL. Đối với ứng dụng ô tô, hãy sử dụng API ô tô (ví dụ: CarPropertyManager
) để truy cập vào các thuộc tính VHAL thay vì giao tiếp trực tiếp với VHAL. Trên thực tế, SELinux chặn quyền truy cập trực tiếp. Để biết thông tin chi tiết, hãy xem tài liệu về API ô tô tại Chỉ mục gói.
Đối với ứng dụng gốc, bắt đầu từ Android 13, hãy sử dụng libvhalclient
thay vì kết nối trực tiếp với VHAL. Đây là một thư viện ứng dụng hiển thị một giao diện chung, IVhalClient.h
để triển khai AIDL và HIDL VHAL. Ví dụ sau đây cho thấy cách tạo ứng dụng gốc VHAL và sử dụng ứng dụng đó để lấy Mã nhận dạng xe (VIN):
#include <IVhalClient.h>
#include <VehicleHalTypes.h>
#include <VehicleUtils.h>
using ::aidl::android::hardware::automotive::vehicle::VehicleProperty;
using ::android::frameworks::automotive::vhal::IVhalClient;
using ::android::hardware::automotive::vehicle::toInt;
int main(int argc, char** argv) {
auto vhalClient = IVhalClient::tryCreate();
if (vhalClient == nullptr) {
// handle error.
return -1;
}
auto result = vhalClient->getValueSync(
*vhalClient->createHalPropValue(toInt(VehicleProperty::INFO_VIN)));
// Use result
return 0;
}
Bạn phải định cấu hình chính sách SELinux để cho phép ứng dụng gốc truy cập vào VHAL. Ví dụ:
# Define my domain
type my_native_daemon, domain;
# Define the exec file type.
type my_native_daemon_exec, exec_type, file_type, system_file_type;
# Initialize domain.
init_daemon_domain(my_native_daemon)
# Allow using hwbinder for HIDL VHAL, not required if AIDL is used.
hwbinder_use(my_native_daemon)
# Allow using binder for AIDL VHAL
binder_use(my_native_daemon)
# Allow my_native_daemon to be a VHAL client.
hal_client_domain(my_native_daemon, hal_vehicle)
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-26 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-26 UTC."],[],[],null,["# Use VHAL with the native client\n\nVHAL supports Java and native clients. Car Service is the only\nJava client for VHAL. For car apps, use the Car APIs (for example,\n[`CarPropertyManager`](https://developer.android.com/reference/android/car/hardware/property/CarPropertyManager))\nto access VHAL properties instead of directly communicating with\nthe VHAL. In fact, SELinux blocks direct access. For details, see the Car API documentation at\n[Package Index](https://developer.android.com/reference/android/car/packages).\n\nFor native clients, starting with Android 13, use\n[`libvhalclient`](https://android.googlesource.com/platform/packages/services/Car/+/refs/heads/android16-release/cpp/vhal/client/) instead of directly\nconnecting with VHAL. This is a client library that exposes one common interface,\n`IVhalClient.h` for AIDL and HIDL VHAL implementations. The following example shows\nhow to create a VHAL native client and use it to get a Vehicle Identification Number (VIN) number: \n\n```c++\n#include \u003cIVhalClient.h\u003e\n#include \u003cVehicleHalTypes.h\u003e\n#include \u003cVehicleUtils.h\u003e\n\nusing ::aidl::android::hardware::automotive::vehicle::VehicleProperty;\nusing ::android::frameworks::automotive::vhal::IVhalClient;\nusing ::android::hardware::automotive::vehicle::toInt;\n\nint main(int argc, char** argv) {\n auto vhalClient = IVhalClient::tryCreate();\n if (vhalClient == nullptr) {\n // handle error.\n return -1;\n }\n auto result = vhalClient-\u003egetValueSync(\n *vhalClient-\u003ecreateHalPropValue(toInt(VehicleProperty::INFO_VIN)));\n // Use result\n\n return 0;\n}\n```\n\nYou must configure SELinux policy to allow your native client to access VHAL. For example: \n\n```text\n# Define my domain\ntype my_native_daemon, domain;\n\n# Define the exec file type.\ntype my_native_daemon_exec, exec_type, file_type, system_file_type;\n\n# Initialize domain.\ninit_daemon_domain(my_native_daemon)\n\n# Allow using hwbinder for HIDL VHAL, not required if AIDL is used.\nhwbinder_use(my_native_daemon)\n# Allow using binder for AIDL VHAL\nbinder_use(my_native_daemon)\n# Allow my_native_daemon to be a VHAL client.\nhal_client_domain(my_native_daemon, hal_vehicle)\n```\n\n\u003cbr /\u003e"]]