P2P Download from Robot Vacuums

Last Updated on : 2024-05-10 05:47:29download

A robot vacuum that supports peer-to-peer (P2P) capabilities provides P2P download channels. Map data and cleaning routes are directly transmitted from the robot vacuum to the app through P2P channels. The data does not need to be uploaded to a cloud storage server for downloading. Therefore, data transmission becomes more efficient at a lower cost of cloud storage.

The robot vacuum P2P feature is developed based on the P2P capabilities of the IPC vertical category. To integrate with the robot vacuum P2P library, you must also integrate the latest version of Tuya P2P SDK.

Entry to the feature

IThingSweeperKitSdk iThingSweeperKitSdk = ThingOptimusSdk.getManager(IThingSweeperKitSdk.class);

IThingSweeperP2P thingSweeperP2P = iThingSweeperKitSdk.getSweeperP2PInstance(devId);

Access the P2P service

API description

/**
*
* @param callback The callback that is executed when a P2P connection is created.
*/
void connectDeviceByP2P( SweeperP2PCallback callback);

Parameters

Parameter Description
callback The callback that is executed when a P2P connection is created.

Example

thingSweeperP2P.connectDeviceByP2P(new SweeperP2PCallback() {
    @Override
    public void onSuccess() {
            // The success callback.
            }

    @Override
    public void onFailure(int i) {
            // The failure callback.
            }
            });

Enable P2P downloading

API description

Enable file transmission after the success callback of connectDeviceByP2P. If a P2P connection is closed unexpectedly, a failure callback is executed. For more information about the error codes, see Error codes. The failure callback indicates that the device might have a network exception. To fix the problem, you can call connectDeviceByP2P to try to reconnect the device.

/**
*
* @param downloadType     The method to download data.
* @param callback         The callback that is executed when P2P downloading is enabled.
* @param callback2        The callback for P2P data.
*/
void startObserverSweeperDataByP2P(DownloadType downloadType, SweeperP2PCallback callback, SweeperP2PDataCallback callback2);

Parameters

Parameter Description
downloadType The method to download data through P2P connections. Valid values:
  • DownloadType.P2PDownloadTypeOnce: The data downloading is stopped after the existing device data is downloaded. New data will not be downloaded.
  • DownloadType.P2PDownloadTypeStill: Device data is continuously downloaded until the device automatically stops transferring data.
callback The callback that is executed when P2P downloading is enabled.
callback2 The callback that is executed when data is received through P2P connections.

Parameters of SweeperP2PBean

Parameter Description
type The data type. Valid values:
  • 0: map data
  • 1: cleaning route data
  • 3: navigation route data
  • -1: error data
data The data downloaded through P2P connections.

Example

thingSweeperP2P.startObserverSweeperDataByP2P(DownloadType.P2PDownloadTypeOnce, new SweeperP2PCallback() {
    @Override
    public void onSuccess() {
            // The success callback.
            }

    @Override
    public void onFailure(int i) {
            // The failure callback.
            }
            }, new SweeperP2PDataCallback() {
    @Override
    public void receiveData(int type, @Nullable SweeperP2PBean sweeperP2PBean) {
            // The callback that is executed when data is received through P2P connections. `type` indicates the type of received data.
            }

    @Override
    public void onFailure(int i) {
            // The failure callback.
            }
            });

Do not call thingSweeperP2P.onDestroyP2P(); in the onFailure of this callback.

Disable P2P downloading

Pauses data transmission through P2P channels, but retains P2P channels. You can call startObserverSweeperDataByP2P to continue downloading data.

/**
*
* @param callback         The callback that is executed when P2P downloading is disabled.
*/
void stopObserverSweeperDataByP2P(SweeperP2PCallback callback);

Parameters

Parameter Description
callback The callback that is executed when P2P downloading is disabled.

Example

thingSweeperP2P.stopObserverSweeperDataByP2P(new SweeperP2PCallback() {
    @Override
    public void onSuccess() {
            // The success callback.
            }

    @Override
    public void onFailure(int i) {
            // The failure callback.
            }
            });

Do not call thingSweeperP2P.onDestroyP2P(); in the onFailure of this callback.

Check P2P connection status


/**
* Checks P2P connection status.
*/
boolan checkP2PActive();

Example

thingSweeperP2P.checkP2PActive();

Disable the P2P service

Stops downloading data, destroys P2P channels, and releases the occupied memory and thread resources.

/**
*
* @param callback         The callback that is executed when P2P downloading is disabled.
*/
void onDestroyP2P();

Example

thingSweeperP2P.onDestroyP2P();

Stop P2P reconnection

P2P SDK performs automatic reconnection after a session is closed in the foreground or is brought to the foreground from the background. Currently, Tuya provides the method to stop P2P automatic reconnection. Thus, you can determine when reconnection will be implemented.

/**
* Stop automatic reconnection
* @param isStopReconnect Specifies whether to stop automatic reconnection. Default value: `false`.
*/
void stopReconnect(boolean isStopReconnect);

Example

thingSweeperP2P.stopReconnect(true);

Error codes

Error code Description
-9000 The data of deviceBean is missing.
-9001 The required P2P SDK is not integrated.
-9002 The P2P connection is being created.
-9003 The specified device is offline.
-9004 Timeout has occurred.
-9005 No map files are found.
-9006 The P2P connection is closed.

For more information about error codes, see the error codes of the IPC SDK.

FAQs

  • Problem: The SweeperP2PCallback API method or the SweeperP2PCallback class is marked in red and cannot be found. The arr file of the dependency library is found. But the following error message is returned:

    // This class file was compiled with different version of Kotlin compiler and can't be decompiled.
    // Current compiler ABI version is 1.1.16
    // File ABI version is 1.5.1
    
  • Solution: Try to update the Kotlin version of Android Studio at Preferences > Languages & Framework > Kotlin.