Device Pairing

Last Updated on : 2024-06-05 03:22:21download

The device pairing modes supported by 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 cannot scan the QR code, try the Wi-Fi EZ mode.

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

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

QR code mode

Class and Protocol

Class (Protocol) Description
ThingSmartActivator Pairing package
ThingSmartActivatorDelegate Pairing result delegate

Declaration

Get a token from the service.

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

Parameters

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

Declaration

Start network pairing.

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

Parameters

Parameter Description
mode Pairing mode
ssid SSID of the Wi-Fi network
password Password of the Wi-Fi network
token Token
timeout Timeout

Declaration

Stop network pairing.

- (void)stopConfigWiFi;

Declaration

Network pairing result delegate callback.

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

Parameters

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

Get a token

Before starting network pairing, 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

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

Concatenate a Wi-Fi string

After the token is obtained, the SSID and password of the Wi-Fi network which the device is expected to connect are also needed, which are concatenated into a string in the following way. Then, a QR code picture is generated according to this string.

Example

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

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 pairing results through the following interface.

Example

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

Use this method to stop pairing.

Example

Objective-C

[[ThingSmartActivator sharedInstance] stopConfigWiFi];

Swift

ThingSmartActivator.sharedInstance()?.stopConfigWiFi()

Delegate a callback

The result of pairing is returned by ThingSmartActivatorDelegate.

Example

Objective-C

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

Cameras support three binding modes: strong, medium, and weak. After the device is successfully activated in a home of the account, the verification and unbinding methods vary depending on the binding mode.

  • Strong mode: Only after the previous user removes the device from the app, the device can be paired and bound with another account.
  • Medium mode: Without the previous user removing the device from the app, the device can be paired and bound with 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 pair the device to bind with another account.

Cameras support the strong binding mode, and cannot change to other modes. If you have a requirement and have evaluated the impact of changing the binding mode, you can submit a ticket to request technical support.