English
English
简体中文
Contact Us
Register
Log In
layoutIndex

Fast Integration with Industry App SDK for Android

Last Updated on : 2022-03-17 05:53:54download

This topic describes how to quickly integrate Tuya Industry 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 app development by using the Industry App SDK.

Integrate with the SDK

  • Before you start, make sure that you have registered an account and completed app authorization. For more information, see Preparation.
  • If you have not installed Android Studio, visit the Android Studio official website to download Android Studio.

Step 1: Create an Android project

  1. Create a project in Android Studio.

  2. Copy the key file key.jks to the root directory of the project.

    key.jks is the key file generated during the preparation steps.

Step 2: Configure build.gradle

  1. 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/*/libc++_shared.so' // This `.so` file exists in multiple `.aar` files. Select the first one.
    		}
    	}
    
    	// Configure the key information with the `keyStore` file generated in the preparation steps.
    	signingConfigs {
    		release {
    			storeFile file("../key.jks")
    			storePassword "xxxx"
    			keyAlias "xxxx"
    			keyPassword "xxxx"
    			v1SigningEnabled true
    			v2SigningEnabled true
    		}
    
    		debug {
    			storeFile file("../key.jks")
    			storePassword "xxxx"
    			keyAlias "xxxx"
    			keyPassword "xxxx"
    			v1SigningEnabled true
    			v2SigningEnabled true
    		}
    	}
    }
    
    dependencies {
    	implementation 'androidx.appcompat:appcompat:1.2.0'
    	implementation 'com.google.android.material:material:1.3.0'
    	implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    
    	// The dependencies to be added.
    	implementation 'com.tencent:mmkv-static:1.2.7'
    	implementation 'com.squareup.okhttp3:okhttp-urlconnection:3.14.9'
    	implementation 'com.alibaba:fastjson:1.2.32'
    	implementation 'com.google.zxing:core:3.3.3'
    	implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
    
    	// The latest stable Industry App SDK for Android.
    	implementation 'com.tuya.smart:iot-app-sdk:1.0.2'
    }
    
  2. Add the Tuya IoT Maven repository URL to the build.gradle file in the root directory.

    repositories {
    	jcenter()
    	google()
    	// The Tuya IoT Maven repository URL.
    	maven {
    	url "https://maven-other.tuya.com/repository/maven-releases/"
    	}
    }
    

Step 3: 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 4: Get AppId and AppSecret

  1. Log in to the Tuya IoT Development Platform.

  2. Go to Cloud > Development, click the target project, and then choose Authorization > App Authorization to view the values for AppId and AppSecret.

    Fast Integration with Industry App SDK for Android

Step 5: Initialize the SDK

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

public class BaseApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        // Configure `appId` and `appSecret`.
        TuyaIoTSDK.builder().init(this, "appId", "appSecret")
                .hostConfig(RegionHostConst.REGION_HOST_CN)
                // `true` means that the debug mode is enabled. Default value: `false`.
                .debug(true)
                .build();
    }
}
  • In debug mode, you can enable SDK logging to facilitate troubleshooting.

  • We recommend that you disable logging in release mode.

Run the demo app

After you integrate with the SDK, you can get the values of AppId, AppSecret, and ApplicationId. Make sure that the values of AppId, AppSecret, and ApplicationId are consistent with those used on the Tuya IoT Development Platform. Any mismatch will cause the SDK development to be failed.

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

Feature overview

The demo app provides basic features such as device pairing, registration, login, and asset management. The features are implemented based on the RESTful APIs of Tuya’s OpenAPI. Tuya Industry App SDK forms the backbone of the Tuya SaaS Development Framework. For more information, see the GitHub project tuya-android-iot-app-sdk-sample.

The demo app supports the following features:

  • User management: log in to and log out of the app

  • Asset management: query and select assets

  • Device pairing: Wi-Fi access point (AP) mode, Wi-Fi Easy Connect (EZ) mode, wired mode, Zigbee sub-device pairing, QR code mode, and QR code scanning to pair NB-IoT devices

  • Device management: query and unbind devices

    Fast Integration with Industry App SDK for Android

Run the demo

  1. According to the keyStore file, configure signingConfigs in the build.gradle file of the project. The storeFile file indicates the location where the .jks file is stored.

    Fast Integration with Industry App SDK for Android

  2. Get the package name. As shown in the following figure, you can customize the package name, but it must be the same as that used on the Tuya IoT Development Platform.

    Fast Integration with Industry App SDK for Android

  3. In Application, copy and paste the values for AppId and AppSecret to the code, as shown in the following figure.

    Fast Integration with Industry App SDK for Android

  4. Run the sample.