How to create object of type CarPropertyManager

769 views
Skip to first unread message

Rohit Iti

unread,
Nov 8, 2020, 5:44:29 AM11/8/20
to Android Automotive OS Discussion Group
public CarPropertyManager mCarPropertytManager;

And trying this in Service connection. 

ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "onServiceConnected: ");
mCarPropertytManager = new CarPropertyManager(mCar, service);
.....
}

I think i need to use some ICarInterface stub but not getting how exactly to get this. 

Frank Bouwens

unread,
Nov 8, 2020, 12:06:29 PM11/8/20
to Android Automotive OS Discussion Group, rohit...@gmail.com
I've seen something like that in the sample app

https://github.com/android/car-samples/blob/main/AutomotiveOS/CarGearViewerKotlin/automotive/src/main/java/com/example/cargearviewer/MainActivity.kt#L88

Please try the following:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //setContentView(R.layout.activity_main)

        //currentGearTextView = findViewById(R.id.currentGearTextView)

        // createCar() returns a "Car" object to access car service APIs. It can return null if
        // car service is not yet ready but that is not a common case and can happen on rare cases
        // (for example car service crashes) so the receiver should be ready for a null car object.
        //
        // Other variants of this API allows more control over car service functionality (such as
        // handling car service crashes graciously). Please see the SDK documentation for this.
        car = Car.createCar(this)

        carPropertyManager = car.getCarManager(Car.PROPERTY_SERVICE) as CarPropertyManager; // <--- THIS !!!

        // Subscribes to the gear change events.
        carPropertyManager.registerCallback(
            carPropertyListener,
            VehiclePropertyIds.CURRENT_GEAR,
            CarPropertyManager.SENSOR_RATE_ONCHANGE
        )
    }

Op zondag 8 november 2020 om 11:44:29 UTC+1 schreef rohit...@gmail.com:

Rohit Iti

unread,
Nov 8, 2020, 12:27:05 PM11/8/20
to Frank Bouwens, Android Automotive OS Discussion Group
Hello Frank, 
Thank you for replying. 
The one which you mentioned that example I have in kotlin bot not able to get it in java . 

Because of that am currently stuck. 

Abhilash Mb

unread,
Nov 9, 2020, 6:17:01 AM11/9/20
to Android Automotive OS Discussion Group, rohit...@gmail.com, Android Automotive OS Discussion Group, frankk...@gmail.com
HI All,

I am using the below code to get car properties such as speed and gear changed events, So far working properly. Hops this helps.

private Car myCar;

private void initCar() {
     myCar = Car.createCar(this);
     CarPropertyManager carPropertyManager = (CarPropertyManager) myCar.getCarManager(Car.PROPERTY_SERVICE);
     carPropertyManager.registerCallback(this.carPropertyListener, VehiclePropertyIds.PERF_VEHICLE_SPEED,
     CarPropertyManager.SENSOR_RATE_ONCHANGE);
}

private CarPropertyManager.CarPropertyEventCallback carPropertyListener = new CarPropertyManager.CarPropertyEventCallback() {
@Override
public void onChangeEvent(CarPropertyValue carPropertyValue) {
Log.d(TAG, "Received car property changed event" + carPropertyValue.getValue());

Rohit Iti

unread,
Nov 10, 2020, 5:51:44 AM11/10/20
to Android Automotive OS Discussion Group, ambina...@gmail.com, Rohit Iti, Android Automotive OS Discussion Group, frankk...@gmail.com
        private void initcar() {

            if(!getBaseContext().getPackageManager().hasSystemFeature(getBaseContext().getPackageManager().FEATURE_AUTOMOTIVE)){
                Log.d(TAG, "initcar: hasSystemFeature failed");
                return;
            }
            mCar = Car.createCar(this);
            carPropertyManager = (CarPropertyManager) mCar.getCarManager(Car.PROPERTY_SERVICE);

            mpropertyids = carPropertyManager.getPropertyList();
            Log.d(TAG, "initcar: List = " + mpropertyids.size());   // Always returns 0
            carPropertyManager.registerCallback(this.carPropertyListener, VehiclePropertyIds.PERF_VEHICLE_SPEED,
                    CarPropertyManager.SENSOR_RATE_ONCHANGE);
        }

        public CarPropertyManager.CarPropertyEventCallback carPropertyListener = new CarPropertyManager.CarPropertyEventCallback() {

            @Override
            public void onChangeEvent(CarPropertyValue carPropertyValue) {
                Log.d(TAG, "onChangeEvent: " + carPropertyValue.getValue());
            }

            @Override
            public void onErrorEvent(int i, int i1) {
                Log.d(TAG, "onErrorEvent: ");
            }

        };

When i run this code  am getting "CarPropertyManager: registerListener:  propId is not in config list:  291504647"

PERF_VEHICLE_SPEED = 291504647
I am assuming we need to add List<CarPropertyConfig> mpropertyids or register it somewhere . 
Am not aware how to do this .

Can anyone help me get through this ?

Abhilash Mb

unread,
Nov 10, 2020, 6:03:46 AM11/10/20
to Android Automotive OS Discussion Group, rohit...@gmail.com, Abhilash Mb, Android Automotive OS Discussion Group, frankk...@gmail.com
Hi 

if(!getBaseContext().getPackageManager().hasSystemFeature(getBaseContext().getPackageManager().FEATURE_AUTOMOTIVE)){
                Log.d(TAG, "initcar: hasSystemFeature failed");
                return;
   }

I don't this this piece of code is necessary since you would have already specified it in your manifest file. 

However for properties, you need to add speed permission in your manifest.
<uses-permission android:name="android.car.permission.CAR_SPEED" />

and request for that permission before you initialize the car, something like below,

ActivityCompat.requestPermissions(this, new String[]{Car.PERMISSION_SPEED}, 101); 

Reply all
Reply to author
Forward
0 new messages