Last Updated on : 2023-06-05 02:49:27download
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. The class ThingSmartP2pChannel
is implemented for this purpose.
Add the following content to the Podfile:
pod 'ThingSmartP2pChannelKit'
This initialization must be bound with the current user ID (UID). The initialization API method must work together with the deinitialization API method for the same P2P resource.
API description
- (void)initSDKWithUid:(NSString *)uid;
Parameters
Parameter | Description |
---|---|
uid | The user’s ID. The value for a logged-in account can be obtained in the query of ThingSmartUser.uid . |
Example
NSString *uid = [ThingSmartUser sharedInstance].uid;
[[ThingSmartP2pChannel sharedInstance] initSDKWithUid:uid]
Releases P2P internal resources if P2P capabilities are unused. Then, to use P2P capabilities again, initialize the resources.
API description
- (void)deInitSDK;
Creates a P2P session with a specific device. This feature is supported by App SDK v3.4.8.
API description
- (NSString *)connectWithDeviceId:(NSString *)devId mode:(ThingSmartP2pConnectMode)mode timeout:(NSInteger)timeout success:(void(^)(int p2pHandle))success failure:(void(^)(NSInteger errCode))failure;
Parameters
Parameter | Description |
---|---|
devId | The unique identifier for the device. |
mode | The prioritized connection mode. |
timeout | The timeout value. Unit: milliseconds. |
success | The success callback. The ID of the P2P session is returned. If a negative number is returned, the connection attempt failed. |
failure | The failure callback. |
ThingSmartP2pConnectMode
Enum value | Description |
---|---|
ThingSmartP2pConnectModeInternet | Internet connections prioritized |
ThingSmartP2pConnectModeLAN | LAN connections prioritized |
Return values
Return value | Description |
---|---|
traceId | The value of traceId identifies and tracks the lifecycle of a connection. To close a connection, traceId is required. |
Cancels an ongoing connection attempt. The existing connections are not affected.
API description
- (void)breakConnectWithTraceId:(NSString *)traceId;
Parameters
Parameter | Description |
---|---|
traceId | The value of traceId returned for the created P2P connection. |
Closes a P2P session from a specific device.
API description
- (void)disconnectWithP2pHandle:(int)handle;
Parameters
Parameter | Description |
---|---|
handle | The ID of the P2P session to be closed. The ID is returned when the connection is created. |
Reads data from a specific channel through which a P2P session is created.
API description
- (int)readDataWithP2pHandle:(int)handle channel:(int)channel buffer:(unsigned char*)buffer timeout:(int)timeout;
Parameters
Parameter | Description |
---|---|
handle | The ID of the P2P session. |
channel | The channel number. |
buffer | The buffer that receives data. |
timeout | The timeout value. Unit: milliseconds. |
Return values
Return value | Description |
---|---|
< 0 | An error has occurred. A value of -3 indicates a timeout error. Other values indicate that the connection is closed. |
≥ 0 | The length of the received data. |
Sends data to a specific channel through which a P2P session is created.
API description
- (int)sendDataWithP2pHandle:(int)handle channel:(int)channel buffer:(unsigned char*)buffer length:(int)len timeout:(int)timeout;
Parameters
Parameter | Description |
---|---|
handle | The ID of the P2P session. |
channel | The channel number. |
buffer | The pointer to the data to be sent. |
len | The length of the buffer. |
timeout | The timeout value. Unit: milliseconds. |
Return values
Return value | Description |
---|---|
< 0 | An error has occurred. A value of -3 indicates a timeout error. Other values indicate that the connection is closed. |
≥ 0 | The length of the transmitted data. |
Checks the status of the current P2P session. Polling operations are performed to implement the check.
API description
- (int)activeCheckWithHandle:(int)p2pHandle;
Parameters
Parameter | Description |
---|---|
p2pHandle | The ID of the P2P session. |
Return values
Return value | Description |
---|---|
< 0 | The connection is closed. |
≥ 0 | The connection is normal. |
API description
@property (nonatomic, assign, readonly) NSString *p2pVersion;
In the existing data protocols for Powered by Tuya
(PBT) 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. |
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 initP2PSDKWithUid:
and deinitialize deInitP2PSDK
by yourself during successful login and logout.
Initializes the P2P module. This API method is bound with users’ UID. We recommend that you call it after login.
API description
- (void)initP2PSDKWithUid:(NSString *)uid;
Parameters
Parameter | Description |
---|---|
uid | The user ID (UID). |
Example
ThingSmartP2pChannel *p2pChannel = [ThingSmartP2pChannel sharedInstance];
[p2pChannel initP2PSDKWithUid:uid];
We recommend that you deinitialize the P2P module during logout. Otherwise, P2P messages might be disordered.
API description
- (void)deInitP2PSDK;
Example
ThingSmartP2pChannel *p2pChannel = [ThingSmartP2pChannel sharedInstance];
[p2pChannel deInitP2PSDK];
For more information, see Error Codes.
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback