The recommended method for obtaining the SDK is via Gradle or Maven through the Maven Central repository. Direct download links are also provided below.
compile "com.smartcar.sdk:java-sdk:4.7.0"
<dependency>
<groupId>com.smartcar.sdk</groupId>
<artifactId>java-sdk</artifactId>
<version>4.7.0</version>
</dependency>
Signatures and other downloads available at Maven Central.
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.
-
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
-
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();
-
Allow the user to complete their portion of the OAuth flow using the generated URL.
-
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>
-
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);
Now that you have authenticated and can access the Smartcar platform, you can start making requests to vehicles.
-
Obtain a list of authorized vehicles:
VehicleIds response = AuthClient.getVehicleIds(auth.getAccessToken()); String[] vehicleIds = response.getVehicleIds();
-
Create an instance of
Vehicle
:Vehicle vehicle = new Vehicle(vehicleIds[0], auth.getAccessToken());
-
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();
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.