Filter results by

ARTIK Modules

ARTIK Cloud

Developer

Forums

IoT Marketplace

Build an application to manage Cloud Connector devices

Today we’re developing an Android app that adds Cloud Connector devices to ARTIK Cloud and authorizes them with the corresponding third-party cloud. This allows your users to do everything within your application, rather than performing the necessary operations in My ARTIK Cloud. We will mention the REST APIs used for these functionalities.

Introduction

Third-party devices (e.g., Withings and Fitbit) may send data to a corresponding third-party cloud. Instead of retrieving data directly from these devices, ARTIK Cloud can connect to an eligible third-party cloud and use it as the data source. To achieve this, a Cloud Connector for that type of device must first be built on ARTIK Cloud. A user can then connect a device that belongs to this Cloud Connector device type to their ARTIK Cloud account. If the third-party cloud requires authentication, the user must authenticate once with the third-party cloud and grant permissions to ARTIK Cloud.

Demo

Click “login” and enter your ARTIK Cloud Account credentials to login. Note that these are also your Samsung account credentials.

Click “Allow” when the app asks for permission to access data from the “Withings Device” Cloud Connector. This screen appears when you use the app for the first time.

After you finish the authentication, you will see the “Manage Cloud Connector devices” screen.

Click the “Add device” button to create a Cloud Connector device in ARTIK Cloud. Afterward, the screen shows some info about the device you created (it will have the “Withings Device” device type ID). From there, you can see that this device needs third-party authentication in order to receive data from Withings. The “Authorize” button is enabled.

Click the “Authorize” button to begin authentication with Withings. Enter your Withings credentials and then grant permissions to ARTIK Cloud.

After successfully completing authentication with Withings, you are taken back to the “Manage Cloud Connector devices” screen. You should see that your Withings Cloud Connector device is now authenticated. From now on, the device ID specified here can receive data from the physical Withings device via the Withings cloud.

Click “Remove authorization” button to revoke the authentication with Withings. The corresponding device stops receiving data from the Withings cloud. You can re-initiate the authorization or delete the device.

Installation and setup

Log in to the Developer Dashboard to register your Android application on ARTIK Cloud. To properly test the application, I use two separate Samsung accounts with different email addresses:

My official developer account. This is the account I use to log into the Developer Dashboard and register my application.

A simulated user account. This is the account that I used in the above demo to log into ARTIK Cloud on phone and to log into the My ARTIK Cloud web portal.

Follow these instructions to create an application. For this Android app, select the following:

Prepare source files and libraries

Use the client ID (obtained above, when registering the app in the Developer Dashboard) to replace YOUR CLIENT ID in ArtikCloudSession.java.

Now build the project, which will download the ARTIK Cloud SDK JAR from Maven Central Repository. Deploy the APK to an Android phone. You can play with the app like in the above demo.

Implementation

We need to implement the following functionalities:

Authenticate a user using the OAuth 2.0 workflow.

Collect user information such as username and user ID.

Connect a device to a user’s ARTIK Cloud account.

Delete a device from a user’s ARTIK Cloud account.

Authenticate with the third-party cloud.

Revoke authorization from the third party cloud.

For the first two functionalities, you can refer to the Your first Android app tutorial (“Login to obtain access token” and “Get user’s name from ARTIK Cloud”) for implementation details.

Add a Withings device to the user’s account

Clicking the “Add device” button creates a new device on the user’s ARTIK Cloud account. This device has the Cloud Connector device type “Withings Device”. Later on, this device will be authenticated with the Withings cloud.

To programmatically create a device, call the Create a Device REST API. The following method in DeviceActivity illustrates an easy way to do it using the SDK:

Authenticate with Withings cloud

After “My Withings” is added to the user’s ARTIK Cloud account, the user must authenticate this device with the Withings cloud and grant ARTIK Cloud the permission to access data on the Withings account. “My Withings” can then receive data from the user’s physical device via the Withings cloud.

To do this within the application (instead of My ARTIK Cloud), call the Authenticate a Cloud Connector device REST API. This call triggers a sequence of redirections between ARTIK Cloud, the Withings cloud, and the Android application. Refer to the API reference for various options to construct the HTTP request. For this implementation, we pack the access token and referer in the HTTP POST request.

After making the above call, the webview presents the login and permission screens to the user as in the demo. When the whole process finishes, ARTIK Cloud makes a callback to the link provided in the referer field of the HTTP request body. This is how the app is notified of success. In the following code, the app catches the redirection to the referer link.

Yes! Connect me with Samsung IoT partners to hear more about their products and services. *

* By checking either box, you may receive notifications by phone, email, text, and/or other electronic means from Samsung Semiconductor, Inc. and its affiliates. If you choose to receive partner notifications, we may forward your contact information to ourpartners. You may unsubscribe from these services at any time by clicking on the unsubscribe link in our communications or by submitting a request here. Please see our Privacy Policy and Terms of Use for more information about how your data is stored and used.