Last Updated on : 2024-06-27 08:31:39download
In traditional solutions, data exchange between the app and devices is forwarded through a server. Transmitting large volumes of data requires substantial server space and bandwidth, resulting in high operating costs. Tuya’s peer-to-peer (P2P) connection solution effectively reduces network costs with the ThingSmartP2pChannel
class.
Add the code snippet below to the Podfile:
pod 'ThingSmartP2pChannelKit'
Initialize the internal P2P resource and bind it to the user’s UID. Resource initialization and deinitialization are used as a pair.
API description
- (void)initSDKWithUid:(NSString *)uid;
Parameters
Parameter | Description |
---|---|
uid | The user ID, which can be obtained through ThingSmartUser.uid after the user logs in. |
Example
NSString *uid = [ThingSmartUser sharedInstance].uid;
[[ThingSmartP2pChannel sharedInstance] initSDKWithUid:uid]
Release internal P2P resources when P2P capabilities are not in use. To use P2P capabilities again, resource initialization is required.
API description
- (void)deInitSDK;
Establish a P2P connection session with the specified device, applying to v3.4.8.
API description
A new method, where the errCode
in the callback is the detailed error code.
- (NSString *)connectWithDevId:(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 of the device. |
mode | The preferred connection mode. |
timeout | The connection timeout, in milliseconds. |
success | The success callback, returning the ID of the P2P session. A negative number indicates connection failure. |
failure | The failure callback. |
ThingSmartP2pConnectMode
Enum value | Description |
---|---|
ThingSmartP2pConnectModeInternet | Internet connections prioritized |
ThingSmartP2pConnectModeLAN | LAN connections prioritized |
Return value
Return value | Description |
---|---|
traceId | The tracking identifier for the connection lifecycle. traceId must be specified when canceling a P2P connection. |
API description
This method does not provide detailed error codes. Use - (NSString *)connectWithDevId:mode:timeout:success:failure:
instead for detailed error codes.
- (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 of the device. |
mode | The preferred connection mode. |
timeout | The connection timeout, in milliseconds. |
success | The success callback, returning the ID of the P2P session. A negative number indicates connection failure. |
failure | The failure callback. |
ThingSmartP2pConnectMode
Enum value | Description |
---|---|
ThingSmartP2pConnectModeInternet | Internet connections prioritized |
ThingSmartP2pConnectModeLAN | LAN connections prioritized |
Return value
Return value | Description |
---|---|
traceId | The tracking identifier for the connection lifecycle. traceId must be specified when canceling a P2P connection. |
Cancel a P2P connection attempt. Established connections will not be affected.
API description
- (void)breakConnectWithTraceId:(NSString *)traceId;
Parameters
Parameter | Description |
---|---|
traceId | The tracking identifier for the established P2P connection. |
API description
- (void)disconnectWithP2pHandle:(int)handle;
Parameters
Parameter | Description |
---|---|
handle | The ID of the P2P session to disconnect, which is returned when P2P is connected. |
Read data from the specified channel through a specific P2P session.
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 ID. |
buffer | The receive buffer. |
timeout | The timeout, in milliseconds. |
Return values
Return value | Description |
---|---|
< 0 | Data receiving error. -3 indicates a timeout, while other errors indicate a lost connection. |
≥ 0 | The length of the received data. |
Send data to the specified channel through a specific P2P session.
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 ID. |
buffer | The pointer to the data to be sent. |
len | The length of the buffer. |
timeout | The timeout, in milliseconds. |
Return values
Return value | Description |
---|---|
< 0 | Data sending error. -3 indicates a timeout, while other errors indicate a lost connection. |
≥ 0 | The length of the data sent. |
Poll the current status of a P2P session.
API description
- (int)activeCheckWithHandle:(int)p2pHandle;
Parameters
Parameter | Description |
---|---|
p2pHandle | The ID of the P2P session. |
Return value
Return value | Description |
---|---|
< 0 | The connection is lost. |
≥ 0 | The connection is normal. |
Property description
@property (nonatomic, copy, readonly) NSString *p2pVersion;
Tuya’s data protocol for smart devices specifies that each P2P session is initialized with eight data channels, six of which are predefined and in use.
Channel ID | Description |
---|---|
0 | Receive and send control commands. |
1 | Transmit video data. |
2 | Transmit audio data. |
3 | Transmit video clips from the memory card for playback. |
4 | Transmit raw data. |
5 | Download files. |
If you are using a non-Tuya login system, the P2P SDK cannot monitor the user’s login status. You should initialize the P2P SDK with initP2PSDKWithUid:
when users log in and deinitialize it with deInitP2PSDK
when users log out.
Initialize the P2P SDK module and bind it to the user’s UID. It is recommended to call this method after users log in.
API description
- (void)initP2PSDKWithUid:(NSString *)uid;
Parameters
Parameter | Description |
---|---|
uid | The UID. |
Example
ThingSmartP2pChannel *p2pChannel = [ThingSmartP2pChannel sharedInstance];
[p2pChannel initP2PSDKWithUid:uid];
Deinitialize the P2P module. It is recommended to call this method when users log out to avoid P2P message errors.
API description
- (void)deInitP2PSDK;
Example
ThingSmartP2pChannel *p2pChannel = [ThingSmartP2pChannel sharedInstance];
[p2pChannel deInitP2PSDK];
See the complete list of error codes.
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback