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

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();
主站蜘蛛池模板: 沁源县| 高陵县| 绍兴县| 宁德市| 嘉荫县| 青田县| 永寿县| 宣城市| 西乡县| 江山市| 衡南县| 肥乡县| 雷山县| 阿克| 梅州市| 南郑县| 介休市| 诸城市| 德保县| 元朗区| 江门市| 汽车| 永泰县| 寿宁县| 明星| 定日县| 时尚| 高尔夫| 高陵县| 监利县| 颍上县| 台安县| 牡丹江市| 旌德县| 河北省| 富裕县| 文安县| 白沙| 离岛区| 惠水县| 长岭县|