Skip to content

Example to get gear state information from CarPropertyManager

Notifications You must be signed in to change notification settings

mkett/android-car-gear-example

Repository files navigation

Example to fetch gear informations from Car API

This example will show how to use Car Api, get an CarPropertyManager and register it to a listener.

Create an automotive android project

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
        )
    }

Automotive Emulator

On emulator, you can find and change the gear at Car data -> Car sensor data.

Android Automotive Emulator Gear Example

About

Example to get gear state information from CarPropertyManager

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages