Backup Wi-Fi Networks

Last Updated on : 2023-05-22 06:38:26

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 check whether the current device can use the backup network by the method [device devAttributeIsSupport:12] .

Get current Wi-Fi information

API description

Returns information about the Wi-Fi network connected to the current device.

- (void)getCurrentWifiInfoWithSuccess:(TYSuccessDict)success failure:(TYFailureError)failure;

Parameter description

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

Example

ObjC:

- (void)getCurrentWifiInfo {
    // self.device = [TuyaSmartDevice 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:(TYSuccessDict)success failure:(TYFailureError)failure;

Parameter description

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

Example

ObjC:

- (void)getBackupWifiList {
    // self.device = [TuyaSmartDevice 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 TuyaSmartBackupWifiModel. Each backup network can be specified with a combination of ssid and hash or ssid and password.

- (void)setBackupWifiList:(NSArray<TuyaSmartBackupWifiModel *> *)list success:(TYSuccessDict)success failure:(TYFailureError)failure;

Parameter description

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

Example

ObjC:

- (void)setBackupWifiList {
    // Adds a backup Wi-Fi network and sets its password.
    TuyaSmartBackupWifiModel *newWifi = [[TuyaSmartBackupWifiModel 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";
    TuyaSmartDeviceModel *dev = self.device.deviceModel;
    NSString *hashStr = [TuyaSmartBackupWifiModel getBase64HashValueWithLocalKey:dev.localKey ssid:ssid psw:pwd];
    TuyaSmartBackupWifiModel *savedWifi = [[TuyaSmartBackupWifiModel 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 = [TuyaSmartBackupWifiModel]()
    // Adds a backup Wi-Fi network and sets a combination of `ssid` and `password`.
    let newWifi = TuyaSmartBackupWifiModel()
    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 = TuyaSmartBackupWifiModel.getBase64HashValue(withLocalKey: localKey, ssid: ssid, psw: pwd)
        let savedWifi = TuyaSmartBackupWifiModel()
        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 between Wi-Fi networks

Switch to a backup Wi-Fi network

API description

Switches to a backup Wi-Fi network.

- (void)switchToBackupWifiWithHash:(NSString *)hash success:(TYSuccessDict)success failure:(TYFailureError)failure;

Parameter description

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

Example

ObjC:

- (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:(TYSuccessDict)success failure:(TYFailureError)failure;

Parameter description

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

ObjC:

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