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

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();
主站蜘蛛池模板: 蒙城县| 大姚县| 同仁县| 孟村| 县级市| 枣庄市| 化德县| 黄龙县| 肥西县| 霍城县| 隆化县| 县级市| 海兴县| 诸暨市| 盐亭县| 奇台县| 霞浦县| 从江县| 时尚| 维西| 昭觉县| 万山特区| 开阳县| 浪卡子县| 白银市| 汾阳市| 阳曲县| 闻喜县| 方正县| 万荣县| 汉阴县| 福贡县| 巫溪县| 射洪县| 博乐市| 普定县| 平南县| 浦城县| 张家口市| 定结县| 西华县|