使用 Azure IoT SDK 针对 Android Things 平台进行开发Develop for Android Things platform using Azure IoT SDKs

Azure IoT 中心 SDK 为 Windows、Linux、OSX、MBED 等流行平台,以及 Android 和 iOS 等移动平台提供第一层支持。Azure IoT Hub SDKs provide first tier support for popular platforms such as Windows, Linux, OSX, MBED, and mobile platforms like Android and iOS. 作为我们在 IoT 部署中实现更多选择和灵活性的承诺的一部分,Java SDK 还支持 Android Things 平台。As part of our commitment to enable greater choice and flexibility in IoT deployments, the Java SDK also supports Android Things platform. 使用 Azure IoT 中心作为可扩展到数百万个同时连接的设备的中央消息中心时,开发人员可以在设备端利用 Android Things 操作系统的好处。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.

本教程概述使用 Azure IoT Java SDK 在 Android Things 上构建设备端应用程序的步骤。This tutorial outlines the steps to build a device side application on Android Things using the Azure IoT Java SDK.

先决条件Prerequisites

  • 受 Android Things 支持且运行 Android Things OS 的硬件。An Android Things supported hardware with Android Things OS running. 可以按照有关如何刷新 Android Things OS 的 Android Things 文档进行操作。You can follow Android Things documentation on how to flash Android Things OS. 确保 Android Things 设备连接到 Internet 并连接了基本外围设备(例如键盘、显示器和鼠标)。Make sure your Android Things device is connected to the internet with essential peripherals such as keyboard, display, and mouse attached. 本教程使用 Raspberry Pi 3。This tutorial uses Raspberry Pi 3.
  • 最新版本的 Android StudioLatest version of Android Studio
  • 最新版本的 GitLatest version of Git

创建 IoT 中心Create an IoT hub

此部分介绍如何使用 Azure 门户创建 IoT 中心。This section describes how to create an IoT hub using the Azure portal.

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 从 Azure 主页中选择“+ 创建资源”按钮,然后在“搜索市场”字段中输入“IoT 中心”。 From the Azure homepage, select the + Create a resource button, and then enter IoT Hub in the Search the Marketplace field.

  3. 在搜索结果中选择“IoT 中心”,然后选择“创建” 。Select IoT Hub from the search results, and then select Create.

  4. 在“基本信息”选项卡上,按如下所示填写字段: On the Basics tab, complete the fields as follows:

    • 订阅:选择要用于中心的订阅。Subscription: Select the subscription to use for your hub.

    • 资源组:选择一个资源组或新建一个资源组。Resource Group: Select a resource group or create a new one. 若要新建资源组,请选择“新建”并填写要使用的名称。 To create a new one, select Create new and fill in the name you want to use. 若要使用现有的资源组,请选择它。To use an existing resource group, select that resource group. 有关详细信息,请参阅管理 Azure 资源管理器资源组For more information, see Manage Azure Resource Manager resource groups.

    • 区域:选择中心所在的区域。Region: Select the region in which you want your hub to be located. 选择最靠近你的位置。Select the location closest to you.

    • IoT 中心名称:输入中心的名称。IoT Hub Name: Enter a name for your hub. 此名称必须全局唯一。This name must be globally unique. 如果输入的名称可用,会显示一个绿色复选标记。If the name you enter is available, a green check mark appears.

    重要

    IoT 中心将公开为 DNS 终结点,因此,命名时请务必避免包含任何敏感信息。The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while naming it.

    在 Azure 门户中创建中心

  5. 在完成时选择“下一步: 大小和规模”,以继续创建中心。Select Next: Size and scale to continue creating your hub.

    使用 Azure 门户为新的中心设置大小和规模

    在此屏幕中可以设置以下值:This screen allows you to set the following values:

    • 定价和缩放层:选择的层。Pricing and scale tier: Your selected 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. 允许 500 台设备连接到中心,每天最多可传输 8,000 条消息。It allows 500 devices to be connected to the hub and up to 8,000 messages per day. 每个 Azure 订阅可以在免费层中创建一个 IoT 中心。Each Azure subscription can create one IoT Hub in the free tier.

    • IoT 中心单元:每个单位每日允许的消息数取决于中心的定价层。IoT Hub units: The number of messages allowed per unit per day depends on your hub's pricing tier. 例如,如果希望中心支持 700,000 条消息引入,请选择两个 S1 层单位。For example, if you want the hub to support ingress of 700,000 messages, you choose two S1 tier units. 有关其他层选项的详细信息,请参阅选择合适的 IoT 中心层For details about the other tier options, see Choosing the right IoT Hub tier.

    • 高级设置 > 设备到云的分区:此属性将设备到云消息与这些消息的同步读取器数目相关联。Advanced Settings > Device-to-cloud partitions: This property relates the device-to-cloud messages to the number of simultaneous readers of the messages. 大多数中心只需要 4 个分区。Most hubs need only four partitions.

  6. 对于本文,请接受默认选项,然后选择“查看 + 创建”以查看所做的选择。 For this article, accept the default choices, and then select Review + create to review your choices. 会显示类似于以下的屏幕。You see something similar to this screen.

    查看用于创建新中心的信息

  7. 选择“创建”以创建新的中心 。Select Create to create your new hub. 创建中心需要几分钟时间。Creating the hub takes a few minutes.

注册设备Register a device

必须先将设备注册到 IoT 中心,然后该设备才能进行连接。A device must be registered with your IoT hub before it can connect. 在本快速入门中,将使用 Azure CLI 来注册模拟设备。In this quickstart, you use the Azure CLI to register a simulated device.

  1. 运行以下命令,以添加 IoT 中心 CLI 扩展并创建设备标识。Run the following commands to add the IoT Hub CLI extension and to create the device identity.

    YourIoTHubName:将下面的占位符替换为你为 IoT 中心选择的名称。YourIoTHubName : Replace this placeholder below with the name you choose for your IoT hub.

    MyAndroidThingsDevice:这是为注册的设备提供的名称。MyAndroidThingsDevice : This is the name given for the registered device. 如图所示使用 MyAndroidThingsDevice。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
    
  2. 在 Azure CLI 中运行以下命令,以获取刚注册设备的设备连接字符串。Run the following commands in Azure CLI 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 MyAndroidThingsDevice --output table
    

    记下如下所示的设备连接字符串:Make a note of the device connection string, which looks like:

    HostName={YourIoTHubName}.azure-devices.cn;DeviceId=MyAndroidThingsDevice;SharedAccessKey={YourSharedAccessKey}

    稍后会在快速入门中用到此值。You use this value later in the quickstart.

构建 Android Things 应用程序Building an Android Things application

  1. 构建 Android Things 应用程序的第一步是连接到 Android Things 设备。The first step to building an Android Things application is connecting to your Android Things devices. 将 Android Things 设备连接到一个显示器,并将它连接到 Internet。Connect your Android Things device to a display and connect it to the internet. Android Things 提供了有关如何连接到 WiFi 的文档Android Things provide documentation on how to connect to WiFi. 连接到 Internet 之后,记下“网络”下列出的 IP 地址。After you have connected to the internet, take a note of the IP address listed under Networks.
  2. 使用 adb 工具,通过上面记下的 IP 地址连接到 Android Things 设备。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"
    adb devices
    
  3. 从此存储库下载我们适用于 Android/Android Things 的示例,或使用 Git。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
    
  4. 在 Android Studio 中,打开位于“\azure-iot-samples-java\iot-hub\Samples\device\AndroidSample”中的 Android 项目。In Android Studio, open the Android Project in located in "\azure-iot-samples-java\iot-hub\Samples\device\AndroidSample".
  5. 打开 gradle.properties 文件,将“Device_connection_string”替换为前面记下的设备连接字符串。Open gradle.properties file, and replace "Device_connection_string" with your device connection string noted earlier.
  6. 单击“运行 - 调试”,然后选择你的设备以将此代码部署到 Android Things 设备。Click on Run - Debug and select your device to deploy this code to your Android Things devices.
  7. 成功启动了应用程序后,可以看到应用程序在 Android Things 设备上运行。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

可通过 IoT 中心查看接收的数据。You can view the data through your IoT hub as it is received. IoT 中心 CLI 扩展可以连接到 IoT 中心上的服务端事件终结点。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. IoT 中心后端应用程序通常在云中运行,接收和处理设备到云的消息。An IoT Hub back-end application typically runs in the cloud to receive and process device-to-cloud messages.

运行以下命令,并将 YourIoTHubName 替换为 IoT 中心的名称:Run the following commands, 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.

否则,可删除本文中创建的 Azure 资源,避免收费。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. 如果在现有的包含要保留资源的资源组中创建了 IoT 中心,则只删除 IoT 中心资源本身,而不要删除资源组。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:

  1. 登录到 Azure 门户,然后选择“资源组”。 Sign in to the Azure portal and select Resource groups.

  2. 在“按名称筛选”文本框中,键入包含 IoT 中心的资源组的名称 。In the Filter by name textbox, type the name of the resource group containing your IoT Hub.

  3. 在结果列表中的资源组右侧,选择“...”,然后选择“删除资源组” 。To the right of your resource group in the result list, select ... then Delete resource group.

    Delete

  4. 系统会要求确认是否删除资源组。You will be asked to confirm the deletion of the resource group. 再次键入资源组的名称进行确认,然后选择“删除” 。Type the name of your resource group again to confirm, and then select Delete. 片刻之后,将会删除该资源组及其包含的所有资源。After a few moments, the resource group and all of its contained resources are deleted.

后续步骤Next steps