有线设备配网

更新时间:2024-06-26 07:35:37下载pdf

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

配网流程

  1. 准备阶段
    引导用户,将设备重置到配网状态。

  2. 获取配网 Token
    App 通过调用 SDK 提供的接口,获取配网 Token。

  3. 获取设备信息
    App 通过调用 SDK 提供的 API,获取设备信息。

  4. 开始配网
    App 通过调用 SDK 提供的配网接口,设置配网参数,开始为设备配网。

  5. 完成配网
    配网完成后,App 会收到 SDK 的完成回调,结束配网流程。

发现设备

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

通过 ThingSmartActivatorDelegate 发现设备

/// Callback for the wired gateway discovery, Wi-Fi gateway, Zigbee gateway.
/// @param activator instance
/// @param deviceId the device id
/// @param productId the product id
- (void)activator:(ThingSmartActivator *)activator didFindGatewayWithDeviceId:(nullable NSString *)deviceId productId:(nullable NSString *)productId;

示例代码

Objective-C:

@interface YourClass : NSObject <ThingSmartActivatorDelegate>
@end

@implementation YourClass

- (void)activator:(ThingSmartActivator *)activator didFindGatewayWithDeviceId:(nullable NSString *)deviceId productId:(nullable NSString *)productId {
    if (deviceId && productId) {
        NSLog(@"Found gateway with device ID: %@ and product ID: %@", deviceId, productId);
        // TODO: Handle the discovered gateway, e.g., store device info or update UI
    }
}

Swift:

class YourClass: NSObject, ThingSmartActivatorDelegate {
    
    func activator(_ activator: ThingSmartActivator, didFindGatewayWithDeviceId deviceId: String?, productId: String?) {
        if let deviceId = deviceId, let productId = productId {
            print("Found gateway with device ID: \(deviceId) and product ID: \(productId)")
            // TODO: Handle the discovered gateway, e.g., store device info or update UI
        }
    }
}

通过转发通知发现设备

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

获取设备信息

- (void)getHomekitDeviceInfWithProductId: (NSString *)productId success: (ThingSuccessDict) success
failure: (ThingFailureError) failure;

示例代码

Objective-C:

- (void)getHomekitDeviceInfo {
    ThingSmartHomeKitActivator *homeKitActivator = [[ThingSmartHomeKitActivator alloc] init];
    NSString *productId = @"yourProductId";
    
    [homeKitActivator getHomekitDeviceInfWithProductId:productId success:^(NSDictionary *deviceInfo) {
        NSLog(@"getHomekitDeviceInfo success: %@", deviceInfo);
        // TODO: handle successful retrieval
    } failure:^(NSError *error) {
        NSLog(@"getHomekitDeviceInfo failure: %@", error.localizedDescription);
        // TODO: handle failure
    }];
}

Swift:

func getHomekitDeviceInfo() {
    let homeKitActivator = ThingSmartHomeKitActivator()
    let productId = "yourProductId"
    
    homeKitActivator.getHomekitDeviceInfWithProductId(productId, success: { (deviceInfo) in
        print("getHomekitDeviceInfo success: \(deviceInfo)")
        // TODO: handle successful retrieval
    }, failure: { (error) in
        if let e = error {
            print("getHomekitDeviceInfo failure: \(e.localizedDescription)")
            // TODO: handle failure
        }
    })
}

获取 Token

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

接口说明

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

参数说明

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

示例代码

Objective-C:

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

Swift:

func getToken() {
    let wiredActivator = ThingSmartActivator()
    wiredActivator.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 超时时间

示例代码

Objective-C:

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

    // 开始配网
    [self.wiredActivator startConfigWiFiWithToken:token timeout:100];
}

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

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

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

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

}

Swift:

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

    // 开始配网
    wiredActivator.startConfigWiFi(withToken: token, timeout: 100)
}

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

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

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

停止配网

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

接口说明

- (void)stopConfigWiFi;

示例代码

Objective-C:

- (void)stopConfigWifi {
    self.wiredActivator.delegate = nil;
    [self.wiredActivator stopConfigWiFi];
}

Swift:

func stopConfigWifi() {
    wiredActivator.delegate = nil
    wiredActivator.stopConfigWiFi()
}