java Android:如何将蓝牙连接传递给另一个活动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6872970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:40:57  来源:igfitidea点击:

Android: How to pass a Bluetooth connection to another Activity?

javaandroidnetbeansbluetooth

提问by richard

I have my first Activity in which the BT connection is established. There is an option presented to the user and, based on their selection, a different Activity will load.

我有我的第一个活动,其中建立了 BT 连接。有一个选项呈现给用户,根据他们的选择,将加载不同的活动。

The problem is, both activities need a BT connection and I don't see the point in destroying one connection just to make another.

问题是,这两个活动都需要一个 BT 连接,我不认为为了建立另一个连接而破坏一个连接有什么意义。

Is there a way that I could pass the connection between Activities?

有没有办法可以传递活动之间的连接?

Does anyone have some example for me or perhaps a link?

有没有人给我一些例子或者一个链接?

I've tried "class MyApplication extends Application", but then I can't use:

我试过“ class MyApplication extends Application”,但后来我不能使用:

super.onCreate(savedInstanceState);
setContentView(R.layout.blablabla);

This may be a pretty silly question but I've only been at Android +- 2 weeks.

这可能是一个非常愚蠢的问题,但我只在 Android +- 2 周。

回答by kibibyte

Have you tried using the Application object to store the Bluetooth connection in an object and using your Activities to get it?

您是否尝试过使用 Application 对象将蓝牙连接存储在一个对象中并使用您的活动来获取它?

Try something like this. (Note: I have never worked with Bluetooth on Android, so I don't know which relevant classes to use. In this case, I'll use BluetoothDevice, since it seems to be the right class based on the library documentation)

尝试这样的事情。(注意:我从未在 Android 上使用过蓝牙,所以我不知道要使用哪些相关类。在这种情况下,我将使用BluetoothDevice,因为它似乎是基于库文档的正确类)

public class MyApplication extends Application {
    BluetoothDevice device;
    ...
    public synchronized BluetoothDevice getBtConnection() {
        if (device == null) {
            // construct a BluetoothDevice object and put it into variable device
        }
        return device;
    }
}

That way, your first activity just has to do this:

这样,您的第一个活动只需执行以下操作:

public class FirstActivity extends Activity {
    private BluetoothDevice device;
    ...
    @Override
    protected void onCreate(Bundle b) {
        super(b);
        ...
        device = ((MyApplication) getApplication()).getBtDevice();
        ...
    }
    ...
}

And then, any time your other Activities need to use that connection, they just need to call getBtDevice(), because FirstActivityalready instantiated it.

然后,任何时候您的其他活动需要使用该连接,他们只需要调用getBtDevice(),因为FirstActivity已经实例化了它。

回答by Androider

Have you tried using a Bundle?

您是否尝试过使用 Bundle?

Check relevant topic

检查相关主题

http://www.anddev.org/putting_an_object_into_a_bundle-t6431.html

http://www.anddev.org/putting_an_object_into_a_bundle-t6431.html

回答by melledijkstra

I know it's an old question, but for the new people visiting this topic:

我知道这是一个老问题,但对于访问此主题的新人:

I think Kibibyte's answer would also work, but otherwise there is the option to use a (Bound)Service. This would run even if the app closes

我认为 Kibibyte 的答案也可以,但除此之外,还可以选择使用(Bound)Service. 即使应用程序关闭,这也会运行

Official Android Service documentation

官方 Android 服务文档

回答by Marjan Davodinejad

i had same problem ,and finally solve it! so at first you should create your connection in an activity and be sure that the connection store in public staticvariable and you can call that connection variable in each activity that you want to have Bluetooth Connection. I suggest you to use service class to create connection and use connection variable like this

我有同样的问题,终于解决了!所以首先你应该在一个活动中创建你的连接,并确保连接存储在公共静态变量中,你可以在你想要蓝牙连接的每个活动中调用该连接变量。我建议你使用服务类来创建连接并使用这样的连接变量

BluetoothChatService mChatService=DeviceListActivity.chatService