Oracle by Example brandingQuick Start Tutorial Using an Android Application

section 0Before 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?


section 1Send Your First Message

  1. Download the most recent Android Client Software Library binaries from Oracle Technology Network.
  2. Extract the library. Note the location of the device-library.aar file. This tutorial uses iotcs\csl\android\lib\device-library.aar.
  3. 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.

    Your IoTAndroidHelloWorld project opens in Android Studio.

  4. In the project explorer window, for your project, expand Gradle Scripts and select build.gradle (Module: app).
    open build file
    Description of the illustration build_01.png
  5. Modify the build.gradle file:
    • To provide the path of the device-library.aar file, add a flatDir closure.
    • Remove the dependency for junit.
    • Add a dependency for device-libray.aar.
    Shows content of build file
    Description of the illustration build_02.png
  6. Copy and replace the MainActivity.java file that you can find in the pre-requisites into the app/src/main/java/iot/myandroid/iotandroidhelloworld/ directory.
  7. 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"/>
    Shows content of manifest file
    Description of the illustration build_03.png
  8. In your file browser window, go to your IoTAndroidHelloWorld project. Under the app/src/main directory, create a folder named assets and an empty file named trustedAssets.properties.
  9. 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.
  10. Add the following lines to the trustedAssets.properties file and save it; replace provisioning.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
  11. 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.
  12. Run the IoTAndroidHelloWorld project in an emulator.
    Application running in emulator
    Description of the illustration run_01.png

section 2Verify Your Message

  1. Sign in to your Oracle IoT Cloud Service instance.
  2. 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.
  3. Verify that the Messages table contains your message.
    Shows the Messages page displaying the new message
    Description of the illustration messages_table.png

more informationWant to Learn More?