Last Updated on : 2024-08-21 10:13:46download
A smart device scans the QR code generated by the mobile app to get the Wi-Fi credentials and token. It then connects to the router and gets activated in the cloud. This pairing method is typically used for devices with a camera and Wi-Fi module, such as smart cameras and doorbells.
The ThingSmartBusinessExtensionKit
component offers more features than the ThingSmartActivator
. If you are still using ThingSmartActivator
, please refer to this document.
When the device pairing service is initialized, register the pairing type, which is ThingSmartActivatorTypeQRCodeModel
.
API description
/// Initialize network configuration types
/// @param typeList Network configuration types
- (void)registerWithActivatorList:(NSArray<ThingSmartActivatorTypeModel *>*)typeList;
Parameter description
Parameters | Description |
---|---|
typeList | The list of pairing types. |
The SDK gets a pairing token from the cloud before it can start the pairing process. 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:(ThingSuccessString)success
failure:(ThingFailureError)failure;
Parameter description
Parameters | Description |
---|---|
homeId | The ID of the home with which the device is bound. |
success | The success callback. A pairing token is returned. |
failure | The failure callback. An error message is returned. |
Concatenate the pairing token and Wi-Fi credentials (SSID and password) into a string based on the following code block. Then, use the string to generate a QR code.
Objective-C:
NSDictionary *dictionary = @{
@"s": self.ssid,
@"p": self.pwd,
@"t": self.token
};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:nil];
self.wifiJsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
Swift:
let dictionary = [
"s": self.ssid,
"p": self.pwd,
"t": self.token
]
let jsonData = JSONSerialization.data(withJSONObject: dictionary, options: JSONSerialization.WritingOptions.Element)
self.wifiJsonStr = String(data: jsonData, encoding: String.Encoding.utf8)
Pass in the registered typeList
to start searching.
API description
/// Start searching
/// @param typeList Network configuration types
- (void)startSearch:(NSArray <ThingSmartActivatorTypeModel *>*)typeList;
Parameter description
Parameters | Description |
---|---|
typeList | The list of pairing types. |
API description
/// Stop searching
/// @param typeList Network configuration types
/// @param clearCache Whether to clear the cache
- (void)stopSearch:(NSArray <ThingSmartActivatorTypeModel *>*)typeList clearCache:(BOOL)clearCache;
Parameter description
Parameters | Description |
---|---|
typeList | The list of pairing types. |
clearCache | Specifies whether to clear the cache search result. |
After the device is paired, the callback returns the device information. If the operation fails, the callback returns the error message.
API description
/// Device search callback
/// @param service Search instance
/// @param type Network configuration type
/// @param device Discovered device
/// @param errorModel Error callback
- (void)activatorService:(id<ThingSmartActivatorSearchProtocol>)service
activatorType:(ThingSmartActivatorTypeModel *)type
didFindDevice:(nullable ThingSmartActivatorDeviceModel *)device
error:(nullable ThingSmartActivatorErrorModel *)errorModel;
Parameter description
Parameters | Description |
---|---|
service | The pairing service. |
type | The pairing type. ThingSmartActivatorTypeQRCodeModel is returned. |
device | The discovered device. The device model is returned on success. nil is returned on failure. |
errorModel | This model is returned if pairing fails or times out. nil is returned on success. |
API description
/// Device information update or device rediscovered on a different channel
/// @param service Search instance
/// @param type Network configuration type
/// @param device Device model
- (void)activatorService:(id<ThingSmartActivatorSearchProtocol>)service
activatorType:(ThingSmartActivatorTypeModel *)type
didUpdateDevice:(ThingSmartActivatorDeviceModel *)device;
Parameter description
Parameters | Description |
---|---|
service | The pairing service. |
type | The pairing type. ThingSmartActivatorTypeQRCodeModel is returned. |
device | The updated device information. |
ThingSmartActivatorErrorModel
is returned if pairing fails or times out.
@interface ThingSmartActivatorErrorModel : NSObject
@property (nonatomic, strong) ThingSmartActivatorDeviceModel *deviceModel;
@property (nonatomic) NSError *error;
@end
ThingSmartActivatorDiscoveryError
includes the definition of error codes. The following table lists the common error codes.
Error codes | Description |
---|---|
ThingSmartActivatorDiscoveryErrorTimeout | Pairing timeout. |
ThingSmartActivatorDiscoveryErrorDeviceAlreadyBound | Strong binding error. The device is already bound with a user. Pairing can work only after the device is unbound from the current user. |
ThingSmartActivatorDiscoveryErrorAPPUnsupportProduct | The app used for pairing is not bound with the product. |
ThingSmartActivatorDiscoveryErrorTokenExpired | Token expired. |
ThingSmartActivatorDiscoveryErrorGuestNotSupportStrongBind | A guest account is not allowed to pair a device of strong binding. |
ThingSmartActivatorDiscoveryErrorRemoteApiParamIllegal | Invalid parameter. |
Swift:
class qrmodeConfigurationVC: UITableViewController {
private var token: String = ""
private var typeModel:ThingSmartActivatorTypeQRCodeModel = {
let type = ThingSmartActivatorTypeQRCodeModel()
type.type = ThingSmartActivatorType.qrCode
type.typeName = NSStringFromThingSmartActivatorType(ThingSmartActivatorType.qrCode)
type.timeout = 120
return type
}()
lazy var discovery: ThingSmartActivatorDiscovery = {
let discovery = ThingSmartActivatorDiscovery()
discovery.register(withActivatorList: [self.typeModel])
discovery.setupDelegate(self)
discovery.loadConfig()
return discovery
}()
private func startConfiguration() {
guard let homeID = Home.current?.homeId else { return }
SVProgressHUD.show(withStatus: NSLocalizedString("Requesting for Token", comment: ""))
ThingSmartActivator.sharedInstance()?.getTokenWithHomeId(homeID, success: { [weak self] (token) in
guard let self = self else { return }
self.token = token ?? ""
self.startConfiguration(with: self.token)
}, failure: { (error) in
let errorMessage = error?.localizedDescription ?? ""
SVProgressHUD.showError(withStatus: errorMessage)
})
}
private func startConfiguration(with token: String) {
SVProgressHUD.show(withStatus: NSLocalizedString("Configuring", comment: ""))
guard let homeID = Home.current?.homeId else { return }
typeModel.token = self.token;
typeModel.spaceId = homeID
discovery.startSearch([typeModel])
}
}
extension qrmodeConfigurationVC: ThingSmartActivatorSearchDelegate {
func activatorService(_ service: ThingSmartActivatorSearchProtocol, activatorType type: ThingSmartActivatorTypeModel, didFindDevice device: ThingSmartActivatorDeviceModel?, error errorModel: ThingSmartActivatorErrorModel?) {
if (errorModel != nil) {
// Error
SVProgressHUD.showError(withStatus: errorModel?.error.localizedDescription)
return
}
if (device != nil) {
if device?.step == ThingActivatorStep.found {
// device find
}
}
}
func activatorService(_ service: ThingSmartActivatorSearchProtocol, activatorType type: ThingSmartActivatorTypeModel, didUpdateDevice device: ThingSmartActivatorDeviceModel) {
if device.step == ThingActivatorStep.intialized {
// Success
let name = device.name
SVProgressHUD.showSuccess(withStatus: NSLocalizedString("Successfully Added \(name)", comment: "Successfully added one device."))
navigationController?.popViewController(animated: true)
}
}
}
Objective-C:
/// Get token
- (void)getToken {
ThingSmartActivator *qrActivator = [[ThingSmartActivator alloc] init];
[qrActivator getTokenWithHomeId:homeId success:^(NSString *token) {
NSLog(@"getToken success: %@", token);
[self startConfigWiFi:token];
} failure:^(NSError *error) {
NSLog(@"getToken failure: %@", error.localizedDescription);
}];
}
/// Start activate
- (void)startConfigQRCode:(NSString *)token {
ThingSmartActivatorTypeQRCodeModel *qrCodeModel = [[ThingSmartActivatorTypeQRCodeModel alloc] init];
qrCodeModel.type = ThingSmartActivatorTypeQRCode;
qrCodeModel.typeName = NSStringFromThingSmartActivatorType(ThingSmartActivatorTypeQRCode);
qrCodeModel.timeout = 120;
qrCodeModel.spaceId = homeId;
[self.discovery registerWithActivatorList:@[qrCodeModel]];
[self.discovery setupDelegate:self];
[self.discovery startSearch:@[qrCodeModel]];
}
- (void)activatorService:(id<ThingSmartActivatorSearchProtocol>)service activatorType:(ThingSmartActivatorTypeModel *)type didFindDevice:(ThingSmartActivatorDeviceModel *)device error:(ThingSmartActivatorErrorModel *)errorModel {
if (errorModel) {
[self _connectWifiError:errorModel];
return;
}
if (device) {
[self _handleDevice:device];
}
}
- (void)activatorService:(id<ThingSmartActivatorSearchProtocol>)service activatorType:(ThingSmartActivatorTypeModel *)type didUpdateDevice:(ThingSmartActivatorDeviceModel *)device {
if (device) {
[self _handleDevice:device];
}
}
- (void)_handleDevice:(ThingSmartActivatorDeviceModel *)device {
ThingActivatorStep step = device.step;
if (step == ThingActivatorStepFound) {
// discover device
} else if (step == ThingActivatorStepRegisted) {
// device registration
} else if (step == ThingActivatorStepIntialized) {
/// activated successfully
}
}
- (ThingSmartActivatorDiscovery *)discovery {
if (!_discovery) {
_discovery = [[ThingSmartActivatorDiscovery alloc] init];
}
return _discovery;
}
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback