Getting Started with Android
In this tutorial you'll find detailed instructions for getting started with the Android SDK. The Android SDK consists of libraries hmkit-android and hmkit-auto-api. These frameworks enable your application to work with vehicle data.
Introduction and Setup
Requirements
HMKit requires Android 5.0 or above. You can run it on your Mobile or Wear device.
INTEGRATION
HMKit is packed as an aar library (Android Archive Library) for easy inclusion into an Android project. There are also many different sample applications, all of which showcase different features. These can be downloaded on the Samples page.
Add the SDK to Your Project
In case you already have an Android Studio project, add the SDKs and Java 8 support in the module's build.gradle file.
android {
...
compileOptions {
targetCompatibility 1.8
sourceCompatibility 1.8
}
}
dependencies {
implementation('com.highmobility:hmkit-android:2.0.9@aar') {
transitive = true
}
implementation 'com.highmobility:hmkit-auto-api:13.2.0'
implementation 'at.favre.lib:slf4j-timber:1.0.0' // AutoAPI log binding to Timber
}
Click Tools ›› Android ›› Sync Project with Gradle Files.
Create an App
In "Develop mode" you will find your sandbox apps. In the app details page you are going to set the permissions it requires, manage devices and link virtual vehicles from your garage to test. Let's see how to create a new app.
- Go to the Build tab, select "Develop mode" and click the big plus (+) button, select "Driver" as the type and then select "Device App".
- In the left section, select the permissions that your app needs under the "Permissions" tap.
Note: Each permission must be manually added to each new app. - You can edit your app name and image from the menu icon next to the app name.
Create a Vehicle
In the "Simulation studio" you are able to create vehicles to link with your app and to test with the simulators. Go to the simulation studio and click the big plus (+) button.
Initialise the SDK
For a straight forward example of how to initialise the SDK, download the Android Scaffold sample app from GitHub.
- Once you have downloaded the SDK or sample app, go to the Develop mode tab, and select the app you created in the previous steps.
- Generate a key-pair using the SDK
- Use the Device Management API
POST /device/certificates
and store the response. - Insert the snippet into into the Android Studio project to initialise the SDK.
We have published a video that shows the full workflow for creating a Device Certificate: See video on Loom
Link Vehicle and Authorise Device
Now to use the Android SDK to fetch data you will need to retrieve an Access Token for the simulator. This is achieved by implementing a consent journey.
- Go to the Develop mode tab and select the app you created in the previous steps.
- Select the "OAuth2 Client" section where you will find the OAuth2 credentials
- Use the SDK OAuth Class to initiate the consent flow with your app credentials.
OAuth2 sample app
On GitHub you will find a sample app that implements the full consent journey.
The code snippet in your Android Studio project should look something like this:
HMKit.getInstance().downloadAccessCertificate("Qe8_nLatDfWzQ1HCi30cUjRUZguZVPTHZ046zVXfkAxgj1bpvem_N752EcTd_6BN2xnT_bsGJhFjvB3Xuhh4jRrjKX2UG0_lVqGyTrFVFHbjXkb1DLdi95DnARO8fNTc-g", new HMKit.DownloadCallback() { ... });
Before that, logs can be enabled with Timber
if (BuildConfig.DEBUG) Timber.plant(new Timber.DebugTree());
Send a Telematics Command
Once the simulator is open, fire away a Telematics command. For example to unlock the doors of the vehicle.
If you see that the app lacks permissions, you will need to revisit step two of the "Create an App" section of this document and select the appropriate permissions for your telematics command.
Read more about Telematics
Auto API
Check out the Android code reference for Auto API for further details on how to use vehicle commands.
HMKit.getInstance().getTelematics().sendCommand(new Doors.LockUnlockDoors(LockState.UNLOCKED), serial, new Telematics.CommandCallback() {
@Override
public void onCommandResponse(Bytes bytes) {
// Parse command here
Command command = CommandResolver.resolve(bytes);
if (command instanceof Doors.State) {
Doors.State doorsState = (Doors.State) command;
// access the doors state object
}
}
@Override
public void onCommandFailed(TelematicsError error) {}
});