To make requests to a vehicle, your end user must connect their vehicle using Smartcar Connect. Smartcar Connect uses the OAuth 2.0 protocol to provide a secure and elegant authorization flow for your users.

1. Initialize the Smartcar object

Instantiate a Smartcar object in the onCreate function of the MainActivity.

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
// /app/src/main/java/smartcar/com/getting_started_android_sdk/MainActivity.java

// TODO: Authorization Step 1a: Initialize the Smartcar object
private static String CLIENT_ID;
private static String REDIRECT_URI;
private static String[] SCOPE;
private SmartcarAuth smartcarAuth;

protected void onCreate(Bundle savedInstanceState) {
    ...

    // TODO: Authorization Step 1b: Initialize the Smartcar object
    CLIENT_ID = getString(R.string.client_id);
    REDIRECT_URI = getString(R.string.smartcar_auth_scheme) + "://" + getString(R.string.smartcar_auth_host);
    SCOPE = new String[]{"required:read_vehicle_info"};
    
    smartcarAuth = new SmartcarAuth(
        CLIENT_ID,
        REDIRECT_URI,
        SCOPE,
        true,
        new SmartcarCallback() {
            // TODO: Authorization Step 3b: Receive an authorization code
        }
    );
}

2. Launch Connect

The Android application will launch a Chrome Tab with Smartcar Connect to request access to a user’s vehicle. On Connect, the user logs in with the username and password for their vehicle’s connected services account and grants the application access to their vehicle.

To launch Connect, we can use the addClickHandler function that our Smartcar object has access to.

  • 1
  • 2
  • 3
  • 4
// /app/src/main/java/smartcar/com/getting_started_android_sdk/MainActivity.java

// TODO: Authorization Step 2: Launch Connect
smartcarAuth.addClickHandler(appContext, connectButton);

3. Receive an authorization code

Once a user has authorized the application to access their vehicle, the user is redirected to the redirect_uri with an authorization code as a query parameter.

Android applications use custom URI schemes to intercept calls and launch the relevant application. This is defined within the AndroidManifest.

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
// /app/src/main/AndroidManifest.xml

<!-- TODO: Authorization Step 3a: Receive an authorization code -->
<activity android:name="com.smartcar.sdk.SmartcarCodeReceiver">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data
            android:host="@string/smartcar_auth_host"
            android:scheme="@string/smartcar_auth_scheme" />
    </intent-filter>
</activity>

Using the Android SDK, the application can receive the code in the SmartcarCallback object passed into the Smartcar object.

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
// /app/src/main/java/smartcar/com/getting_started_android_sdk/MainActivity.java

smartcarAuth = new SmartcarAuth(
    CLIENT_ID,
    REDIRECT_URI,
    SCOPE,
    true,
    new SmartcarCallback() {
        // TODO: Authorization Step 3b: Receive an authorization code
        @Override
        public void handleResponse(final SmartcarResponse smartcarResponse) {
            Log.i("MainActivity", smartcarResponse.getCode());

            // TODO: Request Step 1: Obtain an access token

            // TODO: Request Step 2: Get vehicle information
        }
    }
)

Try it out

Let’s try authenticating a vehicle in test mode.

In test mode, any username or password is valid for each brand.

Build your application in Android Studio and click on the “Connect your vehicle” button. Notice once you log in, Smartcar showcases all the permissions your application is asking for, in this case, read_vehicle_info. A user has to consent to all the permissions.

Once you have logged in and accepted the permissions, you should see your authorization code printed to the Logcat console in Android Studio.


In the next section, we will cover how to exchange the authorization code for an access_token and make your first request to Smartcar API with it!