初始化驱动程序 SDK

在使用 Driver SDK 之前,您必须先按照以下步骤初始化 Navigation SDK 和 Driver SDK:

  1. NavigationApi 获取 Navigator 对象。

    Java

    NavigationApi.getNavigator(
        this, // Activity
        new NavigationApi.NavigatorListener() {
          @Override
          public void onNavigatorReady(Navigator navigator) {
            // Keep a reference to the Navigator (used to configure and start nav)
            this.navigator = navigator;
          }
        }
    );
    

    Kotlin

    NavigationApi.getNavigator(
      this, // Activity
      object : NavigatorListener() {
        override fun onNavigatorReady(navigator: Navigator) {
          // Keep a reference to the Navigator (used to configure and start nav)
          this@myActivity.navigator = navigator
        }
      },
    )
    
  2. 创建一个 DriverContext 对象,并填充必需字段。如需初始化 DriverContext 对象,您必须输入 Google Cloud 项目的项目 ID 作为 providerId。如需了解如何设置 Google Cloud 项目,请参阅创建 Fleet Engine 项目

    Java

    DriverContext driverContext = DriverContext.builder(application)
        .setProviderId(providerId)
        .setVehicleId(vehicleId)
        .setAuthTokenFactory(authTokenFactory)
        .setNavigator(navigator)
        .setRoadSnappedLocationProvider(
            NavigationApi.getRoadSnappedLocationProvider(application))
        .build();
    

    Kotlin

    val driverContext =
      DriverContext.builder(application)
        .setProviderId(providerId)
        .setVehicleId(vehicleId)
        .setAuthTokenFactory(authTokenFactory)
        .setNavigator(navigator)
        .setRoadSnappedLocationProvider(NavigationApi.getRoadSnappedLocationProvider(application))
        .build()
    
  3. 使用 DriverContext 对象初始化 *DriverApi

    Java

    RidesharingDriverApi ridesharingDriverApi = RidesharingDriverApi.createInstance(driverContext);
    

    Kotlin

    val ridesharingDriverApi = RidesharingDriverApi.createInstance(driverContext)
    
  4. 从 API 对象获取 RidesharingVehicleReporter。(*VehicleReporter 扩展了 NavigationVehicleReporter。)

    Java

    RidesharingVehicleReporter vehicleReporter = ridesharingDriverApi.getRidesharingVehicleReporter();
    

    Kotlin

    val vehicleReporter = ridesharingDriverApi.getRidesharingVehicleReporter()
    

SSL/TLS 注意事项

在内部,Driver SDK 实现使用 SSL/TLS 与车队引擎服务进行安全通信。Android API 版本 23 或更低版本可能需要 SecurityProvider 补丁才能与服务器通信。如需详细了解如何在 Android 中使用 SSL,请参阅 GMS 安全提供程序。本文还包含用于修补安全提供程序的代码示例。

后续步骤

准备好车辆