Backup Wi-Fi Networks

Last Updated on : 2024-03-04 07:31:44

To ensure network security, Wi-Fi passwords might be regularly changed. If backup Wi-Fi networks are unavailable, all smart devices connected to the Wi-Fi network must be paired again after the Wi-Fi password is changed. If you predefine multiple Wi-Fi passwords to create backup Wi-Fi networks, the system automatically switches to backup Wi-Fi networks and reconnects to the smart devices. They do not need to be paired again.

You can call [device devAttributeIsSupport:12] to check whether a specific device supports backup Wi-Fi networks.

Get current Wi-Fi information

API description

Returns the current Wi-Fi information.

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

Parameters

Parameter Description
success The success callback.
failure The failure callback.

Example

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)")
        }
    })
}

Get current backup Wi-Fi networks

API description

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

Parameters

Parameter Description
success The success callback.
failure The failure callback.

Example

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)")
        }
    })
}

Set a list of backup Wi-Fi networks

API description

Sets a list of backup Wi-Fi networks by using ThingSmartBackupWifiModel. Each backup network can be specified with a combination of ssid and hash or ssid and password.

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

Parameters

Parameter Description
list The list of backup Wi-Fi networks.
success The success callback.
failure The failure callback.

Example

Objective-C:

- (void)setBackupWifiList {
    // Adds a backup Wi-Fi network and sets its password.
    ThingSmartBackupWifiModel *newWifi = [[ThingSmartBackupWifiModel alloc] init];
    newWifi.ssid = @"test1";
    newWifi.password = @"12345678";

    // Sets a hash value for an existing backup Wi-Fi network.
    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]()
    // Adds a backup Wi-Fi network and sets a combination of `ssid` and `password`.
    let newWifi = ThingSmartBackupWifiModel()
    newWifi.ssid = "test1"
    newWifi.password = "12345678"
    list.append(newWifi)

    // Sets `ssid` and `hash` for an existing backup Wi-Fi network.
    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)")
        }
    })
}

Switch to a backup Wi-Fi network

API description

Switches to a backup Wi-Fi network.

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

Parameters

Parameter Description
hash The hash value of the target Wi-Fi password.
success The success callback.
failure The failure callback.

Example

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)")
        }
    })
}

Switch to a new Wi-Fi network

API description

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

Parameters

Parameter Description
ssid The name of the target Wi-Fi network.
password The password of the target Wi-Fi network.
success The success callback.
failure The failure callback.

Example

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)")
        }
    })
}