Skip to content

Latest commit

 

History

History
200 lines (121 loc) · 13 KB

quickstart-control-device-android.md

File metadata and controls

200 lines (121 loc) · 13 KB
title description author manager ms.service services ms.devlang ms.topic ms.custom ms.date ms.author
Control a device from Azure IoT Hub quickstart (Android) | Microsoft Docs
In this quickstart, you run two sample Java applications. One application is a service application that can remotely control devices connected to your hub. The other application runs on a physical or simulated device connected to your hub that can be controlled remotely.
wesmc7777
philmea
iot-hub
iot-hub
java
quickstart
mvc
mqtt
devx-track-java
06/21/2019
wesmc

Quickstart: Control a device connected to an IoT hub (Android)

[!INCLUDE iot-hub-quickstarts-2-selector]

In this quickstart, you use a direct method to control a simulated device connected to Azure IoT Hub. IoT Hub is an Azure service that enables you to manage your IoT devices from the cloud and ingest high volumes of device telemetry to the cloud for storage or processing. You can use direct methods to remotely change the behavior of a device connected to your IoT hub. This quickstart uses two applications: a simulated device application that responds to direct methods called from a back-end service application and a service application that calls the direct method on the Android device.

Prerequisites

[!INCLUDE cloud-shell-try-it.md]

Add Azure IoT Extension

Run the following command to add the Microsoft Azure IoT Extension for Azure CLI to your Cloud Shell instance. The IoT Extension adds IoT Hub, IoT Edge, and IoT Device Provisioning Service (DPS) specific commands to Azure CLI.

az extension add --name azure-iot

[!INCLUDE iot-hub-cli-version-info]

Create an IoT hub

If you completed the previous Quickstart: Send telemetry from a device to an IoT hub, you can skip this step and use the IoT hub you have already created.

[!INCLUDE iot-hub-include-create-hub]

Register a device

If you completed the previous Quickstart: Send telemetry from a device to an IoT hub, you can skip this step and use the same device registered in the previous quickstart.

A device must be registered with your IoT hub before it can connect. In this quickstart, you use the Azure Cloud Shell to register a simulated device.

  1. Run the following command in Azure Cloud Shell to create the device identity.

    YourIoTHubName: Replace this placeholder below with the name you chose for your IoT hub.

    MyAndroidDevice: This is the name of the device you're registering. It's recommended to use MyAndroidDevice as shown. If you choose a different name for your device, you also need to use that name throughout this article, and update the device name in the sample applications before you run them.

    az iot hub device-identity create \
      --hub-name {YourIoTHubName} --device-id MyAndroidDevice
    
  2. Run the following commands in Azure Cloud Shell to get the device connection string for the device you just registered:

    YourIoTHubName: Replace this placeholder below with the name you choose for your IoT hub.

    az iot hub device-identity show-connection-string \
      --hub-name {YourIoTHubName} \
      --device-id MyAndroidDevice \
      --output table
    

    Make a note of the device connection string, which looks like:

    HostName={YourIoTHubName}.azure-devices.net;DeviceId=MyAndroidDevice;SharedAccessKey={YourSharedAccessKey}

    You use this value later in the quickstart.

Retrieve the service connection string

You also need a service connection string to enable the back-end service applications to connect to your IoT hub in order to execute methods and retrieve messages. The following command retrieves the service connection string for your IoT hub:

YourIoTHubName: Replace this placeholder below with the name you chose for your IoT hub.

az iot hub show-connection-string --policy-name service --name {YourIoTHubName} --output table

Make a note of the service connection string, which looks like:

HostName={YourIoTHubName}.azure-devices.net;SharedAccessKeyName=service;SharedAccessKey={YourSharedAccessKey}

You use this value later in the quickstart. This service connection string is different from the device connection string you noted in the previous step.

Listen for direct method calls

Both of the samples for this quickstart are part of the azure-iot-samples-java repository on GitHub. Download or clone the azure-iot-samples-java repository.

