請上車

本節說明如何讓車輛準備好執行排定的工作。你必須完成下列每個步驟,後端才能將車輛與工作配對。

設定事件監聽器

由於 Driver 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()

(選用) 設定更新間隔

根據預設,Driver SDK 會每隔 10 秒傳送一次位置資訊更新。每次位置更新也代表車輛處於連線狀態。您可以使用 reporter.setLocationReportingInterval(long, TimeUnit) 變更這個間隔。支援的更新間隔下限為 5 秒。如果更新頻率過高,可能會導致要求速度變慢和發生錯誤。