Pair with QR Code on App

Last Updated on : 2024-06-06 01:48:26download

This topic describes how to enable a device with a camera to scan the QR code on the mobile app for pairing.

QR code pairing

Class and protocol

Class (protocol) Description
ThingSmartActivator Device pairing methods.
ThingSmartActivatorDelegate Device pairing callbacks.

API description

Get the pairing token

- (void)getTokenWithHomeId:(long long)homeId
                   success:(ThingSuccessString)success
                   failure:(ThingFailureError)failure;

Parameters

Parameter Description
homeId The ID of the home with which the device is bound.
success The success callback. A pairing token is returned.
failure The failure callback. An error message is returned to indicate the cause.

API description

Start pairing

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

Parameters

Parameter Description
mode The pairing mode.
ssid The SSID of the target Wi-Fi network.
password The password of the target Wi-Fi network.
token The pairing token.
timeout The timeout period.

API description

Stop pairing

- (void)stopConfigWiFi;

API description

The delegate callback for the pairing result.

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

Parameters

Parameter Description
activator The ThingSmartActivator object for pairing.
deviceModel The device model returned on success. nil is returned on failure.
error The error message returned on failure. nil is returned on success.

Get a token

Before the pairing process, the SDK requests a pairing token from the cloud in the networked state and then generates a QR code for the token and the Wi-Fi’s SSID and password. The token is valid for 10 minutes and expires immediately after the device is paired.

Device pairing depends on the home, requiring a device to be bound to a specific home. When requesting a pairing token, pass in the home ID. Once the device is activated using this token, it will be included in the device list of the corresponding home.

Objective-C:

- (void)getToken {
    [[ThingSmartActivator sharedInstance] getTokenWithHomeId:homeId success:^(NSString *token) {
        NSLog(@"getToken success: %@", token);
        // TODO: startConfigWiFi
    } failure:^(NSError *error) {
        NSLog(@"getToken failure: %@", error.localizedDescription);
    }];
}

Swift:

func getToken() {
                       ThingSmartActivator.sharedInstance()?.getTokenWithHomeId(homeId, success: { (token) in
        print("getToken success: \(token)")
        // TODO: startConfigWiFi
    }, failure: { (error) in
        if let e = error {
            print("getToken failure: \(e)")
        }
    })
}

Generate a QR code

Concatenate the pairing token and Wi-Fi credentials (SSID and password) into a string based on the following code block. Then, use the string to generate a QR code.

Objective-C:

NSDictionary *dictionary = @{
@"s": self.ssid,
@"p": self.pwd,
@"t": self.token
};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:nil];
self.wifiJsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

Swift:

let dictionary = [
    "s": self.ssid,
    "p": self.pwd,
    "t": self.token
]
let jsonData = JSONSerialization.data(withJSONObject: dictionary, options: JSONSerialization.WritingOptions.Element)
self.wifiJsonStr = String(data: jsonData, encoding: String.Encoding.utf8)

Start pairing

Generate a QR code with wifiJsonStr. Put the device in pairing mode and point it at the QR code on the app. Upon successful scanning, the device will emit a sound. Listen for the pairing result through the following method.

Objective-C:

[[ThingSmartActivator sharedInstance] startConfigWiFi:ThingActivatorModeQRCode ssid:self.ssid password:self.pwd token:self.token timeout:100];

Swift:

                   ThingSmartActivator.sharedInstance()?.startConfigWiFi(ThingActivatorModeQRCode, ssid: self.ssid, password: self.pwd, token: self.token, timeout: 100)

Stop pairing

Stop the pairing process.

Objective-C:

[[ThingSmartActivator sharedInstance] stopConfigWiFi];

Swift:

                   ThingSmartActivator.sharedInstance()?.stopConfigWiFi()

Activation callback

The delegate callback ThingSmartActivatorDelegate returns the pairing result.

Objective-C:

// deviceModel contains the information about the paired device.
- (void)activator:(ThingSmartActivator *)activator didReceiveDevice:(ThingSmartDeviceModel *)deviceModel error:(NSError *)error;

Swift:

func activator(_ activator: ThingSmartActivator!, didReceiveDevice deviceModel: ThingSmartDeviceModel!, error: Error!)

Binding mode

There are three binding modes for associating a Tuya-enabled device with a home, each with varying unbinding processes.

  • Strong binding: A device can only be bound with another account after the previous user removes it from the app.
  • Medium binding: A device can be bound with another account without the previous user removing it from the app, but a push message will be sent to the home or default group admin of the previous account.
  • Weak binding: A device can be bound with another account without the previous user removing it from the app.

The smart camera defaults to strong binding mode due to its audio and video streaming and privacy features. Switching to a different binding mode is not allowed. If you do need to change the mode and have assessed the risk, submit a service ticket.