- Tutorials
- Working with mim OE in an Android project
Working with mim OE in an Android project
Objective
The objective of this article is to demonstrate how to use the mimik Client Library interfaces when working with the mim OE Runtime in an Android project.
Intended Readers
The intended readers of this document are Android software developers, who want to familiarize themselves with the basics of the mimik Client Library interface, specifically methods for interfacing with the mim OE Runtime.
What You'll Be Doing
In this tutorial you'll be covering the following topics of working with the mim OE Runtime:
- Starting
- Generating Access Token
- Retrieving Runtime Information
- Stopping
- Resetting
Prerequisites
- Understanding the mimik Client Library components integration and initialization process as layed out in this article.
Technical Prerequisites
This tutorial has the following technical prerequisites:
- A working Android Studio environment. You can download Android Studio here.
- An ARM-based Android device. The mim OE Runtime for Android does not support virtual or x86-based devices.
Getting the mim OE license and Developer ID Token from the mimik Developer Portal.
First, we get our developer mim OE (Edge) license from mimik Developer Console and set the value.
Generating the Access Token that's used to work with various API within the mimik Client Library requires that the develop have a Developer ID Token on hand. Developers get a Developer ID Token from the mimik Developer Portal as shown in the illustration below.
A Developer ID Token is generated from within the particular mimik project that's registered on the mimik Developer Portal.
A mim OE license can be copied from the Edge Projects page in the mimik Developer Console.
Initializing the mimik Client Library
In order to get the mimik Client Library up and running in an Android project, a developer needs to initialize it. Initialization involves importing a reference to EdgeMobileClient
and then creating an instance variable using the constructor.
The code listed below shows an example of the code a developer writes to create an instance of the mimik Client Library. Notice the use of the MimOEClient(Context context, MimOEConfig mimOEConfig)
constructor at Line 2.
The default MimOEConfig()
constructor automatically selects default parameters. This will have the mim OE Runtime host URLs using port 8083, and use the default license required to work with the mim OE Runtime.
1: MimOEClient mimOEClient =2: new MimOEClient(getContext(), new MimOEConfig());
Optionally, custom parameters may be used to use a specific mim OE license.
1: MimOEClient mimOEClient =2: new MimOEClient(getContext(), new MimOEConfig().license("YOUR-MIM-OE-LICENSE");
You now have an initialized mimik Client Library in your Android application.
Starting the mim OE Runtime
In order to do work using the mimik Client Library APIs, the initialized mim OE Runtime needs to be running. To start up the mim OE Runtime using the mimik Client Library, add the following code to your Android project.
1: if (mimOEClient.startMimOE()) { // Start mim OE Runtime2: // mim OE started successfully3: Log.d("mim OE", "Started successfully");4: } else {5: // error with mim OE startup6: Log.d("mim OE", "Failed to start");7: }
Authorizing Access to the mim OE Runtime
Most of mimik Client Library APIs require an Access Token in order to be operational. Developers use the mimik Client Library to create an Access Token. The Access Token is based on the Developer ID Token they generated when creating their account in the mimik Developer Portal.
Generating the Access Token
Once developers have a Developer ID Token in hand, they'll use it in code with the instance of the mimik Client Library in order to generate an Access Token.
The segment below shows code that a developer uses in an Android application to generate an Access Token.
1: String developerIdToken = "<DEVELOPER_ID_TOKEN>";2: String clientId = "CLIENT_ID";3: String authorizationRootUri = "https://devconsole-mid.mimik.com"; // This is the default authorization URI4:5: DeveloperTokenLoginConfig config = new DeveloperTokenLoginConfig();6: config.setClientId(clientId);7: config.setAuthorizationRootUri(authorizationRootUri);8: config.setDeveloperToken(developerIdToken);9: mimOEClient.loginWithDeveloperToken(this, config, new MimOEResponseHandler() {10: @Override11: public void onError(MimOERequestError mimOERequestError) {12: Log.d("mim OE", "Failed to generate access token " + mimOERequestError.getErrorMessage());13: }14:15: @Override16: public void onResponse(MimOERequestResponse mimOERequestResponse) {17: String accessToken = ((AuthResponse)mimOERequestResponse).getAccessToken();18: Log.d("mim OE", "Got access token " + accessToken);19: }20: });
WHERE:
<DEVELOPER_ID_TOKEN>
is the Developer ID Token the developer retrieved from the mimik Developer Portal<CLIENT_ID>
is the Client ID the developer retrieved from the mimik Developer Portal
Notice that the Access Token is the value of accessToken
as set at Line 18
in the code above.
Retrieving the Access Token
The mimik Client Library saves the registered access token automatically. The access token can be retrieved using the method getCombinedAccessTokens()
.
1: CombinedAuthResponse savedTokens = mimOEClient.getCombinedAccessTokens();2: String storedAccessToken = savedTokens.getMimikTokens().getAccessToken();
Stopping down the mim OE Runtime using the mimik Client Library
To shut down the mim OE Runtime, developers add the following code to their Android project:
1: mimOEClient.stopEdge();2: Log.d("mim OE", "Stopped");
Please note that due to the nature of mim OE shutdown process design this call is intentionally synchronous, main thread blocking API.
Getting additional information from the mim OE Runtime
The mimik Client Library provides a number of API methods that return information about the mim OE Runtime. The segment below show an examples of some of the information a developer can get using the mimik Client Library API.
1: MimOEInfoResponse response = mimOEClient.getInfo();2: Log.d("mim OE", "Node ID " + response.getNodeId());
Resetting mim OE Runtime
When clearing user data, developers may need to shut down the mim OE Runtime and erase all content stored in its working directory. The following code example demonstrates how to do this in a single call, followed by an explanation.
1: // Stop mim OE and clear authorization, state, and stored data2: mimOEClient.clearMimOEData();
Resetting the mim OE Runtime will remove all authorization data and reset the authorization status. Resetting the mim OE Runtime also clears any data stored and and removes the deployed microservices. Finally, resetting stops the mim OE Runtime. The mim OE Runtime will return to an unauthorized, pristine state.
NOTE: Once the mim OE Runtime is reset, all deployed edge microservices are stopped, removed, and their data is erased. |
---|
Additional reading
In order to get more out of this article, the reader could further familiarize themselves with the following concepts and techniques: