P2P Capabilities (New)

Last Updated on : 2025-01-13 09:05:57download

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.

Entry to the feature

val iThingSweeperKitSdk = ThingOptimusSdk.getManager(IThingSweeperKitSdk::class.java)
val mSweeperP2P = iThingSweeperKitSdk.newSweeperP2PKitInstance(devId)

Initialization interfaces

Initialization

API description

fun initP2PSDKWithUserId(initConfig: ThingInitConfig, callback: SweeperP2PKitCallback?)

Parameter description

Parameter Description
ThingInitConfig Configure the user ID.
callback A callback indicating whether initialization is successful.

Sample code

mSweeperP2P?.initP2PSDKWithUserId(ThingInitConfig().apple {
    this.userId = "userId"
}, object : SweeperP2PKitCallback {
    override fun onError(errorCode: Int, errorMsg: String?) {
    }

    override fun onSuccess() {
    }
})

Deinitialization

API description

fun deInit()

Sample code

mSweeperP2P?.deInit(object : SweeperP2PKitCallback {
    override fun onSuccess() {
    }

    override fun onError(errorCode: Int, errorMsg: String?) {
    }

})

P2P connection

Create a P2P connection

API description

fun connectDevice(devId: String, p2pParams: ThingConnConfig, callback: SweeperP2PKitCallback?)

Parameter description

ThingConnConfig

Parameter Description
mode The connection mode. Valid values:
  • 0: internet
  • 1: LAN
timeout The timeout value, in milliseconds.
0 indicates a default value is used.

Sample code

mSweeperP2P?.connectDevice(devId, ThingConnConfig().apply {
    this.mode = 1
    this.timeout = 15000
}, object : SweeperP2PKitCallback {
    override fun onSuccess() {
    }

    override fun onError(errorCode: Int, errorMsg: String?) {
    }
})

Close a P2P connection

API description

fun disconnectDevice(devId: String, callback: SweeperP2PKitCallback?)

Parameter description

Parameter Description
devId The device ID.
callback The callback that is executed when a P2P connection is closed.

Sample code

mSweeperP2P?.disconnectDevice("devId", object : SweeperP2PKitCallback {
    override fun onSuccess() {
    }

    override fun onError(errorCode: Int, errorMsg: String?) {
    }

})

Check P2P connection status

API description

/**
 * @return Indicates whether the device is connecting.
 */
fun isActiveSync(devId: String): Boolean

Parameter description

Parameter Description
devId The device ID.

Sample code

if (mSweeperP2P?.isActiveSync("devId") == true) {

}

Download

Download files

Call querySweeperFile to get the list of map files, and then call this interface to download the final map files.

The file extension .stream indicates continuous downloading of data generated by the device until the device automatically stops sending data or the download is canceled by calling cancelDownload.

API description

/**
 * @param params  The request parameter.
 * @param callback   The callback that indicates whether the API operation is successful.
 */
fun downloadFile(devId: String, params: ThingDownloadFile, callback: SweeperP2PKitCallback?)

Parameter description

ThingDownloadFile

Parameter Description
albumName The album name. Default value: ipc_sweeper_robot.
filePath The local storage path of the downloaded file in the app.
jsonfiles The downloaded file in the format of {"files":["filesname1", "filesname2", "filesname3" ]}.

Sample code

//init file path
val sweeperCacheFile = FileUtil.getSDPath(this) + File.separator + "Sweeper" + File.separator + "Cache" + File.separator + "map" + File.separator
FileUtil.createDir(sweeperCacheFile)
val file = File(sweeperCacheFile)
FileUtil.deleteFile(file)
//Filter Files (result is downloadFile response)
val unCachedList: MutableList<String> = ArrayList()
for (itemBean in result) {
    if (!TextUtils.isEmpty(itemBean?.filename)) {
        val path: String = sweeperCacheFile + itemBean?.filename
        val file = File(path)
        if (file.exists()) {
            file.delete()
        }
        // One download task.
        itemBean?.filename?.let {
            if (!it.endsWith(".stream")) {
                unCachedList.add(itemBean.filename)
            }
        }
        // Device data is continuously downloaded until the device automatically stops transferring data.
        if (itemBean?.filename!!.endsWith(".stream")) {
            unCachedList.add(itemBean.filename)
        }
    }
}
val jsonFiles = JSON.toJSONString(
    DownloadAlbumFiledBean2(
        unCachedList
    )
)
// Download files
mSweeperP2P?.downloadFile("devId", ThingDownloadFile().apply {
    this.filePath = sweeperCacheFile
    this.albumName = "ipc_sweeper_robot"
    this.jsonfiles = jsonFiles
}, object : SweeperP2PKitCallback {
    override fun onSuccess() {

    }

    override fun onError(errorCode: Int, errorMsg: String?) {

    }

})

