In this guide, you’ll learn how to incorporate the Esper Device SDK into a simple Android project.
Requirements:
- An Esper API Key
- An Esper-provisioned Android device
- Android Studio
In this article:
Preparing a Device
Whether you are using an already provisioned device, or provisioning a device for the first time, you’ll need to enable some blueprint settings before using the SDK.
In addition to the default blueprint settings, enable the following configurations.
In Apps & Configuration, enable:
- Allow Local App Install
- Allow Application Uninstall
In Device Security, enable:
- ADB Access
And in Hardware Settings, enable:
- External Device
- File Transfer
Be sure to change or disable these settings before the device is used in the field.
Creating a Project
If you’ve already built an application, you can skip this question. However, if this is your first time using an SDK, or you’re curious about how it works, learn how to get a project started.
Open Android Studio and create a new project. In this example, we’ll use the Empty Activity template.
Then, input the following:
- Name of the application
- The file path
- Java
- Choose the minimum SDK. This may vary based on your device version.
You’ll start with a project with boilerplate code.
Integrating Dependencies
Next, we’ll declare the dependencies that allow the SDK to run.
Integrate in the Maven Dependency Manager
In settings.gradle, paste the following code block. In some software applications, this will be in dependencyResolutionManagement.
maven {
url "https://artifact.esper.io/artifactory/esper-device-sdk/"
}
Integrate in the Gradle File
In build.gradle (Module :app), paste the dependency for the SDK version. Check the Device SDK documentation for the latest SDK version.
implementation "io.esper.devicesdk:app:3.2.0760.25+"
Note: You’ll need to adjust the version depending on the device, Esper updates, and more. We recommend testing the SDK and then adjusting the version.
Declare the Package
In app > manifests > AndroidManifest.xml, declare the package.
<queries> <package android:name="io.shoonya.shoonyadpc"/> </queries>
Testing the App
Main Activity is the gateway or starting point for our Android App. Every action goes through this file.
In app > java > com.example.myapplication > MainActivity, import the following packages.
import android.util.Log;
import androidx.annotation.Nullable;
import java.util.Arrays;
import io.esper.devicesdk.EsperDeviceSDK;Altogether, our imports will look something like this:
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.Nullable;
import java.util.Arrays;
import androidx.appcompat.app.AppCompatActivity;
import io.esper.devicesdk.EsperDeviceSDK;Then sync and build
the project.
Next, we’ll attach our device to the computer.
Press Allow.
Then, in Available Devices, select the device. You may need to select it from the Device Manager.
First, we’ll test that everything is working as expected by creating a log method in the MainActivity file.
private void testing() {
Log.d(TAG, "testing testing testing");
}Paste this at the end of the file before the closing ‘}’ bracket.
Then, in the onCreate method, call the testing method.
testing();We’ll also instantiate the TAG function for the MainActivity method at the toplevel of our MainActivity class.
private static final String TAG = "MainActivity";Altogether, it should look like this:
Build and Run
the app. Then, check the log cat. The testing message should appear if we search for tag:mainactivity.
If everything works, then we’re ready to apply the SDK.
Not working?
- You may need to install the app on the device and set it to the Show state in a blueprint or through it's App page.
- Check if you have an old version of the Android Studio installed by going to Settings > Apps & notifications > See All Apps. If you do, try uninstalling it and then building and running it again.
Incorporate the SDK
First, we’ll instantiate the SDK at the top level of our MainActivity class.
private EsperDeviceSDK sdk;Then we’ll add the declaration to our onCreate method ( just like testing() ).
sdk = EsperDeviceSDK.getInstance(getApplicationContext());Now, we’ll call a method provided by the SDK. Paste the method at the bottom of the class, before the ending ‘}’ bracket.
We’ll be using the getDeviceTemperatures method to grab our current device temperature.
The API Key is declared in MainActivity as an example. If you’re implementing an SDK, consider storing the API Key in a secure location, such as an environment variable outside of the application.
private void activateSDK() {
String token = "ESPER API KEY";
Log.d(TAG, "here");
sdk.activateSDK(token, new EsperDeviceSDK.Callback<Void>() {
@Override
public void onResponse(Void response) {
Log.d(TAG, "SDK was successfully activated");
getTemps();
}
@Override
public void onFailure(Throwable t) {
Log.d(TAG, "SDK activation failed", t);
}
});
}
private void getTemps() {
sdk.getDeviceTemperatures(0, 0, new EsperDeviceSDK.Callback<float[]>() {
@Override
public void onResponse(@Nullable float[] response) {
Log.d(TAG, "getDeviceTemperatures successful. temperatures : " + Arrays.toString(response));
}
@Override
public void onFailure(Throwable t) {
Log.d(TAG, "getDeviceTemperatures failure. error : " + t.getMessage());
}
});
We’ll also need to call the activateSDK and getDeviceTemperatures methods in the onCreate method.
activateSDK();
getDeviceTemperatures();We’ve removed the testing method from OnCreate and added getDeviceTemperatures() instead.
Next, Sync , Build
, and click Run
and check the logcat. The device temperature should be displayed.
As you’ve seen through this tutorial, the Esper Device SDK can elevate device permissions that wouldn’t otherwise be available for applications. With the Device SDK, you can notify device users when it's close to overheating, or set the brightness level every time the user opens your app.
Integrate the Device SDK to create a more responsive, reliable app experience with deeper control over your devices.