The device SDK sample application can be run on a physical Android device or an Android emulator. The sample connects to a device-specific endpoint on your IoT hub, sends simulated telemetry, and listens for direct method calls from your hub. In this quickstart, the direct method call from the hub tells the device to change the interval at which it sends telemetry. The simulated device sends an acknowledgment back to your hub after it executes the direct method.

  1. Open the GitHub sample Android project in Android Studio. The project is located in the following directory of your cloned or downloaded copy of azure-iot-sample-java repository: \azure-iot-samples-java\iot-hub\Samples\device\AndroidSample.

  2. In Android Studio, open gradle.properties for the sample project and replace the Device_Connection_String placeholder with the device connection string you made a note of earlier.

    DeviceConnectionString=HostName={YourIoTHubName}.azure-devices.net;DeviceId=MyAndroidDevice;SharedAccessKey={YourSharedAccessKey}
    
  3. In Android Studio, click File > Sync Project with Gradle Files. Verify the build completes.

    [!NOTE] If the project sync fails, it may be for one of the following reasons:

    • The versions of the Android Gradle plugin and Gradle referenced in the project are out of date for your version of Android Studio. Follow these instructions to reference and install the correct versions of the plugin and Gradle for your installation.
    • The license agreement for the Android SDK has not been signed. Follow the instructions in the Build output to sign the license agreement and download the SDK.
  4. Once the build has completed, click Run > Run 'app'. Configure the app to run on a physical Android device or an Android emulator. For more information on running an Android app on a physical device or emulator, see Run your app.

  5. Once the app loads, click the Start button to start sending telemetry to your IoT Hub:

    Sample screenshot of client device android app

This app needs to be left running on a physical device or emulator while you execute the service SDK sample to update the telemetry interval during run-time.

Read the telemetry from your hub

In this section, you will use the Azure Cloud Shell with the IoT extension to monitor the messages that are sent by the Android device.

  1. Using the Azure Cloud Shell, run the following command to connect and read messages from your IoT hub:

    YourIoTHubName: Replace this placeholder below with the name you choose for your IoT hub.

    az iot hub monitor-events --hub-name {YourIoTHubName} --output table
    

    The following screenshot shows the output as the IoT hub receives telemetry sent by the Android device:

    Read the device messages using the Azure CLI

By default, the telemetry app sends telemetry from the Android device every five seconds. In the next section, you will use a direct method call to update the telemetry interval for the Android IoT device.

Call the direct method

The service application connects to a service-side endpoint on your IoT Hub. The application makes direct method calls to a device through your IoT hub and listens for acknowledgments.

Run this app on a separate physical Android device or Android emulator.

An IoT Hub back-end service application typically runs in the cloud, where it's easier to mitigate the risks associated with the sensitive connection string that controls all devices on an IoT Hub. In this example, we are running it as an Android app for demonstration purposes only. The other-language versions of this quickstart provide examples that align more closely with a typical back-end service application.

  1. Open the GitHub service sample Android project in Android Studio. The project is located in the following directory of your cloned or downloaded copy of azure-iot-sample-java repository: \azure-iot-samples-java\iot-hub\Samples\service\AndroidSample.

  2. In Android Studio, open gradle.properties for the sample project. Update the values for the ConnectionString and DeviceId properties with the service connection string you noted earlier and the Android device ID you registered.

    ConnectionString=HostName={YourIoTHubName}.azure-devices.net;SharedAccessKeyName=service;SharedAccessKey={YourSharedAccessKey}
    DeviceId=MyAndroidDevice
    
  3. In Android Studio, click File > Sync Project with Gradle Files. Verify the build completes.

    [!NOTE] If the project sync fails, it may be for one of the following reasons:

    • The versions of the Android Gradle plugin and Gradle referenced in the project are out of date for your version of Android Studio. Follow these instructions to reference and install the correct versions of the plugin and Gradle for your installation.
    • The license agreement for the Android SDK has not been signed. Follow the instructions in the Build output to sign the license agreement and download the SDK.
  4. Once the build has completed, click Run > Run 'app'. Configure the app to run on a separate physical Android device or an Android emulator. For more information on running an Android app on a physical device or emulator, see Run your app.

  5. Once the app loads, update the Set Messaging Interval value to 1000 and click Invoke.

    Th telemetry messaging interval is in milliseconds. The default telemetry interval of the device sample is set for 5 seconds. This change will update the Android IoT device so that telemetry is sent every second.

    Enter telemetry interval

  6. The app will receive an acknowledgment indicating whether the method executed successfully or not.

    Direct Method acknowledgment

Clean up resources

[!INCLUDE iot-hub-quickstarts-clean-up-resources]

Next steps

In this quickstart, you called a direct method on a device from a back-end application, and responded to the direct method call in a simulated device application.

To learn how to route device-to-cloud messages to different destinations in the cloud, continue to the next tutorial.

[!div class="nextstepaction"] Tutorial: Route telemetry to different endpoints for processing