Cancel a download task

API description

/**
 * @param callback The callback.
 */
fun cancelDownload(devId: String, callback: SweeperP2PKitCallback?)

Sample code


mSweeperP2P?.cancelDownload("devId", object : SweeperP2PKitCallback {
    override fun onSuccess() {
    }

    override fun onError(errorCode: Int, errorMsg: String?) {
    }

})

Listener

Register a listener

API description

/**
 * Register a listener
 * @param listener Mainly listens for the connection status, as well as single file download success and progress.
 */
fun addFileTransListener(listener: SweeperFileTransListener)

Callback description

interface SweeperFileTransListener {

    /**
     * The connection status.
     */
    fun onSessionStatusChanged(status: ThingSessionStatus)

    /**
     * The download success of a single file.
     */
    fun onFileFinished(complete: ThingDownloadComplete)

    /**
     * The download progress of a single file.
     */
    fun onFileProgress(progress: ThingDownloadProgress)

    /**
     * The overall download progress.
     */
    fun onFileTotalProgress(totalProgress: ThingDownloadTotalProgress)

}

Parameter description

ThingSessionStatus

Parameter Description
deviceId The device ID.
status The status value. A value less than 0 indicates disconnection. For more information, see Error codes for P2P SDK.

ThingDownloadComplete

Parameter Description
deviceId The device ID.
fileName The file name.
index The index.

ThingDownloadProgress

Parameter Description
deviceId The device ID.
fileName The file name.
progress The progress.

ThingDownloadTotalProgress

Parameter Description
deviceId The device ID.
progress The progress.

Sample code

mSweeperP2P?.addFileTransListener(object : SweeperFileTransListener{
    override fun onFileFinished(complete: ThingDownloadComplete) {
        TODO("Not yet implemented")
    }

    override fun onFileProgress(progress: ThingDownloadProgress) {
        TODO("Not yet implemented")
    }

    override fun onFileTotalProgress(totalProgress: ThingDownloadTotalProgress) {
        TODO("Not yet implemented")
    }

    override fun onSessionStatusChanged(status: ThingSessionStatus) {
        TODO("Not yet implemented")
    }

})

Remove a listener

API description

fun removeFileTransListener(listener: SweeperFileTransListener)

Query the list of file indexes

API description

/**
 * @param callback The callback.
 */
fun querySweeperFile(devId: String, albumName: String, callback: SweeperP2PKitDataCallback<List<ThingAlbumFileIndex?>?>?)

Parameter description

Parameter Description
devId The device ID.
albumName The album name. Default value: ipc_sweeper_robot.

Sample code

mSweeperP2P?.querySweeperFile(
    "devId",
    "ipc_sweeper_robot",
    object : SweeperP2PKitDataCallback<List<ThingAlbumFileIndex?>?> {
        override fun onSuccess(result: List<ThingAlbumFileIndex?>?) {
            //TODO downFile
        }

        override fun onError(errorCode: Int, errorMsg: String?) {
        }

    })

Error codes

Code Description
-1025 Failed to get P2P capability (P2P capability was not loaded).
-1030 The P2P connection is not created. An exception has occurred during the function call, such as calling and connecting the methods without a P2P connection.
-1031 A P2P connection is being created.
-1040 Querying the file directory is in progress.
-1041 Failed to query the file directory.
-1060 Failed to download the file.
-1061 No download task is in progress and the additional download fails.

For more information, see the error codes for P2P SDK.