Scene UI BizBundle

Last Updated on : 2024-04-01 06:08:48download

Scene UI BizBundle for Android provides the service logic and UI to implement adding and editing smart scenes in the smart scene module.

Background

Smart scenes include the following types: tap-to-run and automation.

  • Tap-to-run: Users can add actions to a smart scene. Later, they can tap the tap-to-run scene to run it when needed.
  • Automation: Users can set specific conditions to run a smart scene. When the conditions are met, the smart scene is automatically triggered.

Tuya supports users’ smart life requirements. For example, set weather conditions or device status as triggers for a specific smart scene. When any triggers occur, one or more linked devices will run predefined tasks.

Integrate with the UI BizBundle

Create a project

  1. Integrate Smart Life App SDK for Android into your project with Android Studio and add the framework of the UI BizBundle to your project.

    • You must add the Hilt dependencies before the UI BizBundle can be used. For more information, see the document Dependency injection with Hilt on Google Developers. For more information about how plug-in versions match each other, see the relationship among the versions of Android Gradle plug-in (AGP), Hilt, and Kotlin, as specified in tuya-ui-bizbundle-android-demo.
    • UI BizBundle v5.8.0 and later require the minimum version 2.43.2 of Hilt, including the Hilt plug-in, Hilt artifact, and Hilt annotation processor. Normal compiling can be ensured only in this way.
  2. To use Scene UI BizBundle in your project, add the following configuration in the resource directory res/values/values.xml of the main project:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <bool name="is_scene_support">true</bool>
    </resources>
    
  3. If you have a strong demand for online real-time synchronization of scene data across multiple terminals, it is recommended that you manually execute the following code once after logging in and before using the scene business (it only needs to be called once during the application life cycle):

    SceneHomePipeLine().run()
    

Configure build.gradle


dependencies {
    api enforcedPlatform("com.thingclips.smart:thingsmart-BizBundlesBom:${biz_bom_version}")
    api ("com.thingclips.smart:thingsmart-bizbundle-scene")
    api "com.thingclips.smart:thingsmart:${sdk_version}}"
}

Implement service API methods

Provide services

The UI BizBundle relies on the implementation of IThingSceneBusinessService to provide services.

Example

// Returns the services provided by the Scene UI BizBundle.
IThingSceneBusinessService iThingSceneBusinessService = MicroContext.findServiceByInterface(IThingSceneBusinessService.class.getName());

Add a scene

Accesses the page for adding a scene by home ID.

API description

public abstract void addSceneBean(Activity activity, long homeId, int requestCode);
  • Starting from App SDK v5.0.0, you can call iThingSceneBusinessService.addSceneBean to add a scene. The type of response parameter of onActivityResult is NormalScene.

  • For App SDK versions earlier than v5.0.0, ITuyaSceneBusinessService.addScene is used to add a scene. The type of response parameter of onActivityResult is SmartSceneBean. However, this method has been deprecated and not recommended. You can go to the legacy API document and check the implementation of this feature.

Parameters

Parameter Type Description
activity Activity The object of Activity.
homeId long The home ID. For more information, see Home Information Management.
requestCode int The request code. The value can be returned by onActivityResult.

Example

if(null != iThingSceneBusinessService && homeId != 0) {
  iThingSceneBusinessService.addSceneBean(activity, homeId, requestCode);
}

Edit a scene

Accesses the page for editing a scene by scene data and home ID.

API description

Opens the page for editing a scene.

public abstract void editSceneBean(Activity activity, long homeId, NormalScene sceneBean, int requestCode);
  • Starting from App SDK v5.0.0, you can call iThingSceneBusinessService.editSceneBean to edit a scene. The type of response parameter of onActivityResult is NormalScene.

  • For App SDK versions earlier than v5.0.0, iThingSceneBusinessService.editScene is used to edit a scene. The type of response parameter of onActivityResult is SmartSceneBean. However, this method has been deprecated and is not recommended. You can go to the legacy API document and check the implementation of this feature.

Parameters

Parameter Type Description
activity Activity The object of Activity.
homeId long The home ID. For more information, see Home Information Management.
sceneBean NormalScene The object of scene data. You can call smart scene API methods to get the required data.
requestCode int The request code. The value can be returned by onActivityResult.

Example

ThingHomeSdk.getSceneServiceInstance().baseService().getSimpleSceneAll(homeId,
                new IResultCallback<List<NormalScene>>() {

            @Override
            public void onSuccess(List<NormalScene> normalScenes) {
                if (!normalScenes.isEmpty()) {
                    NormalScene sceneBean = normalScenes.get(0);
                    if (null != iThingSceneBusinessService) {
                        iThingSceneBusinessService.editSceneBean(SceneActivity.this, homeId, sceneBean, requestCode);
                    }
                }
            }

            @Override
            public void onError(String errorCode, String errorMessage) {

            }
        });

Set geolocation options

Sets the geographic location before a user can use the scene conditions and validity period. Otherwise, city information cannot be automatically returned in specific searches. However, users can still select a target city from the city list.

API description

public abstract void setAppLocation(double longitude, double latitude);

Parameters

Parameter Description
longitude The longitude, provided by the third-party map service that is integrated into your app.
latitude The latitude, provided by the third-party map service that is integrated into your app.

Example

if(null != iThingSceneBusinessService){
    iThingSceneBusinessService.setAppLocation(lng, lat);
}

Set app map class

Provides the geographic location in the scene conditions. It can be ignored if accounts that are registered in locations outside mainland China are not managed. The map class must be set for accounts registered in locations outside mainland China. Otherwise, the cities in mainland China are queried by default.

API description

public abstract void setMapActivity(Class<? extends Activity> clazz);

Parameters

Parameter Description
clazz The object of the map Activity class. You can integrate the Maps Service UI BizBundle to set this parameter.

Example

if(null != iThingSceneBusinessService){
    // The following example shows the built-in map package that can be available after the Maps Service UI BizBundle is integrated. You can use a third-party map for implementation as needed.
    iThingSceneBusinessService.setMapActivity(com.thingclips.smart.map.generalmap.ui.GeneralMapActivity.class);
}

Set selected location from map

Sends a specified location on the map to the UI BizBundle after the map class is integrated. This helps to update the location information.

API description

public abstract void saveMapData(double longitude, double latitude, String city, String address);

Parameters

Parameter Description
longitude The longitude.
latitude The latitude.
city The city information.
address The address information.

Example

if(null != iThingSceneBusinessService){
    iThingSceneBusinessService.saveMapData(lng, lat, city, address);
}