Last Updated on : 2023-05-22 06:38:32
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 operations and maintenance (O&M). Tuya’s peer-to-peer (P2P) connection solution is an ideal option to resolve this issue and achieve the best price performance in O&M. The class TuyaSmartP2pChannel
is implemented for this purpose.
Add TuyaSmartP2pChannelKit
to your Podfile
like this:
pod 'TuyaSmartP2pChannelKit'
Initializes the P2P SDK. This API method is called during login with an account in most cases. The initialization API method must work together with the deinitialization API method for the same P2P object.
API description
- (void)initP2PSDKWithUid:(NSString *)uid;
Parameters
Parameter | Description |
---|---|
uid | The user’s ID. The value for a logged-in account can be obtained in the query of TuyaSmartUser.uid . |
Example
NSString *uid = [TuyaSmartUser sharedInstance].uid;
[[TuyaSmartP2pChannel sharedInstance] initP2PSDKWithUid:uid]
The deinitialization API method is called during logout in most cases. Otherwise, P2P messages might be disordered.
API description
- (void)deInitP2PSDK;
Initializes internal resources to use the P2P capabilities. 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. Initialization is followed by deinitialization at a proper stage.
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 TuyaSmartUser.uid . |
Example
NSString *uid = [TuyaSmartUser sharedInstance].uid;
[[TuyaSmartP2pChannel 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 API method is supported by v3.4.8
.
API description
- (NSString *)connectWithDeviceId:(NSString *)devId mode:(TuyaSmartP2pConnectMode)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. |
TuyaSmartP2pConnectMode
Enum value | Description |
---|---|
TuyaSmartP2pConnectModeInternet | Internet connections prioritized |
TuyaSmartP2pConnectModeLAN | Local area network (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. |
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback