Sub-Device Pairing

Last Updated on : 2024-06-26 09:48:49download

This topic describes the mode of pairing a sub-device. This type of device is connected to the Tuya Cloud through a gateway such as a Zigbee gateway or Bluetooth gateway. Therefore, the pairing mode depends on a specific gateway that processes device activation and notifications.

Pairing process

The following figure shows how to pair a sub-device that is connected through a Zigbee gateway.

AppSDKZigbee GatewayCloudReset Zigbeesub-deviceSend a sub-device activationcommandSend a sub-device activationcommandReceive sub-deviceactivation dataNotify the cloud to activatethe sub-deviceSub-device is activatedsuccessfullySub-device is activatedsuccessfullySub-device is activatedsuccessfullyAppSDKZigbee GatewayCloud(Zigbee) Sub-Device Activation and Pairing

Start sub-device pairing

API description

- (void)activeSubDeviceWithGwId:(NSString *)gwId timeout:(NSTimeInterval)timeout

Parameters

Parameter Description
gwId The gateway ID.
timeout The timeout period.

Sample code

Objective-C:

- (void)activeSubDevice {
    // Implements the delegate method of ThingSmartActivator.
    self.subActivator.delegate = self;

    [self.subActivator activeSubDeviceWithGwId:@"your_device_id" timeout:100];
}

- (ThingSmartActivator *)subActivator {
    if (!_subActivator) {
        _subActivator = [[ThingSmartActivator alloc] init];
    }
    return _subActivator;
}

#pragma mark - ThingSmartActivatorDelegate
 - (void)activator:(ThingSmartActivator *)activator didReceiveDevice:(ThingSmartDeviceModel *)deviceModel error:(NSError *)error {

    if (!error && deviceModel) {
         // The device is paired.
    }

    if (error) {
        // Failed to pair the device.
    }
}

Swift:

func activeSubDevice() {
    // Implements the delegate method of ThingSmartActivator.
    subActivator.delegate = self
    subActivator.activeSubDevice(withGwId: "your_device_id", timeout: 100)
}

lazy var subActivator: ThingSmartActivator = {
     let activator = ThingSmartActivator()
    return activator
}()

#pragma mark - ThingSmartActivatorDelegate
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)")
    }
}

Stop activating a sub-device

API description

- (void)stopActiveSubDeviceWithGwId:(NSString *)gwId

Parameters

Parameter Description
gwId The gateway ID.

Sample code

Objective-C:

- (void)stopActiveSubDevice {
        self.subActivator.delegate = nil;
    [self.subActivator stopActiveSubDeviceWithGwId:@"your_device_id"];
}

Swift:

func stopActiveSubDevice() {
    subActivator.delegate = nil
    subActivator.stopActiveSubDevice(withGwId: "your_device_id")
}