Pair with QR Code on Device

Last Updated on : 2023-10-11 10:23:37download

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

Pairing process

DeviceHTTP DNSMQTTAppCloudPower on device andconnect to internetReset device to get itready for pairingSelect HTTP DNS and requestdomain name of mqttsUrlReturn domain name ofmqttsUrlUse UUID to connect to MQTTScan QR code on deviceGet device UUID and createtoken in cloudPoll for device to be pairedbased on tokenPush token and regionPass pairing data to deviceRequest device activationDevice activatedDevice activated and devicelist returnedDeviceHTTP DNSMQTTAppCloudProcess of pairing with QR code on device

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