Pair with QR Code on Device

Last Updated on : 2024-05-15 08:23:26download

This pairing mode only applies to devices that are supplied with a QR code and connected to the internet.

Get device UUID

parseQRCode is a method used for parsing the QR code on the device. It can scan the device’s QR code to get the URL and device UUID.

Parameter Description

  • content: The content of the QR code.
  • success: Callback function after the QR code is successfully parsed. You can use actionName to find the pairing mode and proceed to the next step.
  • failure: Callback function when an error occurs while parsing the QR code.

Example Code

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)")
}

The "yourQRCodeContent" in the examples should be replaced with your QR code content.

Pair with QR code on device

Get pairing token

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

Example

Objective-C:

 ThingSmartActivator *activator = [[ThingSmartActivator alloc] init];
 [activator getTokenWithUUID:uuid homeId:homeId success:^(NSString *result) {
       // Returns the pairing 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)")
            }
        })

Start pairing

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

Example

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)

Callback delegate for getting device information

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

Example

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 {
        // The device is paired.
    }

    if let e = error {
        // Failed to pair the device.
        print("\(e)")
    }
}