P2P Download from Robot Vacuums

Last Updated on : 2024-05-10 06:37:03download

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.

Get the robot vacuum object

Example

Objective-C:

ThingSmartSweeperDevice *sweeper = [[ThingSmartSweeperDevice alloc] initWithDeviceId:@"devId"]

Swift:

sweeper = ThingSmartSweeperDevice.init(deviceId: "devId")

Access the P2P service

- (void)connectDeviceByP2P:(void (^)(void))success
                   failure:(void (^)(NSError * _Nullable error))failure;

Parameters

Parameter Description
success The success callback.
failure The failure callback.

Example

Objective-C:

[self.sweeper connectDeviceByP2P:^{

} failure:^(NSError * _Nullable error) {

}];

Swift:

sweeper?.connect(byP2P: {

}, failure: { error in

})

Start P2P downloading

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.

Do not call the onDestroyP2P method in the failure of this callback.

- (void)startObserverSweeperDataByP2P:(ThingSmartSweeperP2PDownloadType)downloadType
                       receiveP2PData:(void (^)(NSDictionary *data))receiveP2PData
                              failure:(void (^)(NSError * _Nullable error))failure;

Parameters

Parameter Description
downloadType The method to download data through P2P connections. Valid values:
  • ThingSmartSweeperP2PDownloadTypeOnce: The data downloading is stopped after the existing device data is downloaded. New data will not be downloaded.
  • ThingSmartSweeperP2PDownloadTypeStill: Device data is continuously downloaded until the device automatically stops transferring data.
receiveP2PData The callback to invoke when hexadecimal data is received through P2P connections.
failure The failure callback.

Callback description

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

Objective-C:

[self.sweeper startObserverSweeperDataByP2P:downloadType receiveP2PData:^(NSDictionary * _Nonnull data) {
        if (data) {
            NSLog(@"%@",data);
        }
} failure:^(NSError * _Nullable error) {

}];

Swift:

sweeper?.startObserverSweeperData(byP2P: ThingSmartSweeperP2PDownloadType.still, receiveP2PData: { data in

}, failure: { error in

})

Check P2P connection status

Checks P2P connection status. A successful response is returned to indicate that the device is connected as expected through a P2P channel. A failed response is returned to indicate that the P2P connection is closed unexpectedly. You can call connectDeviceByP2P to try to reconnect the device.

- (BOOL)P2PActiveCheck;

Stop P2P downloading

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

Do not call the onDestroyP2P method in the failure of this callback.

- (void)stopObserverSweeperDataByP2P:(void (^)(void))success
                             failure:(void (^)(NSError * _Nullable error))failure;

Parameters

Parameter Description
success The success callback.
failure The failure callback.

Example

Objective-C:

[self.sweeper stopObserverSweeperDataByP2P:^{

} failure:^(NSError * _Nullable error) {

}];

Swift:

sweeper?.stopObserverSweeperData(byP2P: {

}, failure: { error in

})

Disable the P2P service

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

Example

Objective-C:

[self.sweeper onDestroyP2P];

Swift:

sweeper?.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.

- (void)connectDeviceByP2P:(BOOL)isStopReconnect success:(void (^)(void))success failure:(void (^)(NSError * _Nullable error))failure;

Example

[self.sweeper connectDeviceByP2P:NO success:^{

} failure:^(NSError * _Nullable error) {

}];

Error codes

Error code Description
-9000 The device information has exceptions.
-9001 The SDK dependency is not added.
-9002 The P2P connection is being created.
-9003 The specified device is offline.
-9004 A timeout error 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.