1

I'm trying to display all possible information about vehicle when android auto is connected to my android app.

Unfortunately I get nulls everywhere.

I'm using Desktop Head Unit emulator and actual mobile device. I also tested app on real vehicle, and it showed same results.

Method for getting information:

private fun getData() {
    Log.i(TAG, "getData()")
    val carService = carContext.getCarService(CarHardwareManager::class.java)
    val carInfo = carService.carInfo
    val carSensors = carService.carSensors

    textMap["Car App Api Level"] = carContext.carAppApiLevel.toString()
    textMap["Host info"] = carContext.hostInfo.toString()
    textMap["Is dark mode"] = carContext.isDarkMode.toString()

    try {
        carService.carClimate
        textMap["Car climate"] = "Available"
    } catch (t: Throwable) {
        textMap["Car climate"] = "Unavailable"
    }

    carSensors.addGyroscopeListener(UPDATE_RATE_NORMAL, carContext.mainExecutor) {
        textMap["Gyroscope"] = it.toString()
        invalidate()
    }

    carSensors.addCarHardwareLocationListener(UPDATE_RATE_NORMAL, carContext.mainExecutor) {
        textMap["Car Hardware Location"] = it.location.value.toString()
        invalidate()
    }

    carSensors.addCompassListener(UPDATE_RATE_NORMAL, carContext.mainExecutor) {
        textMap["Compass"] = it.orientations.value.toString()
        invalidate()
    }

    carSensors.addAccelerometerListener(UPDATE_RATE_NORMAL, carContext.mainExecutor) {
        textMap["Accelerometer"] = it.forces.value.toString()
        invalidate()
    }

    carInfo.fetchEnergyProfile(carContext.mainExecutor) {
        textMap["Energy Profile"] =
            it.fuelTypes.value.toString() + " " + it.evConnectorTypes.value.toString()
        invalidate()
    }

    carInfo.addTollListener(carContext.mainExecutor) {
        textMap["Toll Card"] = it.cardState.value.toString()
        invalidate()
    }

    carInfo.addSpeedListener(carContext.mainExecutor) {
        textMap["Speed"] =
            it.speedDisplayUnit.value.toString() + " " + it.rawSpeedMetersPerSecond.value.toString() + " " + it.displaySpeedMetersPerSecond.value.toString()
        invalidate()
    }

    carInfo.addMileageListener(carContext.mainExecutor) {
        textMap["Mileage"] = it.odometerMeters.value.toString()
        invalidate()
    }

    carInfo.addEvStatusListener(carContext.mainExecutor) {
        textMap["Ev Status"] =
            it.evChargePortConnected.value.toString() + " " + it.evChargePortOpen.value.toString()
        invalidate()
    }

    carInfo.addEnergyLevelListener(carContext.mainExecutor) {
        textMap["Energy level"] =
                    "Battery percent: " + it.batteryPercent.value.toString() +
                    ", Fuel percent: " + it.fuelPercent.value.toString() +
                    ", Distance display unit: " + it.distanceDisplayUnit.value.toString()
                    ", Energy is low: " + it.energyIsLow.value.toString() +
                    ", Fuel volume display unit: " + it.fuelVolumeDisplayUnit.value.toString() +
                    ", Range rmaining meters: " + it.rangeRemainingMeters.value.toString()

        invalidate()
    }

    carInfo.fetchModel(carContext.mainExecutor) {
        textMap["Model"] =
            "Name: " + it.name.value.toString() +
            ", Manufacturer: " + it.manufacturer.value.toString() +
            ", Value: " + it.year.value.toString()
        invalidate()
    }
}

Why are all fields null and how to fix it?

SMGhost
  • 3,867
  • 6
  • 38
  • 68
  • 1
    For the DHU testing, you'll need to [add some configuration to the `.ini` file](https://developer.android.com/training/cars/testing/dhu#sensor-configuration) you're using and can then [send mock data](https://developer.android.com/training/cars/testing/dhu#sensors) via the terminal where the DHU is running. As far as real vehicles go, not all vehicles support all of the fields, so it's best to use data to augment your app, but not rely on it being available. – Ben Sagmoe Aug 22 '23 at 15:03
  • Thanks Ben, I wasn't aware that there's no requirement for manufacturers to implement available sensor data. – SMGhost Aug 25 '23 at 04:58

0 Answers0