Device Control (Android)

Last Updated on : 2022-02-17 05:53:44download

Smart devices have gained ground in many homes and industries, but how to deliver secure and flexible permission management has become a challenge. In the old days, application scenarios are pretty simple, and a mobile app was used to control only one smart device. However, nowadays, more and more IoT devices are serving smart scenes. Each mobile app is used to control a host of smart devices.

In specific scenarios, certain devices are controlled by multiple users. For example, furniture-like smart power strips can be switched on or off by all home members. In this case, the following requirements must be met: a mobile app can be used to control multiple devices. Multiple users can be authorized to mutually control multiple devices. Therefore, group management and smart scenes are designed to achieve these purposes.

This topic describes how to achieve device control.

DPs

  • The dps attribute of the DeviceBean class defines the status of a device. The status is known as one or more data points (DPs).

  • In each array of dps, key matches dpId of a DP, and dpValue matches the value of the DP.

    You can check the definitions of DPs for a product on the Tuya IoT Platform. The following figure shows a list of DPs.

    Device Control (Android)

    For more information, see Function Definition.

Command format

Send a control command to a device in the following format:

{
	"(dpId)":"(dpValue)"
}

Examples of DPs

The following sample code shows the DPs 101, 102, 103, 104, and 105 of a light product that is created on the Tuya IoT Platform:

// The DP of Boolean type with the `dpId` value of `101`. Command: switches on the light.
dps = {"101": true};

// The DP of String type with the `dpId` value of `102`. Command: sets the red, green, and blue (RGB) value to `ff5500`.
dps = {"102": "ff5500"};

// The DP of Enumeration type with the `dpId` value of `103`. Command: sets the lighting level to `2`.
dps = {"103": "2"};

// The DP of Value type with the `dpId` value of `104`. Command: sets the temperature value to `20`.
dps = {"104": 20};

// The DP of Raw type with the `dpId` value of `105`. Command: passes through the infrared data `1122`.
dps = {"105": "1122"};

// Sends multiple DPs in one command.
dps = {"101": true, "102": "ff5500"};

mDevice.publishDps(dps, new IResultCallback() {
		@Override
		public void onError(String code, String error) {
		// The error code 11001 is returned due to the following causes:
		//1: Data has been sent in an incorrect format. For example, the data of String type has been sent in the format of Boolean data.
		//2: Read-only DPs cannot be sent. For more information, see SchemaBean getMode. `ro` indicates the read-only type.
		//3: Data of Raw type has been sent in a format rather than a hexadecimal string.
		}
		@Override
		public void onSuccess() {
		}
	});

Data types must be correct when DPs are sent. Examples:

  • A DP of Value type can be sent in the format of {"104": 25} rather than {"104": "25"}.
  • A byte array of Raw type is a hexadecimal string with an even number of digits. In this case, the array can be sent in the format of {"105": "0110"} rather than {"105": "110"}.

Control a device

You must initialize device control first. DPs can be sent to a device to change the device status or features and control the device.

API description

Devices can be controlled in three modes: local area network (LAN) control, cloud control, and automatic control. If a device is connected to a LAN, it is controlled over the LAN in priority. If not, it is controlled in the cloud.

  • Control over the LAN

    ITuyaDevice.publishDps(dps, TYDevicePublishModeEnum.TYDevicePublishModeLocal, callback);
    
  • Control in the cloud

    ITuyaDevice.publishDps(dps, TYDevicePublishModeEnum.TYDevicePublishModeInternet, callback);
    
  • Automatic control

    ITuyaDevice.publishDps(dps, TYDevicePublishModeEnum.TYDevicePublishModeAuto, callback);
    

    or

    ITuyaDevice.publishDps(dps, callback);
    

    We recommend that you call ITuyaDevice.publishDps(dps, callback) to control devices.

  • Control the device with a specific channel

    ITuyaDevice.publishDps(dps, orders, callback);
    

Parameters

Parameter Description
dps The DPs of the device in the format of JSON strings. For more information, see Set DPs.
publishModeEnum The device control method.
callback The callback that indicates whether the control request is successful.
orders For the channel order, please refer to the CommunicationEnum enumeration class. For example, “[3, 1]” specifies priority Bluetooth control, and Bluetooth does not go online to cloud control.

Java example

The following sample code shows how to switch on a light by using DP 101:

mDevice.publishDps("{\"101\": true}", new IResultCallback() {
	@Override
	public void onError(String code, String error) {
		 Toast.makeText(mContext, "Failed to switch on the light.", Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onSuccess() {
		 Toast.makeText(mContext, "The light is switched on successfully.", Toast.LENGTH_SHORT).show();
	}
});
  • Device control is not finished after a command is sent. The device has been controlled as expected only after IDevListener onDpUpdate returns the response.
  • The command string is converted to a JSON string in the format of Map<String,Object>. In this string, String matches dpId, and Object matches dpValue.
  • Each command string can include multiple DPs in each request.