Smart Camera Pairing in QR Code Mode

Last Updated on : 2022-03-03 06:43:57download

This topic describes the QR code pairing mode, especially for smart cameras.

Pairing overview

The following Wi-Fi pairing modes are supported by Powered by Tuya (PBT) smart cameras:

  • Wi-Fi Easy Connect (EZ), also known as the SmartConfig mode

  • Access point (AP), also known as the hotspot mode

  • QR code mode

    The QR code mode is recommended for its easy use. If the device cannot scan a QR code, try the Wi-Fi EZ mode.

The Wi-Fi EZ mode and AP mode are similar to those for other Powered by Tuya devices. For more information, see Device Pairing.

Functional description

Class name (protocol name) Description
TuyaSmartActivator The device pairing features.
TuyaSmartActivatorDelegate The callback delegate of the pairing result.

Pairing process

Smart Camera Pairing in QR Code Mode

Get a token

Before the AP pairing process, the SDK must get a pairing token from the cloud in the networked state. Then, the SSID and password of the Wi-Fi network along with the token are concatenated into a string to generate a QR code. The token is valid for 10 minutes and expires immediately after the device is paired. A new token must be generated if the device needs to be paired again.

The device to be paired must be bound with a site. To get a pairing token, the site ID is required. After the device is activated with the token, it will be added to the list of devices on the site.

API description

- (void)getTokenWithHomeId:(long long)homeId
                   success:(TYSuccessString)success
                   failure:(TYFailureError)failure;

Parameters

Parameter Description
homeId The ID of the site with which the target device is bound.
success The success callback. The pairing token is returned.
failure The failure callback. An error message is returned.

Example

ObjC:

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

Swift:

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

URL of QR code

After you get the pairing token and the SSID and password of the current Wi-Fi network, concatenate them into a string based on the following code block. Then, you can use the string to generate a QR code.

ObjC:

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

API description

- (void)startConfigWiFi:(TYActivatorMode)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 netwrok.
password The password of the target Wi-Fi netwrok.
token The pairing token.
timeout The timeout value for a pairing task.

Example

The wifiJsonStr field is used to generate a QR code. Then, guide users to point it to the IPC in the pairing state. Then, the device will get the QR code information and emit a prompt tone. The following API method starts listening for the pairing result.

ObjC:

[[TuyaSmartActivator sharedInstance] startConfigWiFi:TYActivatorModeQRCode ssid:self.ssid password:self.pwd token:self.token timeout:100];

Swift:

TuyaSmartActivator.sharedInstance()?.startConfigWiFi(TYActivatorModeQRCode, ssid: self.ssid, password: self.pwd, token: self.token, timeout: 100)

Stop pairing

Stops pairing during the pairing process.

ObjC:

[[TuyaSmartActivator sharedInstance] stopConfigWiFi];

Swift:

TuyaSmartActivator.sharedInstance()?.stopConfigWiFi()

Callback of an activated device

Returns the pairing result by using the delegate method TuyaSmartActivatorDelegate.

API description

- (void)activator:(TuyaSmartActivator *)activator didReceiveDevice:(TuyaSmartDeviceModel *)deviceModel error:(NSError *)error;

Parameters

Parameter Description
activator The TuyaSmartActivator object for pairing.
deviceModel The model of the paired device. If pairing failed, nil is returned.
error The failure callback. An error message is returned. If the device is paired, nil is returned.

Example

ObjC:

// `deviceModel` returns the information about the paired device.
- (void)activator:(TuyaSmartActivator *)activator didReceiveDevice:(TuyaSmartDeviceModel *)deviceModel error:(NSError *)error;

Swift:

func activator(_ activator: TuyaSmartActivator!, didReceiveDevice deviceModel: TuyaSmartDeviceModel!, error: Error!)

Binding patterns

Powered by Tuya (PBT) devices support three levels of binding patterns: strong, medium, and weak. A device can be activated on a site of a specific account based on the specified binding pattern. The verification method for unbinding the device varies based on different binding patterns.

  • Strong: To change the account that is associated with a device, remove the device from the app with the currently associated account and pair the device with the app by using the preferred account.
  • Medium: A device can be bound to a preferred account. You do not need to remove the device from the app with the currently associated account. In this case, the site administrator or default administrator of this account receives a push notification of the binding operation.
  • Weak: A device can be bound to a preferred account. You do not need to remove the device from the app with the currently associated account.

IPCs support audio and video transmission and require privacy protection. To achieve this purpose, by default, the strong binding pattern is used and cannot be changed. To meet special requirements, after the impact of binding pattern changes has been estimated, you can submit a ticket.