官术网_书友最值得收藏!

How to do it...

The following steps will be taken to perform a direct method with an IoT device connected with IoT Hub:

  1. 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;
}
  1. 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();
主站蜘蛛池模板: 泉州市| 老河口市| 富源县| 景洪市| 油尖旺区| 富民县| 鸡西市| 秦安县| 同心县| 台山市| 明溪县| 石柱| 黄骅市| 吴忠市| 科技| 广水市| 昆明市| 贵德县| 绥滨县| 和平县| 娱乐| 馆陶县| 松江区| 仪陇县| 高陵县| 天祝| 句容市| 金沙县| 雷山县| 泾川县| 同仁县| 门源| 宁强县| 丘北县| 九江县| 淮南市| 嵊州市| 海宁市| 贡觉县| 汝城县| 新建县|