title | description | author | manager | ms.service | services | ms.devlang | ms.topic | ms.custom | ms.date | ms.author | |||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Send telemetry to Azure IoT Hub quickstart (Android) | Microsoft Docs |
In this quickstart, you run a sample Android application to send simulated telemetry to an IoT hub and to read telemetry from the IoT hub for processing in the cloud. |
wesmc7777 |
philmea |
iot-hub |
iot-hub |
java |
quickstart |
|
03/15/2019 |
wesmc |
[!INCLUDE iot-hub-quickstarts-1-selector]
In this quickstart, you send telemetry to an Azure IoT Hub from an Android application running on a physical or simulated device. IoT Hub is an Azure service that enables you to ingest high volumes of telemetry from your IoT devices into the cloud for storage or processing. This quickstart uses a pre-written Android application to send the telemetry. The telemetry will be read from the IoT Hub using the Azure Cloud Shell. Before you run the application, you create an IoT hub and register a device with the hub.
-
An Azure account with an active subscription. Create one for free.
-
Android Studio with Android SDK 27. For more information, see android-installation. Android SDK 27 is used by the sample in this article.
-
A sample Android application. This sample is part of the azure-iot-samples-java repository.
-
Port 8883 open in your firewall. The device sample in this quickstart uses MQTT protocol, which communicates over port 8883. This port may be blocked in some corporate and educational network environments. For more information and ways to work around this issue, see Connecting to IoT Hub (MQTT).
[!INCLUDE cloud-shell-try-it.md]
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]
[!INCLUDE iot-hub-include-create-hub]
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.
-
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'll 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
-
Run the following command 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 chose 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'll use this value later in this quickstart to send telemetry.
-
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.
-
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}
-
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.
-
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.
-
Once the app loads, click the Start button to start sending telemetry to your IoT Hub:
In this section, you will use the Azure Cloud Shell with the IoT extension to monitor the device messages that are sent by the Android device.
-
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 chose 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:
[!INCLUDE iot-hub-quickstarts-clean-up-resources]
In this quickstart, you set up an IoT hub, registered a device, sent simulated telemetry to the hub using an Android application, and read the telemetry from the hub using the Azure Cloud Shell.
To learn how to control your simulated device from a back-end application, continue to the next quickstart.
[!div class="nextstepaction"] Quickstart: Control a device connected to an IoT hub