子设备配网

更新时间:2022-03-03 06:44:26下载pdf

子设备配网是指智能设备依赖于网关连接到涂鸦,例如 Zigbee 网关、蓝牙网关等。因此,子设备配网的过程必须具体到某个网关,由网关完成设备激活和消息通知。

配网流程

下图以 Zigbee 网关为例,描述子设备配网流程:

子设备配网

开始子设备配网

接口说明

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

参数说明

参数 说明
gwId 网关 ID
timeout 超时时间

示例代码

Objc:

- (void)activeSubDevice {
    // 设置 TuyaSmartActivator 的 delegate,并实现 delegate 方法
	[TuyaSmartActivator sharedInstance].delegate = self;

	[[TuyaSmartActivator sharedInstance] activeSubDeviceWithGwId:@"your_device_id" timeout:100];
}

#pragma mark - TuyaSmartActivatorDelegate
- (void)activator:(TuyaSmartActivator *)activator didReceiveDevice:(TuyaSmartDeviceModel *)deviceModel error:(NSError *)error {

    if (!error && deviceModel) {
		// 配网成功
    }

    if (error) {
        // 配网失败
    }
}

Swift:

func activeSubDevice() {
    // 设置 TuyaSmartActivator 的 delegate,并实现 delegate 方法
    TuyaSmartActivator.sharedInstance()?.delegate = self
    TuyaSmartActivator.sharedInstance()?.activeSubDevice(withGwId: "your_device_id", timeout: 100)
}

#pragma mark - TuyaSmartActivatorDelegate
func activator(_ activator: TuyaSmartActivator!, didReceiveDevice deviceModel: TuyaSmartDeviceModel!, error: Error!) {
    if deviceModel != nil && error == nil {
        //配网成功
    }

    if let e = error {
        //配网失败
        print("\(e)")
    }
}

停止激活子设备

接口说明

- (void)stopActiveSubDeviceWithGwId:(NSString *)gwId

参数说明

参数 说明
gwId 网关 ID

示例代码

Objc:

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

Swift:

func stopActiveSubDevice() {
    TuyaSmartActivator.sharedInstance()?.delegate = nil
    TuyaSmartActivator.sharedInstance()?.stopActiveSubDevice(withGwId: "your_device_id")
}