Skip to content

smartcar/java-sdk

Repository files navigation

Smartcar Java SDK Build Status Code Coverage JavaDoc Maven Central

Resources

Installation

The recommended method for obtaining the SDK is via Gradle or Maven through the Maven Central repository. Direct download links are also provided below.

Gradle

compile "com.smartcar.sdk:java-sdk:4.5.0"

Maven

<dependency>
  <groupId>com.smartcar.sdk</groupId>
  <artifactId>java-sdk</artifactId>
  <version>4.5.0</version>
</dependency>

Jar Direct Download

Signatures and other downloads available at Maven Central.

Usage

Authentication

For authentication, Smartcar uses the authorization code request flow of the OAuth 2.0 specification. Before you can make successful calls to the Smartcar platform, you will need to authenticate with Smartcar, and then obtain a valid access token for the target vehicle.

  1. Make sure you have your application set up in the Smartcar Developer Dashboard. You will need the following 3 pieces of information associated with your application:

    • Client ID
    • Client Secret
    • Redirect URI
  2. You can then generate an authentication URL for your user:

    // Setup
    String clientId = "";
    String clientSecret = "";
    String redirectUri = "";
    String[] scope = {};
    String mode = "test";
    
    // Initialize a new AuthClient with your credentials.
    AuthClient authClient = new AuthClient.Builder
        .clientId(clientId)
        .clientSecret(clientSecret)
        .redirectUri(redirectUri)
        .mode(mode);
    
    // Retrieve the auth URL to start the OAuth flow.
    String authUrl = authClient.authUrlBuilder(scope)
            .setApprovalPrompt(true)
            .setState("some state")
            .build();
  3. Allow the user to complete their portion of the OAuth flow using the generated URL.

  4. Once the user is sent back to the redirect url, the required authorization code will be included in the query string:

    https://redirect-url.example.com/?code=<AUTHORIZATION_CODE>

  5. Given the authorization code, you can now exchange it for an authorization token which can be used to access the Smartcar platform:

    Auth auth = authClient.exchangeCode(code);

Vehicle Data & Commands

Now that you have authenticated and can access the Smartcar platform, you can start making requests to vehicles.

  1. Obtain a list of authorized vehicles:

    VehicleIds response = AuthClient.getVehicleIds(auth.getAccessToken());
    String[] vehicleIds = response.getVehicleIds();
  2. Create an instance of Vehicle:

    Vehicle vehicle = new Vehicle(vehicleIds[0], auth.getAccessToken());
  3. You can now access all information about the specified vehicle:

    // Retrieve the vehicle's VIN
    String vin = vehicle.vin().getVin();
    
    // Read the vehicle's odometer
    VehicleOdometer odometerData = vehicle.odometer();
    double odometer = odometerData.getDistance();
    
    // Retrieve the vehicle's location
    VehicleLocation locationData = vehicle.location();
    String latLong = locationData.getLatitude() + ", " + locationData.getLongitude();
    
    // Lock and unlock the vehicle
    vehicle.lock();
    vehicle.unlock();

Supported Java Releases

Smartcar aims to support the SDK on all LTS Java releases (and Java 8) until the "Extended Support" date as defined in the Oracle Java SE Support Roadmap

In accordance with the Semantic Versioning specification, the addition of support for new Java releases would result in a MINOR version bump and the removal of support for Java releases would result in a MAJOR version bump.