Chaosflori 25
Chaosflori 25

Reputation: 13

How can I implement the Android Car API for Andorid Studio to read out my EVs percentage?

I'm currently trying to display the percentage of my Ev Via Android Auto. I can't manage to get CarInfo carInfo = getCarContext().getCarService(CarHardwareManager.class).getCarInfo(); to run. I used this in my automotive build.gradle useLibrary 'android.car' and tried importing many things but that didn't help obviously. This is my first file:

package com.example.aatest4;

import android.car.Car;
import android.car.CarInfoManager;
import android.content.Intent;
import androidx.annotation.NonNull;
import androidx.car.app.CarAppMetadataHolderService;
import androidx.car.app.CarAppService;
import androidx.car.app.CarContext;
import androidx.car.app.Screen;
import androidx.car.app.Session;
import androidx.car.app.hardware.CarHardwareManager;
import androidx.car.app.hardware.info.CarInfo;
import androidx.car.app.validation.HostValidator;
//import android.content.ServiceConnection;




public class HelloWorldService extends CarAppService {
    @NonNull
    @Override
    public HostValidator createHostValidator() {
        return HostValidator.ALLOW_ALL_HOSTS_VALIDATOR;
    }

    public Session onCreateSession() {
        return new Session() {
            @NonNull
            @Override
            public Screen onCreateScreen(@NonNull Intent intent) {
            
              CarInfo carInfo = getCarContext().getCarService(CarHardwareManager.class).getCarInfo();

                return new HelloWorldScreen(getCarContext());
            }
        };
    }
}

and this my second:

package com.example.aatest4;

//import android.car.Car;
//import android.car.CarInfoManager;

import androidx.annotation.NonNull;
import androidx.car.app.CarContext;
import androidx.car.app.Screen;
import androidx.car.app.model.Pane;
import androidx.car.app.model.PaneTemplate;
import androidx.car.app.model.Row;
import androidx.car.app.model.Template;

public class HelloWorldScreen extends Screen {
    //public HelloWorldScreen() {
    public HelloWorldScreen(CarContext carContext) {
        super(carContext);
    }
    @NonNull
    @Override
    public Template onGetTemplate() {
        String akku = String.valueOf(50);
        Row row = new Row.Builder().setTitle(akku + "%   ").addText(akku + "%").build();
        return new PaneTemplate.Builder(new Pane.Builder().addRow(row).build()) .setTitle("Akkuanzeige").build();
        //Todo: Center?
    }
}

Upvotes: 1

Views: 1317

Answers (1)

Arunkrishna
Arunkrishna

Reputation: 436

I tried below steps and 'android.car.hardware' package is importing.

Find android.car.jar related to your API level (I think only available after API 29). This will be residing inside the sdk manager. Example -> /~/Android/Sdk/platforms/android-29/optional/android.car.jar

Copy above android.car.jar and put into android "libs" folder

Add it to the dependencies section in your build.gradle

compileOnly files("libs/android.car.jar")

Also it's possible to link directly from sdk. I haven't tried that. But it wiil look like below code

def sdkDir = project.android.sdkDirectory
def androidCarJar = "$sdkDirplatforms/android-29/optional/android.car.jar"
implementation(files(androidCarJar))

To get EV percentage follow below code

mCarPropertyManager.registerCallback(new CarPropertyManager.CarPropertyEventCallback() {
    @Override
    public void onChangeEvent(CarPropertyValue carPropertyValue) {
        Log.d(TAG, "EV_BATTERY_VALUE: onChangeEvent(" + carPropertyValue.getValue() + ")");
    }

    @Override
    public void onErrorEvent(int propId, int zone) {
        Log.d(TAG, "EV_BATTERY_VALUE: onErrorEvent(" + propId + ", " + zone + ")");
    }
}, VehiclePropertyIds.EV_BATTERY_LEVEL, CarPropertyManager.SENSOR_RATE_ONCHANGE);

Upvotes: 0

Related Questions