Sub-Device Pairing

Last Updated on : 2024-08-22 06:10:20download

This topic describes the mode of pairing a sub-device. This type of device is connected to the 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.
The app sends a sub-device pairing command to the cloud, which forwards the command to the gateway to initiate the pairing process. After the sub-device receives the pairing command, it registers and activates with the cloud through the gateway. On success, the cloud notifies the app of the result.

The ThingSmartBusinessExtensionKit component offers more features than the ThingSmartActivator. If you are still using ThingSmartActivator, please refer to this document.

## Pairing process
AppCloudGatewaySub-DeviceEnter pairing mode.Send a command to the sub-device.1Send a command to the sub-device.2Activate.3Activate and register.4Activate and register.5Sub-device is added.6Query sub-device information.7Return sub-device information.8Sub-device is added.AppCloudGatewaySub-DeviceSub-Device Pairing

Register the pairing type

When the device pairing service is initialized, register the pairing type, which is ThingSmartActivatorTypeSubDeviceModel.

API description

/// Initialize network configuration types
/// @param typeList Network configuration types
- (void)registerWithActivatorList:(NSArray<ThingSmartActivatorTypeModel *>*)typeList;

Parameter description

Parameters Description
typeList The list of pairing types.

Start searching

Pass in the registered typeList to start searching.

API description

/// Start searching
/// @param typeList Network configuration types
- (void)startSearch:(NSArray <ThingSmartActivatorTypeModel *>*)typeList;

Parameter description

Parameters Description
typeList The list of pairing types.

Stop searching

API description

/// Stop searching
/// @param typeList Network configuration types
/// @param clearCache Whether to clear the cache
- (void)stopSearch:(NSArray <ThingSmartActivatorTypeModel *>*)typeList clearCache:(BOOL)clearCache;

Parameter description

Parameters Description
typeList The list of pairing types.
clearCache Specifies whether to clear the cache search result.

Device search callback

After the device is paired, the callback returns the device information. If the operation fails, the callback returns the error message.

API description

/// Device search callback
/// @param service Search instance
/// @param type Network configuration type
/// @param device Discovered device
/// @param errorModel Error callback
- (void)activatorService:(id<ThingSmartActivatorSearchProtocol>)service
            activatorType:(ThingSmartActivatorTypeModel *)type
             didFindDevice:(nullable ThingSmartActivatorDeviceModel *)device
                     error:(nullable ThingSmartActivatorErrorModel *)errorModel;

Parameter description

Parameters Description
service The pairing service.
type The pairing type.
device The discovered device. The device model is returned on success. nil is returned on failure.
errorModel This model is returned if pairing fails or times out. nil is returned on success.

Error codes

ThingSmartActivatorErrorModel is returned if pairing fails or times out.

@interface ThingSmartActivatorErrorModel : NSObject
@property (nonatomic, strong) ThingSmartActivatorDeviceModel *deviceModel;
@property (nonatomic) NSError *error;
@end

ThingSmartActivatorDiscoveryError includes the definition of error codes. The following table lists the common error codes.

Error codes Description
ThingSmartActivatorDiscoveryErrorTimeout Pairing timeout.
ThingSmartActivatorDiscoveryErrorDeviceAlreadyBound Strong binding error. The device is already bound with a user. Pairing can work only after the device is unbound from the current user.
ThingSmartActivatorDiscoveryErrorAPPUnsupportProduct The app used for pairing is not bound with the product.
ThingSmartActivatorDiscoveryErrorSubDeviceOverLimit The number of sub-devices connected to the gateway exceeds the limit.
ThingSmartActivatorDiscoveryErrorGuestNotSupportStrongBind A guest account is not allowed to pair a device of strong binding.
ThingSmartActivatorDiscoveryErrorRemoteApiParamIllegal Invalid parameter.

Example

Swift:

class zigbeeSubmodeConfigurationVC: UITableViewController {

    var gateway: ThingSmartDeviceModel?
    var deviceList:[ThingSmartActivatorDeviceModel] = []

    private var typeModel: ThingSmartActivatorTypeSubDeviceModel = {
        let type = ThingSmartActivatorTypeSubDeviceModel()
        type.type = ThingSmartActivatorType.subDevice
        type.typeName = NSStringFromThingSmartActivatorType(ThingSmartActivatorType.subDevice)
        type.timeout = 120
        return type
    }()

    lazy var discovery: ThingSmartActivatorDiscovery = {
        let discovery = ThingSmartActivatorDiscovery()
        discovery.register(withActivatorList: [self.typeModel])
        discovery.setupDelegate(self)
        discovery.loadConfig()
        return discovery
    }()

    @IBAction func searchTapped(_ sender: UIBarButtonItem) {
        guard let gateway = gateway else {
            Alert.showBasicAlert(on: self, with: NSLocalizedString("Select Zigbee Gateway", comment: ""), message: NSLocalizedString("You must have one Zigbee gateway selected.", comment: ""))
            return
        }

        SVProgressHUD.show(withStatus: NSLocalizedString("Configuring", comment: ""))
        typeModel.gwDevId = gateway.devId
        discovery.startSearch([typeModel])
    }

}

extension zigbeeSubmodeConfigurationVC: ThingSmartActivatorSearchDelegate {
    func activatorService(_ service: ThingSmartActivatorSearchProtocol, activatorType type: ThingSmartActivatorTypeModel, didFindDevice device: ThingSmartActivatorDeviceModel?, error errorModel: ThingSmartActivatorErrorModel?) {
        if device != nil && errorModel == nil {
            // Success
            let name = device?.name ?? NSLocalizedString("Unknown Name", comment: "Unknown name device.")
            SVProgressHUD.showSuccess(withStatus: NSLocalizedString("Successfully Added \(name)", comment: "Successfully added one device."))
            SVProgressHUD.dismiss()
        }

        if let error = errorModel?.error {
            // Error
            SVProgressHUD.showError(withStatus: error.localizedDescription)
        }
    }

}

Objective-C:

- (void)startConfig {
  ThingSmartActivatorTypeSubDeviceModel *subModel  = [[ThingSmartActivatorTypeSubDeviceModel alloc] init];
  subModel.type = ThingSmartActivatorTypeSubDevice;
  subModel.typeName = NSStringFromThingSmartActivatorType(ThingSmartActivatorTypeSubDevice);
  subModel.timeout = 120;
  subModel.gwDevId = gwDevId;


  [self.discovery registerWithActivatorList:@[subModel]];
  [self.discovery setupDelegate:self];

  [self.discovery startSearch:@[subModel]];
}

- (void)activatorService:(id<ThingSmartActivatorSearchProtocol>)service activatorType:(ThingSmartActivatorTypeModel *)type didFindDevice:(ThingSmartActivatorDeviceModel *)device error:(ThingSmartActivatorErrorModel *)errorModel {

    if (errorModel) {
        [self _connectWifiError:errorModel];
        return;
    }

    if (device) {
        [self _handleDevice:device];
    }
}
- (ThingSmartActivatorDiscovery *)discovery {
    if (!_discovery) {
        _discovery = [[ThingSmartActivatorDiscovery alloc] init];
    }
    return _discovery;
}