Fast Integration

Last Updated on : 2024-03-21 07:04:01download

This topic describes how to integrate the Commercial Lock App SDK into your project.

Integrate with the SDK

Step 1: Create an Android project

Create a project in Android Studio.

Step 2: Configure build.gradle

Add dependencies downloaded in the preparation steps to the file build.gradle of the Android project.

android {
    defaultConfig {
        ndk {
            abiFilters "armeabi-v7a", "arm64-v8a"
        }
    }
    packagingOptions {
        pickFirst 'lib/*/libv8android.so'
        pickFirst 'lib/*/libv8wrapper.so'
        pickFirst 'lib/*/libc++_shared.so'
    }
}

dependencies {
    implementation 'com.facebook.soloader:soloader:0.10.4'
    implementation 'com.alibaba:fastjson:1.1.67.android'
    implementation 'com.squareup.okhttp3:okhttp-urlconnection:3.14.9'
    implementation 'com.thingclips.smart:thingsmartlocksdk:1.3.1-lock'
}

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/' }
   maven { url "https://maven-other.tuya.com/repository/maven-commercial-releases/" }
   maven { url 'https://jitpack.io' }
   google()
   mavenCentral()
   maven { url 'https://maven.aliyun.com/repository/public' }
   maven { url 'https://central.maven.org/maven2/' }
   maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
   maven { url 'https://developer.huawei.com/repo/' }
}

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

  1. Go to SDK Development on the Tuya IoT Development Platform and open the target app.

  2. Click the Get Key tab and download the App Security Image for Android.

    Fast Integration
  3. Rename the security image as t_s.bmp and put the image in the assets folder of your project.

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

    <meta-data
    android:name="THING_SMART_APPKEY"
    android:value="Appkey" />
    <meta-data
    android:name="THING_SMART_APPKEY"
    android:value="AppSecret" />
    
  5. Configure the app certificate.

    1. Generate an SHA-256 hash value. For more information, see Android documentation and How to Get SHA-1 and SHA-256 Keys.
    2. Enter the SHA-256 key in the certificate.

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.thingclips.smart.mqttclient.mqttv3.** { *; }
-dontwarn com.thingclips.smart.mqttclient.mqttv3.**

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

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

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

# Matter SDK
-keep class chip.** { *; }
-dontwarn chip.**

# Keep native methods
-keepclasseswithmembernames class * {
    native <methods>;
}
-keepclassmembers class * {
    native <methods>;
}

Step 5: Initialize the SDK

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

public class TuyaSmartApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        ThingSdk.init(this);
        ThingSmartNetWork.mSdkVersion = "1.3.0";
    }
}

Configure appKey and appSecret in AndroidManifest.xml, or set them during initialization.

ThingSdk.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.

    ThingSdk.setDebugMode(true);
    

Demo app

Run the demo app.

After you integrate with the SDK, you can get the AppKey and AppSecret. Make sure the AppKey and AppSecret are consistent with those used on the Tuya IoT Development Platform. Any mismatch will cause the SDK not to work properly. 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 developing with the SDK. Before the development of your app, we recommend that you run the demo app.

Feature overview

The demo app supports the following features:

  • User registration and login
  • The list and details of sites
  • The list and details of locks
  • The list and details of e-keys
  • The list and details of passwords
  • Operation, unlocking, and alert logs

Demo app:

Fast Integration

Run the demo

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

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

  3. Run the code.

Error: Permission Verification Failed

Problem: The following error message is thrown when you run the demo.

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

Solution:

Check whether the AppKey and AppSecret are correctly configured.

Fast Integration