有线设备配网

更新时间:2022-03-03 06:44:12

有线设备已通过网线连接着网络,设备激活过程中,用户无需输入路由器的名称和密码。

配网流程

下图以 Zigbee 有线网关为例,描述有线网关配网流程:

有线设备配网

发现设备

SDK 提供发现待配网有线设备的功能,获取设备前,手机需与设备接入同一网络。然后注册获取有线设备的通知,待 SDK 收到有线设备的广播即会通过通知转发设备信息。

转发通知

// 收到有线配网设备的广播后,会发送此通知。objec为dictionary,@{@"productId":productId, @"gwId":gwId}
extern NSString *const TuyaSmartActivatorNotificationFindGatewayDevice;

获取 Token

开始配网之前,SDK 需要在联网状态下从涂鸦查询配网 Token,然后才可以开始有线设备激活配网。Token 的有效期为 10 分钟,且配置成功后就会失效。再次配网时,需要重新查询 Token。

接口说明

- (void)getTokenWithHomeId:(long long)homeId
                   success:(TYSuccessString)success
                   failure:(TYFailureError)failure;

参数说明

参数 说明
homeId 设备将要绑定到的站点的 ID
success 成功回调,返回配网 Token
failure 失败回调,返回失败原因

示例代码

Objc:

- (void)getToken {
    [[TuyaSmartActivator sharedInstance] getTokenWithHomeId:homeId success:^(NSString *token) {
        NSLog(@"getToken success: %@", token);
        // TODO: startConfigWiFi
    } failure:^(NSError *error) {
        NSLog(@"getToken failure: %@", error.localizedDescription);
    }];
}

Swift:

func getToken() {
    TuyaSmartActivator.sharedInstance()?.getTokenWithHomeId(homeId, success: { (token) in
        print("getToken success: \(token)")
        // TODO: startConfigWiFi
    }, failure: { (error) in
        if let e = error {
            print("getToken failure: \(e)")
        }
    })
}

开始配网

接口说明

- (void)startConfigWiFiWithToken:(NSString *)token timeout:(NSTimeInterval)timeout

参数说明

参数 说明
token 配网 Token
timeout 超时时间

示例代码

Objc:

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

    // 开始配网
    [[TuyaSmartActivator sharedInstance] startConfigWiFiWithToken:token timeout:100];
}

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

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

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

}

Swift:

func startConfigWifiToken(_ token: String) {
    // 设置 TuyaSmartActivator 的 delegate,并实现 delegate 方法
    TuyaSmartActivator.sharedInstance()?.delegate = self

    // 开始配网
    TuyaSmartActivator.sharedInstance()?.startConfigWiFi(withToken: token, 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)")
    }
}

停止配网

开始配网操作后,App 会持续广播配网信息,直到配网成功,或者超时。如果需要中途取消操作或配网完成,需要调用 [TuyaSmartActivator stopConfigWiFi] 方法。

接口说明

- (void)stopConfigWiFi;

示例代码

Objc:

- (void)stopConfigWifi {
    [TuyaSmartActivator sharedInstance].delegate = nil;
    [[TuyaSmartActivator sharedInstance] stopConfigWiFi];
}

Swift:

func stopConfigWifi() {
    TuyaSmartActivator.sharedInstance()?.delegate = nil
    TuyaSmartActivator.sharedInstance()?.stopConfigWiFi()
}