Skip to content

Repository files navigation

Smartcar Android Auth SDK Build Status JavaDoc Maven Central

The SmartcarAuth Android SDK makes it easy to integrate with Smartcar Connect from Android.

Reference Documentation

Installation

Add the following to your application's build.gradle dependencies:

dependencies {
    implementation("com.smartcar.sdk:smartcar-auth:4.2.1")
}

Bluetooth support

For Bluetooth pairing support, add the permissions to AndroidManifest.xml:

<manifest>
    <!-- Request legacy Bluetooth permissions on older devices. -->
    <uses-permission android:name="android.permission.BLUETOOTH"
                     android:maxSdkVersion="30" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
                     android:maxSdkVersion="30" />

    <!-- Assert that Bluetooth scan results are not used to derive
         physical location information -->
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN"
        android:usesPermissionFlags="neverForLocation"
        tools:targetApi="s" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    ...
</manifest>

Usage

  1. Instantiate a new SmartcarAuth instance with a SmartcarCallback handler:
import com.smartcar.sdk.SmartcarAuth;
import com.smartcar.sdk.SmartcarCallback;
import com.smartcar.sdk.SmartcarResponse;

SmartcarAuth smartcarAuth = new SmartcarAuth(
    "your-application-id", // same as the former alias: client-id
    "your-redirect-uri",
    new String[] {"read_vehicle_info", "read_odometer"},

    // Create a callback to handle the redirect response
    new SmartcarCallback() {
        @Override
        public void handleResponse(SmartcarResponse smartcarResponse) {
            // Retrieve the authorization code
            Log.d("SmartcarAuth", "Authorization code: " + smartcarResponse.getCode());
        }
});
  1. Launch the Smartcar Connect flow:
smartcarAuth.launchAuthFlow(getApplicationContext());

Alternatively, add a click handler to any view to launch the Smartcar Connect flow.

Button connectButton = findViewById(R.id.connect_button);
smartcarAuth.addClickHandler(getApplicationContext(), connectButton);
  1. Use the AuthUrlBuilder for additional configuration:
String authUrl = smartcarAuth.authUrlBuilder()
    .setState("foo")
    .setForcePrompt(true)
    .setMakeBypass("TESLA")
    .build();

// Launch Smartcar Connect with the configured parameters
smartcarAuth.launchAuthFlow(getApplicationContext(), authUrl);

// Alternatively attach a click handler
Button connectButton = findViewById(R.id.connect_button);
smartcarAuth.addClickHandler(getApplicationContext(), button, authUrl);

responseType = "none"

For M2M / server-side flows where you don't need an authorization code exchanged in the app, pass responseType = "none" to the constructor along with an externalId to correlate the connection:

SmartcarAuth smartcarAuth = new SmartcarAuth(
    "your-application-id",
    "your-redirect-uri", // optional when responseType is "none"
    new String[] {"read_vehicle_info", "read_odometer"},
    false, // testMode
    "none", // responseType
    new SmartcarCallback() {
        @Override
        public void handleResponse(SmartcarResponse smartcarResponse) {
            // smartcarResponse.getCode() is null; correlate via externalId
            Log.d("SmartcarAuth", "External ID: " + smartcarResponse.getExternalId());
        }
});

String authUrl = smartcarAuth.authUrlBuilder()
    .setExternalId("your-external-id")
    .build();

smartcarAuth.launchAuthFlow(getApplicationContext(), authUrl);

For the response_type=none flow, smartcarResponse.getCode() will return null; correlate via externalId via a webhook or the /connections endpoint.

Contributing

Please use Android Studio 3.5 to develop on the Smartcar Android SDK.

About

Smartcar Android Auth SDK

Topics

Resources

Contributing

Stars

21 stars

Watchers

17 watching

Forks

Releases

Used by

Contributors

Languages