Is this page helpful?
YesNoLast Updated on : 2022-01-07 07:03:31download
This tutorial introduces to you the Tuya Industry App SDK for Android. The techniques you learn in the tutorial are fundamental to build IoT apps by using this SDK. In the following hands-on approaches, the sample code for Java is used to describe the process.
In this tutorial, you will learn the following sections:
Get a Free Developer Account Code Sample in GitHub
Tuya IoT Industry App SDK for Android is an Android library. Therefore, this tutorial assumes that you have a basic understanding of Android development, either in Java or Kotlin, and you are familiar with Android Studio.
The SDK is written in Java, but the concept and API can be applied to Kotlin. This tutorial is using Java as the programming language.
Tuya IoT Industry App SDK is an important part of the Tuya SaaS Development Framework product families. This tutorial provides examples of multiple functions based on Tuya OpenAPI, such as device pairing, user log in and registration, and asset management.
To use the SDK and the capabilities of the Tuya IoT Development Platform, you must first register your app to the Tuya IoT Development Platform and then install the SDK.
targetSdkVersion
is set to 30
or later in your project.Add the maven repository in the build.gradle
of your project.
maven {
url "https://maven-other.tuya.com/repository/maven-releases/"
}
Add Tuya IoT Industry App SDK in the build.gradle
of your module.
dependencies {
implementation 'com.tuya.smart:iot-app-sdk:1.0.0'
}
In your application, add the following code:
TuyaIoTSDK.builder().init(getApplicationContext(),
"$clientID","$clientSecret")
.hostConfig(RegionHostConst.REGION_HOST_CN)
.debug(true)
.build();
The preceding clientID
and clientSecret
are obtained in the Authorization Key field that is used when you create the application.
The class TYUserManager provides methods to log in as a user and keep the session.
Currently, the SDK cannot be used to register a user. All registration processes are finished on the platform. You must manually add users to your project to log in successfully.
Call TYUserManager.getUserBusiness().login(String userName, String passWord, ResultListener<T> listener)
, and add the userName
and passWord
to log in as a user:
TYUserManager.getUserBusiness().login(userName, password,
new ResultListener<BizResponse>() {
@Override
public void onFailure(String s, String s1) {
//failure
}
@Override
public void onSuccess(BizResponse bizResponse) {
//success
}
});
Tuya IoT Industry App SDK provides several pairing modes for different conditions, such as the Access Point (AP) mode, wired mode, Narrowband Internet of Things (NB-IoT) mode, QR code mode, and sub-device mode.
AP mode: also known as the hotspot mode. A mobile phone connects to the hotspot of a smart device, and the two parties establish a socket connection to exchange data through the agreed port.
Wi-Fi Easy Connect mode: also known as the EZ mode. This mode relies on the communication between the devices. A device can match the network after an existing device is successfully matched.
Wired devices mode: Devices are connected to the router through an Ethernet cable. For example, Zigbee wired gateways and wired cameras can be such devices.
Sub-device mode: Zigbee sub-devices and Bluetooth sub-devices can be such devices. These devices interact with the app and cloud through gateways.
QR code mode: A camera device gets the pairing information by scanning the QR code on the app.
Process:
Get a token:
Before the pairing process in AP mode, the SDK must get the pairing token from the Tuya IoT Cloud. The validity period of the token is 10 minutes, and the token becomes invalid once the pairing operation succeeds. A new token must be obtained if you want to pair devices again.
To get the token, call TYActivatorManager.getActivator().getRegistrationToken
.
TYActivatorManager.getActivator().getRegistrationToken(mAssetId,
mUid,
Constant.CONFIG_TYPE_AP,
IoTCommonUtil.getTimeZoneId(),
"",
new ResultListener<RegistrationTokenBean>() {
@Override
public void onFailure(String s, String s1) {
}
@Override
public void onSuccess(RegistrationTokenBean registrationTokenBean) {
}
);
There are three properties in RegistrationTokenBean:
region
: the device region generated by the server.token
: the device registration token generated by the server.secret
: the device registration secret generated by the server.Activate the device:
Initialize ActivatorBuilder.
activatorBuilder = new ActivatorBuilder(mContext,
ssid != null ? ssid : "",
password != null ? password : "",
region != null ? region : "",
token != null ? token : "",
secret != null ? secret : "");
Then, create IAPActivator with the builder, and call start()
.
activator = TYActivatorManager.newAPActivator(activatorBuilder);
activator.start();
After the successful or failed pairing operation, call stop()
.
activator.stop()
Get the result:
To get the pairing result, call TYActivatorManager.getActivator().getRegistrationResultToken
in TYActivatorManager.
Device pairing is an asynchronous process. The duration of device pairing depends on the network quality. You must make the API request to check the device pairing result. The polling task is stopped when a list of paired devices is returned or when the pairing task timed out.
The polling task is suggested to run once per second. The timeout value of the pairing task is recommended to be 100
seconds.
The process is similar to AP mode. The difference is that you must use IEZActivator instead of IAPActivator
.
activator = TYActivatorManager.newEZActivator(activatorBuilder);
activator.start();
In this mode, the wired device is connected to the network. You do not need to provide the name and password of the router during the device pairing.
In the following figure, a Zigbee wired gateway is used as an example to describe the wired gateway pairing process.
Get a token:
This process is the same as that in AP mode.
Search the device:
Initialize IWiredActivator with ActivatorBuilder and call start()
.
wiredActivator = TYActivatorManager.newWiredActivator(activatorBuilder);
wiredActivator.start();
After the successful or failed pairing operation, call stop()
.
wiredActivator.stop()
Get the result:
This process is the same as that in AP mode.
Search the device:
To pair a sub-device with a gateway, firstly, you need to enable the gateway to discover sub-devices. For this purpose, you can call TYActivatorManager.getActivator().discoverSubDevices
in TYActivatorManager.
TYActivatorManager.getActivator().discoverSubDevices(mDeviceId, 100, new ResultListener<Boolean>() {
@Override
public void onFailure(String s, String s1) {
}
@Override
public void onSuccess(Boolean result) {
}
});
Get the result:
This process is the same as that in AP mode.
In this mode, a QR code is used to pair a device, such as an IP camera. The device scans the QR code. A generated QR code image must be displayed on a mobile phone.
IQRCodeActivator gives you methods to implement the pairing mode. Initialize a device instance by calling TYActivatorManager.newQRCodeActivator(mBuilder)
, and then call generateQRCodeImage()
on the instance. The first call returns the data that is required to generate a QR code. Then, you must create the QR code image. The latter call returns a value of Bitmap
that can be rendered in ImageView
.
TYDeviceManager includes device management methods.
To get the details of one or more devices, call queryDevicesInfo()
.
To control a device, call queryCommandSetFromCategory()
to return a device instruction set from a specific category, and call queryCommandSetFromDeviceID()
to return a device instruction set from a device ID. StandardCommandBean
in DeviceStandardCommandBean
contains the following information about a command:
code
: used to send the command.value
: the range of this command.type
: the value type of the command.To send a command to control a device, you must construct StandardCommandBean
including code
and value
. For example, to switch on or off a light bulb, you can set code
to switch
and set type
to boolean
in DeviceStatusBean
. You must construct DeviceStatusBean
in a similar way as the following setting:
commend = new DeviceStatusBean(code: "switch", value: true)
Then, call sendCommands(command)
to send the command.
To get the latest status of the device, use queryDeviceStatus()
or queryDevicesStatus()
.
TYAssetManager contains methods to interact with assets. Currently, the SDK cannot be used to add, modify, or delete an asset. All these operations can only be finished on the Tuya IoT Development Platform. For more information, see Manage Assets.
You can call queryAssets()
to get all assets that are assigned to a specific user. You can also call queryDevicesByAssetId()
to get the devices that are bound to a specific asset.
Is this page helpful?
YesNoIs this page helpful?
YesNo