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 = "simulated_device_one";
varmethodName = "GetState";
var uint8Pointer:UnsafeMutablePointer<UInt8>!
uint8Pointer = UnsafeMutablePointer<UInt8>.allocate(capacity:8)
var size = size_t(10000)
var bytes: =
uint8Pointer?.initialize(from: &bytes, count: 8)
var intValue : UnsafeMutablePointer<UInt8>?
intValue = UnsafeMutablePointer(uint8Pointer)
var char: UInt8 =UInt8(20)
var charPointer = UnsafeMutablePointer<UInt8>(&char)
var prediction = intValue
let serviceClientDeviceMethodHandle =IoTHubDeviceMethod_Create(service_client_handle)
let payLoad = "test"
var responsePayload = ""
let invoke = IoTHubDeviceMethod_Invoke(serviceClientDeviceMethodHandle, deviceId, methodName, payLoad , 100, &status, &prediction,&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 = "Enter your connection String";
// 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: "Failed to create IoT Service handle", sendState: false)
}
</code></pre>
<ol 开始=“2”>
<li>创建方法调用函数</li>
</ol>
<p>我是根据发送消息提供的demo创建的</p>
<pre><code>func openIothubMethodInvoke() -> Bool
{
print("In openIotHub method invoke")
let result: Bool;
iot_device_method_handle = IoTHubDeviceMethod_Create(service_client_handle);
let testValue : Any? = iot_device_method_handle;
if (testValue == nil) {
showError(message: "Failed to create IoT devicemethod", 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 && !openIothubMethodInvoke() ) {
print("Failued to open IoThub messaging");
}
else {
let size = UnsafeMutablePointer<Int>.allocate(capacity: 1)
let responseStatus = UnsafeMutablePointer<Int32>.allocate(capacity: 1)
// Payload is the main change it is like specifying the format
var payload = UnsafeMutablePointer<UnsafeMutablePointer<UInt8>?>.allocate(capacity: 1)
// ifpayload is not expected please send empty json "{}"
let result = IoTHubDeviceMethod_Invoke(iot_device_method_handle, "nameOfTheDeviceYouWantToCallOn", "MethodName", "{payload you want to send}", 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]