菜鸟教程小白 发表于 2022-12-13 05:37:07

ios - 如何使用 azure iot sdk 在 ios 中调用设备方法


                                            <p><p>我正在尝试使用连接字符串调用与设备关联的方法。
我尝试使用其他语言提供的示例,我可以在设备中调用该方法。例如:灯的“setState”或“getState”。
但我无法使用 swift 在 iOS 中实现。</p>

<p>我尝试通过引用C示例来匹配参数参数要求。但我越来越
1. Func:sendHttpRequestDeviceMethod Line:337 Http失败状态码400。
2. Func:IoTHubDeviceMethod_Invoke Line:492 发送设备方法调用HTTP请求失败</p>

<pre><code>   var status :Int32! = 0
   var deviceId = &#34;simulated_device_one&#34;;
   varmethodName = &#34;GetState&#34;;
    var uint8Pointer:UnsafeMutablePointer&lt;UInt8&gt;!
    uint8Pointer = UnsafeMutablePointer&lt;UInt8&gt;.allocate(capacity:8)

    var size = size_t(10000)
    var bytes: =
    uint8Pointer?.initialize(from: &amp;bytes, count: 8)
    var intValue : UnsafeMutablePointer&lt;UInt8&gt;?
    intValue = UnsafeMutablePointer(uint8Pointer)
    var char: UInt8 =UInt8(20)
    var charPointer = UnsafeMutablePointer&lt;UInt8&gt;(&amp;char)
    var prediction = intValue
    let serviceClientDeviceMethodHandle =IoTHubDeviceMethod_Create(service_client_handle)

    let payLoad = &#34;test&#34;

    var responsePayload = &#34;&#34;


    let invoke = IoTHubDeviceMethod_Invoke(serviceClientDeviceMethodHandle, deviceId, methodName, payLoad , 100, &amp;status, &amp;prediction,&amp;size )
</code></pre>

<p>我想使用 IoTHubDeviceMethod_Invoke 调用设备中的方法</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以下载我处理过的 View Controller 文件 <a href="https://drive.google.com/file/d/1TUB1J9Gc-Rxsu0tR9HlOcsfQU4J5fbdO/view?usp=sharing" rel="noreferrer noopener nofollow">from here</a> </p>

<p>1.在 View 中创建连接确实加载了</p>

<pre><code>// declaring your connection string you can find it in azure iot dashboard
private let connectionString = &#34;Enter your connection String&#34;;


// creating service handler
private var service_client_handle: IOTHUB_SERVICE_CLIENT_AUTH_HANDLE!;


// handler for the method invoke   
private var iot_device_method_handle:IOTHUB_SERVICE_CLIENT_DEVICE_METHOD_HANDLE!;



// In view did load establish the connection
service_client_handle = IoTHubServiceClientAuth_CreateFromConnectionString(connectionString)

if (service_client_handle == nil) {
    showError(message: &#34;Failed to create IoT Service handle&#34;, sendState: false)
}
</code></pre>

<ol 开始=“2”>
<li>创建方法调用函数</li>
</ol>

<p>我是根据发送消息提供的demo创建的</p>

<pre><code>func openIothubMethodInvoke() -&gt; Bool
{
    print(&#34;In openIotHub method invoke&#34;)
    let result: Bool;
    iot_device_method_handle = IoTHubDeviceMethod_Create(service_client_handle);
    let testValue : Any? = iot_device_method_handle;
    if (testValue == nil) {
      showError(message: &#34;Failed to create IoT devicemethod&#34;, sendState: false);
      result = false;
    }
    else
    {
      result = true;
    }
    return result;
}
</code></pre>

<ol 开始=“3”>
<li>调用方法调用</li>
</ol>

<p>** 这是调用方法的主函数</p>

<pre><code>func methodInvoke()
    {
      let testValue : Any? = iot_device_method_handle;
      if (testValue == nil &amp;&amp; !openIothubMethodInvoke() ) {
            print(&#34;Failued to open IoThub messaging&#34;);
      }
      else {
            let size = UnsafeMutablePointer&lt;Int&gt;.allocate(capacity: 1)
            let responseStatus = UnsafeMutablePointer&lt;Int32&gt;.allocate(capacity: 1)
      // Payload is the main change it is like specifying the format
            var payload = UnsafeMutablePointer&lt;UnsafeMutablePointer&lt;UInt8&gt;?&gt;.allocate(capacity: 1)
            // ifpayload is not expected please send empty json &#34;{}&#34;
            let result = IoTHubDeviceMethod_Invoke(iot_device_method_handle, &#34;nameOfTheDeviceYouWantToCallOn&#34;, &#34;MethodName&#34;, &#34;{payload you want to send}&#34;, 100, responseStatus, payload , size)
      //extracting the data from response
            let b = UnsafeMutableBufferPointer(start: payload.pointee, count:size.pointee)

            let data = Data(buffer: b)

         let str = String(bytes: data, encoding: .utf8)
            print(str)
            do{
                let value = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
                  print(value)
            }catch{
                print(error)
            }
      }
    }
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何使用 azure iot sdk 在 ios 中调用设备方法,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/57590121/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/57590121/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何使用 azure iot sdk 在 ios 中调用设备方法