Vehicle
The Vehicle module offers a VehicleProvider object that is responsible for controlling, accessing, and listening to changes in the vehicle state.
Project setup
You have to add the Vehicle module dependency to the build.gradle.kts file of your application module and synchronize the project:
implementation("com.tomtom.sdk.vehicle:vehicle-provider:2.1.2")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.
val currentCharge = Energy.kilowattHours(30.0)val maxCharge = Energy.kilowattHours(60.0)
val electricCar = Vehicle.Car( electricEngine = ElectricEngine( chargeLevel = ChargeLevel(currentCharge, maxCharge), ),)VehicleProvider.vehicle = electricCar
// Switch to the bicycleVehicleProvider.vehicle = Vehicle.Bicycle()Updating the vehicle’s state
Vehicle data classes' properties are separated into VehicleProperties , which offer the integrator a simpler way to update the current state of the vehicle. If a property—other than MaxSpeed or CommercialVehicle—was not previously set, or if the update would lead to an inconsistent vehicle state, an IllegalArgumentException will be thrown. 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:
val updatedCurrentCharge = CurrentChargeProperty(Energy.kilowattHours(25.0))val updatedElectricEngineProperties = ElectricEngineProperties( listOf(updatedCurrentCharge), )val updatedVehicleProperties = listOf(updatedElectricEngineProperties)
VehicleProvider.updateVehicleProperties(updatedVehicleProperties)Current vehicle
The following code snippet shows how to get the currently set vehicle.
val vehicle = VehicleProvider.vehicleListening to vehicle updates
The integrator can listen for specific changes of the VehicleProperties or for every change in the vehicle’s state. If the integrator tries to add the same listener twice, an IllegalArgumentException is thrown, regardless of how it was added.
All changes
The following code snippet shows how to add a listener whose method will be called whenever the vehicle’s state changes:
val listener = VehicleUpdatedListener { vehicle, updatedProperties -> // YOUR CODE GOES HERE }
VehicleProvider.addVehicleUpdatedListener(listener)Specific change
If the integrator only wants to listen to changes in specific VehicleProperties , this can be done by specifying their corresponding PropertyId . The following code snippet shows how to listen for changes in the current state of charge:
val vehicleOptionToBeTracked = PropertyId.CurrentCharge
val listener = VehicleUpdatedListener { vehicle, updatedProperties -> if (vehicleOptionToBeTracked in updatedProperties) { // YOUR CODE GOES HERE } }
VehicleProvider.addVehicleUpdatedListener(listener)Next steps
Since you have learned how to set up a vehicle provider, here are recommendations for the next steps: