This example will show how to use Car Api, get an CarPropertyManager and register it to a listener.
First of all you need to add car library to the gradle
android {
...
useLibrary ("android.car")
}
Add automotive feature and add gear permission in manifest.xml
<uses-feature android:name=„android.hardware.type.automotive" android:required="true" />
<uses-permission android:name="android.car.permission.CAR_POWERTRAIN" />
To use car services you need to create a car connention. Through the car object you can get a special manager that provides different car informations. You can use CarServiceLifecycleListener to verify, if the connection was seccessful. The connection has to be disconnected at the end.
car = Car.createCar(
this, null, Car.CAR_WAIT_TIMEOUT_WAIT_FOREVER
) { car: Car, ready: Boolean ->
if (ready) {
carPropertyManager = car.getCarManager(Car.PROPERTY_SERVICE) as CarPropertyManager
}
}
Finally you define a CarPropertyEventCallback to get change events for gear and register our callback.
carPropertyManager?.let {
it.registerCallback(carPropertyCallback, VehiclePropertyIds.GEAR_SELECTION,
CarPropertyManager.SENSOR_RATE_NORMAL
)
}
On emulator, you can find and change the gear at Car data -> Car sensor data.