- Azure IoT Development Cookbook
- Yatish Patil
- 77字
- 2021-07-02 20:50:37
How to do it...
The following steps will be taken to perform a direct method with an IoT device connected with IoT Hub:
- Invoke this method from the application:
public async Task<CloudToDeviceMethodResult> InvokeDirectMethodOnDevice(string deviceId, ServiceClient serviceClient)
{
var methodInvocation = new CloudToDeviceMethod("WriteToMessage") { ResponseTimeout = TimeSpan.FromSeconds(300) };
methodInvocation.SetPayloadJson("'1234567890'");
var response = await serviceClient.InvokeDeviceMethodAsync(deviceId, methodInvocation);
return response;
}
- Direct method execution on the IoT device:
deviceClient = DeviceClient.CreateFromConnectionString("", TransportType.Mqtt);
deviceClient.SetMethodHandlerAsync("WriteToMessage", new DeviceSimulator().WriteToMessage, null).Wait();
deviceClient.SetMethodHandlerAsync("GetDeviceName", new DeviceSimulator().GetDeviceName, new DeviceData("DeviceClientMethodMqttSample")).Wait();