Fast Integration with Smart Life App SDK for Android

Last Updated on : 2024-05-30 06:43:41download

This topic describes how to quickly integrate Tuya cube App SDK for Android into your development environment, such as Android Studio. It also sheds light on the initialization method and how to enable the debugging mode with a few simple steps. This allows you to run the demo app and get started with your cube app development by using the App SDK.

Prerequisites

SDK versions

  • If you have integrated cube App SDK v1.x.x into your project, follow the instructions in Upgrade Guide and upgrade to the latest version.

  • Starting from cube App SDK for Android v1.0.0, the SDK is classified into the development edition and official edition. For more information, see Pricing.

  • Starting from the cube App SDK for Android v1.0.0, the Android package name configured on the Tuya IoT Development Platform must be the same as the value of packageName in your project. Otherwise, ILLEGAL_CLIENT_ID will be returned.

  • SHA256 hash values are required. For more information, see How to Get SHA1 and SHA256 Keys.

    If you plan to launch the app to Google Play, you must enable the re-signing feature. If this feature is enabled, go to the Tuya IoT Development Platform and configure the SHA256 hash values generated by Google. Otherwise, an illegal client error will occur. For more information, see Enable Signature Protection for Android.

  • If you have added .so libraries to the project, you must remove them and only use the library included in the SDK. If you want to integrate the .so library of a later version, you must remove the library of an earlier version to avoid conflicts or other possible issues.

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.

sdk_version description

  • When the sdk_version is 2.0.0-cube.4, make sure the Cube deployed in the cloud is 2.0 or later.
  • When the sdk_version is 2.1.0-cube, make sure the Cube deployed in the cloud is 2.1 or later.
android {
   defaultConfig {
      ndk {
         abiFilters "armeabi-v7a", "arm64-v8a"
      }
   }
   packagingOptions {
      pickFirst 'lib/*/libc++_shared.so' // An Android Archive (AAR) file contains an Android library. If the .so file exists in multiple AAR files, select the first AAR file.
   }
}
dependencies {
   implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0-alpha04'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    api 'com.thingclips.smart:thingsmart:${sdk_version}'
}

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

buildscript {
    repositories {
        google()
        mavenCentral()
        maven {
            url 'https://maven-other.tuya.com/repository/maven-private/'
            credentials {
                username "xxxxxx"
                password "xxxxxx"
            }
        }
        maven { url "https://maven-other.tuya.com/repository/maven-releases/" }
        maven { url "https://maven-other.tuya.com/repository/maven-commercial-releases/" }
        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/' }
        maven { url 'https://jitpack.io' }
        jcenter() //Warning:this repository is going to shut down soon
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.1.1"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        maven {
            url 'https://maven-other.tuya.com/repository/maven-private/'
            credentials {
                username "xxxxxx"
                password "xxxxxx"
            }
        }
        maven { url "https://maven-other.tuya.com/repository/maven-releases/" }
        maven { url "https://maven-other.tuya.com/repository/maven-commercial-releases/" }
        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/' }
        maven { url 'https://jitpack.io' }
        google()
        mavenCentral()
        jcenter() //Warning:this repository is going to shut down soon
    }
}

Step 3: Integrate with security component

  1. Go to the Get SDK tab, select one or more required SDKs or BizBundles, and then download the App SDK for iOS or Android.

    Fast Integration with Smart Life App SDK for Android
  2. Extract the downloaded package, put security-algorithm.aar in the libs directory of the project, and then make sure dependencies in build.gradle of your project include: implementation fileTree(include: ['*.aar'], dir: 'libs').

    Fast Integration with Smart Life App SDK for Android

Step 4: Configure AppKey, AppSecret, and certificate signature

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

    Fast Integration with Smart Life App SDK for Android
  2. Get the AppKey and AppSecret from the previous step and configure them in AndroidManifest.xml:

    <meta-data
    android:name="THING_SMART_APPKEY"
    android:value="AppKey" />
    <meta-data
    android:name="THING_SMART_SECRET"
    android:value="AppSecret" />
    
  3. Configure the app certificate:

    1. Generate an SHA-256 hash value. For more information, see the document of Android and How to Get SHA1 and SHA256 Keys.
    2. Enter the SHA256 keys in the certificate.

Step 5: Set t_cdc.tcfg

Contact the project manager for specific details.

Fast Integration with Smart Life App SDK for Android

Step 6: 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.**

Step 7: Initialize the SDK

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

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

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

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

Step 8: Destroy the cloud connection

Before you exit the app, you must call the following operation to destroy the cloud connection.

ThingHomeSdk.onDestroy();

Step 9: Enable or disable logging

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

  • We recommend that you disable logging in release mode.

    ThingHomeSdk.setDebugMode(true);
    

Run the demo app

  • After the SDK is integrated into your project, you can get the values of 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 development to be failed. For more information, see Step 4: Configure AppKey, AppSecret, and certificate signature.
  • The demo app that is created in the sample project of cube App SDK is used for reference only. Do not use the demo app for commercial purposes. For more information, see Tuya Developing Service Agreement.

In the following example, a demo app is used to describe the process of app development with the App SDK and indicate the SDK capabilities that you can integrate to implement cube scenarios. Before the development of your app, we recommend that you run the demo app.

Feature overview

The demo app supports the following features:

  • User management: Register and log in to the app account by mobile phone number or email address.

  • Home and device management:

    • Create a home and switch between homes.
    • Display a list of devices in the home and control data points (DPs) of the devices.
    • Rename and remove devices.
  • Pair devices: Multiple pairing methods are supported, including EZ Mode, AP Mode, Bluetooth Low Energy, Zigbee Gateway, Zigbee Subdevice, QR Code, and Bluetooth LE Mesh Sub Device.

    Fast Integration with Smart Life App SDK for Android

For more information, see the GitHub projects tuya-home-android-sdk-sample-java.

Run the demo

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

    Fast Integration with Smart Life App SDK for Android
  2. Make sure that you have completed Step 3: Integrate with security component and Step 4: Configure AppKey, AppSecret, and certificate signature.

  3. Click Run to run the sample.

FAQs

SING_VALIDATE_FALED

Troubleshoot the error message: SING_VALIDATE_FALED

  • 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 AppKey, and AppSecret are correctly configured and consistent with those obtained in Preparation.

ILLEGAL_CLIENT_ID

  • An error message ILLEGAL_CLIENT_ID is returned to indicate an illegal client. How do I troubleshoot this problem?

    • data security is enhanced and SHA256 hash values are required.
    • You must follow the steps in the documentation to get the SHA256 hash values and add the hash values to the Tuya IoT Development Platform. For more information, see Preparation.
  • An error message ILLEGAL_CLIENT_ID is returned to indicate an illegal client. How do I troubleshoot this problem?

    • data security is enhanced.
    • The Android package name configured on the Tuya IoT Development Platform must be the same as the value of packageName in your project. Otherwise, ILLEGAL_CLIENT_ID will be returned.
  • After SHA256 hash values are configured on the Tuya IoT Development Platform and the sample is run, the error message saying ILLEGAL_CLIENT_ID is still returned. How do I troubleshoot the problem?

    Solution: Before you run the sample, add the following signature information to build.gradle of the app module.

    android {
        ...
        signingConfigs {
            debug {
                storeFile file('xxxx.jks')
                storePassword 'xxxxxx'
                keyAlias 'xxx'
                keyPassword 'xxxxxx'
            }
            release {
                storeFile file('xxxx.jks')
                storePassword 'xxxxxx'
                keyAlias 'xxx'
                keyPassword 'xxxxxx'
            }
        }
    }