- Manager注册回调,然后通过onChangeEvent获取汽车属性值CarPropertyValue的改变:
mCarHvacManager.registerCallback(mHvacCallback);
private CarHvacEventCallback mHvacCallback = new CarHvacEventCallback() {
@Override
public void onChangeEvent(CarPropertyValue carPropertyValue) {
//Log.d(TAG, "onChangeEvent id = " + toHexString(carPropertyValue.getPropertyId()));
int value = -1;
int areaId;
switch (carPropertyValue.getPropertyId()) {
case ID_HVAC_POWER_ON:
value = (Integer)carPropertyValue.getValue();
areaId = carPropertyValue.getAreaId();
break;
default:
break;
}
}
@Override
public void onErrorEvent(int i, int i1) { }
};
- SET 和 GET,以HVAC空调为例:
mCarHvacManager.setIntProperty(ID_HVAC_LOCK_VENTILATION_ON, VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL, HVAC_ON);
float temp = mCarHvacManager.getFloatProperty(ID_HVAC_TEMPERATURE_SET, SEAT_ROW_1_LEFT);
VehicleAreaType分为GLOBAL(全局)、WINDOW(车窗)、MIRROR(镜子)、SEAT(座椅)、DOOR(车门)、WHEEL(车轮)等:
enum VehicleArea : int32_t {
GLOBAL = 0x01000000,
/** WINDOW maps to enum VehicleAreaWindow */
WINDOW = 0x03000000,
/** MIRROR maps to enum VehicleAreaMirror */
MIRROR = 0x04000000,
/** SEAT maps to enum VehicleAreaSeat */
SEAT = 0x05000000,
/** DOOR maps to enum VehicleAreaDoor */
DOOR = 0x06000000,
/** WHEEL maps to enum VehicleAreaWheel */
WHEEL = 0x07000000,
/** IN_OUT_CAR maps to enum VehicleAreaInOutCar,车内外 */
IN_OUT_CAR = 0x08000000,
MASK = 0x0f000000,
};
CarAPI (car-lib:android.car.jar)
1.createCar:
public static Car createCar(Context context, ServiceConnection serviceConnectionListener, @Nullable Handler handler) {
if (!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) {
Log.e(CarLibLog.TAG_CAR, “FEATURE_AUTOMOTIVE not declared while android.car is used”);
return null;
}
try {
return new Car(context, serviceConnectionListener, handler);
} catch (IllegalArgumentException e) { // Expected when car service loader is not available.
}
return null;
}
- getCarManager(“serviceName”) -> createCarManager(“serviceName”, IBinder)
public Object getCarManager(String serviceName) throws CarNotConnectedException {
CarManagerBase manager;
ICar service = getICarOrThrow();
synchronized (mCarManagerLock) {
manager = mServiceMap.get(serviceName);
if (manager == null) {
try {
if(TBOX_SERVICE.equals(serviceName)){
manager = createCarManager(serviceName, null);
return manager;
}
IBinder binder = service.getCarService(serviceName);
if (binder == null) {
Log.w(CarLibLog.TAG_CAR, “could not get binder for service:” + serviceName);
return null;
}
manager = createCarManager(serviceName, binder);