Last Updated on : 2023-05-22 06:38:24download
This topic describes the access point (AP) or hotspot mode to pair devices. It is a connection capability for pairing over Wi-Fi. After a mobile phone is connected to the Wi-Fi hotspot of a target device, the app is paired and communicates with the mobile phone over Wi-Fi. This pairing mode features a high success rate and excellent reliability, and supports routers that can process data using both 2.4 GHz and 5 GHz frequencies. Users must manually change the Wi-Fi band on their mobile phones to enable pairing.
Before the AP pairing process, the SDK must get a pairing token from the cloud in the networked state. The token is valid for 10 minutes and expires immediately after the device is paired. A new token must be generated if the device needs to be paired again.
API description
- (void)getTokenWithHomeId:(long long)homeId
success:(TYSuccessString)success
failure:(TYFailureError)failure;
Parameters
Parameter | Description |
---|---|
homeId | The ID of the home with which the device is bound. |
success | The success callback with a pairing token returned. |
failure | The failure callback with an error message returned. |
Example
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)activator:(TuyaSmartActivator *)activator didReceiveDevice:(TuyaSmartDeviceModel *)deviceModel error:(NSError *)error;
Parameters
Parameter | Description |
---|---|
activator | The instance of the TuyaSmartActivator object for pairing. |
deviceModel | The paired device model that is returned if the request is successful. nil is returned if the request failed. |
error | The error message that is returned if the request failed. nil is returned if the request is successful. |
API description
- (void)startConfigWiFi:(TYActivatorMode)mode
ssid:(NSString *)ssid
password:(NSString *)password
token:(NSString *)token
timeout:(NSTimeInterval)timeout;
Parameters
Parameter | Description |
---|---|
mode | The pairing mode. |
ssid | The name of the target Wi-Fi network. |
password | The password of the target Wi-Fi network. |
token | The pairing token. |
timeout | The timeout value of a pairing task. |
Example
ObjC:
- (void)startConfigWiFi:(NSString *)ssid password:(NSString *)password token:(NSString *)token {
// Implements the delegate method of `TuyaSmartActivator`.
[TuyaSmartActivator sharedInstance].delegate = self;
// Start AP pairing, in which `mode` is set to `TYActivatorModeAP`.
[[TuyaSmartActivator sharedInstance] startConfigWiFi:TYActivatorModeAP ssid:ssid password:password token:token timeout:100];
}
#pragma mark - TuyaSmartActivatorDelegate
- (void)activator:(TuyaSmartActivator *)activator didReceiveDevice:(TuyaSmartDeviceModel *)deviceModel error:(NSError *)error {
if (!error && deviceModel) {
// The device is paired.
}
if (error) {
// Failed to pair the device.
}
}
// Added to v4.0.0: The callback for the intermediate process is triggered in the task of pairing a certain security device.
- (void)activator:(TuyaSmartActivator *)activator didPassWIFIToSecurityLevelDeviceWithUUID:(NSString *)uuid {
// Then, you can guide users to connect to the specified Wi-Fi network.
// This way, the mobile is connected to the Wi-Fi network with the same service set identifier (SSID) as specified by `- startConfigWiFi:password:token:`.
// You can get the same SSID and call `- continueConfigSecurityLevelDevice` to continue with the pairing task.
// UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"SecurityLevelDevice" message:@"continue pair? (Please check you phone connected the same Wi-Fi as you Inputed)" preferredStyle:UIAlertControllerStyleAlert];
// [vc addAction:[UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:nil]];
// [vc addAction:[UIAlertAction actionWithTitle:@"continue" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//
// NSString *wifi = [self getCurrentWiFi];
// if ([wifi isEqualToString:self.inputSSID]) {
// [[TuyaSmartActivator sharedInstance] continueConfigSecurityLevelDevice];
// }
// }]];
// [self presentViewController:vc animated:YES completion:nil];
}
Swift:
func startConfigWiFi(withSsid ssid: String, password: String, token: String) {
// Implements the delegate method of `TuyaSmartActivator`.
TuyaSmartActivator.sharedInstance()?.delegate = self
// Starts pairing.
TuyaSmartActivator.sharedInstance()?.startConfigWiFi(TYActivatorModeAP, ssid: ssid, password: password, token: token, timeout: 100)
}
#pragma mark - TuyaSmartActivatorDelegate
func activator(_ activator: TuyaSmartActivator!, didReceiveDevice deviceModel: TuyaSmartDeviceModel!, error: Error!) {
if deviceModel != nil && error == nil {
// The device is paired.
}
if let e = error {
// Failed to pair the device.
print("\(e)")
}
}
// Added to v4.0.0: The callback for the intermediate process is triggered in the task of pairing a certain security device.
func activator(_ activator: TuyaSmartActivator!, didPassWIFIToSecurityLevelDeviceWithUUID uuid: String!) {
// Then, you can guide users to connect to the specified Wi-Fi network.
// This way, the mobile is connected to the Wi-Fi network with the same SSID as specified by `- startConfigWiFi:password:token:`.
// You can get the same SSID and call `- continueConfigSecurityLevelDevice` to continue with the pairing task.
// let alert = UIAlertController(title: "SecurityLevelDevice", message: "continue pair? (Please check you phone connected the same Wi-Fi as you Inputed)", preferredStyle: .alert);
// alert.addAction(UIAlertAction(title: "cancel", style: .cancel))
// alert.addAction(UIAlertAction(title: "continue", style: .destructive, handler: { _ in
// let wifi = getCurrentWiFi()
// if wifi == inputSSID {
// TuyaSmartActivator.sharedInstance().continueConfigSecurityLevelDevice()
// }
// }))
// present(alert, animated: true)
}
The AP mode is similar to the EZ mode, except that the first parameter of [TuyaSmartActivator startConfigWiFi:ssid:password:token:timeout:]
is set TYActivatorModeAP
for the AP mode.
ssid
and password
respectively specify the hotspot name and password of the router rather than that of the device.
After the pairing process is started, the app continuously broadcasts the pairing data until a device is paired or the process times out. To allow users to cancel or complete pairing during the process, you must call the API method [TuyaSmartActivator stopConfigWiFi]
to stop pairing.
API description
- (void)stopConfigWiFi;
Example
ObjC:
- (void)stopConfigWifi {
[TuyaSmartActivator sharedInstance].delegate = nil;
[[TuyaSmartActivator sharedInstance] stopConfigWiFi];
}
Swift:
func stopConfigWifi() {
TuyaSmartActivator.sharedInstance()?.delegate = nil
TuyaSmartActivator.sharedInstance()?.stopConfigWiFi()
}
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback