扫设备二维码配网

更新时间:2024-06-07 06:37:32下载pdf

该功能仅适用于设备已配备配网二维码,且已连接互联网的设备。

获取设备 UUID

parseQRCode 是一个用于解析设备上的 QR 码的方法。可扫描设备二维码获取 URL 和设备 UUID。

参数说明

  • content:QR 码的内容。
  • success:成功解析 QR 码后的回调函数。通过 actionName,可以找到对应的配网方式进行下一步处理。
  • failure:解析 QR 码出错时的回调函数。

示例代码

Objective-C:

[ThingSmartActivator parseQRCode:@"yourQRCodeContent" success:^(id result) {
    NSDictionary *qrCodeDict = (NSDictionary *)result;
    NSLog(@"Action Name: %@", [qrCodeDict objectForKey:@"actionName"]);
    NSLog(@"Action Data: %@", [qrCodeDict objectForKey:@"actionData"]);
} failure:^(NSError *error) {
    NSLog(@"Error: %@", error.localizedDescription);
}];

Swift:

ThingSmartActivator.parseQRCode("yourQRCodeContent", success: { result in
    guard let qrCodeDict = result as? [String: Any] else {
        return
    }
    print("Action Name: \(qrCodeDict["actionName"] ?? "")")
    print("Action Data: \(qrCodeDict["actionData"] ?? "")")
}) { error in
    print("Error: \(error.localizedDescription)")
}

二维码配网

获取配网 Token

- (void)getTokenWithUUID:(NSString *)uuid
                  homeId:(long long)homeId
                 success:(ThingSuccessString)success
                 failure:(ThingFailureError)failure;

示例代码

Objective-C:

 ThingSmartActivator *activator = [[ThingSmartActivator alloc] init];
 [activator getTokenWithUUID:uuid homeId:homeId success:^(NSString *result) {
       // 获取 token
    } failure:^(NSError *error) {

    }];

Swift:

         let activator = ThingSmartActivator()
        activator.getTokenWithUUID(uuid, homeId: homeid, success: { (token) in
            print("getToken success: \(token)")
            // TODO: startConfigWiFi
        }, failure: { (error) in
            if let e = error {
                print("getToken failure: \(e)")
            }
        })

开始配网

- (void)startConfigWiFi:(ThingActivatorMode)mode
                   ssid:(NSString *)ssid
               password:(NSString *)password
                  token:(NSString *)token
                timeout:(NSTimeInterval)timeout;

示例代码

Objective-C:

[self.activator startConfigWiFi:ThingActivatorModeEZ ssid:@"" password:@"" token:self.token timeout:Timeout];

Swift:

 activator.startConfigWiFi(ThingActivatorModeEZ, ssid:nil, password: nil, token: token, timeout: Timeout)

获取设备回调代理

- (void)activator:(ThingSmartActivator *)activator didReceiveDevice:(ThingSmartDeviceModel *)deviceModel error:(NSError *)error;

示例代码

Objective-C:

self.activator.delegate = self;

- (void)activator:(ThingSmartActivator *)activator didReceiveDevice:(TuyaSmartDeviceModel *)deviceModel error:(NSError *)error {
      // get deviceModel
}

Swift:

func activator(_ activator: ThingSmartActivator!, didReceiveDevice deviceModel: ThingSmartDeviceModel!, error: Error!) {
    if deviceModel != nil && error == nil {
        //配网成功
    }

    if let e = error {
        //配网失败
        print("\(e)")
    }
}