Skip to content

Commit 6d7dbcf

Browse files
committed
Added Java article
1 parent 5a7925d commit 6d7dbcf

File tree

1 file changed

+116
-1
lines changed

1 file changed

+116
-1
lines changed

includes/iot-hub-howto-device-management-java.md

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,138 @@ This section describes how to use device application code to:
2121

2222
* Respond to a direct method called by the cloud
2323
* Trigger a simulated device reboot
24-
* Use the reported properties to enable device twin queries to identify devices and when they were last rebooted
24+
25+
[!INCLUDE [iot-authentication-device-connection-string.md](iot-authentication-device-connection-string.md)]
26+
27+
### Device import statements
28+
29+
Use the following device import statement to access the Azure IoT SDK for Java.
30+
31+
```java
32+
import com.microsoft.azure.sdk.iot.device.*;
33+
```
2534

2635
### Connect to a device
2736

37+
The [DeviceClient](/java/api/com.microsoft.azure.sdk.iot.device.deviceclient) class exposes all the methods you require to interact with direct methods from the device.
38+
39+
To connect to a device:
40+
41+
1. Use [IotHubClientProtocol](/java/api/com.microsoft.azure.sdk.iot.device.iothubclientprotocol) to choose a transport protocol. For example:
42+
43+
```java
44+
IotHubClientProtocol protocol = IotHubClientProtocol.MQTT;
45+
```
46+
47+
1. Use the `DeviceClient` constructor to add the device primary connection string and protocol.
48+
49+
```java
50+
String connString = "{IoT hub device connection string}";
51+
DeviceClient client = new DeviceClient(connString, protocol);
52+
```
53+
54+
1. Use [open](/java/api/com.microsoft.azure.sdk.iot.device.deviceclient?#com-microsoft-azure-sdk-iot-device-deviceclient-open()) to connect the device to IoT hub. If the client is already open, the method does nothing.
55+
56+
```java
57+
client.open(true);
58+
```
2859

2960
### Create a direct method callback
3061

62+
Call [subscribeToDeviceMethod](/java/api/com.microsoft.azure.sdk.iot.device.deviceclient?#com-microsoft-azure-sdk-iot-device-deviceclient-subscribetodevicemethod(com-microsoft-azure-sdk-iot-device-devicetwin-devicemethodcallback-java-lang-object-com-microsoft-azure-sdk-iot-device-iothubeventcallback-java-lang-object)) to initialize a direct method callback listener. The listener is associated with a method name keyword, such as "reboot". The method name can be used in an IoT Hub or backend application to trigger the callback method on the device.
63+
64+
This example sets up a callback listener named `reboot` that will trigger when the "reboot" direct method name is called.
65+
66+
```java
67+
client.subscribeToDeviceMethod(new DirectMethodCallback(), null, new DirectMethodStatusCallback(), null);
68+
```
69+
70+
In this example, the `DirectMethodCallback` callback method implements the direct method on the device.
71+
72+
```java
73+
protected static class DirectMethodCallback implements com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceMethodCallback
74+
{
75+
@Override
76+
public DeviceMethodData call(String methodName, Object methodData, Object context)
77+
{
78+
DeviceMethodData deviceMethodData;
79+
switch (methodName)
80+
{
81+
case "reboot" :
82+
{
83+
int status = METHOD_SUCCESS;
84+
System.out.println("Received reboot request");
85+
deviceMethodData = new DeviceMethodData(status, "Started reboot");
86+
RebootDeviceThread rebootThread = new RebootDeviceThread();
87+
Thread t = new Thread(rebootThread);
88+
t.start();
89+
break;
90+
}
91+
default:
92+
{
93+
int status = METHOD_NOT_DEFINED;
94+
deviceMethodData = new DeviceMethodData(status, "Not defined direct method " + methodName);
95+
}
96+
}
97+
return deviceMethodData;
98+
}
99+
}
100+
```
101+
102+
> [!NOTE]
103+
> To keep things simple, this article does not implement any retry policy. In production code, you should implement retry policies (such as an exponential backoff), as suggested in [Transient fault handling](/azure/architecture/best-practices/transient-faults).
31104
32105
### SDK device samples
33106

107+
The Azure IoT SDK for Java includes a working sample to test the device app concepts described in this article. For more information, see [Direct Method Sample](https://github.com/Azure/azure-iot-sdk-java/tree/main/iothub/device/iot-device-samples/direct-method-sample).
108+
34109
## Create a backend application
35110

111+
This section describes how to initiate a remote reboot on a device using a direct method.
112+
113+
The `ServiceClient` [DeviceMethod](/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.devicemethod) class contains methods that services can use to access device twins.
114+
115+
### Service import statements
116+
117+
Use the following service import statements to access the Azure IoT SDK for Java.
118+
119+
```java
120+
import com.microsoft.azure.sdk.iot.service.devicetwin.*;
121+
import com.microsoft.azure.sdk.iot.service.exceptions.IotHubException;
122+
```
36123

37124
### Connect to IoT hub
38125

126+
Use the [DeviceMethod](/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.devicemethod?#com-microsoft-azure-sdk-iot-service-devicetwin-devicemethod-devicemethod(java-lang-string)) constructor to add the service primary connection string and connect to IoT Hub.
127+
128+
To invoke a direct method on a device through IoT Hub, your service needs the **service connect** permission. By default, every IoT Hub is created with a shared access policy named **service** that grants this permission.
129+
130+
As a parameter to the `DeviceMethod` constructor, supply the **service** shared access policy. For more information about shared access policies, see [Control access to IoT Hub with shared access signatures](/azure/iot-hub/authenticate-authorize-sas).
131+
132+
For example:
133+
134+
```java
135+
public static final String iotHubConnectionString = "HostName=xxxxx.azure-devices.net;SharedAccessKeyName=service;SharedAccessKey=xxxxxxxxxxxxxxxxxxxxxxxx";
136+
DeviceMethod methodClient = new DeviceMethod(iotHubConnectionString);
137+
```
39138

40139
### Invoke a method on a device
41140

141+
Call [invoke](/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.devicemethod?#method-details) to invoke a method on a device and return its result.
142+
143+
This example calls the "reboot" method to initiate a reboot on the device. The "reboot" method is mapped to a listener on the device as described in the **Create a direct method callback** section of this article.
144+
145+
```java
146+
public static final String deviceId = "myFirstDevice2";
147+
private static final String methodName = "reboot";
148+
private static final Long responseTimeout = TimeUnit.SECONDS.toSeconds(30);
149+
private static final Long connectTimeout = TimeUnit.SECONDS.toSeconds(5);
150+
MethodResult result = methodClient.invoke(deviceId, methodName, responseTimeout, connectTimeout, null);
151+
```
152+
42153
### SDK service samples
43154

155+
The Azure IoT SDK for Java provides a working sample of service apps that handle direct method tasks. For more information, see:
156+
157+
* [Direct Method Sample](https://github.com/Azure/azure-iot-service-sdk-java/tree/main/service/iot-service-samples/direct-method-sample).
158+
* [Thermostat service sample](https://github.com/Azure/azure-iot-service-sdk-java/blob/aeea7806be7e894d8a977c16b7e6618728267a94/service/iot-service-samples/pnp-service-sample/thermostat-service-sample/src/main/java/samples/com/microsoft/azure/sdk/iot/service/Thermostat.java#L69)

0 commit comments

Comments
 (0)