Sample Code

Last Updated on : 2024-03-04 08:54:46

This topic describes sample code related to the P2P operation based on the robot vacuum SDK.

Sample code

public class P2pConnectActivity extends AppCompatActivity {

    private IThingSweeperP2P mSweeperP2P;

    private SpannableStringBuilder stringBuilder;

    private TextView tvP2pConnectShow;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_p2p_connect);

        tvP2pConnectShow = findViewById(R.id.tvP2pConnectShow);

        // Get the device ID
        String devId = getIntent().getStringExtra("deviceId");

        showText("Device ID: " + devId);

        // Initialize
        IThingSweeperKitSdk iThingSweeperKitSdk = ThingOptimusSdk.getManager(IThingSweeperKitSdk.class);
        if (null != iThingSweeperKitSdk) {
            mSweeperP2P = iThingSweeperKitSdk.getSweeperP2PInstance(devId);
            mSweeperP2P.stopReconnect(true);
            showText("Initializing");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (null != mSweeperP2P) {
            if (!mSweeperP2P.checkP2PActive()) {
                showText("Not connected, start connecting");
                connectDevice();
            } else {
                showText("Connected, remain unchanged");
            }
        }
    }

    private void connectDevice() {
        long startConnectTime = System.currentTimeMillis();
        mSweeperP2P.connectDeviceByP2P(new SweeperP2PCallback() {
            @Override
            public void onSuccess() {
                long endConnectTime = System.currentTimeMillis() - startConnectTime;
                showText("The P2P channel is connected, connection time:" + endConnectTime);
                startObserverSweeperDataByP2P();
            }

            @Override
            public void onFailure(int i) {
                showText("Connection failed, error code: " + i);
                if (i == -3 | | i == -105) {
                    long startConnectTime = System.currentTimeMillis();
                    showText("The P2P channel starts to reconnect");
                    mSweeperP2P.connectDeviceByP2P(new SweeperP2PCallback() {
                        @Override
                        public void onSuccess() {
                            long endConnectTime = System.currentTimeMillis() - startConnectTime;
                            showText("The P2P channel is reconnected successfully, connection time:" + endConnectTime);
                            startObserverSweeperDataByP2P();
                        }

                        @Override
                        public void onFailure(int i) {
                            showText("Reconnection failed, error code: " + i);
                        }
                    });
                }
            }
        });
    }

    private void startObserverSweeperDataByP2P() {
        mSweeperP2P.startObserverSweeperDataByP2P(DownloadType.P2PDownloadTypeStill,
                new SweeperP2PCallback() {
                    @Override
                    public void onSuccess() {
                        showText("The P2P data download status is enabled successfully");
                    }

                    @Override
                    public void onFailure(int i) {
                        showText("Failed to enable the P2P data download status, error code: " + i);
                    }
                }, new SweeperP2PDataCallback() {
                    @Override
                    public void receiveData(int i, SweeperP2PBean sweeperP2PBean) {
                        if (null != sweeperP2PBean && null != sweeperP2PBean.getData()) {
                            // Get data
                            showText("Receive robot vacuum data index:" + i + ", dataSize:" + sweeperP2PBean.getData().length());
                        }
                    }

                    @Override
                    public void onFailure(int i) {
                        showText("Failed to get the robot vacuum data, error code: " + i);
                    }
                });
    }

    private void showText(String tips) {
        if (null == stringBuilder) {
            stringBuilder = new SpannableStringBuilder();
        }
        stringBuilder.append(tips);
        stringBuilder.append("\n");
        tvP2pConnectShow.post(() -> tvP2pConnectShow.setText(stringBuilder));
    }

    @Override
    protected void onDestroy() {
        // Stop the P2P
        if (null != mSweeperP2P) {
            mSweeperP2P.stopObserverSweeperDataByP2P(new SweeperP2PCallback() {
                @Override
                public void onSuccess() {
                    mSweeperP2P.onDestroyP2P();
                }

                @Override
                public void onFailure(int i) {
                    mSweeperP2P.onDestroyP2P();
                }
            });
        }
        super.onDestroy();
    }

}