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

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();
主站蜘蛛池模板: 德州市| 揭东县| 东港市| 老河口市| 孝义市| 茌平县| 清流县| 莒南县| 双峰县| 赣榆县| 山丹县| 阿勒泰市| 玉溪市| 江口县| 定兴县| 炉霍县| 安阳市| 辽源市| 麻阳| 盐城市| 类乌齐县| 江陵县| 合阳县| 绍兴县| 杂多县| 万源市| 嘉祥县| 穆棱市| 石棉县| 英吉沙县| 福州市| 长武县| 黄大仙区| 长春市| 儋州市| 正蓝旗| 东明县| 民权县| 沙洋县| 镶黄旗| 沁源县|