# Vehicle management

## Create a custom vehicle

It is possible to create a custom vehicle from your own data. Create your vehicle using the method:

{% tabs %}
{% tab title="DriveKitVehicle" %}

```kotlin
fun createCustomVehicle(characteristics: VehicleBaseCharacteristics,
                        name: String,
                        id: String,
                        listener: VehicleCreateQueryListener
)
```

{% endtab %}
{% endtabs %}

Each vehicle will have a unique identifier and this unique ID will be added into the trip synchronization response.

This method have the following parameters:

| Name            | Type                                                                                                            | Description                                                                                  |
| --------------- | --------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- |
| characteristics | [VehicleBaseCharacteristics](https://docs.drivequant.com/vehicle/references-android#vehiclebasecharacteristics) | Characteristics of the vehicle you want to create. Model is detailed in the References part. |
| name            | String                                                                                                          | The name of the vehicle you want to create                                                   |
| id              | String                                                                                                          | The unique identifier of the vehicle you want to create                                      |

Check the `VehicleManagerStatus` value to ensure your vehicle is correctly created:

| Value                            | Description                                                                                                                                     |
| -------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
| SUCCESS                          | Vehicle is successfully created and saved in the local database.                                                                                |
| INVALID\_CHARACTERISTICS         | Vehicle is not created and **not** saved in the local database because provided characteristics are not valid.                                  |
| VEHICLE\_ID\_ALREADY\_USED       | Vehicle is not created and **not** saved in the local database because the provided vehicleId already exists.                                   |
| ONLY\_ONE\_GPS\_VEHICLE\_ALLOWED | *This value is not returned for this service.*                                                                                                  |
| ERROR                            | <p>Vehicle is not created and not saved in the local database because there was an unexpected error.</p><p>e.g: User not logged to DriveKit</p> |

[Vehicle](https://docs.drivequant.com/vehicle/references-android#vehicle) model is described in the References part.

## Synchronize vehicle list

You can retrieve the user’s vehicle list from the DriveQuant servers by calling the following method:

{% tabs %}
{% tab title="DriveKitVehicle" %}

```kotlin
fun getVehiclesOrderByNameAsc(
    listener: VehicleListQueryListener,
    type: SynchronizationType = DEFAULT
)
```

{% endtab %}
{% endtabs %}

This method have the following parameter:

| Name | Type                | Description                                                                                                                                                                                                                                                              |
| ---- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| type | SynchronizationType | <p></p><p>Can be <code>DEFAULT</code> or <code>CACHE</code>.</p><ul><li><code>DEFAULT</code> will synchronize the vehicles by calling the DriveQuant servers</li><li><code>CACHE</code> will retrieve the vehicles already synchronized in the local database.</li></ul> |

[VehicleSyncStatus](https://docs.drivequant.com/vehicle/references-android#vehiclesyncstatus) and [Vehicle](https://docs.drivequant.com/vehicle/references-android#vehicle) models are described in the References part.

## Retrieve local vehicle list

You can retrieve the list of vehicles stored in the DriveKit local database by calling the previous method but with the `type` parameter to `CACHE`.

## Replace a vehicle

Instead of deleting and then creating a new vehicle, you can use only one method to replace an existing vehicle.

{% hint style="info" %}
A previously paired beacon to that vehicle is automatically paired to the new vehicle.
{% endhint %}

### Replace a car

{% tabs %}
{% tab title="DriveKitVehicle" %}

<pre class="language-kotlin"><code class="lang-kotlin"><strong>fun replaceCarVehicle(
</strong>    	oldVehicleId: String,
    	carCharacteristics: CarCharacteristics,
    	name: String,
    	liteConfig: Boolean = false,
    	callback: (status: VehicleReplaceStatus, vehicle: Vehicle?) -> Unit
)
</code></pre>

{% endtab %}
{% endtabs %}

This method have the following parameter:

| Name               | Type                                                                                            | Description                                              |
| ------------------ | ----------------------------------------------------------------------------------------------- | -------------------------------------------------------- |
| oldVehicleId       | String                                                                                          | The unique identifier of the car you want to replace     |
| carCharacteristics | [CarCharacteristics](https://docs.drivequant.com/vehicle/references-android#carcharacteristics) | Characteristics of the car you want to create            |
| name               | String                                                                                          | The name of the car                                      |
| liteConfig         | Boolean                                                                                         | Advanced configuration. Leave this parameter as `false`. |

[VehicleReplaceStatus](https://docs.drivequant.com/vehicle/references-android#vehiclereplacestatus) and [Vehicle](https://docs.drivequant.com/vehicle/references-android#vehicle) models are described in the References part.

### Replace a truck

{% tabs %}
{% tab title="DriveKitVehicle" %}

```kotlin
fun replaceTruckVehicle(
    	oldVehicleId: String,
    	truckCharacteristics: TruckCharacteristics,
    	name: String,
    	callback: (status: VehicleReplaceStatus, vehicle: Vehicle?) -> Unit
)
```

{% endtab %}
{% endtabs %}

This method have the following parameters:

| Name                 | Type                                                                                                | Description                                            |
| -------------------- | --------------------------------------------------------------------------------------------------- | ------------------------------------------------------ |
| oldVehicleId         | String                                                                                              | The unique identifier of the truck you want to replace |
| truckCharacteristics | [TruckCharacteristics](https://docs.drivequant.com/vehicle/references-android#truckcharacteristics) | Characteristics of the truck you want to create        |
| name                 | String                                                                                              | The name of the truck                                  |

[VehicleReplaceStatus](https://docs.drivequant.com/vehicle/references-android#vehiclereplacestatus) and [Vehicle](https://docs.drivequant.com/vehicle/references-android#vehicle) models are described in the References part.

## Delete a vehicle

{% hint style="info" %}
When you delete a vehicle, a previously paired beacon or Bluetooth device to that vehicle is automatically unpaired and released from the vehicle. By this way, you can immediately pair the beacon or the Bluetooth device to another vehicle.
{% endhint %}

To delete a vehicle, you have to call the following method:

{% tabs %}
{% tab title="DriveKitVehicle" %}

```kotlin
fun deleteVehicle(vehicle: Vehicle, listener: VehicleDeleteQueryListener)
```

{% endtab %}
{% endtabs %}

This method have the following parameter:

| Name    | Type                                                                      | Description                     |
| ------- | ------------------------------------------------------------------------- | ------------------------------- |
| vehicle | [Vehicle](https://docs.drivequant.com/vehicle/references-android#vehicle) | The vehicle you want to delete. |

Check the `VehicleManagerStatus` value to ensure your vehicle is successfully deleted:

| Value                            | Description                                                                                                                                              |
| -------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
| SUCCESS                          | Vehicle is successfully deleted and changes are saved in the local database.                                                                             |
| UNKNOWN\_VEHICLE                 | The specified vehicle could not be deleted because the vehicle id is not a vehicle or belongs to another user account.                                   |
| INVALID\_CHARACTERISTICS         | *This value is not returned for this service.*                                                                                                           |
| VEHICLE\_ID\_ALREADY\_USED       | *This value is not returned for this service.*                                                                                                           |
| ONLY\_ONE\_GPS\_VEHICLE\_ALLOWED | *This value is not returned for this service.*                                                                                                           |
| ERROR                            | <p>Vehicle is not deleted and changes are <strong>not</strong> saved in the local database because there was an unexpected error.<br>e.g: No network</p> |

## Rename a vehicle

To rename a vehicle, you have to call the following method:

{% tabs %}
{% tab title="DriveKitVehicle" %}

```kotlin
fun renameVehicle(name: String, vehicle: Vehicle, listener: VehicleRenameQueryListener)
```

{% endtab %}
{% endtabs %}

This method have the following parameters:

| Name    | Type                                                                      | Description                    |
| ------- | ------------------------------------------------------------------------- | ------------------------------ |
| name    | String                                                                    | The new name for the vehicle   |
| vehicle | [Vehicle](https://docs.drivequant.com/vehicle/references-android#vehicle) | The vehicle you want to rename |

Check the `VehicleManagerStatus` value to ensure your vehicle is successfully deleted:

| Value                            | Description                                                                                                                                |
| -------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
| SUCCESS                          | Vehicle is successfully renamed and changes are saved in the local database.                                                               |
| UNKNOWN\_VEHICLE                 | The specified vehicle could not be renamed because the vehicle id is not a vehicle or belongs to another user account.                     |
| INVALID\_CHARACTERISTICS         | *This value is not returned for this service.*                                                                                             |
| VEHICLE\_ID\_ALREADY\_USED       | *This value is not returned for this service.*                                                                                             |
| ONLY\_ONE\_GPS\_VEHICLE\_ALLOWED | *This value is not returned for this service.*                                                                                             |
| ERROR                            | <p>Vehicle is not renamed and changes are not saved in the local database because there was an unexpected error.</p><p>e.g: No network</p> |

## **Change the detection mode of a vehicle**

When you have a vehicle, it is necessary to set its [DetectionMode](https://docs.drivequant.com/vehicle/references-android#detectionmode) type. The following function is used to select the DetectionMode of a vehicle:

{% tabs %}
{% tab title="DriveKitVehicle" %}

```kotlin
fun updateDetectionMode(
    vehicle: Vehicle,
    detectionMode: DetectionMode,
    listener: VehicleUpdateDetectionModeQueryListener,
    forceGPSVehicleUpdate: Boolean = false
)
```

{% endtab %}
{% endtabs %}

This method have the following parameters:

| Name                  | Type                                                                                  | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| --------------------- | ------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| vehicle               | [Vehicle](https://docs.drivequant.com/vehicle/references-android#vehicle)             | The vehicle which you want to update the DetectionMode                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| detectionMode         | [DetectionMode](https://docs.drivequant.com/vehicle/references-android#detectionmode) | The detection mode you want for the vehicle                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| forceGPSVehicleUpdate | Boolean                                                                               | <p>Optional parameter to force the <code>GPS</code> detection mode for a new vehicle if there is already a vehicle with this mode.</p><p>The default setting is false. In this case, the response of this service request will be <code>GPS\_MODE\_ALREADY\_EXISTS</code> and the new vehicle will not be configured in GPS mode. If this setting is true, the vehicle will inherit the <code>GPS</code> trigger mode and the existing vehicle that had this mode will be switched to the <code>DISABLED</code> mode.</p> |

[DetectionModeStatus](https://docs.drivequant.com/vehicle/references-android#detectionmodestatus) model is described in the References part.

{% hint style="warning" %}
Information: If a driver has more than one vehicle, only one can be configured in `GPS` mode. If you choose a `GPS` trigger mode for a vehicle while there is already another vehicle with this trigger mode, then you must specify the appropriate action with the `forceGPSVehicleUpdate` parameter to `true`. The other vehicle will have its detection mode updated to `DISABLED`.
{% endhint %}

## Get Vehicle Last Location

This function returns the location of the end of the last trip for a given vehicle.&#x20;

The returned location type is [DKTripLocation](https://docs.drivequant.com/trip-analysis/android/references#dktriplocation)

To retrieve the location at which the last recorded trip for the vehicle ended, use the following method:

{% tabs %}
{% tab title="DriveKitVehicle" %}

```kotlin
fun getVehicleLocation(vehicleId: String): DKTripLocation?
```

{% endtab %}
{% endtabs %}
