Android automotive CAR API usage example.
The example is based on the use of android.car.hardware.CarPropertyManager
and shows how to work with SYSTEM
(speed, transfer, etc.) and VENDOR
custom properties that belong to all possible permission protection levels:
- normal
- dangerous
- signature|privileged
Project support both gradle and AOSP (Android.bp) builds.
To use and test the CAR API, the following is done in the project:
-
Add
android.car
library (to gradle, to Android.bp). -
Add permissions to AndroidManifest.xml.
-
Request
dangerous
permissions in runtime inonCreate()
in MainActivity.java. -
Use
CarPropertyManager
API in MainActivity.java:- listeners:
registerCallback()
- getters:
getIntProperty()
- setters:
setBooleanProperty
- listeners:
- AOSP Android Automotive Emulator (e.g
aosp_car_x86-userdebug
product) - NCAR - Android Automotive Project (is used for testing
VENDOR
properties supported by custom VHAL) - Polestar2 Emulator
To change car speed, gear, location, etc. use AVD Extended Controls
:
Start CarApiHelloWorldApp
app via ADB ActivityManager:
$ am start -n com.example.carapihelloworld/.MainActivity
Or manually through the app list if it is part of the entire AOSP build (e.g. NCAR - Android Automotive Project)
After compiling and running this project, the following logcat
messages are expected:
2022-02-14 17:29:18.154 4979-4979/com.example.carapihelloworld D/CarApiHelloWorld: Test CarPropertyManager getters:
2022-02-14 17:29:18.155 4979-4979/com.example.carapihelloworld D/CarApiHelloWorld: GEAR_SELECTION: getIntProperty(289408000, 0)=4
2022-02-14 17:29:18.156 4979-4979/com.example.carapihelloworld D/CarApiHelloWorld: VENDOR_TEST_COUNTER: getIntProperty(557842433, 0)=4
2022-02-14 17:29:18.156 4979-4979/com.example.carapihelloworld D/CarApiHelloWorld: Test CarPropertyManager callbacks:
2022-02-14 17:29:18.282 4979-4979/com.example.carapihelloworld D/CarApiHelloWorld: GEAR_SELECTION: onChangeEvent(4)
2022-02-14 17:29:18.282 4979-4979/com.example.carapihelloworld D/CarApiHelloWorld: PERF_VEHICLE_SPEED: onChangeEvent(0.0)
2022-02-14 17:29:18.282 4979-4979/com.example.carapihelloworld D/CarApiHelloWorld: EV_BATTERY_LEVEL: onChangeEvent(150000.0)
2022-02-14 17:29:18.283 4979-4979/com.example.carapihelloworld D/CarApiHelloWorld: FUEL_DOOR_OPEN: onChangeEvent(false)
2022-02-14 17:29:18.283 4979-4979/com.example.carapihelloworld D/CarApiHelloWorld: VENDOR_TEST_COUNTER: onChangeEvent(4)
Examples of permissions error messages:
2021-11-01 03:16:36.840 26840-26840/com.example.carapihelloworld E/CarApiHelloWorld: FUEL_DOOR_OPEN: setBooleanProperty(), Exception: permission is null
2021-11-02 13:13:55.293 2642-2642/com.example.carapihelloworld E/CarApiHelloWorld: FUEL_DOOR_OPEN: setBooleanProperty(), Exception: requires android.car.permission.CONTROL_CAR_ENERGY_PORTS