Device Pairing

Last Updated on : 2023-09-19 03:00:53download

The device pairing modes supported by Powered by Tuya cameras include:

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

  • Hotspot mode (AP mode)

  • QR code mode

    QR code mode is relatively simple. We recommend that you use the QR code mode. If the device can not scan the QR code, try the Wi-Fi EZ mode.

EZ mode and AP mode are the same for other Powered by Tuya devices. For more information, see Pair devices.

The following sections focus on the QR code mode for smart cameras.

QR code mode

Flow chart

Device Pairing

Class and Protocol

Class (Protocol) Description
ThingSmartActivator Network configuration package
ThingSmartActivatorDelegate Network configuration result delegate

Declaration

Get token from service.

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

Parameters

Parameter Description
homeId The home id of which the device will be bound
success Success callback, response token
failure Failure callback, the error indicates the reason of failure

Declaration

Start network configuration.

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

Parameters

Parameter Description
mode Configuration mode
ssid Ssid of Wi-Fi
password Password of Wi-Fi
token Token
timeout Timeout

Declaration

Stop network configuration.

- (void)stopConfigWiFi;

Declaration

Network configuration result delegate callback.

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

Parameters

Parameter Description
activator ThingSmartActivator object
deviceModel ThingSmartDeviceModel object, nil if failed
error Indicates the reason of failure, nil if succeed

Get a token

Before starting network configuration, the SDK needs to get the token from the service, and then generate the QR code with the SSID of Wi-Fi and the password. The Token is valid for 10 minutes and will be invalidated upon successful pairing. To pair the device again, a new QR code must be generated. The device must be bound in a home, so the token is associated with the home ID. After the device is successfully activated with this token, the device is displayed in the device list of this home.

Example

ObjC

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

Concatenate a Wi-Fi string

After got the token, the SSID and password of the wi-fi which the device is expected to connect are also needed, which are concatenated into a string in the following way, and then a QR code picture is generated according to this string.

Example

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

Use the wifiJsonStr to generate the QR code, reset the device, point the QR code at the camera, and the device will sound a prompt after capturing the information of the QR code. At this point, start listening to the distribution results through the following interface.

Example

ObjC

[[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

Use this method to stop pairing.

Example

ObjC

[[ThingSmartActivator sharedInstance] stopConfigWiFi];

Swift

ThingSmartActivator.sharedInstance()?.stopConfigWiFi()

Delegate a callback

The result of pairing is returned by ThingSmartActivatorDelegate.

Example

ObjC

- (void)activator:(ThingSmartActivator *)activator didReceiveDevice:(ThingSmartDeviceModel *)deviceModel error:(NSError *)error {
      if (deviceModel) {
          // success
    }else {
          // error
    }
}

Swift

func activator(_ activator: ThingSmartActivator!, didReceiveDevice deviceModel: ThingSmartDeviceModel?, error: Error!) {
        if deviceModel != nil {
          // success
    }else {
          // error
    }
}

Binding modes

Powered by Tuya cameras support three binding modes: strong, medium, and weak. After the device is successfully activated in a home of the account, different binding modes and different verification methods are required for unbinding.

  • Strong mode: Only after the previous user removes the device from the App, the device can be reconfigured and bound to another account.
  • Medium mode: Without the previous user removing the device from the App, the device can be reconfigured and bound to another account, but a PUSH notification will be sent to the previous account.
  • Weak mode: Without the previous user removing the device from the App, you can reconfigure the device to bind to another account.

Powered by Tuya cameras support the strong binding mode, and cannot change to other modes. If you have a strong demand and have evaluated the impact of changing the binding mode, you can submit a ticket to request technical support.