Get the vehicle ready

This section shows how to get the vehicle ready for scheduled tasks. You must complete each of the following steps before your backend can match a vehicle to a task.

Set up listener

Since the Driver SDK performs actions in the background, use the DriverStatusListener to trigger notifications when certain events occur, such as errors, warnings, or debug messages. Errors can be transient in nature (such as BACKEND_CONNECTIVITY_ERROR), or they might cause location updates to stop permanently. For example, if you receive a VEHICLE_NOT_FOUND error, it indicates a configuration error.

The following example shows a DriverStatusListener implementation:

class MyStatusListener implements DriverStatusListener {
  /** Called when background status is updated, during actions such as location reporting. */
  @Override
  public void updateStatus(
      StatusLevel statusLevel, StatusCode statusCode, String statusMsg, @Nullable Throwable cause) {
    // Existing implementation

    if (cause != null && cause instanceof StatusRuntimeException) {
      if (Status.NOT_FOUND.getCode().equals(cause.getStatus().getCode())) {
        // NOT_FOUND gRPC exception thrown by Fleet Engine.
      }
    }
  }
}

DriverContextBuilder.setDriverStatusListener(new MyStatusListener());

Enable location updates

Once you have a *VehicleReporter instance, enable location updates as follows:

Java

DeliveryVehicleReporter reporter = ...;

reporter.enableLocationTracking();

Kotlin

val reporter = ...

reporter.enableLocationTracking()

(Optional) Set the update interval

By default, the Driver SDK sends location updates at 10-second intervals. Each location update also indicates that the vehicle is online. You can change this interval with reporter.setLocationReportingInterval(long, TimeUnit). The minimum supported update interval is 5 seconds. More frequent updates may result in slower requests and errors.