准备好车辆

本部分介绍如何让车辆准备好执行预定的任务。您必须 请完成以下每个步骤,然后您的后端才能将车辆 任务。

设置监听器

由于驱动程序 SDK 在 后台运行时,使用 DriverStatusListener 在发生特定情况时触发通知 比如错误、警告或调试消息。错误可能 具有瞬时性(例如 BACKEND_CONNECTIVITY_ERROR),或者 会导致位置更新永久停止。例如,如果您收到 VEHICLE_NOT_FOUND 错误,则表示配置错误。

以下示例展示了 DriverStatusListener 实现:

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());

启用位置信息更新

有了 *VehicleReporter 实例后,请按以下步骤启用位置信息更新:

Java

DeliveryVehicleReporter reporter = ...;

reporter.enableLocationTracking();

Kotlin

val reporter = ...

reporter.enableLocationTracking()

(可选)设置更新间隔

默认情况下,驱动程序 SDK 以 10 秒为间隔发送位置更新。每个 位置信息更新还表示车辆已联网。您可以更改此设置 间隔时间 reporter.setLocationReportingInterval(long, TimeUnit)。支持的下限 更新间隔为 5 秒。更新频率越高,速度越慢 请求和错误。