You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use the following device import statement to access the Azure IoT SDK for Java.
30
+
31
+
```java
32
+
importcom.microsoft.azure.sdk.iot.device.*;
33
+
```
25
34
26
35
### Connect to a device
27
36
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:
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
+
```
28
59
29
60
### Create a direct method callback
30
61
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 IoTHub 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.
deviceMethodData =newDeviceMethodData(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).
31
104
32
105
### SDK device samples
33
106
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
+
34
109
## Create a backend application
35
110
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.
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).
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.
*[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