Before You Begin
This 20-minute tutorial shows you how to build an Android application that simulates a virtual device and sends a message to Oracle Internet of Things (IoT) Cloud Service.
Background
Oracle IoT Cloud Service provides communication services between devices and enterprise applications. In this tutorial, you build a simulated device in an Android application, and then transmit a Hello World message to Oracle IoT Cloud Service.
The application runs in any Android device with Android KitKat 4.4 or later.
What Do You Need?
- Access to an Oracle IoT Cloud Service instance
- A computer running Windows 7, Mac OS 10.9 or later, or Linux with a web browser and a recent version of Java Development Kit (JDK)
- Installed software:
- Java Standard Edition Development Kit (Java SE JDK) 6.0 or later
- Android Studio (in this tutorial we use Windows version)
- Intel Hardware Accelerated Execution Manager (HAXM)
- Oracle Client Software Library for Android
- This Java code file:
MainActivity.java
- Experience with Android programming in Android Studio, good knowledge of Java, basic knowledge of command-line tools
- Complete the following tutorial: Configuring an Application in Oracle Internet of Things Cloud Service. You will need the same spreadsheet used to keep track of your data, and the Provisioning file, obtained in that tutorial
Send Your First Message
- Download the most recent Android Client Software Library binaries from Oracle Technology Network.
-
Extract the library. Note the location of the
device-library.aar
file. This tutorial usesiotcs\csl\android\lib\device-library.aar.
- Open Android Studio (in this tutorial we use Windows version). In the Create New Project wizard, create a new project and enter or select the following values:
- Application name:
IoTAndroidHelloWorld
- Company domain:
myandroid.iot
- Project location:
C:\Users\username\Documents\iotcs\csl\android\samples\IoTAndroidHelloWorld
Note: If needed, create the
samples
folder. - Select Phone and Tablet: Minimum SDK:
API 19: Android: 4.4 (KitKat) and click Next.
- Select Empty Activity. Leave the default values for the activity and layout names and click Finish.
- In Configure Activity type mainactivity in Activity Name and click Finish.
- In the project explorer window, for your project, expand Gradle Scripts and select build.gradle (Module: app).
Description of the illustration build_01.png - Modify the
build.gradle
file:- To provide the path of the
device-library.aar
file, add aflatDir
closure. - Remove the dependency for
junit.
- Add a dependency for
device-libray.aar.
Description of the illustration build_02.png - To provide the path of the
- Copy and replace the
MainActivity.java
file that you can find in the pre-requisites into theapp/src/main/java/iot/myandroid/iotandroidhelloworld/
directory. -
In Android Studio, go to app then manifests and then click on
AndroidManifest.xml
to include:<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>Description of the illustration build_03.png -
In your file browser window, go to your IoTAndroidHelloWorld project. Under the
app/src/main
directory, create a folder namedassets
and an empty file namedtrustedAssets.properties.
-
Copy the Provisioning file obtained from the tutorial in the pre-requisites (Configuring an Application in Oracle Internet of Things Cloud Service) into the
assets
folder. -
Add the following lines to the
trustedAssets.properties
file and save it; replaceprovisioning.file
with the name of the file and note the File_Protection_Password is the one you used to download it:oracle.iot.client.trustedAssetsStore=Provisioning.file oracle.iot.client.trustedAssetsStorePassword=File_Protection_Password oracle.iot.client.trustedAssetsStoreRw=Provisioning.file oracle.iot.client.trustedAssetsStoreRwPassword=File_Protection_Password
-
In Android Studio, build the IoTAndroidHelloWorld project and ensure that it builds successfully. Verify that the
app-debug.apk
file is generated at..\IoTAndroidHelloWorld\app\build\outputs\apk.
- Run the IoTAndroidHelloWorld project in an emulator.
Description of the illustration run_01.png
Your IoTAndroidHelloWorld project opens in Android Studio.
Verify Your Message
- Sign in to your Oracle IoT Cloud Service instance.
- In the Oracle IoT Cloud Service Management Console, navigate to the Oracle IoT Asset Monitoring application, select Data, and then in Filter By, select Device.
- Verify that the Messages table contains your message.
Description of the illustration messages_table.png