Integrate SDK

Last Updated on : 2023-12-07 03:15:41download

Create Project

Build your project in the Android Studio.

Configure the build.gradle

Add the following codes to the build.gradle file.

android { defaultConfig { ndk { abiFilters "armeabi-v7a", "arm64-v8a" } } packagingOptions { pickFirst 'lib/*/libc++_shared.so' // multi aar has such so, should pick first. } } dependencies { implementation 'com.alibaba:fastjson:1.1.67.android' implementation 'com.squareup.okhttp3:okhttp-urlconnection:3.14.9' // Tuya Home's current latest release version: implementation 'com.tuya.smart:tuyasmart:3.20.0' } repositories { mavenLocal() jcenter() google() }
  • The Tuya Smart sdk solely supports the platform of armeabi-v7a architecture by default. Developer may refer to the GitHub if you need other platforms.
  • After version 3.11.0, the so libraries of armeabi-v7a, arm64-v8a, armeabi platforms have been integrated into sdk. Please remove the relevant so-banks of sdk manually placed in the local, and use the default provided by sdk.

Integrated security image

Click the download button to download the security image.

Integrate SDK

Rename the downloaded security image to “t_s.bmp” and move it to the assets/ folder in the project directory.

Integrate SDK

Set the AndroidManifest.xml

Set appkey and appSecret in the AndroidManifest.xml file, and configure corresponding permissions, etc.

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

Proguard Configuration

please add this proguard configuration to proguard-rules.pro files.

#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.**

Use the SDK function in codes

The TuyaHomeSdk is the outbound interface of the Smart Home, and the operations include network configuration, initiation, control, room, group and ZigBee.

Initiate SDK in the Application

Description

It is used to initiate components of communication services, etc.

Example

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

Note:

The appId and appSecret need to be configured in the AndroidManifest.xml file or the build environment or the codes.

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

Logout of the Tuya Smart Cloud connection

The following interface needs to be invoked to log out of the App.

TuyaHomeSdk.onDestroy();

Monitor the invalidity of registered session

Description

Because of abnormality or long-time absence of operation for 45 or more days, the session will become invalid, and user has to log out of the App and log in again to obtain the session.

Declaration

// Monitor the invalidity of session. TuyaHomeSdk.setOnNeedLoginListener(INeedLoginListener needLoginListener); needLoginListener.onNeedLogin(Context context);

Example

public class TuyaSmartApp extends Application { @Override public void onCreate() { super.onCreate(); // Register in the App. TuyaHomeSdk.setOnNeedLoginListener(new INeedLoginListener(){ @Override public void onNeedLogin(Context context) { } });

[info] Note

  • In case of this kind of callback, please go to the login page and require the user to log in again.

Debug mode

In the debug mode, you can enable the sdk log switch to view more log information and help you locate the problem quickly. It is recommended to turn off the log switch in release mode.

TuyaHomeSdk.setDebugMode(true);