P2P Capabilities

Last Updated on : 2023-09-19 02:33:12download

In traditional solutions, a server forwards data between an app and a device. During peak hours, this transmission mode consumes a high number of space and traffic resources on the service and results in costly business operations. Tuya’s peer-to-peer (P2P) connection solution is an ideal option to resolve this issue and achieve the best price performance.

Integrate with component

  • Configure the file build.gradle in the root directory of your project.

    allprojects {
        repositories {
            ...
            maven {
                url "https://maven-other.tuya.com/repository/maven-releases/"
            }
        ...
        }
    }
    
  • Configure build.gradle in the app directory.

    dependencies {
        ...
        implementation 'com.thingclips.smart:thingsmart-p2p-channel-sdk:3.4.40'
        ...
    }
    

    The IPC SDK already supports the P2P capabilities. No additional integration is required to implement the capabilities.

Get a P2P object

Returns a P2P object that is a singleton.

API description

IThingP2P p2pSDK = ThingIPCSdk.getP2P();

Initialize the SDK

Initializes internal P2P resources. This initialization must be bound with the current user ID (UID).

API description

int init(String localId);

Parameters

Parameter Description
localId The user’s UID. The value for a logged-in account can be obtained from User of the data model in User Account Management.

Example

IThingP2P p2p = ThingIPCSdk.getP2P();
if (p2p != null) {
    p2p.init("xxx");
}

Deinitialize the SDK

Destroys internal P2P resources. Then, to use P2P capabilities again, initialize the resources.

API description

int deInit();

Example

IThingP2P p2p = ThingIPCSdk.getP2P();
if (p2p != null) {
    p2p.deInit();
}

Create a P2P connection

API description

Creates a P2P connection to a specific device. The value of traceId in the response indicates the connection identifier that can be used to cancel an ongoing connection attempt. In the callback, the handle of the successful connection is returned.

This API method is only applicable to apps developed based on App SDK v3.4.8 or later.

 String connect(String remoteId, int lanMode, int timeout, IThingP2PCallback callback);

Parameters

Parameter Description
remoteId The device ID.
lanMode The prioritized connection mode.
timeout The connection timeout value.
callback The connection result.
  • When the value is larger than or equal to 0, the P2P connection is successful and the session handle of the connection is returned.
  • If the value is less than 0, the connection failed and an error message is returned.
lanMode Description
0 Automatically selected
1 Internet connections prioritized
2 LAN connections prioritized

Return values

Return value Description
traceId The unique identifier of the connection. It can be used to cancel an ongoing connection attempt.

Cancel an ongoing connection attempt

Cancels an ongoing connection attempt. The existing connections are not affected.

API description

int connectBreak(String traceId);

Parameters

Parameter Description
traceId The unique identifier of the connection.

Close a connection

API description

int disConnect(int handle);

Parameters

Parameter Description
handle The handle of the connection.

Receive data

API description

int recvData(int handle, int channel, byte[] data, int dataLen, int timeout);

Parameters

Parameter Description
handle The handle of the connection.
channel The channel number.
data The buffer that receives data.
dataLen The length of the received data.
timeout The timeout value. Unit: milliseconds.

Return values

Return value Description
A value less than 0 is returned. An error has occurred. A value of -3 indicates a timeout error. Other values indicate that the connection is closed.
A value larger than or equal to 0 is returned. The length of the received data.

Send data to the peer

API description

int sendData(int handle, int channel, byte[] data, int dataLen, int timeout);

Parameters

Parameter Description
handle The handle of the connection.
channel The channel number.
data The content to be sent.
dataLen The length of the data to be sent.
timeout The timeout value. Unit: milliseconds.

Return values

Return value Description
A value less than 0 is returned. An error has occurred. A value of -3 indicates a timeout error. Other values indicate that the connection is closed.
A value larger than or equal to 0 is returned. The length of the transmitted data.

Check the connection status

API description

int activeCheck(int handle);

Parameters

Parameter Description
handle The handle of the connection.

Return values

Return value Description
A value less than 0 is returned. The connection is closed.
A value larger than or equal to 0 is returned. The connection is normal.

Query the P2P SDK version number

API description

String getVersion();

Channel number

In the existing data protocols for Tuya-enabled devices, eight data channels are initialized for each P2P session and six of these channels are defined and used.

Channel number Description
0 The channel to send and receive control commands.
1 The channel to transmit video data.
2 The channel to transmit audio data.
3 The channel to play back and transmit video footage on an SD card.
4 The data pass-through channel.
5 The channel to download and transmit files.

Non-Tuya enabled login system

If you use a login system that is not implemented by Tuya when you integrate with P2P capabilities, the SDK cannot listen for the login status of users. In this case, you must initialize initP2P(String localId) and deinitialize deInitP2P() by yourself during successful login and logout.

Initialize P2P module

Initializes the P2P module. This API method is bound with users’ UID. We recommend that you call it after login.

API description

int initP2P(String localId);

Parameters

Parameter Description
localId The user’s UID. The value for a logged-in account can be obtained from User of the data model in User Account Management.

Example

IThingP2P p2p = ThingIPCSdk.getP2P();
if (p2p != null) {
    p2p.initP2P("xxx");
}

Deinitialize P2P module

We recommend that you deinitialize the P2P module during logout. Otherwise, P2P messages might be disordered.

API description

int deInitP2P();

Error codes

Error code Description Possible cause
0 No error has occurred. -
-1 The SDK is uninitialized. -
-3 A connection timeout error has occurred. Reconnection is recommended. -
-5 The parameter is invalid. -
-11 The session is invalid. -
-12 The connection is closed by the device. -
-13 The session has been closed. -
-14 The session is closed by an IPC. -
-16 The listener is interrupted. -
-19 The connection is interrupted. The break() method has been called.
-23 The number of sessions has reached the upper limit. -
-24 The preconnection is invalid. -
-25 The preconnection is closed. -
-26 The preconnection is activated. -
-29 The system has run out of memory. -
-30 The API request failed. -
-31 The preconnection is not supported. -
-100 The peer end has terminated the peer-to-peer (P2P) communication. -
-102 The device runs in privacy mode and the requested session is closed. -
-103 Failed to create a data sending thread. The requested session is closed. -
-104 The maximum number of sessions has been reached. The requested session is closed. -
-105 The authentication failed. The requested session is closed. Reconnection is recommended. -
-106 The session is closed as expected. The return value of recv() is -13. -
-108 A zombie session that cannot be terminated or rolled back has occurred due to a malfunction or deadlock. The session is closed forcibly. No data communication occurs in a period of more than 20 minutes.
-109 You have called the open API to manually closed a session. -
-110 You have called the open API to exit the P2P module. -
-111 You have called the open API to make the device run in privacy mode. -
-113 The device is processing a call. -
-114 Failed to request application resources. -
-1000 The request parameter is invalid. -
-1001 The API request failed. -
-1002 Invalid data has been returned in the response. -
-1010 The parameters of the method are invalid. -
-1011 Failed to call the API method. -
-1020 The device information is empty. Check whether the list of devices has been returned.
-1021 P2pType is not supported. A device or configuration error has occurred.