gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:20:00 24 4
gpt4 key购买 nike

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

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

     var status :Int32! = 0
var deviceId = "simulated_device_one";
var methodName = "GetState";
var uint8Pointer:UnsafeMutablePointer<UInt8>!
uint8Pointer = UnsafeMutablePointer<UInt8>.allocate(capacity:8)

var size = size_t(10000)
var bytes: [UInt8] = [39, 77, 111, 111, 102, 33, 39, 0]
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 )

我想使用 IoTHubDeviceMethod_Invoke 在设备中调用一个方法

最佳答案

您可以下载我处理过的 View Controller 文件 from here

1.Create Connection in view did load

// 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)
}
  1. 创建方法调用函数

我是根据提供的发送消息的demo创建的

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;
}
  1. 调用方法调用

** 这是调用方法的主要功能

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)
// if payload 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)
}
}
}

关于ios - 如何使用azure iot sdk在ios中调用设备方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57590121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com