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 [SetMethodHandlerAsync](/dotnet/api/microsoft.azure.devices.client.deviceclient.setmethodhandlerasync) 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.
@@ -135,23 +117,14 @@ The Azure IoT SDK for .NET provides working samples of device apps that handle d
135
117
136
118
## Create a backend application
137
119
138
-
This section describes how to trigger a direct method on a device and then use device twin queries to monitor the status of that device.
120
+
This section describes how to trigger a direct method on a device.
139
121
140
122
The [ServiceClient](/dotnet/api/microsoft.azure.devices.serviceclient) class exposes all methods required to create a backend application to send direct method calls to devices.
141
123
142
124
### Required service NuGet package
143
125
144
126
Backend service applications require the **Microsoft.Azure.Devices** NuGet package.
145
127
146
-
### Using statements
147
-
148
-
Add the following `using` statements.
149
-
150
-
```csharp
151
-
usingMicrosoft.Azure.Devices;
152
-
usingMicrosoft.Azure.Devices.Shared;
153
-
```
154
-
155
128
### Connect to IoT hub
156
129
157
130
Connect a backend application using [CreateFromConnectionString](/dotnet/api/microsoft.azure.devices.serviceclient.createfromconnectionstring?#microsoft-azure-devices-serviceclient-createfromconnectionstring(system-string-microsoft-azure-devices-serviceclientoptions)).
@@ -163,7 +136,6 @@ As a parameter to `CreateFromConnectionString`, supply the **service** shared ac
Console.WriteLine("Invoked firmware update on device.");
191
163
```
192
164
193
-
This example gets the device twin for the rebooting device and outputs the reported properties. This output shows that the `onReboot` callback method updated the `lastReboot`, `Reboot`, and `iothubDM` reported properties.
Copy file name to clipboardExpand all lines: includes/iot-hub-howto-device-management-java.md
+10-44Lines changed: 10 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -61,44 +61,19 @@ To connect to a device:
61
61
62
62
### Create a direct method callback
63
63
64
-
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.
64
+
Call [subscribeToMethods](https://azure.github.io/azure-iot-sdk-java/master/device/com/microsoft/azure/sdk/iot/device/InternalClient.html#subscribeToMethods-com.microsoft.azure.sdk.iot.device.twin.MethodCallback-java.lang.Object-int-) to initialize a direct method callback listener. `subscribeToMethods` listens for incoming direct methods until the connection is terminated. The method name and payload is received for each direct method call.
65
65
66
-
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);
97
-
}
98
-
}
99
-
return deviceMethodData;
100
-
}
101
-
}
69
+
client.subscribeToMethods(
70
+
(methodName, methodData, context) ->
71
+
{
72
+
System.out.println("Received a direct method invocation with name "+ methodName +" and payload "+ methodData.getPayloadAsJsonString());
73
+
returnnewDirectMethodResponse(200, methodData);
74
+
},
75
+
null);
76
+
System.out.println("Successfully subscribed to direct methods");
102
77
```
103
78
104
79
> [!NOTE]
@@ -114,15 +89,6 @@ This section describes how to initiate a remote reboot on a device using a direc
114
89
115
90
The `ServiceClient` [DeviceMethod](/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.devicemethod) classcontains methods that services can use to access device twins.
116
91
117
-
### Service import statements
118
-
119
-
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.
0 commit comments