简体   繁体   English

当Sphero同步并且我使用多个活动时,如何保持蓝牙连接

[英]How to keep a Bluetooth connection when Sphero is synchronized and I use several Activities

When I have synchronized with Sphero and I want to pass from one activity to another one, my synchronization is lost, and I have to do in the new activity this method in onCreate to get de synchronism again: 当我与Sphero同步并且想要从一个活动传递到另一个活动时,我的同步丢失了,我必须在新活动中在onCreate中使用此方法来再次获得非同步性:

        **RobotProvider provider = RobotProvider.getDefaultProvider();
        mRobot = provider.findRobot(robot_id);
        provider.initiateConnection(robot_id);
        provider.control(mRobot);
        provider.connectControlledRobots();**

mRobot has the MAC address of Sphero. mRobot具有Sphero的MAC地址。 But it's not good for all times that I try it, I want to keep the bluetooth connection for all application, since I connect for the first time and being able to keep it without synchronize again. 但这并不是每次都尝试的好处,我想为所有应用程序保留蓝牙连接,因为我是第一次连接,并且能够保持它而无需再次同步。

I have seen the official Orbotix application for Sphero and I think is perfect, because syncronization is permanent. 我已经看到了Sphero的官方Orbotix应用程序,并且我认为它是完美的,因为同步是永久的。 Could you help me in this way? 你能以这种方式帮助我吗?

At Orbotix, we generally use a central Activity and either show temporary activities over the top of it, or (more recently) we use a FragmentActivity that first shows a fragment that takes care of the connection (synchronization in your question). 在Orbotix,我们通常使用中央活动,并在其顶部显示临时活动,或者(最近)我们使用FragmentActivity ,该活动首先显示负责连接的片段(问题中的同步)。 From there, we show different screens using different, custom fragments. 从那里开始,我们使用不同的自定义片段显示不同的屏幕。

If you absolutely need to send a Robot object through to another Activity, you can add the robot id to the Intent and then obtain the robot object from RobotProvider in the new Activity. 如果您绝对需要将漫游器对象发送到另一个活动,则可以将漫游器ID添加到Intent,然后在新的活动中从RobotProvider获取漫游器对象。 This also requires you to make sure you don't disconnect from Sphero based on the lifecycle of the original Activity. 这还要求您确保不要根据原始Activity的生命周期与Sphero断开连接。

In your original Activity: 在您的原始活动中:

    private void startNextActivity() {
        Intent nextActivity = new Intent(this, NextActivity.class);
        nextActivity.putExtra("robot.id", mRobot.getUniqueId());
        goingToNextActivity = true;
        startActivity(nextActivity);
    }

    @Override
    protected void onStop() {
        super.onStop();

        // don't disconnect if headed to "NextActivity"
        if (!goingToNextActivity) {
            RobotProvider.getDefaultProvider().disconnect(mRobot);
            mRobot = null;
        }
    }

In your new Activity: 在您的新活动中:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.OnCreate(savedInstanceState);

        // get the robot object sent through to this Activity
        String robotId = getIntent().getStringExtra("robot.id");
        Robot robot = RobotProvider.getDefaultProvider().findRobot(robotId);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM