Last Updated on : 2023-05-22 06:38:22
Tuya IoT App SDK for Android supports comprehensive and flexible management of IoT app users. This tutorial walks you through multiple steps to get started with the IoT App SDK service and implement user account features of your app. For example, users can register an account of the app, log in to the app, log out of the app, reset the password of the app, and modify account information. In the following hands-on approaches, the sample code for Java is used to describe the process.
In this tutorial, you will learn how to implement the following features:
App SDK Development GitHub Sample
Before you start, the following requirements must be met:
An account of the Tuya IoT Platform is registered, an app is built on the platform, and the values of AppKey
and AppSecret
of the IoT App SDK service are obtained. For more information, see Preparation.
Tuya IoT App SDK for Android is integrated into your project with Android Studio. For more information, see Fast Integration.
Note: This tutorial uses Android Studio 4.1.2 to integrate Tuya IoT App SDK on macOS. For SDK v3.26.5, the minimum value of
minSdkVersion
of the SDK is 19.
This tutorial helps you create the following sample app for Android.
To implement registration with a mobile phone number or an email address, you must set the countryCode
parameter to specify the country code. This way, the availability zone closest to users’ location can be selected to serve workloads in the Tuya IoT Cloud. For example, countryCode
for America is 1
and that for mainland China is 86
. The data in different availability zones is isolated from each other. Therefore, an account that is registered in America (1) cannot be used in mainland China (86). Otherwise, an error message is returned to indicate that the account does not exist. For more information, see What is Tuya IoT Cloud Platform?
In this tutorial, the User
object is frequently called. This object stores all information about the current user, including login and registration methods. You can use TuyaHomeSdk.getUserInstance()
to get ITuyaUser
. For more information, see User.
Solution: To strengthen data security, Tuya has optimized the verification code service and placed limits on accounts. The verification code service is available only in limited areas.
Example:
TuyaHomeSdk.getUserInstance().getWhiteListWhoCanSendMobileCodeSuccess(new IWhiteListCallback() {
@Override
public void onSuccess(WhiteList whiteList) {
Toast.makeText(mContext, "Whitelist Success:" + whiteList.getCountryCodes(), Toast.LENGTH_SHORT).show();
}
@Override
public void onError(String code, String error) {
Toast.makeText(mContext, "code: " + code + "error:" + error, Toast.LENGTH_SHORT).show();
}
});
Return value: The value of whiteList
indicates one or more countries or areas that are separated with commas (,
). For example, 86,01
can be returned. For more information, see Availability zone.
Activation: Currently, the verification code service is activated by default in mainland China. If you want to publish your application to other countries or areas, you must verify that the verification code service is available in a specific country or area and contact your account manager of Tuya or submit a ticket to activate the service.
Solution: Registration with a mobile phone number is similar to registration with an email address. To proceed with the registration, users must get a verification code.
API method: In the latest version of the IoT App SDK service, the verification code sending API method has been optimized. This allows you to use the same API method to send verification codes to an account that might be registered with a mobile phone number or an email address. The verification codes are required in multiple operations. For example, register an account, log in to the app, or reset a password.
TuyaHomeSdk.getUserInstance().sendVerifyCodeWithUserName(String userName, String region, String countryCode, int type, IResultCallback callback);
Parameters
Parameter | Description |
---|---|
userName | The mobile phone number or email address. |
region | The area in which the verification code service is available for the account. Default value: "" . |
countryCode | The country code of the mobile phone number. Example: 86 . |
type | The type of verification code. Valid values: 1 : registration. 2 : login. 3 : password resetting. |
callback | The callback. |
Example:
TuyaHomeSdk.getUserInstance().sendVerifyCodeWithUserName("xxx6/7825***66@123.com", "", "86", 1/2/3, new IResultCallback() {
@Override
public void onError(String code, String error) {
Toast.makeText(mContext, "code: " + code + "error:" + error, Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess() {
Toast.makeText(mContext, "Get code success:", Toast.LENGTH_SHORT).show();
}
});
Solution: To allow users to register an account with a mobile phone number, you must call the verification code sending API method and set the type
parameter. Then, call the API method to register an account with a mobile phone number. To register the account, users must provide the country code, mobile phone number, password, and the returned verification code.
Example:
TuyaHomeSdk.getUserInstance().registerAccountWithPhone("86","13666666666","123456","124332", new IRegisterCallback() {
@Override
public void onSuccess(User user) {
Toast.makeText(mContext, "Register success", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(String code, String error) {
Toast.makeText(mContext, "code: " + code + "error:" + error, Toast.LENGTH_SHORT).show();
}
});
After successful registration with mobile phone numbers, users can select their country code and log in to the app with their mobile phone numbers.
Solution: After users register an account with mobile phone numbers, they can log in to the app with their mobile phone numbers.
Example:
TuyaHomeSdk.getUserInstance().loginWithPhonePassword("86", "13666666666", "123456", new ILoginCallback() {
@Override
public void onSuccess(User user) {
Toast.makeText(mContext, "Login success: " +TuyaHomeSdk.getUserInstance().getUser().getUsername(), Toast.LENGTH_SHORT).show();
}
@Override
public void onError(String code, String error) {
Toast.makeText(mContext, "code: " + code + "error:" + error, Toast.LENGTH_SHORT).show();
}
});
Solution: After users log in to the app with their mobile phone numbers, they can bind email addresses to their accounts. This enables login to the app with the email address. To enable binding an email address, you must call sendBindingVerificationCodeWithEmail
to send a verification code. Note that the API method to send a verification code that enables email address-based registration is independent of the API method for binding an email address.
Example to send a verification code:
TuyaHomeSdk.getUserInstance().sendBindVerifyCodeWithEmail("86","123456@123.com", new IResultCallback(){
@Override
public void onError(String code, String error) {
}
@Override
public void onSuccess() {
Toast.makeText(mContext, "Send code success:", Toast.LENGTH_SHORT).show();
}
});
Example to bind an email address:
TuyaHomeSdk.getUserInstance().bindEmail("86", "123456@123.com","123456", TuyaHomeSdk.getUserInstance().getUser().getSid(), new IResultCallback() {
@Override
public void onError(String code, String error) {
}
@Override
public void onSuccess() {
Toast.makeText(mContext, "Bind success:", Toast.LENGTH_SHORT).show();
}
});
Solution: Similar to the registration with a mobile phone number, in this solution, you must call two API methods, namely verification code sending and registration with an email address. Users must provide the country code, email address, password, and the returned verification code. For more information, see Account registration.
Example:
TuyaHomeSdk.getUserInstance().registerAccountWithEmail("86", "123456@123.com","123456","5723", new IRegisterCallback() {
@Override
public void onSuccess(User user) {
Toast.makeText(mContext, "Register success:", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(String code, String error) {
Toast.makeText(mContext, "code: " + code + "error:" + error, Toast.LENGTH_SHORT).show();
}
});
Solution: After users register an account with their email addresses, they can log in to the app with their email addresses.
Example:
TuyaHomeSdk.getUserInstance().loginWithEmail("86", "123456@123.com", "123123", new ILoginCallback() {
@Override
public void onSuccess(User user) {
Toast.makeText(mContext, "Login success: ").show();
}
@Override
public void onError(String code, String error) {
Toast.makeText(mContext, "code: " + code + "error:" + error, Toast.LENGTH_SHORT).show();
}
});
Tuya IoT App SDK for iOS supports login with accounts of common third-party platforms, such as Google, Facebook, WeChat, and Tencent QQ. In the following example, the account of Google is used for login.
To enable login with a Google account, you must log in to the Google Developers platform, configure information about your application, and then get the value of Client ID
that is granted by Google’s authentication platform. Next, enter the value of Client ID
to the field as shown in the following figure on the Tuya IoT Platform. For more information, see Configure Google Sign-In.
Now, you are ready to call the API method to enable login with a Google account. For more information, see Login with Google.
TuyaHomeSdk.getUserInstance().thirdLogin(countryNumberCode,token,"gg","{\"pubVersion\":1}", new ILoginCallback() {
@Override
public void onSuccess(User user) {
}
@Override
public void onError(String code, String error) {
}
});
After successful login, users can modify account information such as the nickname, password, and region. The following examples show how to modify the nickname and login password. For more information about the API method, see Modify user information.
If users log in to the app with their mobile phone numbers or email addresses. Then, you can call the API method updateNickname to modify the nicknames of their accounts. If a WeChat username is used as the default nickname for login with a WeChat account, the nickname cannot be modified. Example:
TuyaHomeSdk.getUserInstance().reRickName(nickName,
new IReNickNameCallback() {
@Override
public void onSuccess() {
}
@Override
public void onError(String code, String error) {
}
});
If users want to reset the password or forgot passwords, the password resetting API method can be used to reset the password. Similar to the registration process, you must call the API method to send a verification code. For more information, see Get a verification code to register with a mobile phone number. Note that the type
parameter must be set to 3. After the verification code is returned, you can call different API methods based on the type of account to reset the password.
Example to reset the password with the mobile phone number:
TuyaHomeSdk.getUserInstance().resetPhonePassword("86", "13555555555", "123456", "123123", new IResetPasswordCallback(){
@Override
public void onSuccess() {
Toast.makeText(mContext, "onSuccess", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(String code, String error) {
Toast.makeText(mContext, "code: " + code + "error:" + error, Toast.LENGTH_SHORT).show();
}
});
Example to reset the password with the email address:
TuyaHomeSdk.getUserInstance().resetEmailPassword("86", "123456@123.com", "123123", "a12345", new IResetPasswordCallback() {
@Override
public void onSuccess() {
Toast.makeText(mContext, "onSuccess", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(String code, String error) {
Toast.makeText(mContext, "code: " + code + "error:" + error, Toast.LENGTH_SHORT).show();
}
});
Solution: Log out of the app or switch to another account.
API method: loginOut
Example:
TuyaHomeSdk.getUserInstance().touristLogOut(new ILogoutCallback() {
@Override
public void onSuccess() {
}
@Override
public void onError(String code, String error) {
}
});
API method: cancelAccount
Example:
TuyaHomeSdk.getUserInstance().cancelAccount(new IResultCallback() {
@Override
public void onError(String code, String error) {
}
@Override
public void onSuccess() {
}
});
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback