Fast Integration with Commercial Lighting App SDK for Android

Last Updated on : 2022-03-11 03:40:29download

Integrate the SDK

  • Before you start, make sure you have completed the steps in Preparation.
  • If you have not installed Android Studio, visit the Android official website to download and install it.

Step 1: Create an Android project

Create a project in Android Studio.

Step 2: Configure the build.gradle file

In the build.gradle file of the Android project, add the dependencies that are downloaded in the preparation steps to dependencies.

android {
    defaultConfig {
        ndk {
            abiFilters "armeabi-v7a", "arm64-v8a"
        }
    }
    packagingOptions {
        pickFirst 'lib/*/libc++_shared.so' // Multiple AAR files exist in the .so file. Select the first AAR file.
    }
}
dependencies {
    implementation 'com.facebook.soloader:soloader:0.8.0'
    implementation 'com.alibaba:fastjson:1.1.67.android'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.squareup.okhttp3:okhttp-urlconnection:3.12.3'
    implementation "androidx.annotation:annotation:1.0.0"

    implementation "com.tuya.smart:tuyacommerciallightingsdk:1.12.5"

}

Add the Tuya Smart Maven repository address to the build.gradle file in the root directory for repository configuration.

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

Step 3: Integrate the security image and set AppKey and AppSecret

  1. On the SDK Development page, find the newly created SDK.

  2. On the Get Key tab, click Download next to App Security Image for Android, and in the dialog box that appears, click Download Security Image to download the security image.

    Fast Integration with Commercial Lighting App SDK for Android

    From version 1.9.7 , you need to set SHA256 before you can use it. How to get SHA256 key, you can refer to the document How to get SHA256 key.

  3. Rename the downloaded security image as t_s.bmp and save it to the assets folder in the project directory.
    Fast Integration with Commercial Lighting App SDK for Android

  4. Return to the Android project, set appKey and appSecret in the AndroidManifest.xml file, and then set permissions for the app.

    <meta-data
    android:name="TUYA_SMART_APPKEY"
    android:value="app Appkey" />
    <meta-data
    android:name="TUYA_SMART_SECRET"
    android:value="AppKey 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

You must initialize the SDK in the main thread of Application. All processes must be initialized. Sample code:

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

Configure appKey and appSecret in AndroidManifest.xml file, 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 Platform. Any mismatch will cause the SDK development to be failed. For more information, see Step 3: Integrate the security image and set 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 features:

  • Manage projects
  • Manage areas
  • Manage groups
  • Manage accounts
  • Connect devices
  • Control devices
  • Manage outdoor projects

Demo app

Fast Integration with Commercial Lighting App SDK for Android Fast Integration with Commercial Lighting App SDK for Android Fast Integration with Commercial Lighting App SDK for Android

For more details, please refer to https://github.com/tuya/tuya-commerciallighting-sdk.

Run the sample

  1. Choose app > build.gradle and change the value of applicationId to your app package name.
    Fast Integration with Commercial Lighting App SDK for Android
  2. Make sure that you have finished the operations in Step 3: Integrate security image and set Appkey and AppSecret.
  3. Click Run to run the sample.

Troubleshoot 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
    }
    
  • Solution:

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