Vehicle

VERSION 0.50.6
PUBLIC PREVIEW

The Vehicle module offers a VehicleProvider interface that is responsible for the control, access, and listening to changes in the vehicle state.

The integrator can choose to provide its implementation of the VehicleProvider or use the TomTom default thread-safe implementation.

The TomTom implementation can be obtained via the VehicleProviderFactory.

VehicleProviderFactory allows the user to pass a custom vehicle object for initialization of the VehicleProvider. If no vehicle object is provided, a non-commercial car with a combustion engine is used by default.

When an instance of VehicleProvider is created, it can be injected into other TomTom modules that depend on it, e.g., Navigation.

Project setup

You have to add the Vehicle module dependency in the build.gradle.kts of your application module and synchronize the project:

implementation("com.tomtom.sdk.vehicle:vehicle-provider:0.50.6")

Setting initial vehicle’s state and switching to another vehicle

The following code snippet shows how to set the profile for an electric engine car and later switch to bicycle, which is another vehicle type.

1val currentCharge = Energy.kilowattHours(30.0)
2val maxCharge = Energy.kilowattHours(60.0)
3
4val electricCar = Vehicle.Car(
5 electricEngine = ElectricEngine(
6 chargeLevel = ChargeLevel(
7 currentCharge,
8 maxCharge
9 )
10 )
11)
12vehicleProvider = VehicleProviderFactory.create(electricCar)
13
14// Switch to the bicycle
15vehicleProvider.vehicle = Vehicle.Bicycle()

Updating the vehicle’s state

Vehicle data classes' properties are torn apart into VehicleProperties which offers the integrator a simpler way of updating the current state of the vehicle. If a property, which is not MaxSpeed or CommercialVehicle, was not previously set or the update would lead to vehicle’s state being inconsistent, it will throw an IllegalArgumentException. MaxSpeed can be updated for every vehicle type. CommercialVehicle can be updated for a motorized vehicle type. This updates the listed VehicleProperties in the current vehicle profile:

1val updatedCurrentCharge = CurrentChargeProperty(Energy.kilowattHours(25.0))
2val updatedElectricEngineProperties = ElectricEngineProperties(
3 listOf(updatedCurrentCharge)
4)
5val updatedVehicleProperties = listOf(updatedElectricEngineProperties)
6
7vehicleProvider.updateVehicleProperties(updatedVehicleProperties)

Current vehicle

The following code snippet shows how to get the currently set vehicle.

val vehicle = vehicleProvider.vehicle

Listening to vehicle updates

The integrator can listen for the specific changes of the VehicleProperties or every change of the vehicle’s state. If the integrator tries to add the same listener twice then the IllegalArgumentException is thrown, regardless of the way it was added.

All changes

The following code snippet shows how to add a listener whose method will be called once the vehicle’s state changes:

1val listener = VehicleUpdatedListener { vehicle, updatedProperties ->
2 /* YOUR CODE GOES HERE */
3}
4
5vehicleProvider.addVehicleUpdatedListener(listener)

Specific change

If the integrator only wants to listen to changes of some of the VehicleProperties, that can be done by specifying their corresponding PropertyId. The following code snippet shows how to listen to the current state of charge changes:

By setting the CombustionEngineId or ElectricEngineId, the listener will be notified on any change of combustion or electric engine’s properties.

1val vehicleOptionToBeTracked = PropertyId.CurrentCharge
2
3val listener = VehicleUpdatedListener { vehicle, updatedProperties ->
4 if (vehicleOptionToBeTracked in updatedProperties) {
5 /* YOUR CODE GOES HERE */
6 }
7}
8
9vehicleProvider.addVehicleUpdatedListener(listener)

Next steps

Since you have learned how to setup a vehicle provider, here are recommendations for the next steps: