Fast Integration with Smart Residence App SDK for Android

Last Updated on : 2023-05-31 03:29:42download

Before you begin

  • Before you start, make sure that you have performed the steps in Preparation.
  • If you have not installed Android Studio, visit the Android Studio official website to download Android Studio.
  • If you need to run the demo, please use the latest version of Android Studio (Android Studio Arctic Fox | 2020.3.1 Patch 3) Build #AI-203.7717.56.2031.7784292, built on October 1, 2021 Runtime version: 11.0.10+0-b96-7249189 aarch64), and upgrade Gradle version to 7.0 or higher. The demo is also developed with Jetpack Compose, please refer to the official documentation to configure the relevant development environment.

Integrate with the SDK

Step 1: Create an Android project

Create a project in Android Studio.

Step 2: Configure build.gradle

Add dependencies to the build.gradle file of the Android project.

android {
    defaultConfig {
        ndk {
            abiFilters "armeabi-v7a", "arm64-v8a"
        }
    }
    android.packagingOptions {
        // Deduplicates the dependencies of `libc++_shared.so`.
        pickFirst 'lib/arm64-v8a/libc++_shared.so'
        pickFirst 'lib/armeabi-v7a/libc++_shared.so'
        pickFirst 'lib/x86/libc++_shared.so'
        pickFirst 'lib/x86_64/libc++_shared.so'
    }
}
dependencies {
    implementation 'com.alibaba:fastjson:1.1.67.android'
    implementation 'com.tuya.smart:smartResidenceSDK:1.2.0'
    api 'com.tuya.smart:tuyasmart:3.34.5'

}

Add the Tuya IoT Maven repository URL to the build.gradle file in the root directory.``

repositories {
    maven {url "https://maven-other.tuya.com/repository/maven-releases/"}
}

Step 3: Configure the security image, AppKey, and AppSecret

  1. Log in to the Tuya IoT Development Platform, go to the SDK Development page, and then click the SDK to be managed.

  2. On the page that appears, click the Get Key tab and click Download in the App Security Image for Android field.

    Fast Integration with Smart Residence App SDK for Android
  3. Rename the security image as t_s.bmp and put the image in the assets folder of your project.

    Fast Integration with Smart Residence App SDK for Android
  4. Return to the Android project, configure appkey and appSecret in AndroidManifest.xml, and then set permissions for the app.

    <meta-data
    android:name="TUYA_SMART_APPKEY"
    android:value="AppKey" />
    <meta-data
    android:name="TUYA_SMART_SECRET"
    android:value="AppSecret" />
    

Step 4: Obfuscate the code

Configure obfuscation in proguard-rules.pro

#fastJson
-keep class com.alibaba.fastjson.**{*;}
-dontwarn com.alibaba.fastjson.**

#mqtt
-keep class com.tuya.smart.mqttclient.mqttv3.** { *; }
-dontwarn com.tuya.smart.mqttclient.mqttv3.**

#OkHttp3
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-dontwarn okhttp3.**

-keep class okio.** { *; }
-dontwarn okio.**

-keep class com.tuya.**{*;}
-dontwarn com.tuya.**

Step 5: Initialize the SDK

Initialize the SDK in Application. Make sure that all processes are initialized. Example:

public class TuyaSmartApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        TuyaSdk.init(this);
    }
}

Configure appKey and appSecret in AndroidManifest.xml, or run the initialization code.

TuyaSdk.init(Application application, String appkey, String appSerect)

Step 6: Enable or disable logging

  • In debug mode, you can enable SDK logging to facilitate troubleshooting.

  • We recommend that you disable logging in release mode.

    TuyaSdk.setDebugMode(true);
    

Run the demo app

After you integrate the SDK, you can get the security image, AppKey, and AppSecret. Make sure the security image, AppKey, and AppSecret are consistent with those used on the Tuya IoT Development Platform. Any mismatch will cause the SDK development to be failed. For more information, see Step 3: Configure the security image, AppKey, and AppSecret.

In the following example, a demo app is used to describe the process of app development with the App SDK. Before the development of your app, we recommend that you run the demo app.

Demo app description

The demo app supports the following functions:

  • Site management
  • Access control
  • Personal information display

Demo app:

Fast Integration with Smart Residence App SDK for Android

更多详情,请参考 https://github.com/tuya/tuya-residence-android-sdk-sample Github项目。

Run the sample

  1. Choose app > build.gradle and change the value of applicationId to your app package name.

  2. Make sure that you have completed Step 3: Configure the security image, AppKey, and AppSecret.

  3. Click Run to run the demo app.

Troubleshooting the error message: Permission Verification Failed

  • Problem: When the system runs the demo app, an error message is returned in the following response:

    {
    	"success": false,
    	"errorCode" : "SING_VALIDATE_FALED",
    	"status" : "error",
    	"errorMsg" : "Permission Verification Failed",
    	"t" : 1583208740059
    }
    
  • Solutions:

    • Check whether your security image, AppKey, and AppSecret are correctly configured.
    • Check whether the security image is placed in the correct directory and whether the file name is t_s.bmp.