/// <summary> /// Test Function: Send start / stop application direct method to all connected devices. /// </summary> /// <param name="packageFamily">Package Family of the UWP to start / stop.</param> /// <param name="isStart">"true" to start, "false" to stop/> instance containing the event data.</param> private async void CallDeviceMethodStartStopAll(string packageFamily, bool isStart) { DeviceTwinAndMethod tempDeviceTwin; AppxLifeCycleDataContract.ManageAppLifeCycleParams parameters = new AppxLifeCycleDataContract.ManageAppLifeCycleParams(); parameters.pkgFamilyName = packageFamily; if (isStart) { parameters.action = AppxLifeCycleDataContract.JsonStart; } else { parameters.action = AppxLifeCycleDataContract.JsonStop; } foreach (var device in _deviceList) { if (device.ConnectionState != DeviceConnectionState.Connected) { continue; //skip devices that are not connected to IoT Hub } tempDeviceTwin = new DeviceTwinAndMethod(App.IOTHUBCONNSTRING, device.Id); if (tempDeviceTwin != null) { CancellationToken cancellationToken = new CancellationToken(); DeviceMethodReturnValue result = await tempDeviceTwin.CallDeviceMethod(AppxLifeCycleDataContract.ManageAppLifeCycleAsync, parameters.ToJsonString(), new TimeSpan(0, 0, 30), cancellationToken); } } }
/// <summary> /// Send direct methods to device. /// </summary> /// <param name="method">The direct method function name.</param> /// <param name="payload">The direct method payload.</param> public async Task <string> CallDeviceMethod(string method, string payload) { CancellationToken cancellationToken = new CancellationToken(); DeviceMethodReturnValue result = await _deviceTwin.CallDeviceMethod(method, payload, new TimeSpan(0, 0, 30), cancellationToken); ShowDialogAsync("Call Device Method", "Request: " + method + "\n" + "Status: " + result.Status + "\nReason: " + result.Payload); return(result.Payload); }