备用网络

更新时间:2024-03-19 10:00:21下载pdf

一些安全场景,可能会定期修改 Wi-Fi 密码。如果没有 Wi-Fi 多组连接功能,意味着所有连接的 Wi-Fi 设备都需要重新配网。通过提前预设多组 Wi-Fi 密码,则可以设置自动尝试连接,从而无需经过重新配网,Wi-Fi 在线切换连接网络环境。

您可以通过 [device devAttributeIsSupport:12] 判断当前设备是否支持设置备用网络。

查询网络信息

接口说明

获取当前设备连接的 Wi-Fi 信息。

- (void)getCurrentWifiInfoWithSuccess:(ThingSuccessDict)success failure:(ThingFailureError)failure;

参数说明

参数 说明
success 获取 Wi-Fi 信息成功回调
failure 失败回调

示例代码

Objective-C:

- (void)getCurrentWifiInfo {
    // self.device = [ThingSmartDevice deviceWithDeviceId:@"your_device_id"];

    [self.device getCurrentWifiInfoWithSuccess:^(NSDictionary *dict) {
        NSLog(@"get current Wifi Info success");
    } failure:^(NSError *error) {
        NSLog(@"get current Wifi Info failure: %@", error);
    }];
}

Swift:

func getCurrentWifiInfo() {
    device?.getCurrentWifiInfo(success: { (wifiInfo) in
        print("get current Wifi Info success")
    }, failure: { (error) in
        if let e = error {
            print("get current Wifi Info failure: \(e)")
        }
    })
}

查询设备的备用 Wi-Fi 网络

接口说明

- (void)getBackupWifiListWithSuccess:(ThingSuccessDict)success failure:(ThingFailureError)failure;

参数说明

参数 说明
success 获取当前设备已有的备用 Wi-Fi 列表成功回调
failure 失败回调

示例代码

Objective-C:

- (void)getBackupWifiList {
    // self.device = [ThingSmartDevice deviceWithDeviceId:@"your_device_id"];

    [self.device getBackupWifiListWithSuccess:^(NSDictionary *dict) {
        NSLog(@"get backupWifi list success");
    } failure:^(NSError *error) {
        NSLog(@"get backupWifi list failure: %@", error);
    }];
}

Swift:

func getBackupWifiList() {
    device?.getBackupWifiList(success: { (wifiList) in
        print("get backupWifi list success")
    }, failure: { (error) in
        if let e = error {
            print("get backupWifi list failure: \(e)")
        }
    })
}

设置备用 Wi-Fi 网络

接口说明

设置备用网络 ThingSmartBackupWifiModel 允许热点 SSID 加 Hash 形式,或者热点 SSID 加密码的形式存入。

- (void)setBackupWifiList:(NSArray<ThingSmartBackupWifiModel *> *)list success:(ThingSuccessDict)success failure:(ThingFailureError)failure;

参数说明

参数 说明
list 备用 Wi-Fi 信息列表
success 设置成功回调
failure 失败回调

示例代码

Objective-C:

- (void)setBackupWifiList {
    // 新添加的网络,设置密码
    ThingSmartBackupWifiModel *newWifi = [[ThingSmartBackupWifiModel alloc] init];
    newWifi.ssid = @"test1";
    newWifi.password = @"12345678";

    // 之前已添加过的网络,设置 hash
    NSString *ssid = @"test2";
    NSString *pwd = @"123123";
    ThingSmartDeviceModel *dev = self.device.deviceModel;
    NSString *hashStr = [ThingSmartBackupWifiModel getBase64HashValueWithLocalKey:dev.localKey ssid:ssid psw:pwd];
    ThingSmartBackupWifiModel *savedWifi = [[ThingSmartBackupWifiModel alloc] init];
    savedWifi.ssid = ssid;
    savedWifi.hashValue = hashStr;

    NSArray *wifiList = @[newWifi, savedWifi];
    [self.device setBackupWifiList:wifiList success:^(NSDictionary *dict) {
        NSLog(@"set backupWifi list success");
    } failure:^(NSError *error) {
        NSLog(@"set backupWifi list failure: %@", error);
    }];
}

Swift:

func setBackupWifiList() {
    var list = [ThingSmartBackupWifiModel]()
    // 新添加的网络,设置 SSID 和密码
    let newWifi = ThingSmartBackupWifiModel()
    newWifi.ssid = "test1"
    newWifi.password = "12345678"
    list.append(newWifi)

    // 之前已添加过的网络,设置 SSID 和 Hash
    if let localKey = device?.deviceModel.localKey {
        let ssid = "test2"
        let pwd = "123123"
        let hashStr = ThingSmartBackupWifiModel.getBase64HashValue(withLocalKey: localKey, ssid: ssid, psw: pwd)
        let savedWifi = ThingSmartBackupWifiModel()
        savedWifi.ssid = "test2"
        savedWifi.hashValue = hashStr
        list.append(savedWifi)
    }

    device?.setBackupWifiList(list, success: { (info) in
        print("set backupWifi list success")
    }, failure: { (error) in
        if let e = error {
            print("set backupWifi list failure: \(e)")
        }
    })
}

切换到备用 Wi-Fi 网络

接口说明

切换到已设置的备用网络。

- (void)switchToBackupWifiWithHash:(NSString *)hash success:(ThingSuccessDict)success failure:(ThingFailureError)failure;

参数说明

参数 说明
hash 已设置 Wi-Fi 的哈希值(Hash)
success 设置成功回调
failure 失败回调

示例代码

Objective-C:

- (void)switchToBackupWifi {
    [self.device switchToBackupWifiWithHash:backupWifiModel.hashValue success:^(NSDictionary *dict) {
        NSLog(@"switch to BackupWifi success");
    } failure:^(NSError *error) {
        NSLog(@"switch to BackupWifi failure: %@", error);
    }];
}

Swift:

func switchToBackupWifi() {
    device?.switchToBackupWifi(withHash: backipWifiModel.hashValue, success: { (info) in
        print("switch to BackupWifi success")
    }, failure: { (error) in
        if let e = error {
            print("switch to BackupWifi failure: \(e)")
        }
    })
}

切换到新的 Wi-Fi 网络

接口说明

- (void)switchToBackupWifiWithSSID:(NSString *)ssid password:(NSString *)password success:(ThingSuccessDict)success failure:(ThingFailureError)failure;

参数说明

参数 说明
ssid Wi-Fi 名称
password Wi-Fi 密码
success 设置成功回调
failure 失败回调

示例代码

Objective-C:

- (void)switchToBackupWifi {
    [self.device switchToBackupWifiWithSSID:@"test" password:@"12345678" success:^(NSDictionary *dict) {
        NSLog(@"switch to BackupWifi success");
    } failure:^(NSError *error) {
        NSLog(@"switch to BackupWifi failure: %@", error);
    }];
}

Swift:

func switchToBackupWifi() {
    device?.switchToBackupWifi(withSSID: "test", password: "12345678", success: { (info) in
        print("switch to BackupWifi success")
    }, failure: { (error) in
        if let e = error {
            print("switch to BackupWifi failure: \(e)")
        }
    })
}