Develop for Android Things platform using Azure IoT SDKs
Azure IoT Hub SDKs provide first tier support for popular platforms such as Windows, Linux, OSX, MBED, and mobile platforms like Android and iOS. As part of our commitment to enable greater choice and flexibility in IoT deployments, the Java SDK also supports Android Things platform. Developers can leverage the benefits of Android Things operating system on the device side, while using Azure IoT Hub as the central message hub that scales to millions of simultaneously connected devices.
This tutorial outlines the steps to build a device side application on Android Things using the Azure IoT Java SDK.
An Android Things supported hardware with Android Things OS running. You can follow Android Things documentation on how to flash Android Things OS. Make sure your Android Things device is connected to the internet with essential peripherals such as keyboard, display, and mouse attached. This tutorial uses Raspberry Pi 3.
Latest version of Android Studio
Latest version of Git
Open Azure Cloud Shell
Azure Cloud Shell is a free, interactive shell that you can use to run the steps in this article. Common Azure tools are preinstalled and configured in Cloud Shell for you to use with your account. Select Copy to copy the code, paste it in Cloud Shell, and then press Enter to run it. There are a few ways to open Cloud Shell:
|Select Try It in the upper-right corner of a code block.|
|Open Cloud Shell in your browser.|
|Select the Cloud Shell button on the menu in the upper-right corner of the Azure portal.|
Create an IoT hub
This section describes how to create an IoT hub using the Azure portal.
Log in to the Azure portal.
Choose +Create a resource, then Search the Marketplace for the IoT Hub.
Select IoT Hub and click the Create button. You see the first screen for creating an IoT hub.
Fill in the fields.
Subscription: Select the subscription to use for your IoT hub.
Resource Group: You can create a new resource group or use an existing one. To create a new one, click Create new and fill in the name you want to use. To use an existing resource group, click Use existing and select the resource group from the dropdown list. For more information, see Manage Azure Resource Manager resource groups.
Region: This is the region in which you want your hub to be located. Select the location closest to you from the dropdown list.
IoT Hub Name: Put in the name for your IoT Hub. This name must be globally unique. If the name you enter is available, a green check mark appears.
The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while naming it.
Click Next: Size and scale to continue creating your IoT hub.
On this screen, you can take the defaults and just click Review + create at the bottom.
Pricing and scale tier: You can choose from several tiers depending on how many features you want and how many messages you send through your solution per day. The free tier is intended for testing and evaluation. It allows 500 devices to be connected to the IoT hub and up to 8,000 messages per day. Each Azure subscription can create one IoT Hub in the free tier.
IoT Hub units: The number of messages allowed per unit per day depends on your hub's pricing tier. For example, if you want the IoT hub to support ingress of 700,000 messages, you choose two S1 tier units.
For details about the other tier options, see Choosing the right IoT Hub tier.
Advanced / Device-to-cloud partitions: This property relates the device-to-cloud messages to the number of simultaneous readers of the messages. Most IoT hubs only need four partitions.
Click Review + create to review your choices. You see something similar to this screen.
Click Create to create your new IoT hub. Creating the hub takes a few minutes.
Register a device
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 commands in Azure Cloud Shell to add the IoT Hub CLI extension and to create the device identity.
YourIoTHubName : Replace this placeholder below with the name you choose for your IoT hub.
MyAndroidThingsDevice : This is the name given for the registered device. Use MyAndroidThingsDevice as shown. If you choose a different name for your device, you will also need to use that name throughout this article, and update the device name in the sample applications before you run them.
az extension add --name azure-cli-iot-ext az iot hub device-identity create --hub-name YourIoTHubName --device-id MyAndroidThingsDevice
Run the following commands in Azure Cloud Shell to get the device connection string for the device you just registered. Replace
YourIoTHubNamebelow with the name you choose for your IoT hub.
az iot hub device-identity show-connection-string --hub-name YourIoTHubName --device-id MyAndroidThingsDevice --output table
Make a note of the device connection string, which looks like:
You use this value later in the quickstart.
Building an Android Things application
The first step to building an Android Things application is connecting to your Android Things devices. Connect your Android Things device to a display and connect it to the internet. Android Things provide documentation on how to connect to WiFi. After you have connected to the internet, take a note of the IP address listed under Networks.
Use the adb tool to connect to your Android Things device with the IP address noted above. Double check the connection by using this command from your terminal. You should see your devices listed as "connected".
Download our sample for Android/Android Things from this repository or use Git.
git clone https://github.com/Azure-Samples/azure-iot-samples-java.git
In Android Studio, open the Android Project in located in "\azure-iot-samples-java\iot-hub\Samples\device\AndroidSample".
Open gradle.properties file, and replace "Device_connection_string" with your device connection string noted earlier.
Click on Run - Debug and select your device to deploy this code to your Android Things devices.
When the application is started successfully, you can see an application running on your Android Things device. This sample application sends randomly generated temperature readings.
Read the telemetry from your hub
You can view the data through your IoT hub as it is received. The IoT Hub CLI extension can connect to the service-side Events endpoint on your IoT Hub. The extension receives the device-to-cloud messages sent from your simulated device. An IoT Hub back-end application typically runs in the cloud to receive and process device-to-cloud messages.
Run the following commands in Azure Cloud Shell, replacing
YourIoTHubName with the name of your IoT hub:
az iot hub monitor-events --device-id MyAndroidThingsDevice --hub-name YourIoTHubName
Clean up resources
If you will be continuing to the next recommended article, you can keep the resources you've already created and reuse them.
Otherwise, you can delete the Azure resources created in this article to avoid charges.
Deleting a resource group is irreversible. The resource group and all the resources contained in it are permanently deleted. Make sure that you do not accidentally delete the wrong resource group or resources. If you created the IoT Hub inside an existing resource group that contains resources you want to keep, only delete the IoT Hub resource itself instead of deleting the resource group.
To delete a resource group by name:
Sign in to the Azure portal and click Resource groups.
In the Filter by name... textbox, type the name of the resource group containing your IoT Hub.
To the right of your resource group in the result list, click ... then Delete resource group.
You will be asked to confirm the deletion of the resource group. Type the name of your resource group again to confirm, and then click Delete. After a few moments, the resource group and all of its contained resources are deleted.
Send feedback about: