您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

用于 Java 的 Azure IoT 库Azure IoT libraries for Java

使用 Azure IoT 中心连接、监视和控制物联网资产。Connect, monitor, and control Internet of Things assets with Azure IoT Hub.

若要开始使用 Azure IoT 中心,请参阅使用 Java 将设备连接到 IoT 中心To get started with Azure IoT Hub, see Connect your device to your IoT hub using Java.

IoT 服务库IoT Service library

使用 IoT 服务库注册设备并将消息从云发送到已注册的设备。Register devices and send messages from the cloud to registered devices using the IoT Service library.

向 Maven pom.xml 文件中添加依赖项,以便在项目中使用客户端库。Add a dependency to your Maven pom.xml file to use the client library in your project.

<dependency>
    <groupId>com.microsoft.azure.sdk.iot</groupId>
    <artifactId>iot-service-client</artifactId>
    <version>1.6.23</version>
</dependency>

Iot 设备库Iot Device library

使用 IoT 设备库将消息发送到云中,以及在设备上接收消息。Send messages to the cloud and receive messages on devices using the IoT Device library.

向 Maven pom.xml 文件中添加依赖项,以便在项目中使用客户端库。Add a dependency to your Maven pom.xml file to use the client library in your project.

<dependency>
    <groupId>com.microsoft.azure.sdk.iot</groupId>
    <artifactId>iot-device-client</artifactId>
    <version>1.3.31</version>
</dependency>

示例Example

将消息从 Azure IoT 中心发送到设备。Send a message from Azure IoT Hub to a device.

Message messageToSend = new Message(messageText);
messageToSend.setDeliveryAcknowledgement(DeliveryAcknowledgement.Full);
messageToSend.setMessageId(java.util.UUID.randomUUID().toString());

// set message properties
Map<String, String> propertiesToSend = new HashMap<String, String>();
propertiesToSend.put(messagePropertyKey,messagePropertyKey);
messageToSend.setProperties(propertiesToSend);

CompletableFuture<Void> future = serviceClient.sendAsync(deviceId, messageToSend);
try {
    future.get();
}
catch (ExecutionException e) {
    System.out.println("Exception : " + e.getMessage());
}

示例Samples

IoT 设备示例 IoT Device samples
IoT 服务示例IoT Service samples

详细了解可在应用中使用的 Azure IoT 示例 Java 代码Explore more sample Java code for Azure IoT you can use in your apps.