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

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();
主站蜘蛛池模板: 叶城县| 桃源县| 石渠县| 武宣县| 宜城市| 崇明县| 大丰市| 长白| 清涧县| 宝兴县| 金门县| 高邑县| 高要市| 黎城县| 大足县| 昆山市| 石泉县| 当涂县| 湖口县| 酒泉市| 富锦市| 东方市| 密云县| 余干县| 迭部县| 金平| 多伦县| 明光市| 彭泽县| 商城县| 韶山市| 马山县| 泾川县| 成武县| 玉林市| 确山县| 永春县| 乌兰察布市| 大竹县| 北碚区| 松原市|