OGeek|极客世界-中国程序员成长平台

标题: ios - 如何使用 azure iot sdk 在 ios 中调用设备方法 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 05:37
标题: ios - 如何使用 azure iot sdk 在 ios 中调用设备方法

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

我尝试通过引用C示例来匹配参数参数要求。但我越来越 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 调用设备中的方法



Best Answer-推荐答案


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

1.在 View 中创建连接确实加载了

// 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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4